create() public static méthode

public static create ( PDO $connection, BaseDriver $driver )
$connection PDO
$driver SQLBuilder\Driver\BaseDriver
 /**
  * @dataProvider driverTypeDataProvider
  */
 public function testTableParserFor($driverType)
 {
     $parser = TableParser::create($this->conn, $this->queryDriver);
     $tables = $parser->getTables();
     $this->assertNotNull($tables);
     foreach ($tables as $table) {
         $this->assertNotNull($table);
         $schema = $parser->reverseTableSchema($table);
         $this->assertNotNull($schema);
         $columns = $schema->getColumns();
         $this->assertNotEmpty($columns);
     }
 }
Exemple #2
0
 /**
  * This method initialize the metadata table if needed.
  */
 public function init()
 {
     $parser = TableParser::create($this->driver, $this->connection);
     $tables = $parser->getTables();
     // if the __meta__table is not found, we should create one to prevent error.
     // this will be needed for the compatibility of the older version lazyrecord.
     if (!in_array('__meta__', $tables)) {
         $schema = new \LazyRecord\Model\MetadataSchema();
         $builder = \LazyRecord\SqlBuilder\SqlBuilder::create($this->driver);
         $sqls = $builder->build($schema);
         foreach ($sqls as $sql) {
             $this->connection->query($sql);
         }
     }
 }
Exemple #3
0
 public function execute()
 {
     $formatter = new \CLIFramework\Formatter();
     $options = $this->options;
     $logger = $this->logger;
     $connectionManager = \LazyRecord\ConnectionManager::getInstance();
     $dsId = $this->getCurrentDataSourceId();
     $conn = $connectionManager->getConnection($dsId);
     $driver = $connectionManager->getQueryDriver($dsId);
     $this->logger->info('Performing Comparison...');
     $parser = TableParser::create($driver, $conn);
     $existingTables = $parser->getTables();
     $tableSchemas = $parser->getDeclareSchemaMap();
     $found = false;
     $comparator = new Comparator();
     foreach ($tableSchemas as $table => $currentSchema) {
         $this->logger->debug("Checking table {$table}");
         $ref = new ReflectionObject($currentSchema);
         $filepath = $ref->getFilename();
         $filepath = substr($filepath, strlen(getcwd()) + 1);
         if (in_array($table, $existingTables)) {
             $before = $parser->reverseTableSchema($table);
             $diffs = $comparator->compare($before, $currentSchema);
             if (count($diffs)) {
                 $found = true;
                 $printer = new ComparatorConsolePrinter($diffs);
                 $printer->beforeName = $table . ":data source [{$dsId}]";
                 $printer->afterName = $table . ':' . $filepath;
                 $printer->output();
             }
         } else {
             $msg = sprintf("+ table %-20s %s", "'" . $table . "'", $filepath);
             echo $formatter->format($msg, 'green'), "\n";
             $a = isset($tableSchemas[$table]) ? $tableSchemas[$table] : null;
             $diff = $comparator->compare(new DeclareSchema(), $currentSchema);
             foreach ($diff as $diffItem) {
                 echo "  ", $diffItem->toColumnAttrsString(), "\n";
             }
             $found = true;
         }
     }
     if (!$found) {
         $this->logger->info("No diff found");
     }
 }
 /**
  * @dataProvider driverTypeDataProvider
  */
 public function testTableParserFor($driverType)
 {
     $config = self::createNeutralConfigLoader();
     $manager = ConnectionManager::getInstance();
     $manager->free();
     $this->registerDataSource($driverType);
     $conn = $manager->getConnection($driverType);
     $driver = $manager->getQueryDriver($driverType);
     $parser = TableParser::create($driver, $conn);
     $tables = $parser->getTables();
     $this->assertNotNull($tables);
     foreach ($tables as $table) {
         $this->assertNotNull($table);
         $schema = $parser->reverseTableSchema($table);
         $this->assertNotNull($schema);
         $columns = $schema->getColumns();
         $this->assertNotEmpty($columns);
     }
 }
 public function setUp()
 {
     if ($this->onlyDriver !== null && $this->getDataSource() != $this->onlyDriver) {
         return $this->markTestSkipped("{$this->onlyDriver} only");
     }
     $this->prepareConnection();
     // Ensure that we use the correct default data source ID
     $this->assertEquals($this->getDataSource(), $this->config->getDefaultDataSourceId());
     $this->assertInstanceOf('SQLBuilder\\Driver\\BaseDriver', $this->queryDriver, 'QueryDriver object OK');
     // Rebuild means rebuild the database for new tests
     $annnotations = $this->getAnnotations();
     $rebuild = true;
     $basedata = true;
     if (isset($annnotations['method']['rebuild'][0]) && $annnotations['method']['rebuild'][0] == 'false') {
         $rebuild = false;
     }
     if (isset($annnotations['method']['basedata'][0]) && $annnotations['method']['basedata'][0] == 'false') {
         $basedata = false;
     }
     $schemas = ClassUtils::schema_classes_to_objects($this->getModels());
     if (false === $this->schemaHasBeenBuilt) {
         $g = new SchemaGenerator($this->config);
         $g->setForceUpdate(true);
         $g->generate($schemas);
         $this->schemaHasBeenBuilt = true;
     }
     if ($rebuild === false) {
         $tableParser = TableParser::create($this->conn, $this->queryDriver, $this->config);
         $tables = $tableParser->getTables();
         $schemas = array_filter($schemas, function ($schema) use($tables) {
             return !in_array($schema->getTable(), $tables);
         });
     }
     $this->sqlBuilder = SqlBuilder::create($this->queryDriver, array('rebuild' => $rebuild));
     $this->bootstrap = new Bootstrap($this->conn, $this->sqlBuilder, $this->logger);
     $this->bootstrap->build($schemas);
     if ($rebuild && $basedata) {
         $seeder = new SeedBuilder($this->logger);
         $seeder->build(new SchemaCollection($schemas));
         $seeder->buildConfigSeeds($this->config);
     }
 }
 public function upgrade()
 {
     $parser = TableParser::create($this->driver, $this->connection);
     $tableSchemas = $parser->getDeclareSchemaMap();
     $comparator = new Comparator();
     $existingTables = $parser->getTables();
     // schema from runtime
     foreach ($tableSchemas as $table => $schema) {
         $this->logger->debug("Checking table {$table} for schema " . get_class($schema));
         if (!in_array($table, $existingTables)) {
             $this->logger->debug("Table {$table} does not exist, try importing...");
             // generate create table statement.
             // use sqlbuilder to build schema sql
             $this->importSchema($schema);
             continue;
         }
         $this->logger->debug("Found existing table {$table}");
         $before = $parser->reverseTableSchema($table);
         $this->logger->info("Comparing table `{$table}` with schema");
         $diffs = $comparator->compare($before, $schema);
         if (count($diffs) == 0) {
             $this->logger->info("Nothing changed.");
             continue;
         }
         $this->logger->debug("Found " . count($diffs) . ' differences');
         $alterTable = $this->alterTable($table);
         foreach ($diffs as $diff) {
             if ($this->options->{'separate-alter'}) {
                 $alterTable = $this->alterTable($table);
             }
             $column = $diff->getAfterColumn();
             switch ($diff->flag) {
                 case 'A':
                     $alterTable->addColumn($column);
                     break;
                 case 'D':
                     if ($this->options->{'no-drop-column'}) {
                         continue;
                     }
                     $alterTable->dropColumnByName($diff->name);
                     break;
                 case 'M':
                     $afterColumn = $diff->getAfterColumn();
                     $beforeColumn = $diff->getBeforeColumn();
                     if (!$afterColumn || !$beforeColumn) {
                         throw new LogicException("afterColumn or beforeColumn is undefined.");
                     }
                     // check foreign key change
                     if ($beforeColumn->primary != $afterColumn->primary) {
                         $alterTable->add()->primaryKey(['id']);
                     }
                     $alterTable->modifyColumn($afterColumn);
                     break;
                 default:
                     throw new LogicException("Unsupported flat: " . $diff->flag);
                     break;
             }
         }
         $this->executeQuery($alterTable);
     }
     $this->logger->info('Done!');
 }
 public function generateWithDiff($taskName, $dataSourceId, array $schemas, $time = null)
 {
     $connectionManager = \LazyRecord\ConnectionManager::getInstance();
     $connection = $connectionManager->getConnection($dataSourceId);
     $driver = $connectionManager->getQueryDriver($dataSourceId);
     $parser = TableParser::create($connection, $driver);
     $tableSchemas = $schemas;
     $existingTables = $parser->getTables();
     $this->logger->info('Found ' . count($schemas) . ' schemas to compare.');
     $template = $this->createClassTemplate($taskName, $time);
     $upgradeMethod = $template->addMethod('public', 'upgrade', array(), '');
     $downgradeMethod = $template->addMethod('public', 'downgrade', array(), '');
     $comparator = new Comparator($driver);
     // schema from runtime
     foreach ($tableSchemas as $key => $a) {
         $table = is_numeric($key) ? $a->getTable() : $key;
         if (!in_array($table, $existingTables)) {
             $this->logger->info(sprintf("Found schema '%s' to be imported to '%s'", $a, $table), 1);
             // generate create table statement.
             // use sqlbuilder to build schema sql
             $upcall = new MethodCallExpr('$this', 'importSchema', [new Raw('new ' . get_class($a))]);
             $upgradeMethod->getBlock()->appendLine(new Statement($upcall));
             $downcall = new MethodCallExpr('$this', 'dropTable', [$table]);
             $downgradeMethod->getBlock()->appendLine(new Statement($downcall));
             continue;
         }
         // revsersed schema
         $b = $parser->reverseTableSchema($table, $a);
         $diffs = $comparator->compare($b, $a);
         if (empty($diffs)) {
             continue;
         }
         // generate alter table statement.
         foreach ($diffs as $diff) {
             switch ($diff->flag) {
                 case 'A':
                     $alterTable = new AlterTableQuery($table);
                     $alterTable->addColumn($diff->getAfterColumn());
                     $this->appendQueryStatement($upgradeMethod, $driver, $alterTable, new ArgumentArray());
                     $alterTable = new AlterTableQuery($table);
                     $alterTable->dropColumn($diff->getAfterColumn());
                     $this->appendQueryStatement($downgradeMethod, $driver, $alterTable, new ArgumentArray());
                     break;
                 case 'M':
                     $alterTable = new AlterTableQuery($table);
                     $after = $diff->getAfterColumn();
                     $before = $diff->getBeforeColumn();
                     if (!$after || !$before) {
                         throw new LogicException('afterColumn or beforeColumn is undefined.');
                     }
                     // Check primary key
                     if ($before->primary != $after->primary) {
                         // primary key requires another sub-statement "ADD PRIMARY KEY .."
                         $alterTable->add()->primaryKey([$after->name]);
                     }
                     $alterTable->modifyColumn($after);
                     $this->appendQueryStatement($upgradeMethod, $driver, $alterTable, new ArgumentArray());
                     $alterTable = new AlterTableQuery($table);
                     $alterTable->modifyColumn($before);
                     $this->appendQueryStatement($downgradeMethod, $driver, $alterTable, new ArgumentArray());
                     break;
                 case 'D':
                     $alterTable = new AlterTableQuery($table);
                     $alterTable->dropColumnByName($diff->name);
                     $this->appendQueryStatement($upgradeMethod, $driver, $alterTable, new ArgumentArray());
                     $alterTable = new AlterTableQuery($table);
                     $alterTable->addColumn($diff->getBeforeColumn());
                     $this->appendQueryStatement($downgradeMethod, $driver, $alterTable, new ArgumentArray());
                     break;
                 default:
                     $this->logger->warn('** unsupported flag.');
                     continue;
             }
         }
     }
     $filename = $this->generateFilename($taskName, $time);
     $path = $this->migrationDir . DIRECTORY_SEPARATOR . $filename;
     if (false === file_put_contents($path, $template->render())) {
         throw new RuntimeException("Can't write migration script to {$path}.");
     }
     return array($template->class->name, $path);
 }
 /**
  * @param DeclareSchema[string tableName]
  */
 public function upgrade(array $tableSchemas)
 {
     $parser = TableParser::create($this->connection, $this->driver);
     $existingTables = $parser->getTables();
     $comparator = new Comparator($this->driver);
     // Schema from runtime
     foreach ($tableSchemas as $key => $a) {
         $table = is_numeric($key) ? $a->getTable() : $key;
         $this->logger->debug("Checking table {$table} for schema " . get_class($a));
         if (!in_array($table, $existingTables)) {
             $this->logger->debug("Table {$table} does not exist, try importing...");
             // generate create table statement.
             // use sqlbuilder to build schema sql
             $this->importSchema($a);
             continue;
         }
         $this->logger->debug("Found existing table {$table}");
         $b = $parser->reverseTableSchema($table, $a);
         $this->logger->debug("Comparing table `{$table}` with schema");
         $diffs = $comparator->compare($b, $a);
         do {
             if (count($diffs) == 0) {
                 $this->logger->debug("Nothing changed in `{$table}`.");
                 break;
             }
             $this->logger->debug('Found ' . count($diffs) . ' differences');
             $alterTable = $this->alterTable($table);
             foreach ($diffs as $diff) {
                 if ($this->options->{'separate-alter'}) {
                     $alterTable = $this->alterTable($table);
                 }
                 $column = $diff->getAfterColumn();
                 switch ($diff->flag) {
                     case 'A':
                         $alterTable->addColumn($column);
                         break;
                     case 'D':
                         if ($this->options->{'no-drop-column'}) {
                             continue;
                         }
                         $alterTable->dropColumnByName($diff->name);
                         break;
                     case 'M':
                         $afterColumn = $diff->getAfterColumn();
                         $beforeColumn = $diff->getBeforeColumn();
                         if (!$afterColumn || !$beforeColumn) {
                             throw new LogicException('afterColumn or beforeColumn is undefined.');
                         }
                         // Check primary key
                         if ($beforeColumn->primary != $afterColumn->primary) {
                             $alterTable->add()->primaryKey(['id']);
                         }
                         $alterTable->modifyColumn($afterColumn);
                         break;
                     default:
                         throw new LogicException('Unsupported flat: ' . $diff->flag);
                         break;
                 }
             }
             $this->executeQuery($alterTable);
         } while (0);
         // Compare references with relationships
         if ($parser instanceof ReferenceParser) {
             $references = $parser->queryReferences($table);
             $relationships = $a->getRelations();
             $relationshipColumns = [];
             foreach ($relationships as $accessor => $rel) {
                 if ($rel['type'] !== Relationship::BELONGS_TO) {
                     continue;
                 }
                 if ($rel['foreign_schema'] == $rel['self_schema']) {
                     continue;
                 }
                 if (isset($rel['self_column']) && $rel['self_column'] == 'id') {
                     continue;
                 }
                 if (!$rel->usingIndex) {
                     continue;
                 }
                 $class = $rel['foreign_schema'];
                 $foreignSchema = new $class();
                 $foreignColumn = $foreignSchema->getColumn($rel['foreign_column']);
                 $col = $rel['self_column'];
                 $relationshipColumns[$col] = $rel;
                 if (isset($references[$col]) && preg_match('/_ibfk_/i', $references[$col]->name)) {
                     $this->logger->debug("Column {$col} foreign key {$references[$col]->name} exists");
                     continue;
                 }
                 if ($constraint = $this->builder->buildForeignKeyConstraint($rel)) {
                     $alterTable = $this->alterTable($table);
                     $add = $alterTable->add();
                     $add->foreignKey($rel['self_column']);
                     $fSchema = new $rel['foreign_schema']();
                     $alterReferences = $add->references($fSchema->getTable(), (array) $rel['foreign_column']);
                     if ($this->driver instanceof MySQLDriver) {
                         if ($act = $rel->onUpdate) {
                             $alterReferences->onUpdate($act);
                         }
                         if ($act = $rel->onDelete) {
                             $alterReferences->onDelete($act);
                         }
                     }
                     $this->executeQuery($alterTable);
                 }
             }
             // Find foreign keys that are dropped (doesn't exist in relationship)
             foreach ($references as $col => $ref) {
                 if (!preg_match('/_ibfk_/i', $ref->name)) {
                     continue;
                 }
                 if (!isset($relationshipColumns[$col])) {
                     // echo "drop {$ref->name} for ({$ref->table}, {$ref->column}) from table $table\n";
                     $alterTable = $this->alterTable($table);
                     $alterTable->dropForeignKey($ref->name);
                     $this->executeQuery($alterTable);
                 }
             }
         }
     }
 }
 public function generateWithDiff($taskName, $dataSourceId, $schemas, $time = null)
 {
     $connectionManager = \LazyRecord\ConnectionManager::getInstance();
     $connection = $connectionManager->getConnection($dataSourceId);
     $driver = $connectionManager->getQueryDriver($dataSourceId);
     $parser = TableParser::create($driver, $connection);
     $tableSchemas = $parser->getTableSchemaMap();
     $this->logger->info('Found ' . count($schemas) . ' schemas to compare.');
     $template = $this->createClassTemplate($taskName, $time);
     $template->useClass('SQLBuilder\\Universal\\Syntax\\Column');
     $upgradeMethod = $template->addMethod('public', 'upgrade', array(), '');
     $downgradeMethod = $template->addMethod('public', 'downgrade', array(), '');
     $comparator = new Comparator();
     // schema from runtime
     foreach ($schemas as $b) {
         $tableName = $b->getTable();
         $foundTable = isset($tableSchemas[$tableName]);
         if ($foundTable) {
             $a = $tableSchemas[$tableName];
             // schema object, extracted from database.
             $diffs = $comparator->compare($a, $b);
             // generate alter table statement.
             foreach ($diffs as $diff) {
                 if ($diff->flag == 'A') {
                     $this->logger->info(sprintf("'%s': add column %s", $tableName, $diff->name), 1);
                     $column = $diff->getAfterColumn();
                     $upcall = new MethodCallExpr('$this', 'addColumn', [$tableName, $column]);
                     $upgradeMethod[] = new Statement($upcall);
                     $downcall = new MethodCallExpr('$this', 'dropColumnByName', [$tableName, $diff->name]);
                     $downgradeMethod[] = new Statement($downcall);
                 } else {
                     if ($diff->flag == 'D') {
                         $upcall = new MethodCallExpr('$this', 'dropColumnByName', [$tableName, $diff->name]);
                         $upgradeMethod->getBlock()->appendLine(new Statement($upcall));
                     } else {
                         if ($diff->flag == 'M') {
                             if ($afterColumn = $diff->getAfterColumn()) {
                                 $upcall = new MethodCallExpr('$this', 'modifyColumn', [$tableName, $afterColumn]);
                                 $upgradeMethod[] = new Statement($upcall);
                             } else {
                                 throw new \Exception("afterColumn is undefined.");
                             }
                             continue;
                         } else {
                             $this->logger->warn("** unsupported flag.");
                             continue;
                         }
                     }
                 }
             }
         } else {
             $this->logger->info(sprintf("Found schema '%s' to be imported to '%s'", $b, $tableName), 1);
             // generate create table statement.
             // use sqlbuilder to build schema sql
             $upcall = new MethodCallExpr('$this', 'importSchema', [new Raw('new ' . get_class($b))]);
             $upgradeMethod->getBlock()->appendLine(new Statement($upcall));
             $downcall = new MethodCallExpr('$this', 'dropTable', [$tableName]);
             $downgradeMethod->getBlock()->appendLine(new Statement($downcall));
         }
     }
     $filename = $this->generateFilename($taskName, $time);
     $path = $this->migrationDir . DIRECTORY_SEPARATOR . $filename;
     if (false === file_put_contents($path, $template->render())) {
         throw new RuntimeException("Can't write migration script to {$path}.");
     }
     return array($template->class->name, $path);
 }