Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4.next merge to 4.x #771

Merged
merged 62 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
f0084b4
Port insert & bulkinsert compatibility from phinx
markstory Sep 3, 2024
d3a9e9f
Fix postgres tests
markstory Sep 3, 2024
a1bc157
Fix sqlserver tests
markstory Sep 5, 2024
c552f42
Merge pull request #742 from cakephp/literal-insert
markstory Sep 5, 2024
3403891
Merge branch '4.x' into 4.next-merge
LordSimal Sep 13, 2024
602ecfa
Merge pull request #746 from cakephp/4.next-merge
LordSimal Sep 13, 2024
491b311
Start to add Migrations versions of base interfaces
markstory Sep 18, 2024
fe677f4
Fix file name
markstory Sep 18, 2024
d69b33d
Merge pull request #749 from cakephp/seedinterface
markstory Sep 21, 2024
f04d92f
4.next - Revise the migrations interfaces
markstory Sep 23, 2024
c83f65d
Merge pull request #750 from cakephp/refine-interface
markstory Sep 24, 2024
fa26f5a
Add and use a ShimAdapter
markstory Sep 26, 2024
8e5faa4
Fix test and phpcs
markstory Sep 27, 2024
2c55d5c
Fix more phpcs
markstory Sep 27, 2024
a094851
Merge pull request #751 from cakephp/seed-adapter
markstory Sep 27, 2024
62b88cd
Fix up returns and doctypes
markstory Sep 29, 2024
131c717
Move phinx compatibility shim logic into a migration wrapper
markstory Sep 29, 2024
0629a5e
Get more tests passing
markstory Sep 29, 2024
46a3443
Fix phpcs and psalm
markstory Sep 30, 2024
892cacf
Merge pull request #752 from cakephp/migrations-adapter
markstory Oct 2, 2024
17ead38
Import most of Phinx\Util\Util
markstory Oct 4, 2024
b720bb5
Import tests as well
markstory Oct 4, 2024
7b018f1
Apply formatting and type checking rules
markstory Oct 4, 2024
c161d43
Fix provider signatures
markstory Oct 5, 2024
33c7c93
Remove isValidPhinxFileName
markstory Oct 5, 2024
438722e
Remove Util::relativePath
markstory Oct 5, 2024
342ce33
Fix namespaces and mark Util as internal
markstory Oct 5, 2024
432c728
Fix phpcs failure
markstory Oct 5, 2024
a2e46b8
Clean up baseline file
markstory Oct 7, 2024
2648e31
Merge pull request #753 from cakephp/import-util
markstory Oct 7, 2024
2b2890a
Merge branch '4.x' into 4.next
markstory Oct 9, 2024
3c490e7
Add deprecations for phinx commands
markstory Oct 10, 2024
5faeedd
Replace phinx constants with Migrations clones
markstory Oct 10, 2024
58023ec
Define an interface for migration backends
markstory Oct 10, 2024
7a7e5e7
Remove references to Symfony
markstory Oct 10, 2024
feb9579
Fix interface reference and add missed file.
markstory Oct 10, 2024
8a97d7c
Merge branch '4.next' into reduce-phinx
markstory Oct 10, 2024
418a103
Fix up broken behavior with changes in 4.next
markstory Oct 10, 2024
928cd37
Add method I missed
markstory Oct 10, 2024
9eed02b
Update baseline for deprecations and interface compat
markstory Oct 10, 2024
a3f90b9
Merge pull request #756 from cakephp/reduce-phinx
markstory Oct 13, 2024
ef65019
Merge branch '4.x' into 4.next
markstory Oct 19, 2024
8681b80
4.next - Add base seed (#760)
markstory Oct 26, 2024
e5836ac
4.next - Add migration BaseClass (#761)
markstory Oct 28, 2024
b0fc9af
Update bake commands to use builtin base classe (#764)
markstory Nov 2, 2024
6768a06
Import documentation for seed + migration authoring (#765)
markstory Nov 5, 2024
6865664
Made the migrationdiff command tests runnable locally
markstory Nov 7, 2024
716199e
Update templates and snapshots for bake migration_diff
markstory Nov 7, 2024
a1dbe87
Leave some todos for the next time.
markstory Nov 7, 2024
4a6b14e
Merge pull request #766 from cakephp/bake-migration-diff
markstory Nov 7, 2024
0336f34
Add support for the builtin backend to migration_snapshot
markstory Nov 14, 2024
5e6d8bb
Get all migration_snapshot tests passing locally
markstory Nov 14, 2024
fb0ab99
Update snapshots and cleanup
markstory Nov 14, 2024
58f2905
Extend class hacking for new base class
markstory Nov 14, 2024
327c371
Chill phpstan
markstory Nov 14, 2024
ca8e100
Change variable names to make types simpler.
markstory Nov 14, 2024
ffe8a41
Allow a deprecated method
markstory Nov 15, 2024
7644abd
Merge pull request #767 from cakephp/bake-migration-snap
markstory Nov 15, 2024
19144eb
Fix timer output being included at non-verbose output levels
markstory Nov 23, 2024
706eaf4
Merge branch '4.x' into 4.next
markstory Nov 23, 2024
5500338
Fix baseline
markstory Nov 23, 2024
e2336ae
Update psalm baseline
markstory Nov 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
229 changes: 229 additions & 0 deletions docs/en/seeding.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
Database Seeding
================

Seed classes are a great way to easily fill your database with data after
it's created. By default they are stored in the `seeds` directory; however, this
path can be changed in your configuration file.

.. note::

Database seeding is entirely optional, and Migrations does not create a `Seeds`
directory by default.

Creating a New Seed Class
-------------------------

Migrations includes a command to easily generate a new seed class:

.. code-block:: bash

$ bin/cake bake seed MyNewSeeder

It is based on a skeleton template:

.. code-block:: php

<?php

use Migrations\BaseSeed;

class MyNewSeeder extends BaseSeed
{
/**
* Run Method.
*
* Write your database seeder using this method.
*
* More information on writing seeders is available here:
* https://book.cakephp.org/migrations/5/en/seeding.html
*/
public function run() : void
{

}
}

The AbstractSeed Class
----------------------

All Migrations seeds extend from the ``BaseSeed`` or ``AbstractSeed`` classes.
These classes provide the necessary support to create your seed classes. Seed
classes are primarily used to insert test data.

The Run Method
~~~~~~~~~~~~~~

The run method is automatically invoked by Migrations when you execute the
``cake migration seed`` command. You should use this method to insert your test
data.

.. note::

Unlike with migrations, seeds do not keep track of which seed classes have
been run. This means database seeders can be run repeatedly. Keep this in
mind when developing them.

The Init Method
~~~~~~~~~~~~~~~

The ``init()`` method is run by Migrations before the run method if it exists. This
can be used to initialize properties of the Seed class before using run.

The Should Execute Method
~~~~~~~~~~~~~~~~~~~~~~~~~

The ``shouldExecute()`` method is run by Migrations before executing the seed.
This can be used to prevent the seed from being executed at this time. It always
returns true by default. You can override it in your custom ``BaseSeed``
implementation.

Foreign Key Dependencies
~~~~~~~~~~~~~~~~~~~~~~~~

Often you'll find that seeders need to run in a particular order, so they don't
violate foreign key constraints. To define this order, you can implement the
``getDependencies()`` method that returns an array of seeders to run before the
current seeder:

.. code-block:: php

<?php

use Migrations\BaseSeed;

class ShoppingCartSeeder extends BaseSeed
{
public function getDependencies()
{
return [
'UserSeeder',
'ShopItemSeeder'
];
}

public function run() : void
{
// Seed the shopping cart after the `UserSeeder` and
// `ShopItemSeeder` have been run.
}
}

.. note::

Dependencies are only considered when executing all seed classes (default behavior).
They won't be considered when running specific seed classes.

Inserting Data
--------------

Using The Table Object
~~~~~~~~~~~~~~~~~~~~~~

Seed classes can also use the familiar `Table` object to insert data. You can
retrieve an instance of the Table object by calling the ``table()`` method from
within your seed class and then use the `insert()` method to insert data:

.. code-block:: php

<?php

use Migrations\BaseSeed;

class PostsSeeder extends BaseSeed
{
public function run() : void
{
$data = [
[
'body' => 'foo',
'created' => date('Y-m-d H:i:s'),
],[
'body' => 'bar',
'created' => date('Y-m-d H:i:s'),
]
];

$posts = $this->table('posts');
$posts->insert($data)
->saveData();
}
}

.. note::

You must call the ``saveData()`` method to commit your data to the table.
Migrations will buffer data until you do so.

Truncating Tables
-----------------

In addition to inserting data Migrations makes it trivial to empty your tables using the
SQL `TRUNCATE` command:

.. code-block:: php

<?php

use Migrations\BaseSeed;

class UserSeeder extends BaseSeed
{
public function run() : void
{
$data = [
[
'body' => 'foo',
'created' => date('Y-m-d H:i:s'),
],
[
'body' => 'bar',
'created' => date('Y-m-d H:i:s'),
]
];

$posts = $this->table('posts');
$posts->insert($data)
->saveData();

// empty the table
$posts->truncate();
}
}

.. note::

SQLite doesn't natively support the ``TRUNCATE`` command so behind the scenes
``DELETE FROM`` is used. It is recommended to call the ``VACUUM`` command
after truncating a table. Migrations does not do this automatically.

Executing Seed Classes
----------------------

This is the easy part. To seed your database, simply use the ``migrations seed`` command:

.. code-block:: bash

$ bin/cake migrations seed

By default, Migrations will execute all available seed classes. If you would like to
run a specific class, simply pass in the name of it using the ``--seed`` parameter:

.. code-block:: bash

$ bin/cake migrations seed --seed UserSeeder

You can also run multiple seeders:

.. code-block:: bash

$ bin/cake migrations seed --seed UserSeeder --seed PermissionSeeder --seed LogSeeder

You can also use the `-v` parameter for more output verbosity:

.. code-block:: bash

$ bin/cake migrations seed -v

The Migrations seed functionality provides a simple mechanism to easily and repeatably
insert test data into your database, this is great for development environment
sample data or getting state for demos.
Loading
Loading