Пример #1
0
 public static function generate($options)
 {
     $path = $options['directory'];
     $tableName = $options['tableName'];
     $exportData = $options['exportData'];
     $migrationsDir = $options['migrationsDir'];
     $originalVersion = $options['originalVersion'];
     $force = $options['force'];
     $config = $options['config'];
     if ($migrationsDir && !file_exists($migrationsDir)) {
         mkdir($migrationsDir);
     }
     if ($originalVersion) {
         if (!preg_match('/[a-z0-9](\\.[a-z0-9]+)*/', $originalVersion, $matches)) {
             throw new \Exception('Version ' . $originalVersion . ' is invalid');
         }
         $originalVersion = $matches[0];
         $version = new VersionItem($originalVersion, 3);
         if (file_exists($migrationsDir . '/' . $version)) {
             if (!$force) {
                 throw new \Exception('Version ' . $version . ' is already generated');
             }
         }
     } else {
         $versions = array();
         $iterator = new \DirectoryIterator($migrationsDir);
         foreach ($iterator as $fileinfo) {
             if ($fileinfo->isDir()) {
                 if (preg_match('/[a-z0-9](\\.[a-z0-9]+)+/', $fileinfo->getFilename(), $matches)) {
                     $versions[] = new VersionItem($matches[0], 3);
                 }
             }
         }
         if (count($versions) == 0) {
             $version = new VersionItem('1.0.0');
         } else {
             $version = VersionItem::maximum($versions);
             $version = $version->addMinor(1);
         }
     }
     if (!file_exists($migrationsDir . '/' . $version)) {
         mkdir($migrationsDir . '/' . $version);
     }
     if (isset($config->database)) {
         ModelMigration::setup($config->database);
     } else {
         throw new \Exception("Cannot load database configuration");
     }
     ModelMigration::setMigrationPath($migrationsDir . '/' . $version);
     if ($tableName == 'all') {
         $migrations = ModelMigration::generateAll($version, $exportData);
         foreach ($migrations as $tableName => $migration) {
             file_put_contents($migrationsDir . '/' . $version . '/' . $tableName . '.php', '<?php ' . PHP_EOL . PHP_EOL . $migration);
         }
     } else {
         $migration = ModelMigration::generate($version, $tableName, $exportData);
         file_put_contents($migrationsDir . '/' . $version . '/' . $tableName . '.php', '<?php ' . PHP_EOL . PHP_EOL . $migration);
     }
     print Color::success('Version ' . $version . ' was successfully generated') . PHP_EOL;
 }
Пример #2
0
 /**
  * Generate migrations
  *
  * @param array $options
  *
  * @throws \Exception
  * @todo Refactor
  */
 public static function generate(array $options)
 {
     $tableName = $options['tableName'];
     $exportData = $options['exportData'];
     $migrationsDir = $options['migrationsDir'];
     $originalVersion = $options['originalVersion'];
     $force = $options['force'];
     $config = $options['config'];
     if ($migrationsDir && !file_exists($migrationsDir)) {
         mkdir($migrationsDir, 0777, true);
     }
     if ($originalVersion) {
         if (!preg_match('/[a-z0-9](\\.[a-z0-9]+)*/', $originalVersion, $matches)) {
             throw new \Exception("Version {$originalVersion} is invalid");
         }
         $originalVersion = $matches[0];
         $version = new VersionItem($originalVersion, 3);
         if (file_exists($migrationsDir . DIRECTORY_SEPARATOR . $version) && !$force) {
             throw new \Exception("Version {$version} is already generated");
         }
     } else {
         $versions = ModelMigration::scanForVersions($migrationsDir);
         if (!count($versions)) {
             $version = new VersionItem('1.0.0');
         } else {
             $version = VersionItem::maximum($versions);
             $version = $version->addMinor(1);
         }
     }
     if (!file_exists($migrationsDir . DIRECTORY_SEPARATOR . $version)) {
         mkdir($migrationsDir . DIRECTORY_SEPARATOR . $version);
     }
     if (!isset($config->database)) {
         throw new \Exception("Cannot load database configuration");
     }
     ModelMigration::setup($config->database);
     ModelMigration::setSkipAutoIncrement($options['no-ai']);
     ModelMigration::setMigrationPath($migrationsDir);
     if ($tableName == 'all') {
         $migrations = ModelMigration::generateAll($version, $exportData);
         foreach ($migrations as $tableName => $migration) {
             file_put_contents($migrationsDir . '/' . $version . '/' . $tableName . '.php', '<?php ' . PHP_EOL . PHP_EOL . $migration);
         }
     } else {
         $migration = ModelMigration::generate($version, $tableName, $exportData);
         file_put_contents($migrationsDir . '/' . $version . '/' . $tableName . '.php', '<?php ' . PHP_EOL . PHP_EOL . $migration);
     }
     if (self::isConsole()) {
         print Color::success('Version ' . $version . ' was successfully generated') . PHP_EOL;
     }
 }
Пример #3
0
 /**
  * Generate migrations
  *
  * @param array $options
  *
  * @throws \Exception
  */
 public static function generate(array $options)
 {
     $tableName = $options['tableName'];
     $exportData = $options['exportData'];
     $migrationsDir = $options['migrationsDir'];
     $version = $options['version'];
     $force = $options['force'];
     $config = $options['config'];
     // Migrations directory
     if ($migrationsDir && !file_exists($migrationsDir)) {
         mkdir($migrationsDir, 0755, true);
     }
     // Use timestamped version if description is provided
     if ($descr = $options['descr']) {
         $version = (string) (int) (microtime(true) * pow(10, 6));
         VersionCollection::setType(VersionCollection::TYPE_TIMESTAMPED);
         $versionItem = VersionCollection::createItem($version . '_' . $descr);
         // Elsewhere use old-style incremental versioning
         // The version is specified
     } elseif ($version) {
         VersionCollection::setType(VersionCollection::TYPE_INCREMENTAL);
         $versionItem = VersionCollection::createItem($version);
         // The version is guessed automatically
     } else {
         VersionCollection::setType(VersionCollection::TYPE_INCREMENTAL);
         $versionItems = ModelMigration::scanForVersions($migrationsDir);
         if (!isset($versionItems[0])) {
             $versionItem = VersionCollection::createItem('1.0.0');
         } else {
             /** @var IncrementalVersion $versionItem */
             $versionItem = VersionCollection::maximum($versionItems);
             $versionItem = $versionItem->addMinor(1);
         }
     }
     // Path to migration dir
     $migrationPath = $migrationsDir . DIRECTORY_SEPARATOR . $versionItem->getVersion();
     if (!file_exists($migrationPath)) {
         mkdir($migrationPath);
     } elseif (!$force) {
         throw new \LogicException('Version ' . $versionItem->getVersion() . ' already exists');
     }
     // Try to connect to the DB
     if (!isset($config->database)) {
         throw new \RuntimeException('Cannot load database configuration');
     }
     ModelMigration::setup($config->database);
     ModelMigration::setSkipAutoIncrement($options['noAutoIncrement']);
     ModelMigration::setMigrationPath($migrationsDir);
     $wasMigrated = false;
     if ($tableName === 'all') {
         $migrations = ModelMigration::generateAll($versionItem->getStamp(), $exportData);
         foreach ($migrations as $tableName => $migration) {
             if ($tableName === self::MIGRATION_LOG_TABLE) {
                 continue;
             }
             $tableFile = $migrationPath . DIRECTORY_SEPARATOR . $tableName . '.php';
             $wasMigrated = file_put_contents($tableFile, '<?php ' . PHP_EOL . PHP_EOL . $migration) || $wasMigrated;
         }
     } else {
         $migration = ModelMigration::generate($versionItem->getStamp(), $tableName, $exportData);
         $tableFile = $migrationPath . DIRECTORY_SEPARATOR . $tableName . '.php';
         $wasMigrated = !!file_put_contents($tableFile, '<?php ' . PHP_EOL . PHP_EOL . $migration);
     }
     if (self::isConsole() && $wasMigrated) {
         print Color::success('Version ' . $versionItem->getVersion() . ' was successfully generated') . PHP_EOL;
     } elseif (self::isConsole()) {
         print Color::info('Nothing to generate. You should create tables at first.') . PHP_EOL;
     }
     exit(0);
 }