function abort_if_migration_exists($classname) { function map($arr) { return $arr['classname']; } $migrations = array_map('map', get_migrations()); if (in_array($classname, $migrations)) { echo "A migration named `{$classname}` already exists. Aborting.\n"; exit; } }
if (@strlen($argv[2])) { $path .= '-' . str_replace(' ', '-', $argv[2]); } $path .= MIGRATE_FILE_POSTFIX; echo "Adding a new migration script: {$path}\n"; $f = @fopen($path, 'w'); if ($f) { fputs($f, "<?php\n\nquery(\$query);\n\n"); fclose($f); echo "Done.\n"; } else { echo "Failed.\n"; } } else { if ($argv[1] == 'migrate') { $files = get_migrations(); $skip_errors = @$argv[2] == '--skip-errors'; // Check to make sure there are no conflicts such as 2 files under the same version. $errors = array(); $last_file = false; $last_version = false; foreach ($files as $file) { $file_version = get_version_from_file($file); if ($last_version !== false && $last_version === $file_version) { $errors[] = "{$last_file} --- {$file}"; } $last_version = $file_version; $last_file = $file; } if (count($errors) > 0) { echo "Error: You have multiple files using the same version. " . "To resolve, move some of the files up so each one gets a unique version.\n";
CREATE TABLE `schema_migrations` ( `version` varchar(255), PRIMARY KEY (`version`) ) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci; EOT; $connection->query($sql); } function should_migrate($migration, $connection) { $should_migrate = $connection->count(array('table' => 'schema_migrations', 'conditions' => array('version' => get_migration_version($migration)))); return !$should_migrate; } function migrate($migration, $connection) { echo 'importing ' . $migration . '... '; $connection->query(utf8_decode(Filesystem::read('db/migrations/' . $migration))); $connection->create(array('table' => 'schema_migrations', 'values' => array('version' => get_migration_version($migration)))); echo 'done' . PHP_EOL; } $connection = Connection::get(Config::read('App.environment')); if (!in_array('schema_migrations', $connection->listSources())) { create_schema_migrations($connection); } $migrations = get_migrations(); foreach ($migrations as $migration) { if (should_migrate($migration, $connection)) { migrate($migration, $connection); } }