agentleFS
AnyOrgNameAnyOrgName/php-skills/skills/yii2/migrations/SKILL.md

migrations

Skill4 starsChanged 5 months agoMITView on GitHub

Evolve the database schema with Yii2 migration classes — generate a migration, implement reversible safeUp and safeDown, and apply migrations through the console. Use when the user needs to apply or revert Yii2 migrations through the yii migrate console command; the user wants to create a Yii2 migration class to change the database schema; the user is implementing a Yii2 migration safeUp and safeDown so the schema change can be rolled back. Keywords: yii2, migrations, migration, schema, safeup, safedown, php74.

8 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use schema-design.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • HamzaSheikh768Skill

    schema-design

    Designs normalized, performant, and future-proof PostgreSQL database schemas with proper normalization, indexing strategy, constraints, partitioning considerations, and scalability planning.

    Also in 3 projects that use this

The file

We split this skill into the rules it gives an agent

5 rules. 0 are common, 3 are distinctive, and 2 are very specific to this project.

  • 0

    Common

    Most of the 5 edited versions we read keep these.

  • 3

    Distinctive

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Do not run ad hoc ALTER TABLE statements directly against a shared database instead of writing a migration.

    line 63
  2. Distinctive:

    Do not use migrations to load large volumes of application data that belongs in a seeder or fixture.

    line 64
  3. Very specific:

    Run yii migrate/create create_order_table to generate a timestamped migration class skeleton.

    line 72
  4. Very specific:

    Run yii migrate/up, then confirm yii migrate/down 1 reverts the change without errors.

    line 74
  5. Distinctive:

    Not applicable — no I/O, auth, or untrusted input.

    line 105

“Common” is measured: kept by at least 6 in 10 of the 5 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md
<!-- generated: do not edit — source: knowledge/atoms/yii2/migrations/ --><!-- schema: atomVersion=3 skillVersion=2 graphVersion=1 --><!-- compile: @php-skills/compiler v1.0.0 (deterministic build) -->

Yii2 Database Migrations

When to use

  • The user needs to apply or revert Yii2 migrations through the yii migrate console command.
  • The user wants to create a Yii2 migration class to change the database schema.
  • The user is implementing a Yii2 migration safeUp and safeDown so the schema change can be rolled back.

When NOT to use

  • Do not run ad hoc ALTER TABLE statements directly against a shared database instead of writing a migration.
  • Do not use migrations to load large volumes of application data that belongs in a seeder or fixture.

Core guidance

  • Prefer safeUp/safeDown over up/down — Implement safeUp() and safeDown() rather than up()/down() so each migration runs inside a transaction on databases that support transactional DDL.

Quick recipe

  1. Run yii migrate/create create_order_table to generate a timestamped migration class skeleton. (verify: A new migration file appears in the migrations directory.)
  2. Build the table in safeUp() with the schema builder and drop it in safeDown() so the change is reversible. (verify: safeDown() cleanly removes everything safeUp() created.)
  3. Run yii migrate/up, then confirm yii migrate/down 1 reverts the change without errors. (verify: The schema returns to its prior state after down.)

Best practices

  • Give foreign keys and indexes explicit, stable names in addForeignKey()/createIndex() so safeDown() can drop them by the same name
  • Scope each migration to a single logical schema change and use a descriptive generated name (create_order_table) rather than batching unrelated changes
  • For each change in safeUp(), implement its inverse in safeDown(); if a change genuinely cannot be reversed, return false from safeDown() to state that explicitly
  • Define columns with the schema builder ($this->string(), $this->integer()->notNull()) instead of raw SQL type strings so the migration works across database drivers

Anti-patterns

Anti-patternWhy it failsDo instead
Bundling unrelated schema changes in one migrationA monolithic migration cannot be rolled back partially and is hard to review or cherry-pick.Split each logical change into its own focused, independently reversible migration.
Calling Active Record models inside migrationsModels evolve over time, so an old migration that references a later model shape breaks when re-run on a fresh database.Manipulate data with direct $this->update()/$this->insert() schema commands that stay stable over time.
Hard-coding driver-specific SQL DDLDriver-specific SQL breaks the moment the migration runs against a different database engine.Use the portable schema builder methods and pass engine options through table options instead.

See references/anti-patterns.md.

Common mistakes

  1. Editing an already-applied migration — Changing a migration file after it has been applied in some environments causes schema drift between them. Fix: Never edit an applied migration; write a new migration for the additional change.
  2. Rollback fails because safeDown was not implemented — Running yii migrate/down errors or does nothing because safeDown() is empty or missing. Fix: Implement the inverse operations in safeDown(), or return false to declare the migration irreversible.
  3. up() leaves a half-applied schema on failure — A migration using up() that fails midway leaves some statements applied and others not. Fix: Use safeUp()/safeDown() so the change runs transactionally and rolls back atomically on error.

Version notes

  • safeUp transactions depend on the database engine (PHP >=7.4) — safeUp/safeDown wrap changes in a transaction only where the database supports transactional DDL; MySQL/MariaDB auto-commit most DDL, so a failed multi-statement migration there may not fully roll back.

Security

  • Not applicable — no I/O, auth, or untrusted input.

Testing

  • Activation and decision behavior are verified in eval/scenarios/yii2/migrations.yaml.

Additional resources

  • references/anti-patterns.md
  • references/core.md
  • references/examples.md
  • references/pitfalls.md
  • references/recipes.md
  • references/version-differences.md

Knowledge graph

Related: yii2/structure-basics

<!-- graph-fragment: graph/fragments/yii2/migrations.yaml -->

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.