Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

database name as option, som small improvements #21

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions Migrate/Command/AbstractEnvCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ protected function init(InputInterface $input, OutputInterface $output, $env = n
$parser = $configLocator->locate($env);

$conf = $parser->parse();

$this->config = $conf;

$driver = ArrayUtil::get($conf['connection'], 'driver');
Expand All @@ -79,9 +78,16 @@ protected function init(InputInterface $input, OutputInterface $output, $env = n
$username = ArrayUtil::get($conf['connection'], 'username');
$password = ArrayUtil::get($conf['connection'], 'password');
$charset = ArrayUtil::get($conf['connection'], 'charset');

if (empty($host)) {
$host = "localhost";
}
if (empty($dbname)) {
$dbname = $input->getOption('database');
}
if (empty($driver)) {
$dbname = $input->getOption('driver');
}
$uri = $driver;

if ($driver == 'sqlite') {
$uri .= ":$dbname";
} else {
Expand Down Expand Up @@ -110,8 +116,10 @@ public function getLocalMigrations()

$migrations = array();
foreach ($fileList as $file) {
$migration = Migration::createFromFile($file, $this->getMigrationDir());
$migrations[$migration->getId()] = $migration;
if (substr($file, -4) == ".sql") { // Skip backup files, etc.
$migration = Migration::createFromFile($file, $this->getMigrationDir());
$migrations[$migration->getId()] = $migration;
}
}

ksort($migrations);
Expand Down
5 changes: 3 additions & 2 deletions Migrate/Command/AddEnvCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
throw new \InvalidArgumentException("environment [$envName] is already defined!");
}

$driverQuestion = new ChoiceQuestion("Please chose your pdo driver", $drivers);
$driverQuestion = new ChoiceQuestion("Please choose your pdo driver", $drivers);
$driver = $questions->ask($input, $output, $driverQuestion);

$dbNameQuestion = new Question("Please enter the database name (or the database file location): ", "~");
$dbNameQuestion =
new Question("Please enter the database name (or the database file location) (if needed): ", "~");
$dbName = $questions->ask($input, $output, $dbNameQuestion);

$dbHostQuestion = new Question("Please enter the database host (if needed): ", "~");
Expand Down
17 changes: 12 additions & 5 deletions Migrate/Command/CreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,31 @@ protected function execute(InputInterface $input, OutputInterface $output)

$descriptionQuestion = new Question("Please enter a description: ");
$description = $questions->ask($input, $output, $descriptionQuestion);

$editorQuestion = new Question("Please chose which editor to use <info>(default vim)</info>: ", "vim");
$questions->ask($input, $output, $editorQuestion);
$editor=getenv("EDITOR");
if (empty($editor)) {
$editor="vi";
}
$editorQuestion = new Question("Please chose which editor to use <info>(default $editor)</info>: ", "$editor");
$editor=$questions->ask($input, $output, $editorQuestion);

$slugger = new Slugify();
$filename = $slugger->slugify($description);
$timestamp = str_pad(str_replace(".", "", microtime(true)), 14, "0");
$filename = $timestamp . '_' . $filename . '.sql';

$templateFile = file_get_contents(__DIR__ . '/../../templates/migration.tpl');
if (file_exists($this->getMigrationDir() . '/../' .'migration.tpl')) {
$templateFile = file_get_contents($this->getMigrationDir() . '/../' .'migration.tpl');
} else {
$templateFile = file_get_contents(__DIR__ . '/../../templates/migration.tpl');
}
$templateFile = str_replace('{DESCRIPTION}', $description, $templateFile);

$migrationFullPath = $this->getMigrationDir() . '/' . $filename;
file_put_contents($migrationFullPath, $templateFile);
$output->writeln("<info>$migrationFullPath created</info>");

if (!defined('PHPUNIT')) {
system("vim $migrationFullPath > `tty`");
system("$editor $migrationFullPath > `tty`");
}
}
}
12 changes: 12 additions & 0 deletions Migrate/Command/DownCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ protected function configure()
null,
InputOption::VALUE_NONE,
'Mark as applied without executing SQL '
)
->addOption(
'database',
null,
InputOption::VALUE_REQUIRED,
'Database '
)
->addOption(
'driver',
null,
InputOption::VALUE_REQUIRED,
'DB driver'
);
}

Expand Down
11 changes: 11 additions & 0 deletions Migrate/Command/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Migrate\Command;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

Expand All @@ -23,6 +24,16 @@ protected function configure()
'env',
InputArgument::REQUIRED,
'Environment'
)->addOption(
'database',
null,
InputOption::VALUE_REQUIRED,
'Database '
)->addOption(
'driver',
null,
InputOption::VALUE_REQUIRED,
'DB driver'
);
}

Expand Down
11 changes: 11 additions & 0 deletions Migrate/Command/StatusCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

Expand All @@ -25,6 +26,16 @@ protected function configure()
'env',
InputArgument::REQUIRED,
'Environment'
)->addOption(
'database',
null,
InputOption::VALUE_REQUIRED,
'Database '
)->addOption(
'driver',
null,
InputOption::VALUE_REQUIRED,
'DB driver'
);
}

Expand Down
13 changes: 11 additions & 2 deletions Migrate/Command/UpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,21 @@ protected function configure()
null,
InputOption::VALUE_REQUIRED,
'If you need to up this migration id only'
)
->addOption(
)->addOption(
'changelog-only',
null,
InputOption::VALUE_NONE,
'Mark as applied without executing SQL '
)->addOption(
'database',
null,
InputOption::VALUE_REQUIRED,
'Database '
)->addOption(
'driver',
null,
InputOption::VALUE_REQUIRED,
'DB driver'
);
}

Expand Down
14 changes: 8 additions & 6 deletions Migrate/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ public static function createFromRow(array $data, $migrationDir)
$slugger = new Slugify();
$filename = $migration->getId() . '_' . $slugger->slugify($migration->getDescription()) . '.sql';
$migration->setFile($filename);

$migration->load($migrationDir);

return $migration;
Expand All @@ -177,11 +176,14 @@ public function toArray()

public function load($migrationDir)
{
$content = file_get_contents($migrationDir . '/' . $this->getFile());
if ($content && strpos($content, '@UNDO') > 0) {
$sql = explode('-- @UNDO', $content);
$this->setSqlUp($sql[0]);
$this->setSqlDown($sql[1]);
$filePath=$migrationDir . '/' . $this->getFile();
if (file_exists($filePath)) {
$content = file_get_contents($filePath);
if ($content && strpos($content, '@UNDO') > 0) {
$sql = explode('-- @UNDO', $content);
$this->setSqlUp($sql[0]);
$this->setSqlDown($sql[1]);
}
}
}
}
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,20 @@ The first thing to do before playing with SQL migrations is to add an environmen
$ ./bin/migrate migrate:addenv
```

You will be prompted to answer a series of questions about your environment, and then a config file will be saved
in `./.php-database-migration/environments/[env].yml`.
You will be prompted to answer a series of questions about your
environment, and then a config file will be saved in
`./.php-database-migration/environments/[env].yml`. You can skip all
questions except the database driver. But if you do not enter a database name,
then you have to suppy it as an option in the following commands (
--database=foo ). This is useful for e.g. testing scripts that create and copy databases on the fly.

Initialization
--------------
Once the environment is added, you have to initialize it. This verifies that the database connection works, and
creates a new database table for tracking the current database changes:

```
$ ./bin/migrate migrate:init [env]
$ ./bin/migrate migrate:init env
```

Create a migration
Expand Down
12 changes: 10 additions & 2 deletions tests/Command/AbstractCommandTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public function createEnv($format = 'yml')
/* @var $question QuestionHelper */
$question = $command->getHelper('question');
$question->setInputStream(InputStreamUtil::type("testing\n$driverKey\ntest.sqlite\n\n\n\n\n\nchangelog\nvim\n"));

$commandTester->execute(array('command' => $command->getName(), 'format' => $format));
$question = $command->getHelper('question');
$question->setInputStream(InputStreamUtil::type("minimal\nsqlite\n\n\n\n\n\n\nchangelog\n\n"));
$commandTester->execute(array('command' => $command->getName(), 'format' => $format));
}

Expand All @@ -64,6 +66,12 @@ public function initEnv()
'command' => $command->getName(),
'env' => 'testing'
));
$commandTester->execute(array(
'command' => $command->getName(),
'env' => 'minimal',
'--database' => 'migrate_test',
'--driver' => 'sqlite'
));
}

public function createMigration($timestamp, $sqlUp, $sqlDown)
Expand All @@ -83,4 +91,4 @@ public function createMigration($timestamp, $sqlUp, $sqlDown)

file_put_contents($filename, $content);
}
}
}
8 changes: 4 additions & 4 deletions tests/Command/AddenvCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,20 @@ public function testExecute()

$pdoDrivers = pdo_drivers();
$driverKey = array_search('sqlite', $pdoDrivers);

if (empty($driverKey)) {
echo "install sqlite php driver (php-sqlite3)\n";
exit(-1);
}
$driverSelect = '';
foreach ($pdoDrivers as $key => $driver) {
$driverSelect .= " [$key] $driver\n";
}

/* @var $question QuestionHelper */
$question = $command->getHelper('question');
$question->setInputStream(InputStreamUtil::type("testing\n$driverKey\nmigrate_test\nlocalhost\n5432\naguidet\naguidet\nutf8\nchangelog\nvim\n"));

$commandTester->execute(array('command' => $command->getName()));

$expected = "Please enter the name of the new environment (default dev): Please chose your pdo driver\n$driverSelect > 0\nPlease enter the database name (or the database file location): Please enter the database host (if needed): Please enter the database port (if needed): Please enter the database user name (if needed): Please enter the database user password (if needed): Please enter the changelog table (default changelog): Please enter the text editor to use by default (default vim): ";

$this->assertRegExp('/Please enter the name of the new environment/', $commandTester->getDisplay());

$envDir = Directory::getEnvPath();
Expand Down
99 changes: 99 additions & 0 deletions tests/Command/OptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
/**
* User: aguidet
* Date: 02/03/15
* Time: 17:08
*/

namespace Command;


use Migrate\Command\DownCommand;
use Migrate\Command\StatusCommand;
use Migrate\Command\UpCommand;
use Migrate\Test\Command\AbstractCommandTester;
use Migrate\Utils\InputStreamUtil;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Tester\CommandTester;

class OptionTest extends AbstractCommandTester
{
public static $application;

public function setUp()
{
$this->cleanEnv();
$this->createEnv();
$this->initEnv();

$this->createMigration('0', "CREATE TABLE test (id INTEGER, thevalue TEXT);", "DROP TABLE test;");
$this->createMigration('1', "SELECT 1", "DELETE FROM test WHERE id = 1;");
$this->createMigration('2', "INSERT INTO test VALUES (2, 'two');", "DELETE FROM test WHERE id = 2;");

self::$application = new Application();
self::$application->add(new UpCommand());
self::$application->add(new DownCommand());
self::$application->add(new StatusCommand());
}

public function tearDown()
{
$this->cleanEnv();
}

public function testUpAllPendingMigrationsInMinimal()
{
$command = self::$application->find('migrate:up');
$commandTester = new CommandTester($command);
$commandTester->execute(array(
'command' => $command->getName(),
'env' => 'minimal',
'--database' => 'migrate_test',
'--driver' => 'sqlite'
));

$expected =<<<EXPECTED
connected
0/3 [>---------------------------] 0 % []
1/3 [=========>------------------] 33 % [migration]
2/3 [==================>---------] 66 % [migration]
3/3 [============================] 100 % [migration]

EXPECTED;

$this->assertEquals($expected, $commandTester->getDisplay());
}



public function testDownLastMigrationInMinimal()
{

$command = self::$application->find('migrate:down');
$commandTester = new CommandTester($command);

/* @var $question QuestionHelper */
$question = $command->getHelper('question');
$question->setInputStream(InputStreamUtil::type("yes\n"));

$commandTester->execute(array(
'command' => $command->getName(),
'env' => 'testing',
'--database' => 'migrate_test',
'--driver' => 'sqlite'
));

$expected =<<<EXPECTED
connected
Are you sure? (yes/no) [no]: your database is already up to date

EXPECTED;

$this->assertEquals($expected, $commandTester->getDisplay());
}




}
Loading