Skip to content

k1T4eR/php-database-migration

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

TODO FULLY REFACTOR README

database-migration

Example. MySql migration. Version is stored in database. Source code is taken from file.

Clone src into DatabaseMigration inside your library folder.

Setup database:

CREATE TABLE IF NOT EXISTS `migration` (
  `version` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- Insert initial record
INSERT INTO `migration` (`version`) VALUES ('0');

Setup autoload:

require_once PATH_TO_YOUR_LIBRARY . '/DatabaseMigration/Autoload.php';

Define database adapter:

class DatabaseAdapter
    implements
      DatabaseMigration\Database\AdapterInterface {

    /**
     * @param \DatabaseMigration\Query $query
     * @return mixed
     */
    public function query(\DatabaseMigration\Query $query) {
        // run $query->getCode() here
    }
    
}

Define version provider:

class VersionProvider
    implements
      \DatabaseMigration\Version\ProviderInterface {

    /**
     * @return int
     */
    public function getNumber() {
        // do something like 'SELECT version FROM migration'
    }

    /**
     * @param int $v
     * @return mixed
     */
    public function setNumber($v) {
      // do something like 'UPDATE migration SET version = $v'
    }
    
}

Instantiate dependencies and run migration:

  $filename = './migration.sql'; // migrations are stored in file
  
  $source = new DatabaseMigration\Source\FileSource($filename);
  $type = new DatabaseMigration\Database\DatabaseType($filename);
  $adapter = new DatabaseAdapter();
  $versionProvider = new VersionProvider();
  
  $migration = new DatabaseMigration\Migration();
  $migration->setSource($source);
  $migration->setDatabaseType($type);
  $migration->setVersionProvider($versionProvider);
  $migration->setDatabaseAdapter($adapter);
  
  $migration->run();

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages