@@ -181,14 +181,120 @@ The Run Method
181181==============
182182
183183The run method is automatically invoked by Migrations when you execute the
184- ``cake migration seed `` command. You should use this method to insert your test
184+ ``cake seeds run `` command. You should use this method to insert your test
185185data.
186186
187+ Seed Execution Tracking
188+ ========================
189+
190+ Seeds track their execution state in the ``cake_seeds `` database table. By default,
191+ a seed will only run once. If you attempt to run a seed that has already been
192+ executed, it will be skipped with an "already executed" message.
193+
194+ To re-run a seed that has already been executed, use the ``--force `` flag:
195+
196+ .. code-block :: bash
197+
198+ bin/cake seeds run Users --force
199+
200+ You can check which seeds have been executed using the status command:
201+
202+ .. code-block :: bash
203+
204+ bin/cake seeds status
205+
206+ To reset all seeds' execution state (allowing them to run again without ``--force ``):
207+
208+ .. code-block :: bash
209+
210+ bin/cake seeds reset
211+
187212 .. note ::
188213
189- Unlike with migrations, seeds do not keep track of which seed classes have
190- been run. This means database seeds can be run repeatedly. Keep this in
191- mind when developing them.
214+ When re-running seeds with ``--force ``, be careful to ensure your seeds are
215+ idempotent (safe to run multiple times) or they may create duplicate data.
216+
217+ Customizing the Seed Tracking Table
218+ ------------------------------------
219+
220+ By default, seed execution is tracked in a table named ``cake_seeds ``. You can
221+ customize this table name by configuring it in your ``config/app.php `` or
222+ ``config/app_local.php ``:
223+
224+ .. code-block :: php
225+
226+ 'Migrations' => [
227+ 'seed_table' => 'my_custom_seeds_table',
228+ ],
229+
230+ This is useful if you need to avoid table name conflicts or want to follow
231+ a specific naming convention in your database.
232+
233+ Idempotent Seeds
234+ ================
235+
236+ Some seeds are designed to be run multiple times safely (idempotent), such as seeds
237+ that update configuration or reference data. For these seeds, you can override the
238+ ``isIdempotent() `` method to skip tracking entirely:
239+
240+ .. code-block :: php
241+
242+ <?php
243+ declare(strict_types=1);
244+
245+ use Migrations\BaseSeed;
246+
247+ class ConfigSeed extends BaseSeed
248+ {
249+ /**
250+ * Mark this seed as idempotent.
251+ * It will run every time without being tracked.
252+ */
253+ public function isIdempotent(): bool
254+ {
255+ return true;
256+ }
257+
258+ public function run(): void
259+ {
260+ // This seed will run every time, so make it safe to run multiple times
261+ $this->execute("
262+ INSERT INTO settings (setting_key, setting_value)
263+ VALUES ('app_version', '2.0.0')
264+ ON DUPLICATE KEY UPDATE setting_value = '2.0.0'
265+ ");
266+
267+ // Or check before inserting
268+ $exists = $this->fetchRow(
269+ "SELECT COUNT(*) as count FROM settings WHERE setting_key = 'maintenance_mode'"
270+ );
271+
272+ if ($exists['count'] == 0) {
273+ $this->table('settings')->insert([
274+ 'setting_key' => 'maintenance_mode',
275+ 'setting_value' => 'false',
276+ ])->save();
277+ }
278+ }
279+ }
280+
281+ When ``isIdempotent() `` returns ``true ``:
282+
283+ - The seed will **not ** be tracked in the ``cake_seeds `` table
284+ - The seed will run **every time ** you execute ``seeds run ``
285+ - You must ensure the seed's ``run() `` method handles duplicate executions safely
286+
287+ This is useful for:
288+
289+ - Configuration seeds that should always reflect current values
290+ - Reference data that may need periodic updates
291+ - Seeds that use ``INSERT ... ON DUPLICATE KEY UPDATE `` or similar patterns
292+ - Development/testing seeds that need to run repeatedly
293+
294+ .. warning ::
295+
296+ Only mark a seed as idempotent if you've verified it's safe to run multiple times.
297+ Otherwise, you may create duplicate data or other unexpected behavior.
192298
193299The Init Method
194300===============
@@ -246,10 +352,28 @@ You can also use the full seed name including the ``Seed`` suffix:
246352
247353 Both forms are supported and work identically.
248354
355+ Automatic Dependency Execution
356+ -------------------------------
357+
358+ When you run a seed that has dependencies, the system will automatically check if
359+ those dependencies have been executed. If any dependencies haven't run yet, they
360+ will be executed automatically before the current seed runs. This ensures proper
361+ execution order and prevents foreign key constraint violations.
362+
363+ For example, if you run:
364+
365+ .. code-block :: bash
366+
367+ bin/cake seeds run ShoppingCartSeed
368+
369+ And ``ShoppingCartSeed `` depends on ``UserSeed `` and ``ShopItemSeed ``, the system
370+ will automatically execute those dependencies first if they haven't been run yet.
371+
249372.. note ::
250373
251- Dependencies are only considered when executing all seed classes (default behavior).
252- They won't be considered when running specific seed classes.
374+ Dependencies that have already been executed (according to the ``cake_seeds ``
375+ table) will be skipped, unless you use the ``--force `` flag which will
376+ re-execute all seeds including dependencies.
253377
254378
255379Calling a Seed from another Seed
@@ -371,37 +495,37 @@ SQL `TRUNCATE` command:
371495Executing Seed Classes
372496======================
373497
374- This is the easy part. To seed your database, simply use the ``migrations seed `` command:
498+ This is the easy part. To seed your database, simply use the ``seeds run `` command:
375499
376500.. code-block :: bash
377501
378- $ bin/cake migrations seed
502+ $ bin/cake seeds run
379503
380504 By default, Migrations will execute all available seed classes. If you would like to
381- run a specific class , simply pass in the name of it using the `` --seed `` parameter .
505+ run a specific seed , simply pass in the seed name as an argument .
382506You can use either the short name (without the ``Seed `` suffix) or the full name:
383507
384508.. code-block :: bash
385509
386- $ bin/cake migrations seed --seed User
510+ $ bin/cake seeds run User
387511 # or
388- $ bin/cake migrations seed --seed UserSeed
512+ $ bin/cake seeds run UserSeed
389513
390514 Both commands work identically.
391515
392- You can also run multiple seeds:
516+ You can also run multiple seeds by separating them with commas :
393517
394518.. code-block :: bash
395519
396- $ bin/cake migrations seed --seed User --seed Permission --seed Log
520+ $ bin/cake seeds run User, Permission, Log
397521 # or with full names
398- $ bin/cake migrations seed --seed UserSeed --seed PermissionSeed --seed LogSeed
522+ $ bin/cake seeds run UserSeed, PermissionSeed, LogSeed
399523
400524 You can also use the `-v ` parameter for more output verbosity:
401525
402526.. code-block :: bash
403527
404- $ bin/cake migrations seed -v
528+ $ bin/cake seeds run -v
405529
406530 The Migrations seed functionality provides a simple mechanism to easily and repeatably
407531insert test data into your database, this is great for development environment
0 commit comments