Migrations are a convenient way to manage the structure and changes of the database schema.Of course, you can do business the old-fashioned way, using many SQL files, or, oh, horror !, editing pieces of SQL code in one large file, which is the actual database schema.
It is assumed that you use Composer to manage dependencies. If not, be sure to try, it's worth it!
Also, if you wish, you can clone the repository with the GitHub example: github.com/ArtemPyanykh/php_migrations_example
"require" : { "ruckusing/ruckusing-migrations": "dev-master" }
~/dev/php_migrations_example(master)$ php composer.phar install
vendors
directory. . βββ db βββ logs βββ migrations β βββ main β βββ php_migrations_example -> main/ β βββ php_migrations_example_test -> main/ βββ utility
db
directory. Consider this directory a point of reference for everything related to migrations.db/migrations/main
.php_migrations_example
and php_migrations_example_test
are the names of the development and test databases.main
directory, since you are unlikely to have different migrations for different environments.db/ruckus
with the following contents: #!/bin/bash ruckus_dir="./../vendor/ruckusing/ruckusing-migrations" if [ ! -d $ruckus_dir ]; then echo "Ruckusing-Migrations wasn't detected. Please, use Composer to install the library." exit fi if [ ! -f "ruckusing.conf.php" ]; then echo "Ruckusing conf. file wasn't detected. Please, create proper ruckusing.conf.php." exit fi if [ "$#" -lt 1 ]; then echo "At least 1 argument required" echo "See ./ruckus --help" exit fi if [ "$1" == "--help" -o "$1" == "-h" ]; then echo "Usage: ./ruckus [--help] <task-name> [<parameter1>[ <parameter2> [...]]]" echo "" echo "The available ruckus commands are:" echo " ./ruckus db:migrate Ruckus scenario for tasks such as db:migrate, db:setup, etc." echo " ./ruckus db:generate <migration_name> Ruckus scenarion for generating migration scaffolding" echo "" exit fi php $ruckus_dir"/ruckus.php" "$1" "${@:2}"
db/ruckusing.conf.php
config file: <?php //---------------------------- // DATABASE CONFIGURATION //---------------------------- /* Valid types (adapters) are Postgres & MySQL: 'type' must be one of: 'pgsql' or 'mysql' */ return array( 'db' => array( 'development' => array( 'type' => 'mysql', 'host' => 'localhost', 'port' => 3306, 'database' => 'php_migrations_example', 'user' => 'root', 'password' => 'root' ), 'test' => array( 'type' => 'mysql', 'host' => 'localhost', 'port' => 3306, 'database' => 'php_migrations_example_test', 'user' => 'root', 'password' => 'root' ) ), 'migrations_dir' => RUCKUSING_WORKING_BASE . '/migrations', 'db_dir' => RUCKUSING_WORKING_BASE . '/utility', 'log_dir' => RUCKUSING_WORKING_BASE . '/logs' ); ?>
~/dev/php_migrations_example(master)$ cd db ~/dev/php_migrations_example/db(master)$ ./ruckus db:generate CreateTestTable Created migration: 20130508145210_CreateTestTable.php
<?php class CreateTestTable extends Ruckusing_Migration_Base { public function up() { }//up() public function down() { }//down() }
<?php class CreateTestTable extends Ruckusing_Migration_Base { public function up() { $table = $this->create_table('test', array('options' => 'ENGINE=InnoDB DEFAULT CHARSET=utf8')); $table->column('this', 'integer', array('unsigned' => true, 'null' => false, 'default' => '42')); $table->column('that', 'string', array('limit' => '7')); $table->column('those', 'datetime'); $table->finish(); $this->add_index('test', array('this', 'that'), array('unique' => true)); }//up() public function down() { $this->drop_table('test'); }//down() }
~/dev/php_migrations_example/db(master)$ ./ruckus db:migrate Started: 2013-05-08 7:05pm MSK [db:migrate]: Schema version table does not exist. Auto-creating. Creating schema version table: schema_migrations Migrating UP: ========= CreateTestTable ======== (0.31) Finished: 2013-05-08 7:05pm MSK
~/dev/php_migrations_example/db(master)$ ./ruckus db:migrate VERSION=-1 Started: 2013-05-08 7:13pm MSK [db:migrate]: Migrating DOWN: ========= CreateTestTable ======== (0.21) Finished: 2013-05-08 7:13pm MSK ~/dev/php_migrations_example/db(master)$ ./ruckus db:migrate ENV=test Started: 2013-05-08 7:14pm MSK [db:migrate]: Schema version table does not exist. Auto-creating. Creating schema version table: schema_migrations Migrating UP: ========= CreateTestTable ======== (0.24) Finished: 2013-05-08 7:14pm MSK
ruckus
script not from the db/
directory, you will get an error. This is due to the fact that all the paths in the script are relative, and if desired, this is easily corrected. It is necessary, however, to take into account one thing: by default, the config is searched in the working directory.Source: https://habr.com/ru/post/179155/
All Articles