public function execute() { $options = $this->options; $logger = $this->logger; $id = $this->getCurrentDataSourceId(); $logger->debug("Finding schema classes..."); $schemas = $this->findSchemasByArguments(func_get_args()); $logger->debug("Initialize schema builder..."); $connectionManager = ConnectionManager::getInstance(); $conn = $connectionManager->getConnection($id); $driver = $connectionManager->getQueryDriver($id); $sqlBuilder = SqlBuilder::create($driver, array('rebuild' => $options->rebuild, 'clean' => $options->clean)); $builder = new DatabaseBuilder($conn, $sqlBuilder, $this->logger); $sqls = $builder->build($schemas); $sqlOutput = join("\n", $sqls); if ($file = $this->options->file) { $fp = fopen($file, 'w'); fwrite($fp, $sqlOutput); fclose($fp); } if ($this->options->basedata) { $collection = new SchemaCollection($schemas); $collection = $collection->evaluate(); $seedBuilder = new SeedBuilder($this->getConfigLoader(), $this->logger); $seedBuilder->build($collection); } $time = time(); $logger->info("Setting migration timestamp to {$time}"); $metadata = new Metadata($driver, $conn); // update migration timestamp $metadata['migration'] = $time; $logger->info($logger->formatter->format('Done. ' . count($schemas) . " schema tables were generated into data source '{$id}'.", 'green')); }
public function seed(array $schemas, ConfigLoader $config = null) { $seedBuilder = new SeedBuilder($this->logger); $seedBuilder->build(new SchemaCollection($schemas)); if ($config) { $seedBuilder->buildConfigSeeds($config); } }
public function setUp() { $annnotations = $this->getAnnotations(); $configLoader = ConfigLoader::getInstance(); $configLoader->loadFromSymbol(true); $configLoader->setDefaultDataSourceId($this->getDriverType()); $connManager = ConnectionManager::getInstance(); $connManager->init($configLoader); try { $dbh = $connManager->getConnection($this->getDriverType()); } catch (PDOException $e) { if ($this->allowConnectionFailure) { $this->markTestSkipped(sprintf("Can not connect to database by data source '%s' message:'%s' config:'%s'", $this->getDriverType(), $e->getMessage(), var_export($configLoader->getDataSource($this->getDriverType()), true))); return; } else { echo sprintf("Can not connect to database by data source '%s' message:'%s' config:'%s'", $this->getDriverType(), $e->getMessage(), var_export($configLoader->getDataSource($this->getDriverType()), true)); throw $e; } } $driver = $connManager->getQueryDriver($this->getDriverType()); $this->assertInstanceOf('SQLBuilder\\Driver\\BaseDriver', $driver, 'QueryDriver object OK'); // Rebuild means rebuild the database for new tests $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; } if ($rebuild) { $builder = SqlBuilder::create($driver, array('rebuild' => true)); $this->assertNotNull($builder); // $schemas = ClassUtils::schema_classes_to_objects($this->getModels()); $schemas = ClassUtils::schema_classes_to_objects($this->getModels()); foreach ($schemas as $schema) { $sqls = $builder->build($schema); $this->assertNotEmpty($sqls); foreach ($sqls as $sql) { $dbh->query($sql); } } if ($basedata) { $runner = new SeedBuilder($this->config, $this->logger); foreach ($schemas as $schema) { $runner->buildSchemaSeeds($schema); } if ($scripts = $this->config->getSeedScripts()) { foreach ($scripts as $script) { $runner->buildScriptSeed($script); } } } } }
public function execute() { $options = $this->options; $logger = $this->logger; $classes = $this->findSchemasByArguments(func_get_args()); SchemaUtils::printSchemaClasses($classes, $this->logger); $collection = new SchemaCollection($classes); $collection = $collection->evaluate(); $seedBuilder = new SeedBuilder($this->getConfigLoader(), $this->logger); $seedBuilder->build($collection); $this->logger->info('Done'); }
public function setUp() { $annnotations = $this->getAnnotations(); $connManager = ConnectionManager::getInstance(); $dataSourceConfig = self::createDataSourceConfig($this->driver); if ($dataSourceConfig) { $connManager->addDataSource('default', $dataSourceConfig); } else { $this->markTestSkipped("{$this->driver} database configuration is missing."); } try { $dbh = $connManager->getConnection('default'); } catch (PDOException $e) { $this->markTestSkipped('Can not connect to database, test skipped: ' . $e->getMessage()); return; } $driver = ConnectionManager::getInstance()->getQueryDriver('default'); ok($driver, 'QueryDriver object OK'); // Rebuild means rebuild the database for new tests $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; } if ($rebuild) { $builder = SqlBuilder::create($driver, array('rebuild' => true)); $this->assertNotNull($builder); // $schemas = ClassUtils::schema_classes_to_objects($this->getModels()); $schemas = ClassUtils::schema_classes_to_objects($this->getModels()); foreach ($schemas as $schema) { $sqls = $builder->build($schema); $this->assertNotEmpty($sqls); foreach ($sqls as $sql) { $dbh->query($sql); } } if ($basedata) { $runner = new SeedBuilder($this->config, $this->logger); foreach ($schemas as $schema) { $runner->buildSchemaSeeds($schema); } if ($scripts = $this->config->getSeedScripts()) { foreach ($scripts as $script) { $runner->buildScriptSeed($script); } } } } }
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); } }