protected function execute(InputInterface $input, OutputInterface $output) { $directory = $input->getArgument('schema-dir'); if (strpos($directory, '/') !== 0) { $directory = getcwd() . '/' . $directory; } if (!file_exists($directory)) { throw new RuntimeException(sprintf("Schema directory does not exist at '%s'.", $directory)); } $dsn = $input->getOption('dsn'); if (isset($_SERVER['DATABASE_URL'])) { $dsn = $_SERVER['DATABASE_URL']; } if (!$dsn) { throw new RuntimeException("Missing environment variable DATABASE_URL in format mysql://user:password@host/database"); } $connection = DriverManager::getConnection(array('url' => $_SERVER['DATABASE_URL'])); $migrator = new DBDeploy($connection, $directory); $output->writeln(sprintf("Reading change scripts from directory %s... \n", $directory)); $status = $migrator->getCurrentStatus(); $appliedString = $status->getAppliedMigrations() ? implode(', ', array_keys($status->getAppliedMigrations())) : '(none)'; $applyString = $status->getApplyMigrations() ? implode(', ', array_keys($status->getApplyMigrations())) : '(none)'; $output->writeln(sprintf("Changes currently applied to database:\n %s\n", $appliedString)); $output->writeln(sprintf("To be applied:\n %s", $applyString)); $migrator->apply($status); }
/** * @test */ public function it_natuarlly_sorts() { $connection = $this->createConnection(); $schemaManager = $connection->getSchemaManager(); $schemaManager->tryMethod('dropTable', 'foo'); $schemaManager->tryMethod('dropTable', 'bar'); $schemaManager->tryMethod('dropTable', 'changelog'); $deploy = new DBDeploy($connection, __DIR__ . '/../schema5'); $deploy->migrate(); $migrations = array_map(function ($row) { return $row['description']; }, $connection->fetchAll('SELECT description FROM changelog')); $this->assertEquals(array('changelog'), $schemaManager->listTableNames()); $this->assertEquals(array('001_create.sql', '002_drop.sql'), $migrations); }