From fa101a131426f7bb6ed45c7bf4d8c86906ec036f Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sat, 30 Nov 2024 11:44:42 -0500 Subject: [PATCH] Tidy up docs and publish migrations/seeds docs - Remove deprecations for old versions - Remove version added for old releases - Remove links and references to phinx docs --- docs/en/contents.rst | 2 + docs/en/index.rst | 114 ++++++++++++--------------------- docs/en/seeding.rst | 29 ++++----- docs/en/writing-migrations.rst | 89 ++++++++++++------------- 4 files changed, 97 insertions(+), 137 deletions(-) diff --git a/docs/en/contents.rst b/docs/en/contents.rst index 899c35b1..4c14c624 100644 --- a/docs/en/contents.rst +++ b/docs/en/contents.rst @@ -3,4 +3,6 @@ :caption: CakePHP Migrations /index + /writing-migrations + /seeding /upgrading-to-builtin-backend diff --git a/docs/en/index.rst b/docs/en/index.rst index 1a92b87d..84bdcf21 100644 --- a/docs/en/index.rst +++ b/docs/en/index.rst @@ -9,8 +9,6 @@ It allows you to evolve your database tables over time. Instead of writing schema modifications in SQL, this plugin allows you to use an intuitive set of methods to implement your database changes. -This plugin is a wrapper for the database migrations library `Phinx `_. - Installation ============ @@ -57,15 +55,15 @@ columns, create indexes and even insert data into your database. Here's an example of a migration:: `_ -in order to know the complete list of methods you can use for writing migration -files. +See the :ref:`creating-a-table` section to learn more about using migrations to +define tables. .. note:: @@ -198,14 +194,6 @@ Migration names can follow any of the following patterns: You can also use the ``underscore_form`` as the name for your migrations i.e. ``create_products``. -.. versionadded:: cakephp/migrations 1.5.2 - - As of v1.5.2 of the `migrations plugin `_, - the migration filename will be automatically camelized. This version of the - plugin is only available with a release of CakePHP >= to 3.1. Prior to this - version of the plugin the migration name would be in the underscore form, - i.e. ``20160121164955_create_products.php``. - .. warning:: Migration names are used as migration class names, and thus may collide with @@ -242,7 +230,7 @@ written between bracket. Fields named ``created`` and ``modified``, as well as any field with a ``_at`` suffix, will automatically be set to the type ``datetime``. -Field types are those generically made available by the ``Phinx`` library. Those +Field types are those generically made available by CakePHP. Those can be: * string @@ -258,6 +246,10 @@ can be: * binary * boolean * uuid +* geometry +* point +* linestring +* polygon There are some heuristics to choosing fieldtypes when left unspecified or set to an invalid value. Default field type is ``string``: @@ -278,15 +270,15 @@ You can use ``bake`` to create a table: The command line above will generate a migration file that resembles:: `_. +Seed classes are a good way to populate your database with default or starter +data. They are also a great way to generate data for development environments. + By default, seed files will be looked for in the ``config/Seeds`` directory of -your application. Please make sure you follow -`Phinx instructions to build your seed files `_. +your application. See the :doc:`seeding` for how to build seed classes. As for migrations, a ``bake`` interface is provided for seed files: @@ -727,16 +714,7 @@ As for migrations, a ``bake`` interface is provided for seed files: # You can specify an alternative connection when generating a seeder. bin/cake bake seed Articles --connection connection -.. versionadded:: cakephp/migrations 1.6.4 - - Options ``--data``, ``--limit`` and ``--fields`` were added to export - data from your database. - -As of 1.6.4, the ``bake seed`` command allows you to create a seed file with -data exported from your database by using the ``--data`` flag: - -.. code-block:: bash - + # Include data from the Articles table in your seed. bin/cake bake seed --data Articles By default, it will export all the rows found in your table. You can limit the @@ -787,16 +765,14 @@ that the same seeder can be applied multiple times. Calling a Seeder from another Seeder ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -.. versionadded:: cakephp/migrations 1.6.2 - Usually when seeding, the order in which to insert the data must be respected to not encounter constraints violations. Since Seeders are executed in the -alphabetical order by default, you can use the ``\Migrations\AbstractSeed::call()`` +alphabetical order by default, you can use the ``\Migrations\BaseSeed::call()`` method to define your own sequence of seeders execution:: - use Migrations\AbstractSeed; + use Migrations\BaseSeed; - class DatabaseSeed extends AbstractSeed + class DatabaseSeed extends BaseSeed { public function run(): void { @@ -808,11 +784,6 @@ method to define your own sequence of seeders execution:: } } -.. note:: - - Make sure to extend the Migrations plugin ``AbstractSeed`` class if you want - to be able to use the ``call()`` method. This class was added with release - 1.6.2. ``dump`` : Generating a dump file for the diff baking feature ------------------------------------------------------------- @@ -984,8 +955,7 @@ pass them to the method:: Feature Flags ============= -Migrations uses Phinx, which has some feature flags that are disabled by default for now, but -can enabled if you want them to: +Migrations offers a few feature flags to compatibility with phinx. These features are disabled by default but can be enabled if required: * ``unsigned_primary_keys``: Should Migrations create primary keys as unsigned integers? (default: ``false``) * ``column_null_default``: Should Migrations create columns as null by default? (default: ``false``) @@ -999,8 +969,6 @@ Set them via Configure to enable (e.g. in ``config/app.php``):: 'column_null_default' => true, ], -For details see `Phinx documentation `_. - Tips and tricks =============== @@ -1012,9 +980,9 @@ adding new tables to the database, you can use the second argument of the ``table()`` method:: `_. Fetching Rows -------------- +============= There are two methods available to fetch rows. The ``fetchRow()`` method will fetch a single row, whilst the ``fetchAll()`` method will return multiple rows. @@ -281,7 +279,7 @@ Both methods accept raw SQL as their only parameter. } Inserting Data --------------- +============== Migrations makes it easy to insert data into your tables. Whilst this feature is intended for the :doc:`seed feature `, you are also free to use the @@ -340,10 +338,7 @@ insert methods in your migrations. `up()` and `down()` methods. Working With Tables -------------------- - -The Table Object -~~~~~~~~~~~~~~~~ +=================== The Table object is one of the most useful APIs provided by Migrations. It allows you to easily manipulate database tables using PHP code. You can retrieve an @@ -379,7 +374,7 @@ You can then manipulate this table using the methods provided by the Table object. Saving Changes -~~~~~~~~~~~~~~ +-------------- When working with the Table object, Migrations stores certain operations in a pending changes cache. Once you have made the changes you want to the table, @@ -400,8 +395,10 @@ the more explicit methods. When in doubt with working with tables, it is always recommended to call the appropriate function and commit any pending changes to the database. +.. _creating-a-table:: + Creating a Table -~~~~~~~~~~~~~~~~ +---------------- Creating a table is really easy using the Table object. Let's create a table to store a collection of users. @@ -531,7 +528,7 @@ comment set a text comment on the table To view available column types and options, see `Valid Column Types`_ for details. Determining Whether a Table Exists -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +---------------------------------- You can determine whether or not a table exists by using the ``hasTable()`` method. @@ -565,7 +562,7 @@ method. } Dropping a Table -~~~~~~~~~~~~~~~~ +---------------- Tables can be dropped quite easily using the ``drop()`` method. It is a good idea to recreate the table again in the ``down()`` method. @@ -610,7 +607,7 @@ plan migrations when more than one table is involved. } Renaming a Table -~~~~~~~~~~~~~~~~ +---------------- To rename a table access an instance of the Table object then call the ``rename()`` method. @@ -647,7 +644,7 @@ To rename a table access an instance of the Table object then call the } Changing the Primary Key -~~~~~~~~~~~~~~~~~~~~~~~~ +------------------------ To change the primary key on an existing table, use the ``changePrimaryKey()`` method. Pass in a column name or array of columns names to include in the primary key, or ``null`` to drop the primary key. @@ -688,7 +685,7 @@ Note that the mentioned columns must be added to the table, they will not be add } Changing the Table Comment -~~~~~~~~~~~~~~~~~~~~~~~~~~ +-------------------------- To change the comment on an existing table, use the ``changeComment()`` method. Pass in a string to set as the new table comment, or ``null`` to drop the existing comment. @@ -726,13 +723,10 @@ Pass in a string to set as the new table comment, or ``null`` to drop the existi } } -Working With Columns --------------------- - .. _valid-column-types: -Valid Column Types -~~~~~~~~~~~~~~~~~~ +Working With Columns +==================== Column types are specified as strings and can be one of: @@ -761,7 +755,7 @@ In addition, the Postgres adapter supports ``interval``, ``json``, ``jsonb``, `` (PostgreSQL 9.3 and above). Valid Column Options -~~~~~~~~~~~~~~~~~~~~ +-------------------- The following are valid column options: @@ -893,7 +887,7 @@ You can pass one or more of these options to any column with the optional third argument array. Limit Option and MySQL -~~~~~~~~~~~~~~~~~~~~~~ +---------------------- When using the MySQL adapter, there are a couple things to consider when working with limits: @@ -936,7 +930,7 @@ For ``binary`` or ``varbinary`` types, if limit is set greater than allowed 255 ->create(); Custom Column Types & Default Values -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +------------------------------------ Some DBMS systems provide additional column types and default values that are specific to them. If you don't want to keep your migrations DBMS-agnostic you can use those custom types in your migrations @@ -973,7 +967,7 @@ is a function, in PostgreSQL. This method of preventing the built-in escaping is } Get a column list -~~~~~~~~~~~~~~~~~ +----------------- To retrieve all table columns, simply create a ``table`` object and call ``getColumns()`` method. This method will return an array of Column classes with basic info. Example below: @@ -1005,7 +999,7 @@ method. This method will return an array of Column classes with basic info. Exam } Get a column by name -~~~~~~~~~~~~~~~~~~~~ +-------------------- To retrieve one table column, simply create a ``table`` object and call the ``getColumn()`` method. This method will return a Column class with basic info or NULL when the column doesn't exist. Example below: @@ -1037,7 +1031,7 @@ method. This method will return a Column class with basic info or NULL when the } Checking whether a column exists -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +-------------------------------- You can check if a table already has a certain column by using the ``hasColumn()`` method. @@ -1066,7 +1060,7 @@ You can check if a table already has a certain column by using the } Renaming a Column -~~~~~~~~~~~~~~~~~ +----------------- To rename a column, access an instance of the Table object then call the ``renameColumn()`` method. @@ -1101,7 +1095,7 @@ To rename a column, access an instance of the Table object then call the } Adding a Column After Another Column -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +------------------------------------ When adding a column with the MySQL adapter, you can dictate its position using the ``after`` option, where its value is the name of the column to position it after. @@ -1130,7 +1124,7 @@ This would create the new column ``city`` and position it after the ``email`` co created as the first column in that table. Dropping a Column -~~~~~~~~~~~~~~~~~ +----------------- To drop a column, use the ``removeColumn()`` method. @@ -1155,7 +1149,7 @@ To drop a column, use the ``removeColumn()`` method. Specifying a Column Limit -~~~~~~~~~~~~~~~~~~~~~~~~~ +------------------------- You can limit the maximum length of a column by using the ``limit`` option. @@ -1179,7 +1173,7 @@ You can limit the maximum length of a column by using the ``limit`` option. } Changing Column Attributes -~~~~~~~~~~~~~~~~~~~~~~~~~~ +-------------------------- To change column type or options on an existing column, use the ``changeColumn()`` method. See :ref:`valid-column-types` and `Valid Column Options`_ for allowed values. @@ -1577,7 +1571,7 @@ plan migrations when more than one table is involved. Using the Query Builder ------------------------ +======================= It is not uncommon to pair database structure changes with data changes. For example, you may want to migrate the data in a couple columns from the users to a newly created table. For this type of scenarios, @@ -1609,7 +1603,7 @@ be easy to work with as it resembles very closely plain SQL. Accesing the query } Selecting Fields -~~~~~~~~~~~~~~~~ +---------------- Adding fields to the SELECT clause: @@ -1629,7 +1623,7 @@ Adding fields to the SELECT clause: Where Conditions -~~~~~~~~~~~~~~~~ +---------------- Generating conditions: @@ -1732,8 +1726,7 @@ When using the expression objects you can use the following methods to create co Aggregates and SQL Functions -~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - +---------------------------- .. code-block:: php @@ -1771,7 +1764,7 @@ other SQL literals. Bound parameters can be used to safely add user data to SQL Getting Results out of a Query -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +------------------------------ Once you’ve made your query, you’ll want to retrieve rows from it. There are a few ways of doing this: @@ -1789,7 +1782,7 @@ Once you’ve made your query, you’ll want to retrieve rows from it. There are Creating an Insert Query -~~~~~~~~~~~~~~~~~~~~~~~~ +------------------------ Creating insert queries is also possible: @@ -1837,7 +1830,7 @@ The above code will generate: Creating an update Query -~~~~~~~~~~~~~~~~~~~~~~~~ +------------------------ Creating update queries is similar to both inserting and selecting: @@ -1853,7 +1846,7 @@ Creating update queries is similar to both inserting and selecting: Creating a Delete Query -~~~~~~~~~~~~~~~~~~~~~~~ +----------------------- Finally, delete queries: