2017 © Pedro Peláez
 

library php-migrations

Database migrations for php 5.x

image

cbaykam/php-migrations

Database migrations for php 5.x

  • Friday, March 13, 2015
  • by cbaykam
  • Repository
  • 1 Watchers
  • 1 Stars
  • 3 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Php Standalone Migrations Plugin v0.0.1

This plugin enables to migrate your db tables as in Ruby on Rails., (*1)

Installation :

  • With Composer
    • Add cbaykam/php-migrations To your composer.json file composer.phar install
  • 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)

  • If you are using composer please make sure that your versions dir is outside the vendor directory

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 

Generating Migrations :

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)

Coding migration files :

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 

Field types and options

  • String length
  • Integer length null autoincrement primarykey first
  • Enum options
  • Datetime null options
  • ForeignKey options

For existing projects

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)

Todos

  • Add more datatypes
  • Make output messages smarter
  • Add schema dump.
  • Add availability to handle migrations with the same class name.
  • Support Postgresql

The Versions

13/03 2015

dev-master

9999999-dev https://github.com/cbaykam/php-migrations

Database migrations for php 5.x

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Cem Baykam

database migration