Automated database migrations are extremely useful when doing Continuous Integration and Delivery. Phinx is a simple PHP standalone tool for writing database migrations for existing or new applications. We like it.
Hello Future • @hellofutur3 on Twitter • DevOps, Continuous Delivery, Chef and cloud automation consultancy • Web application development What do we do?
Migrations • Changes to databases • Store them in your SCM system (Git, SVN, Mercurial, CVS…) • Handy for Continuous Integration, Continuous Delivery and Continuous Deployment What do you mean by that?
Phinx • Portable amongst different database vendors (MySQL, PostgreSQL, SQLite, SQL Server) • Good for existing projects with a custom framework where no existing migration tools exist • Also good for new projects Handy PHP database migration tool
Phinx /** * Change. */ public function change() { $users = $this->table('users'); $users->addColumn('username', ‘string’, array('limit' => 20)) ->addColumn('password', ‘string’, array('limit' => 40)) ->addColumn('salt', 'string', array('limit' => 40)) ->addColumn('email', ‘string’, array('limit' => 100)) ->addIndex(array('username', ‘email’), array('unique' => true)) ->create(); } Change will automatically create the down migration for you, again this may not be what you want!!
Phinx public function up() { $table = $this->table('users'); $table->rename('legacy_users'); } public function down() { $table = $this->table('legacy_users'); $table->rename('users'); } Rename a table
Phinx Handy PHP database migration tool Run migrations $ phinx migrate Rollback migrations $ phinx rollback Get status of which migrations have been run $ phinx status
Phinx • Use on existing projects • Good for CD with projects as you can do migrations as part of your deployment scripts (think about the order you do things) • Simple to use, simple solution to most problems Recap