From d4a981880e0ded6f3d9ceb608372fb379813364c Mon Sep 17 00:00:00 2001 From: Chris Cho Date: Mon, 25 Mar 2024 16:56:02 -0400 Subject: [PATCH] DOCSP-37057 eloquent schema builder (#2776) * DOCSP-37057: Eloquent schema builder --- docs/eloquent-models.txt | 518 +----------------- docs/eloquent-models/schema-builder.txt | 393 +++++++++++++ .../schema-builder/astronauts_migration.php | 30 + .../schema-builder/flights_migration.php | 32 ++ .../schema-builder/passengers_migration.php | 32 ++ .../schema-builder/planets_migration.php | 33 ++ .../schema-builder/spaceports_migration.php | 33 ++ .../schema-builder/stars_migration.php | 27 + 8 files changed, 592 insertions(+), 506 deletions(-) create mode 100644 docs/eloquent-models/schema-builder.txt create mode 100644 docs/includes/schema-builder/astronauts_migration.php create mode 100644 docs/includes/schema-builder/flights_migration.php create mode 100644 docs/includes/schema-builder/passengers_migration.php create mode 100644 docs/includes/schema-builder/planets_migration.php create mode 100644 docs/includes/schema-builder/spaceports_migration.php create mode 100644 docs/includes/schema-builder/stars_migration.php diff --git a/docs/eloquent-models.txt b/docs/eloquent-models.txt index 3ce32c124..c0f7cea57 100644 --- a/docs/eloquent-models.txt +++ b/docs/eloquent-models.txt @@ -6,517 +6,23 @@ Eloquent Models .. facet:: :name: genre - :values: tutorial + :values: reference .. meta:: - :keywords: php framework, odm, code example + :keywords: php framework, odm -This package includes a MongoDB enabled Eloquent class that you can use to -define models for corresponding collections. +Eloquent models are part of the Laravel Eloquent object-relational +mapping (ORM) framework that enable you to work with a database by using +model classes. {+odm-short+} extends this framework to use similar +syntax to work with MongoDB as a database. -Extending the base model -~~~~~~~~~~~~~~~~~~~~~~~~ +This section contains guidance on how to use Eloquent models in +{+odm-short+} to work with MongoDB in the following ways: -To get started, create a new model class in your ``app\Models\`` directory. +- :ref:`laravel-schema-builder` shows how to manage indexes on your MongoDB + collections by using Laravel migrations -.. code-block:: php +.. toctree:: - namespace App\Models; + Schema Builder - use MongoDB\Laravel\Eloquent\Model; - - class Book extends Model - { - // - } - -Just like a regular model, the MongoDB model class will know which collection -to use based on the model name. For ``Book``, the collection ``books`` will -be used. - -To change the collection, pass the ``$collection`` property: - -.. code-block:: php - - use MongoDB\Laravel\Eloquent\Model; - - class Book extends Model - { - protected $collection = 'my_books_collection'; - } - -.. note:: - - MongoDB documents are automatically stored with a unique ID that is stored - in the ``_id`` property. If you wish to use your own ID, substitute the - ``$primaryKey`` property and set it to your own primary key attribute name. - -.. code-block:: php - - use MongoDB\Laravel\Eloquent\Model; - - class Book extends Model - { - protected $primaryKey = 'id'; - } - - // MongoDB will also create _id, but the 'id' property will be used for primary key actions like find(). - Book::create(['id' => 1, 'title' => 'The Fault in Our Stars']); - -Likewise, you may define a ``connection`` property to override the name of the -database connection to reference the model. - -.. code-block:: php - - use MongoDB\Laravel\Eloquent\Model; - - class Book extends Model - { - protected $connection = 'mongodb'; - } - -Soft Deletes -~~~~~~~~~~~~ - -When soft deleting a model, it is not actually removed from your database. -Instead, a ``deleted_at`` timestamp is set on the record. - -To enable soft delete for a model, apply the ``MongoDB\Laravel\Eloquent\SoftDeletes`` -Trait to the model: - -.. code-block:: php - - use MongoDB\Laravel\Eloquent\SoftDeletes; - - class User extends Model - { - use SoftDeletes; - } - -For more information check `Laravel Docs about Soft Deleting `__. - -Prunable -~~~~~~~~ - -``Prunable`` and ``MassPrunable`` traits are Laravel features to automatically -remove models from your database. You can use ``Illuminate\Database\Eloquent\Prunable`` -trait to remove models one by one. If you want to remove models in bulk, you -must use the ``MongoDB\Laravel\Eloquent\MassPrunable`` trait instead: it -will be more performant but can break links with other documents as it does -not load the models. - -.. code-block:: php - - use MongoDB\Laravel\Eloquent\Model; - use MongoDB\Laravel\Eloquent\MassPrunable; - - class Book extends Model - { - use MassPrunable; - } - -For more information check `Laravel Docs about Pruning Models `__. - -Dates -~~~~~ - -Eloquent allows you to work with Carbon or DateTime objects instead of MongoDate objects. Internally, these dates will be converted to MongoDate objects when saved to the database. - -.. code-block:: php - - use MongoDB\Laravel\Eloquent\Model; - - class User extends Model - { - protected $casts = ['birthday' => 'datetime']; - } - -This allows you to execute queries like this: - -.. code-block:: php - - $users = User::where( - 'birthday', '>', - new DateTime('-18 years') - )->get(); - -Extending the Authenticatable base model -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -This package includes a MongoDB Authenticatable Eloquent class ``MongoDB\Laravel\Auth\User`` -that you can use to replace the default Authenticatable class ``Illuminate\Foundation\Auth\User`` -for your ``User`` model. - -.. code-block:: php - - use MongoDB\Laravel\Auth\User as Authenticatable; - - class User extends Authenticatable - { - - } - -Guarding attributes -~~~~~~~~~~~~~~~~~~~ - -When choosing between guarding attributes or marking some as fillable, Taylor -Otwell prefers the fillable route. This is in light of -`recent security issues described here `__. - -Keep in mind guarding still works, but you may experience unexpected behavior. - -Schema ------- - -The database driver also has (limited) schema builder support. You can -conveniently manipulate collections and set indexes. - -Basic Usage -~~~~~~~~~~~ - -.. code-block:: php - - Schema::create('users', function ($collection) { - $collection->index('name'); - $collection->unique('email'); - }); - -You can also pass all the parameters specified :manual:`in the MongoDB docs ` -to the ``$options`` parameter: - -.. code-block:: php - - Schema::create('users', function ($collection) { - $collection->index( - 'username', - null, - null, - [ - 'sparse' => true, - 'unique' => true, - 'background' => true, - ] - ); - }); - -Inherited operations: - - -* create and drop -* collection -* hasCollection -* index and dropIndex (compound indexes supported as well) -* unique - -MongoDB specific operations: - - -* background -* sparse -* expire -* geospatial - -All other (unsupported) operations are implemented as dummy pass-through -methods because MongoDB does not use a predefined schema. - -Read more about the schema builder on `Laravel Docs `__ - -Geospatial indexes -~~~~~~~~~~~~~~~~~~ - -Geospatial indexes can improve query performance of location-based documents. - -They come in two forms: ``2d`` and ``2dsphere``. Use the schema builder to add -these to a collection. - -.. code-block:: php - - Schema::create('bars', function ($collection) { - $collection->geospatial('location', '2d'); - }); - -To add a ``2dsphere`` index: - -.. code-block:: php - - Schema::create('bars', function ($collection) { - $collection->geospatial('location', '2dsphere'); - }); - -Relationships -------------- - -Basic Usage -~~~~~~~~~~~ - -The only available relationships are: - - -* hasOne -* hasMany -* belongsTo -* belongsToMany - -The MongoDB-specific relationships are: - - -* embedsOne -* embedsMany - -Here is a small example: - -.. code-block:: php - - use MongoDB\Laravel\Eloquent\Model; - - class User extends Model - { - public function items() - { - return $this->hasMany(Item::class); - } - } - -The inverse relation of ``hasMany`` is ``belongsTo``: - -.. code-block:: php - - use MongoDB\Laravel\Eloquent\Model; - - class Item extends Model - { - public function user() - { - return $this->belongsTo(User::class); - } - } - -belongsToMany and pivots -~~~~~~~~~~~~~~~~~~~~~~~~ - -The belongsToMany relation will not use a pivot "table" but will push id's to -a **related_ids** attribute instead. This makes the second parameter for the -belongsToMany method useless. - -If you want to define custom keys for your relation, set it to ``null``: - -.. code-block:: php - - use MongoDB\Laravel\Eloquent\Model; - - class User extends Model - { - public function groups() - { - return $this->belongsToMany( - Group::class, null, 'user_ids', 'group_ids' - ); - } - } - -EmbedsMany Relationship -~~~~~~~~~~~~~~~~~~~~~~~ - -If you want to embed models, rather than referencing them, you can use the -``embedsMany`` relation. This relation is similar to the ``hasMany`` relation -but embeds the models inside the parent object. - -**REMEMBER**\ : These relations return Eloquent collections, they don't return -query builder objects! - -.. code-block:: php - - use MongoDB\Laravel\Eloquent\Model; - - class User extends Model - { - public function books() - { - return $this->embedsMany(Book::class); - } - } - -You can access the embedded models through the dynamic property: - -.. code-block:: php - - $user = User::first(); - - foreach ($user->books as $book) { - // - } - -The inverse relation is auto *magically* available. You can omit the reverse -relation definition. - -.. code-block:: php - - $book = User::first()->books()->first(); - - $user = $book->user; - -Inserting and updating embedded models works similar to the ``hasMany`` relation: - -.. code-block:: php - - $book = $user->books()->save( - new Book(['title' => 'A Game of Thrones']) - ); - - // or - $book = - $user->books() - ->create(['title' => 'A Game of Thrones']); - -You can update embedded models using their ``save`` method (available since -release 2.0.0): - -.. code-block:: php - - $book = $user->books()->first(); - - $book->title = 'A Game of Thrones'; - $book->save(); - -You can remove an embedded model by using the ``destroy`` method on the -relation, or the ``delete`` method on the model (available since release 2.0.0): - -.. code-block:: php - - $book->delete(); - - // Similar operation - $user->books()->destroy($book); - -If you want to add or remove an embedded model, without touching the database, -you can use the ``associate`` and ``dissociate`` methods. - -To eventually write the changes to the database, save the parent object: - -.. code-block:: php - - $user->books()->associate($book); - $user->save(); - -Like other relations, embedsMany assumes the local key of the relationship -based on the model name. You can override the default local key by passing a -second argument to the embedsMany method: - -.. code-block:: php - - use MongoDB\Laravel\Eloquent\Model; - - class User extends Model - { - public function books() - { - return $this->embedsMany(Book::class, 'local_key'); - } - } - -Embedded relations will return a Collection of embedded items instead of a -query builder. Check out the available operations here: -`https://laravel.com/docs/master/collections `__ - -EmbedsOne Relationship -~~~~~~~~~~~~~~~~~~~~~~ - -The embedsOne relation is similar to the embedsMany relation, but only embeds a single model. - -.. code-block:: php - - use MongoDB\Laravel\Eloquent\Model; - - class Book extends Model - { - public function author() - { - return $this->embedsOne(Author::class); - } - } - -You can access the embedded models through the dynamic property: - -.. code-block:: php - - $book = Book::first(); - $author = $book->author; - -Inserting and updating embedded models works similar to the ``hasOne`` relation: - -.. code-block:: php - - $author = $book->author()->save( - new Author(['name' => 'John Doe']) - ); - - // Similar - $author = - $book->author() - ->create(['name' => 'John Doe']); - -You can update the embedded model using the ``save`` method (available since -release 2.0.0): - -.. code-block:: php - - $author = $book->author; - - $author->name = 'Jane Doe'; - $author->save(); - -You can replace the embedded model with a new model like this: - -.. code-block:: php - - $newAuthor = new Author(['name' => 'Jane Doe']); - - $book->author()->save($newAuthor); - -Cross-Database Relationships ----------------------------- - -If you're using a hybrid MongoDB and SQL setup, you can define relationships -across them. - -The model will automatically return a MongoDB-related or SQL-related relation -based on the type of the related model. - -If you want this functionality to work both ways, your SQL-models will need -to use the ``MongoDB\Laravel\Eloquent\HybridRelations`` trait. - -**This functionality only works for ``hasOne``, ``hasMany`` and ``belongsTo``.** - -The SQL model must use the ``HybridRelations`` trait: - -.. code-block:: php - - use MongoDB\Laravel\Eloquent\HybridRelations; - - class User extends Model - { - use HybridRelations; - - protected $connection = 'mysql'; - - public function messages() - { - return $this->hasMany(Message::class); - } - } - -Within your MongoDB model, you must define the following relationship: - -.. code-block:: php - - use MongoDB\Laravel\Eloquent\Model; - - class Message extends Model - { - protected $connection = 'mongodb'; - - public function user() - { - return $this->belongsTo(User::class); - } - } diff --git a/docs/eloquent-models/schema-builder.txt b/docs/eloquent-models/schema-builder.txt new file mode 100644 index 000000000..9fd845b55 --- /dev/null +++ b/docs/eloquent-models/schema-builder.txt @@ -0,0 +1,393 @@ +.. _laravel-schema-builder: + +============== +Schema Builder +============== + +.. facet:: + :name: genre + :values: tutorial + +.. meta:: + :keywords: php framework, odm, code example, schema facade, eloquent, blueprint, artisan, migrate + +.. contents:: On this page + :local: + :backlinks: none + :depth: 2 + :class: singlecol + +Overview +-------- + +Laravel provides a **facade** to access the schema builder class ``Schema``, +which lets you create and modify tables. Facades are static interfaces to +classes that make the syntax more concise and improve testability. + +{+odm-short+} supports a subset of the index and collection management methods +in the Laravel ``Schema`` facade. + +To learn more about facades, see `Facades `__ +in the Laravel documentation. + +The following sections describe the Laravel schema builder features available +in {+odm-short+} and show examples of how to use them: + +- :ref:`` +- :ref:`` +- :ref:`` + +.. note:: + + {+odm-short+} supports managing indexes and collections, but + excludes support for MongoDB JSON schemas for data validation. To learn + more about JSON schema validation, see :manual:`Schema Validation ` + in the {+server-docs-name+}. + +.. _laravel-eloquent-migrations: + +Perform Laravel Migrations +-------------------------- + +Laravel migrations let you programmatically create, modify, and delete +your database schema by running methods included in the ``Schema`` facade. +The following sections explain how to author a migration class when you use +a MongoDB database and how to run them. + +Create a Migration Class +~~~~~~~~~~~~~~~~~~~~~~~~ + +You can create migration classes manually or generate them by using the +``php artisan make:migration`` command. If you generate them, you must make the +following changes to perform the schema changes on your MongoDB database: + +- Replace the ``Illuminate\Database\Schema\Blueprint`` import with + ``MongoDB\Laravel\Schema\Blueprint`` if it is referenced in your migration +- Use only commands and syntax supported by {+odm-short+} + +.. tip:: + + If your default database connection is set to anything other than your + MongoDB database, update the following setting to make sure the migration + specifies the correct database: + + - Specify ``mongodb`` in the ``$connection`` field of your migration class + - Set ``DB_CONNECTION=mongodb`` in your ``.env`` configuration file + +The following example migration class contains the following methods: + +- ``up()``, which creates a collection and an index when you run the migration +- ``down()``, which drops the collection and all the indexes on it when you roll back the migration + +.. literalinclude:: /includes/schema-builder/astronauts_migration.php + :dedent: + :language: php + :emphasize-lines: 6, 11 + +Run or Roll Back a Migration +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +To run the database migration from a class file, run the following command +after replacing the placeholder: + +.. code-block:: bash + + php artisan migrate --path= + +This command runs the ``up()`` function in the class file to create the +collection and index in the database specified in the ``config/database.php`` +file. + +To roll back the migration, run the following command after replacing the +placeholder: + +.. code-block:: bash + + php artisan migrate:rollback --path= + +This command runs the ``down()`` function in the class file to drop the +collection and related indexes. + +To learn more about Laravel migrations, see +`Database: Migrations `__ +in the Laravel documentation. + +.. _laravel-eloquent-collection-exists: + +Check Whether a Collection Exists +--------------------------------- + +To check whether a collection exists, call the ``hasCollection()`` method on +the ``Schema`` facade in your migration file. You can use this to +perform migration logic conditionally. + +The following example migration creates a ``stars`` collection if a collection +named ``telescopes`` exists: + +.. literalinclude:: /includes/schema-builder/stars_migration.php + :language: php + :dedent: + :start-after: begin conditional create + :end-before: end conditional create + +.. _laravel-eloquent-indexes: + +Manage Indexes +-------------- + +MongoDB indexes are data structures that improve query efficiency by reducing +the number of documents needed to retrieve query results. Certain indexes, such +as geospatial indexes, extend how you can query the data. + +To improve query performance by using an index, make sure the index covers +the query. To learn more about indexes and query optimization, see the +following {+server-docs-name+} entries: + +- :manual:`Indexes ` +- :manual:`Query Optimization ` + +The following sections show how you can use the schema builder to create and +drop various types of indexes on a collection. + +Create an Index +~~~~~~~~~~~~~~~ + +To create indexes, call the ``create()`` method on the ``Schema`` facade +in your migration file. Pass it the collection name and a callback +method with a ``MongoDB\Laravel\Schema\Blueprint`` parameter. Specify the +index creation details on the ``Blueprint`` instance. + +The following example migration creates indexes on the following collection +fields: + +- Single field index on ``mission_type`` +- Compound index on ``launch_location`` and ``launch_date``, specifying a descending sort order on ``launch_date`` +- Unique index on the ``mission_id`` field, specifying the index name "unique_mission_id_idx" + +Click the :guilabel:`VIEW OUTPUT` button to see the indexes created by running +the migration, including the default index on the ``_id`` field: + +.. io-code-block:: + + .. input:: /includes/schema-builder/flights_migration.php + :language: php + :dedent: + :start-after: begin create index + :end-before: end create index + + .. output:: + :language: json + :visible: false + + [ + { v: 2, key: { _id: 1 }, name: '_id_' }, + { v: 2, key: { mission_type: 1 }, name: 'mission_type_1' }, + { + v: 2, + key: { launch_location: 1, launch_date: -1 }, + name: 'launch_location_1_launch_date_-1' + }, + { + v: 2, + key: { mission_id: 1 }, + name: 'unique_mission_id_idx', + unique: true + } + ] + +Specify Index Options +~~~~~~~~~~~~~~~~~~~~~ + +MongoDB index options determine how the indexes are used and stored. +You can specify index options when calling an index creation method, such +as ``index()``, on a ``Blueprint`` instance. + +The following migration code shows how to add a collation to an index as an +index option. Click the :guilabel:`VIEW OUTPUT` button to see the indexes +created by running the migration, including the default index on the ``_id`` +field: + +.. io-code-block:: + + .. input:: /includes/schema-builder/passengers_migration.php + :language: php + :dedent: + :start-after: begin index options + :end-before: end index options + + .. output:: + :language: json + :visible: false + + [ + { v: 2, key: { _id: 1 }, name: '_id_' }, + { + v: 2, + key: { last_name: 1 }, + name: 'passengers_collation_idx', + collation: { + locale: 'de@collation=phonebook', + caseLevel: false, + caseFirst: 'off', + strength: 3, + numericOrdering: true, + alternate: 'non-ignorable', + maxVariable: 'punct', + normalization: false, + backwards: false, + version: '57.1' + } + } + ] + +To learn more about index options, see :manual:`Options for All Index Types ` +in the {+server-docs-name+}. + +Create Sparse, TTL, and Unique Indexes +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +You can use {+odm-short+} helper methods to create the following types of +indexes: + +- Sparse indexes, which allow index entries only for documents that contain the + specified field +- Time-to-live (TTL) indexes, which expire after a set amount of time +- Unique indexes, which prevent inserting documents that contain duplicate + values for the indexed field + +To create these index types, call the ``create()`` method on the ``Schema`` facade +in your migration file. Pass ``create()`` the collection name and a callback +method with a ``MongoDB\Laravel\Schema\Blueprint`` parameter. Call the +appropriate helper method on the ``Blueprint`` instance and pass the +index creation details. + +The following migration code shows how to create a sparse and a TTL index +by using the index helpers. Click the :guilabel:`VIEW OUTPUT` button to see +the indexes created by running the migration, including the default index on +the ``_id`` field: + +.. io-code-block:: + + .. input:: /includes/schema-builder/planets_migration.php + :language: php + :dedent: + :start-after: begin index helpers + :end-before: end index helpers + + .. output:: + :language: json + :visible: false + + [ + { v: 2, key: { _id: 1 }, name: '_id_' }, + { v: 2, key: { rings: 1 }, name: 'rings_1', sparse: true }, + { + v: 2, + key: { last_visible_dt: 1 }, + name: 'last_visible_dt_1', + expireAfterSeconds: 86400 + } + ] + +You can specify sparse, TTL, and unique indexes on either a single field or +compound index by specifying them in the index options. + +The following migration code shows how to create all three types of indexes +on a single field. Click the :guilabel:`VIEW OUTPUT` button to see the indexes +created by running the migration, including the default index on the ``_id`` +field: + +.. io-code-block:: + + .. input:: /includes/schema-builder/planets_migration.php + :language: php + :dedent: + :start-after: begin multi index helpers + :end-before: end multi index helpers + + .. output:: + :language: json + :visible: false + + [ + { v: 2, key: { _id: 1 }, name: '_id_' }, + { + v: 2, + key: { last_visible_dt: 1 }, + name: 'last_visible_dt_1', + unique: true, + sparse: true, + expireAfterSeconds: 3600 + } + ] + +To learn more about these indexes, see :manual:`Index Properties ` +in the {+server-docs-name+}. + +Create a Geospatial Index +~~~~~~~~~~~~~~~~~~~~~~~~~ + +In MongoDB, geospatial indexes let you query geospatial coordinate data for +inclusion, intersection, and proximity. + +To create geospatial indexes, call the ``create()`` method on the ``Schema`` facade +in your migration file. Pass ``create()`` the collection name and a callback +method with a ``MongoDB\Laravel\Schema\Blueprint`` parameter. Specify the +geospatial index creation details on the ``Blueprint`` instance. + +The following example migration creates a ``2d`` and ``2dsphere`` geospatial +index on the ``spaceports`` collection. Click the :guilabel:`VIEW OUTPUT` +button to see the indexes created by running the migration, including the +default index on the ``_id`` field: + +.. io-code-block:: + .. input:: /includes/schema-builder/spaceports_migration.php + :language: php + :dedent: + :start-after: begin create geospatial index + :end-before: end create geospatial index + + .. output:: + :language: json + :visible: false + + [ + { v: 2, key: { _id: 1 }, name: '_id_' }, + { + v: 2, + key: { launchpad_location: '2dsphere' }, + name: 'launchpad_location_2dsphere', + '2dsphereIndexVersion': 3 + }, + { v: 2, key: { runway_location: '2d' }, name: 'runway_location_2d' } + ] + + +To learn more about geospatial indexes, see +:manual:`Geospatial Indexes ` in +the {+server-docs-name+}. + +Drop an Index +~~~~~~~~~~~~~ + +To drop indexes from a collection, call the ``table()`` method on the +``Schema`` facade in your migration file. Pass it the table name and a +callback method with a ``MongoDB\Laravel\Schema\Blueprint`` parameter. +Call the ``dropIndex()`` method with the index name on the ``Blueprint`` +instance. + +.. note:: + + If you drop a collection, MongoDB automatically drops all the indexes + associated with it. + +The following example migration drops an index called ``unique_mission_id_idx`` +from the ``flights`` collection: + +.. literalinclude:: /includes/schema-builder/flights_migration.php + :language: php + :dedent: + :start-after: begin drop index + :end-before: end drop index + + diff --git a/docs/includes/schema-builder/astronauts_migration.php b/docs/includes/schema-builder/astronauts_migration.php new file mode 100644 index 000000000..1fb7b76e4 --- /dev/null +++ b/docs/includes/schema-builder/astronauts_migration.php @@ -0,0 +1,30 @@ +index('name'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::drop('astronauts'); + } +}; diff --git a/docs/includes/schema-builder/flights_migration.php b/docs/includes/schema-builder/flights_migration.php new file mode 100644 index 000000000..861c339ef --- /dev/null +++ b/docs/includes/schema-builder/flights_migration.php @@ -0,0 +1,32 @@ +index('mission_type'); + $collection->index(['launch_location' => 1, 'launch_date' => -1]); + $collection->unique('mission_id', options: ['name' => 'unique_mission_id_idx']); + }); + // end create index + } + + public function down(): void + { + // begin drop index + Schema::table('flights', function (Blueprint $collection) { + $collection->dropIndex('unique_mission_id_idx'); + }); + // end drop index + } +}; diff --git a/docs/includes/schema-builder/passengers_migration.php b/docs/includes/schema-builder/passengers_migration.php new file mode 100644 index 000000000..f0b498940 --- /dev/null +++ b/docs/includes/schema-builder/passengers_migration.php @@ -0,0 +1,32 @@ +index( + 'last_name', + name: 'passengers_collation_idx', + options: [ + 'collation' => [ 'locale' => 'de@collation=phonebook', 'numericOrdering' => true ], + ], + ); + }); + // end index options + } + + public function down(): void + { + Schema::drop('passengers'); + } +}; diff --git a/docs/includes/schema-builder/planets_migration.php b/docs/includes/schema-builder/planets_migration.php new file mode 100644 index 000000000..90de5bd6e --- /dev/null +++ b/docs/includes/schema-builder/planets_migration.php @@ -0,0 +1,33 @@ +sparse('rings'); + $collection->expire('last_visible_dt', 86400); + }); + // end index helpers + + // begin multi index helpers + Schema::create('planet_systems', function (Blueprint $collection) { + $collection->index('last_visible_dt', options: ['sparse' => true, 'expireAfterSeconds' => 3600, 'unique' => true]); + }); + // end multi index helpers + } + + public function down(): void + { + Schema::drop('planets'); + } +}; diff --git a/docs/includes/schema-builder/spaceports_migration.php b/docs/includes/schema-builder/spaceports_migration.php new file mode 100644 index 000000000..ae96c6066 --- /dev/null +++ b/docs/includes/schema-builder/spaceports_migration.php @@ -0,0 +1,33 @@ +geospatial('launchpad_location', '2dsphere'); + $collection->geospatial('runway_location', '2d'); + }); + // end create geospatial index + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::drop('spaceports'); + } +}; diff --git a/docs/includes/schema-builder/stars_migration.php b/docs/includes/schema-builder/stars_migration.php new file mode 100644 index 000000000..6249da3cd --- /dev/null +++ b/docs/includes/schema-builder/stars_migration.php @@ -0,0 +1,27 @@ +