示例#1
0
 /**
  * Migrate the database.
  *
  * @param InputInterface $input
  * @param OutputInterface $output
  * @throws \RuntimeException
  * @throws \InvalidArgumentException
  * @return void
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->bootstrap($input, $output);
     // get the migration path from the config
     $path = $this->getConfig()->getMigrationPath();
     if (!is_writeable($path)) {
         throw new \InvalidArgumentException(sprintf('The directory "%s" is not writeable', $path));
     }
     $path = realpath($path);
     $className = $input->getArgument('name');
     if (!Util::isValidMigrationClassName($className)) {
         throw new \InvalidArgumentException(sprintf('The migration class name "%s" is invalid. Please use CamelCase format.', $className));
     }
     // Compute the file path
     $fileName = Util::mapClassNameToFileName($className);
     $filePath = $path . DIRECTORY_SEPARATOR . $fileName;
     if (file_exists($filePath)) {
         throw new \InvalidArgumentException(sprintf('The file "%s" already exists', $filePath));
     }
     // load the migration template
     $contents = file_get_contents(dirname(__FILE__) . '/../../Migration/Migration.template.php.dist');
     // inject the class name
     $contents = str_replace('$className', $className, $contents);
     if (false === file_put_contents($filePath, $contents)) {
         throw new \RuntimeException(sprintf('The file "%s" could not be written to', $path));
     }
     $output->writeln('<info>created</info> .' . str_replace(getcwd(), '', $filePath));
 }
示例#2
0
 public function testIsValidMigrationClassName()
 {
     $expectedResults = array('CAmelCase' => false, 'CreateUserTable' => true, 'Test' => true, 'test' => false);
     foreach ($expectedResults as $input => $expectedResult) {
         $this->assertEquals($expectedResult, Util::isValidMigrationClassName($input));
     }
 }
 public function testNotEmptySnapshot()
 {
     $this->Task->params['require-table'] = false;
     $version = Util::getCurrentTimestamp();
     $this->Task->expects($this->once())->method('dispatchShell')->with('migrations', 'mark_migrated', $version);
     $result = $this->Task->bake('NotEmptySnapshot');
     $this->assertSameAsFile(__FUNCTION__ . '.php', $result);
 }
 public function testNotEmptySnapshot()
 {
     $this->Task->params['require-table'] = false;
     $this->Task->params['connection'] = 'test';
     $this->Task->params['plugin'] = 'BogusPlugin';
     $version = Util::getCurrentTimestamp();
     $this->Task->expects($this->once())->method('dispatchShell')->with($this->logicalAnd($this->stringContains('migrations mark_migrated'), $this->stringContains('-c test -p BogusPlugin')));
     $result = $this->Task->bake('NotEmptySnapshot');
     $this->assertSameAsFile(__FUNCTION__ . '.php', $result);
 }
示例#5
0
文件: Manager.php 项目: Slayug/castor
 /**
  * Gets an array of the database migrations.
  *
  * @throws \InvalidArgumentException
  * @return AbstractMigration[]
  */
 public function getMigrations()
 {
     if (null === $this->migrations) {
         $config = $this->getConfig();
         $phpFiles = glob($config->getMigrationPath() . DIRECTORY_SEPARATOR . '*.php');
         // filter the files to only get the ones that match our naming scheme
         $fileNames = array();
         /** @var AbstractMigration[] $versions */
         $versions = array();
         foreach ($phpFiles as $filePath) {
             if (Util::isValidMigrationFileName(basename($filePath))) {
                 $version = Util::getVersionFromFileName(basename($filePath));
                 if (isset($versions[$version])) {
                     throw new \InvalidArgumentException(sprintf('Duplicate migration - "%s" has the same version as "%s"', $filePath, $versions[$version]->getVersion()));
                 }
                 // convert the filename to a class name
                 $class = Util::mapFileNameToClassName(basename($filePath));
                 if (isset($fileNames[$class])) {
                     throw new \InvalidArgumentException(sprintf('Migration "%s" has the same name as "%s"', basename($filePath), $fileNames[$class]));
                 }
                 $fileNames[$class] = basename($filePath);
                 // load the migration file
                 /** @noinspection PhpIncludeInspection */
                 require_once $filePath;
                 if (!class_exists($class)) {
                     throw new \InvalidArgumentException(sprintf('Could not find class "%s" in file "%s"', $class, $filePath));
                 }
                 // instantiate it
                 $migration = new $class($version);
                 if (!$migration instanceof AbstractMigration) {
                     throw new \InvalidArgumentException(sprintf('The class "%s" in file "%s" must extend \\Phinx\\Migration\\AbstractMigration', $class, $filePath));
                 }
                 $migration->setOutput($this->getOutput());
                 $versions[$version] = $migration;
             }
         }
         ksort($versions);
         $this->setMigrations($versions);
     }
     return $this->migrations;
 }
示例#6
0
 /**
  * Migrate the database.
  *
  * @param InputInterface $input
  * @param OutputInterface $output
  * @throws \RuntimeException
  * @throws \InvalidArgumentException
  * @return void
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->bootstrap($input, $output);
     // get the migration path from the config
     $path = $this->getConfig()->getMigrationPath();
     if (!file_exists($path)) {
         $helper = $this->getHelper('question');
         $question = $this->getCreateMigrationDirectoryQuestion();
         if ($helper->ask($input, $output, $question)) {
             mkdir($path, 0755, true);
         }
     }
     $this->verifyMigrationDirectory($path);
     $path = realpath($path);
     $className = $input->getArgument('name');
     if (!Util::isValidMigrationClassName($className)) {
         throw new \InvalidArgumentException(sprintf('The migration class name "%s" is invalid. Please use CamelCase format.', $className));
     }
     // Compute the file path
     $fileName = Util::mapClassNameToFileName($className);
     $filePath = $path . DIRECTORY_SEPARATOR . $fileName;
     if (is_file($filePath)) {
         throw new \InvalidArgumentException(sprintf('The file "%s" already exists', $filePath));
     }
     // Get the alternative template and static class options, but only allow one of them.
     $altTemplate = $input->getOption('template');
     $creationClassName = $input->getOption('class');
     if ($altTemplate && $creationClassName) {
         throw new \InvalidArgumentException('Cannot use --template and --class at the same time');
     }
     // Verify the alternative template file's existence.
     if ($altTemplate && !is_file($altTemplate)) {
         throw new \InvalidArgumentException(sprintf('The alternative template file "%s" does not exist', $altTemplate));
     }
     // Verify the static class exists and that it implements the required interface.
     if ($creationClassName) {
         if (!class_exists($creationClassName)) {
             throw new \InvalidArgumentException(sprintf('The class "%s" does not exist', $creationClassName));
         }
         if (!is_subclass_of($creationClassName, self::CREATION_INTERFACE)) {
             throw new \InvalidArgumentException(sprintf('The class "%s" does not implement the required interface "%s"', $creationClassName, self::CREATION_INTERFACE));
         }
     }
     // Determine the appropriate mechanism to get the template
     if ($creationClassName) {
         // Get the template from the creation class
         $creationClass = new $creationClassName();
         $contents = $creationClass->getMigrationTemplate();
     } else {
         // Load the alternative template if it is defined.
         $contents = file_get_contents($altTemplate ?: $this->getMigrationTemplateFilename());
     }
     // inject the class names appropriate to this migration
     $classes = array('$useClassName' => $this->getConfig()->getMigrationBaseClassName(false), '$className' => $className, '$baseClassName' => $this->getConfig()->getMigrationBaseClassName(true));
     $contents = strtr($contents, $classes);
     if (false === file_put_contents($filePath, $contents)) {
         throw new \RuntimeException(sprintf('The file "%s" could not be written to', $path));
     }
     // Do we need to do the post creation call to the creation class?
     if ($creationClassName) {
         $creationClass->postMigrationCreation($filePath, $className, $this->getConfig()->getMigrationBaseClassName());
     }
     $output->writeln('<info>using migration base class</info> ' . $classes['$useClassName']);
     if (!empty($altTemplate)) {
         $output->writeln('<info>using alternative template</info> ' . $altTemplate);
     } elseif (!empty($creationClassName)) {
         $output->writeln('<info>using template creation class</info> ' . $creationClassName);
     } else {
         $output->writeln('<info>using default template</info>');
     }
     $output->writeln('<info>created</info> .' . str_replace(getcwd(), '', $filePath));
 }
示例#7
0
 /**
  * Gets an array of the database migrations.
  *
  * @throws \InvalidArgumentException
  * @return AbstractMigration[]
  */
 public function getMigrations()
 {
     if (empty($this->_migrations)) {
         $fileNames = [];
         $versions = [];
         $phpFiles = glob($this->_config->getMigrationPath() . DIRECTORY_SEPARATOR . '*.php');
         foreach ($phpFiles as $filePath) {
             if (Util::isValidMigrationFileName(basename($filePath))) {
                 $version = Util::getVersionFromFileName(basename($filePath));
                 if (isset($versions[$version])) {
                     throw new \InvalidArgumentException(__d('union_dev', 'Duplicate migration - "%s" has the same version as "%s"', $filePath, $versions[$version]->getVersion()));
                 }
                 //  Convert the filename to a class name.
                 $class = Util::mapFileNameToClassName(basename($filePath));
                 if (isset($fileNames[$class])) {
                     throw new \InvalidArgumentException(__d('union_dev', 'Migration "%s" has the same name as "%s"', basename($filePath), $fileNames[$class]));
                 }
                 $fileNames[$class] = basename($filePath);
                 // Load the migration file.
                 /** @noinspection PhpIncludeInspection */
                 require_once $filePath;
                 if (!class_exists($class)) {
                     throw new \InvalidArgumentException(__d('union_dev', 'Could not find class "%s" in file "%s"', $class, $filePath));
                 }
                 //  Instantiate migration class.
                 $migration = new $class($version);
                 if (!$migration instanceof AbstractMigration) {
                     throw new \InvalidArgumentException(__d('union_dev', 'The class "%s" in file "%s" must extend \\Phinx\\Migration\\AbstractMigration', $class, $filePath));
                 }
                 $versions[$version] = $migration;
             }
         }
         ksort($versions);
         $this->_migrations = $versions;
     }
     return $this->_migrations;
 }
示例#8
0
 /**
  * {@inheritDoc}
  */
 public function fileName($name)
 {
     $name = $this->getMigrationName($name);
     return Util::mapClassNameToFileName($name);
 }