/** * * @param DataSource $ds * @return Schema */ public static function get(DataSource $ds) { if ($ds->getDriver() == "mysql") { return self::mysql($ds); } if ($ds->getDriver() == "sqlite") { return self::sqlite($ds); } }
/** * @param Migration[] * @param string $connectionName */ public static function run($migrations, $connectionName = null) { if (!is_array($migrations)) { $migrations = array($migrations); } $dataSource = \lasa\db\DataSource::getDataSource($connectionName); foreach ($migrations as $migration) { /* @var $migration Migration */ //変更対象 $dropTables = array(); $createTables = array(); $addColumns = array(); $modifyColumns = array(); $deleteColumns = array(); //テーブルの定義を取得する $schema = Schema::get($dataSource); //実行する $migration->execute(); foreach ($migration->tableList as $table) { if ($table->delete) { $dropTables[] = $table; continue; } //テーブル作成時のみ if ($table->check && !$schema->tableExists($table->getName())) { continue; } //テーブル作成 if (!$schema->tableExists($table->getName())) { $createTables[] = $table; continue; } //modify column $currentTable = $schema->table($table->getName()); $lastColumnName = null; foreach ($table->columnNames() as $columnName) { $column = $table->column($columnName); $column->table = $table->getName(); $currentColumn = $currentTable->column($columnName); //削除カラム if ($column->delete) { if ($currentColumn) { $deleteColumns[] = $column; } continue; } //追加カラム if (!$currentColumn) { $addColumns[] = array($column, $lastColumnName); $lastColumnName = $columnName; continue; } $lastColumnName = $columnName; if ($column->changed) { $modifyColumns[] = $column; continue; } //比較する if (false == $column->compare($currentColumn)) { $modifyColumns[] = $column; continue; } } } foreach ($dropTables as $table) { $sql = $schema->buildDeleteTableQuery($table); $dataSource->executeUpdateQuery($sql); } foreach ($createTables as $table) { $sql = $schema->buildCreateTableQuery($table); $dataSource->executeUpdateQuery($sql); } foreach ($addColumns as $array) { list($column, $prevous) = $array; $position = ""; if ($prevous) { $position = "AFTER " . $prevous; } $sql = $schema->buildAddColumnQuery($column, $position); $dataSource->executeUpdateQuery($sql); } foreach ($modifyColumns as $column) { $sql = $schema->buildModifyColumnQuery($column); $dataSource->executeUpdateQuery($sql); } foreach ($deleteColumns as $column) { $sql = $schema->buildDeleteColumnQuery($column); $dataSource->executeUpdateQuery($sql); } } }
function tearDown() { DataSource::getDataSource()->executeUpdateQuery("drop table if exists " . $this->tableName); }