Пример #1
0
 function __construct($config, $migrationsDir)
 {
     $this->migrationsDir = $migrationsDir;
     $mysqli = new \mysqli($config['host'], $config['username'], $config['password'], $config['dbname']);
     if ($mysqli->connect_errno) {
         throw new \Exception("Failed to connect to MySQL: " . $mysqli->connect_error);
     }
     Connection::setMysqli($mysqli);
     Loader::setMigrationsDir($migrationsDir);
 }
Пример #2
0
 private static function generateTable($version, Table $table)
 {
     $className = Loader::classNameForVersion($table->getName(), $version);
     $template = new Template('table');
     $template->set('className', $className);
     Generator::generateGeneral($template, $table);
     Generator::generateColumns($template, $table);
     Generator::generateIndexes($template, $table);
     Generator::generateReferences($template, $table);
     $file = Loader::fileForVersion($table->getName(), $version);
     file_put_contents($file, $template->getContent());
 }
Пример #3
0
 public function migrate()
 {
     echo "\n" . $this->version . "\n";
     $dir = Loader::dirForVersion($this->version);
     $files = glob($dir . "*.php");
     $tableClasses = array_map(function ($element) {
         return basename($element, ".php");
     }, $files);
     // Load tables
     foreach ($tableClasses as $table) {
         Loader::loadModelVersion($table, $this->version);
         $class = Loader::classNameForVersion($table, $this->version);
         $this->tables[] = new $class();
     }
     $current = Connection::getTableNames();
     $removed = array_diff($current, $tableClasses);
     //we don't want the db_migration table removed
     if (($key = array_search('db_migration', $removed)) !== false) {
         unset($removed[$key]);
     }
     Connection::begin();
     // pre action for every table
     foreach ($this->tables as $table) {
         if (method_exists($table, 'pre')) {
             $table->pre();
         }
     }
     $this->dropReferences();
     $this->dropIndexes();
     $this->doTables();
     $this->createIndexes();
     $this->createReferences();
     echo "Dropping tables\n";
     Connection::dropTables($removed);
     // post action for every table
     foreach ($this->tables as $table) {
         if (method_exists($table, 'post')) {
             $table->post();
         }
     }
     Connection::commit();
     echo "\n";
 }