Exemplo n.º 1
0
 public function testReversibleMigrationsWorkAsExpected()
 {
     if (!TESTS_PHINX_DB_ADAPTER_MYSQL_ENABLED) {
         $this->markTestSkipped('Mysql tests disabled. See TESTS_PHINX_DB_ADAPTER_MYSQL_ENABLED constant.');
     }
     $configArray = $this->getConfigArray();
     $adapter = $this->manager->getEnvironment('production')->getAdapter();
     // override the migrations directory to use the reversible migrations
     $configArray['paths']['migrations'] = $this->getCorrectedPath(__DIR__ . '/_files/reversiblemigrations');
     $config = new Config($configArray);
     // ensure the database is empty
     $adapter->dropDatabase(TESTS_PHINX_DB_ADAPTER_MYSQL_DATABASE);
     $adapter->createDatabase(TESTS_PHINX_DB_ADAPTER_MYSQL_DATABASE);
     $adapter->disconnect();
     // migrate to the latest version
     $this->manager->setConfig($config);
     $this->manager->migrate('production');
     // ensure up migrations worked
     $this->assertFalse($adapter->hasTable('info'));
     $this->assertTrue($adapter->hasTable('statuses'));
     $this->assertTrue($adapter->hasTable('users'));
     $this->assertTrue($adapter->hasTable('user_logins'));
     $this->assertTrue($adapter->hasColumn('users', 'biography'));
     $this->assertTrue($adapter->hasForeignKey('user_logins', array('user_id')));
     // revert all changes to the first
     $this->manager->rollback('production', '20121213232502');
     // ensure reversed migrations worked
     $this->assertTrue($adapter->hasTable('info'));
     $this->assertFalse($adapter->hasTable('statuses'));
     $this->assertFalse($adapter->hasTable('user_logins'));
     $this->assertTrue($adapter->hasColumn('users', 'bio'));
     $this->assertFalse($adapter->hasForeignKey('user_logins', array('user_id')));
 }
 /**
  * Executes migrations
  *
  * @return int Version of the latest migration executed
  *
  * @throws TopologyViolationException If an unprocessed migration is younger than
  *                                    the latest processed migration.
  * @throws MigrationException If a migration cannot be executed due of
  *                            errors (syntax, ...)
  */
 public function migrate()
 {
     $targetVersion = max(array_merge($this->migratedVersions, array_keys($this->availableVersions)));
     $direction = $targetVersion > $this->currentVersion ? MigrationInterface::UP : MigrationInterface::DOWN;
     if ($direction === MigrationInterface::DOWN) {
         throw new TopologyViolationException();
     }
     try {
         $this->manager->migrate($this->environment);
     } catch (\Exception $exc) {
         throw new MigrationException($exc->getMessage());
     }
     return $targetVersion;
 }
Exemplo n.º 3
0
 public function testBreakpointWithInvalidVersion()
 {
     if (!TESTS_PHINX_DB_ADAPTER_MYSQL_ENABLED) {
         $this->markTestSkipped('Mysql tests disabled. See TESTS_PHINX_DB_ADAPTER_MYSQL_ENABLED constant.');
     }
     $configArray = $this->getConfigArray();
     $adapter = $this->manager->getEnvironment('production')->getAdapter();
     $config = new Config($configArray);
     // ensure the database is empty
     $adapter->dropDatabase(TESTS_PHINX_DB_ADAPTER_MYSQL_DATABASE);
     $adapter->createDatabase(TESTS_PHINX_DB_ADAPTER_MYSQL_DATABASE);
     $adapter->disconnect();
     // migrate to the latest version
     $this->manager->setConfig($config);
     $this->manager->migrate('production');
     $this->manager->getOutput()->setDecorated(false);
     // set breakpoint on most recent migration
     $this->manager->toggleBreakpoint('production', 999);
     rewind($this->manager->getOutput()->getStream());
     $output = stream_get_contents($this->manager->getOutput()->getStream());
     $this->assertContains('is not a valid version', $output);
 }
Exemplo n.º 4
0
 /**
  * Overload the basic behavior to add an instance of the InputInterface the shell call is
  * using in order to gives the ability to the AbstractSeed::call() method to propagate options
  * to the other MigrationsDispatcher it is generating.
  *
  * {@inheritdoc}
  */
 public function getSeeds()
 {
     parent::getSeeds();
     if (empty($this->seeds)) {
         return $this->seeds;
     }
     foreach ($this->seeds as $class => $instance) {
         if ($instance instanceof AbstractSeed) {
             $instance->setInput($this->input);
         }
     }
     return $this->seeds;
 }
Exemplo n.º 5
0
<?php

use Symfony\Component\Console\Output\NullOutput;
use Phinx\Config\Config;
use Phinx\Migration\Manager as MigrationManager;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Connection;
$loader = (require __DIR__ . '/../vendor/autoload.php');
// auto-load custom PHPUnit constraints
$loader->add('', __DIR__ . '/constraints/');
/**
 * Create and seed an in-memory sqlite database for testing
 * Using Phinx for migrations/seeding
 */
$config = new Config(['paths' => ['migrations' => __DIR__ . '/migrations'], 'environments' => ['default_migration_table' => 'phinxlog', 'default_database' => 'test', 'test' => ['name' => ':memory:', 'adapter' => 'sqlite', 'memory' => true]]]);
$migrationManager = new MigrationManager($config, new NullOutput());
// get the PDO object used by Phinx
$pdo = $migrationManager->getEnvironment('test')->getAdapter()->getConnection();
/**
 * Make Eloquent use the PDO object provided by Phinx
 */
$capsule = new Capsule();
$capsule->addConnection([], 'default');
$capsule->getDatabaseManager()->extend('default', function () use($pdo) {
    return new Connection($pdo);
});
$capsule->bootEloquent();
// run migrations
$migrationManager->migrate('test');