dev-master
9999999-dev https://github.com/cbaykam/php-migrationsDatabase migrations for php 5.x
MIT
The Requires
- php >=5.3.0
by Cem Baykam
database migration
Database migrations for php 5.x
This plugin enables to migrate your db tables as in Ruby on Rails., (*1)
With Git clone or download To install the plugin download it and put it in a sub directory in your project, (*2)
Copy the config.php.sample to config.php and fill it with you configuration variables, (*3)
Make sure you have the rights to execute the migrate.php Move the config.php.sample to config.php and update the contents with your db credentials., (*4)
./migrate.php install
To generate a migration you have to run the command below. With specifying the class name with underscores., (*5)
./migrate.php generate the_name_of_the_migration
This will generate a php file under versions directory with the unix timestamp., (*6)
Open the migration file you generated and in the up method add the change you want to add. In the down method add the reverse functions for rolling back the change if something goes wrong., (*7)
Adding a table, (*8)
class CreateUsersTable extends Migrations{ public function up(){ $this->create_table('users', array( array('primaryKey', array('name_of_key_1')), // This is for adding a primary key, Supports multiple primary keys. array('username', 'string'), array('password', array('type' => 'string', 'length' => 252)), array('sign_in_count', 'integer') )); } // We have to code the reverse of it for being able to rollback public function down(){ $this->drop_table('users'); } }
you can either choose to specify the field type with string by choosing defaults or an array if you want to customize., (*9)
Adding a field, (*10)
class AddAStrangeFieldToUsers extends Migrations{ public function up(){ $this->add_field('users', 'strange_field', array('type' => 'string', 'length' => 12)); $this->add_field('users', 'strange_notnull', array('type' => 'integer', 'null' => false)); $this->add_field('users', 'strange_two', 'integer'); $this->add_field('users', 'strange_three', 'string'); } public function down(){ $this->remove_field('users', 'strange_field'); $this->remove_field('users', 'strange_notnull'); $this->remove_field('users', 'strange_two'); $this->remove_field('users', 'strange_three'); } }
Removing a field, (*11)
class RemoveVeryImportantField extends Migrations{ public function up(){ $this->remove_field('users', 'strange_two'); } public function down(){ $this->add_field('users', 'strange_two', 'string'); } }
Adding a primary key, (*12)
class AddPrimaryKey extends Migrations{ public function up(){ $this-> add_primary_key('users', array('id')); // For existing fields. Also can be array of multiple fields } $this->remove_primary_key('users'); } }
Removing a primary key, (*13)
class RemovePrimaryKey extends Migrations{ public function up(){ $this->remove_primary_key('users'); } public function down(){ $this-> add_primary_key('users', array('id')); // For existing fields. Also can be array of multiple fields // For adding a new id key: // $this->add_field('users', 'id', array('type' => 'integer', 'null' => false, 'autoincrement' => true, 'primarykey' => true, 'first' => true)); } }
Removing a table, (*14)
class CreateUsersTable extends Migrations{ public function up(){ $this->drop_table('users'); } public function down(){ $this->create_table('users', array( array('username', 'string'), array('password', array('type' => 'string', 'length' => 252)), array('sign_in_count', 'integer') )); } }
Executing a raw sql query, (*15)
class AddSpecialColumn extends Migrations{ public function up(){ $this->runquery('ALTER TABLE `users` ADD `is_advertiser` BOOLEAN NOT NULL DEFAULT FALSE AFTER `is_presenter`;'); } public function down(){ $this->remove_field('users','is_advertiser'); } }
Running migrations, (*16)
./migrate.php run
Install the plugin, export the latest version of your database and generate a new migration ie: database.sql, (*17)
./migrate.php generate initial_version
Then open the migration file and in the up method please insert, (*18)
$this->runsql('database.sql');
The database sql file path is relative to the path you installed the plugin., (*19)
Database migrations for php 5.x
MIT
database migration