public function migrate() { if (empty($this->toSchema)) { throw new \Exception("To schema not defined in Generator\\Database"); } if (empty($this->fromSchema)) { throw new \Exception("From schema not defined in Generator\\Database"); } $sql = $this->fromSchema->getMigrateToSql($this->toSchema, $this->dbal->getDatabasePlatform()); return $sql; }
/** * {@inheritDoc} */ public function migrate() { $entity = $this->mapper->entity(); $table = $entity::table(); $fields = $this->mapper->entityManager()->fields(); $fieldIndexes = $this->mapper->entityManager()->fieldKeys(); $connection = $this->mapper->connection(); $schemaManager = $this->mapper->connection()->getSchemaManager(); $tableObject = $schemaManager->listTableDetails($table); $tableObjects[] = $tableObject; $schema = new Schema($tableObjects); $tableColumns = $tableObject->getColumns(); $tableExists = !empty($tableColumns); if ($tableExists) { $existingTable = $schema->getTable($table); $newSchema = $this->migrateCreateSchema(); $queries = $schema->getMigrateToSql($newSchema, $connection->getDatabasePlatform()); } else { $newSchema = $this->migrateCreateSchema(); $queries = $newSchema->toSql($connection->getDatabasePlatform()); } $lastResult = false; foreach ($queries as $sql) { $lastResult = $connection->exec($sql); } return $lastResult; }
/** * TODO: Document * @param Schema $fromSchema * @param Schema $toSchema */ private function buildSchema(Schema $fromSchema, Schema $toSchema) { $queries = $fromSchema->getMigrateToSql($toSchema, $this->connection->getDatabasePlatform()); foreach ($queries as $query) { $this->connection->exec($query); } }
public function addSchemaChange(Schema $from_schema, $to_schema) { $sql = $from_schema->getMigrateToSql($to_schema, $this->connection->getDatabasePlatform()); foreach ($sql as $key => $part) { $sql[$key] = SqlFormatter::format($part, false); } $this->addSql($sql); }
/** * Create or update a schema. * * @param Schema $schema * @param string $tablePrefix Only tables with this prefix will be updated. * * @throws \Doctrine\DBAL\DBALException */ public function createOrUpdateSchema(Schema $schema, $tablePrefix) { $manager = $this->conn->getSchemaManager(); $platform = $this->conn->getDatabasePlatform(); $origSchema = $manager->createSchema(); $tables = array(); foreach ($origSchema->getTables() as $table) { if (0 === strpos($table->getName(), $tablePrefix)) { $tables[] = $table; } } $migrateSchema = new Schema($tables); $queries = $migrateSchema->getMigrateToSql($schema, $platform); foreach ($queries as $query) { $this->conn->executeQuery($query); } }
/** * @author "Lionel Lecaque, <*****@*****.**>" * @param \Doctrine\DBAL\Schema\Schema $fromSchema * @param \Doctrine\DBAL\Schema\Schema $toSchema */ public function getMigrateSchemaSql($fromSchema, $toSchema) { return $fromSchema->getMigrateToSql($toSchema, $this->dbalPlatform); }
/** * Update plugin schema based on Doctrine metadata. * * WARNING - this is not recommended as Doctrine does not guarantee results. There is a risk * that Doctrine will generate an incorrect query leading to lost data. If using this method, * be sure to thoroughly test the queries Doctrine generates * * @param array $metadata * @param Schema $installedSchema * @param MauticFactory $factory * * @throws \Doctrine\DBAL\ConnectionException * @throws \Exception */ public static function updatePluginSchema(array $metadata, Schema $installedSchema, MauticFactory $factory) { $db = $factory->getDatabase(); $schemaTool = new SchemaTool($factory->getEntityManager()); $toSchema = $schemaTool->getSchemaFromMetadata($metadata); $queries = $installedSchema->getMigrateToSql($toSchema, $db->getDatabasePlatform()); $db->beginTransaction(); try { foreach ($queries as $q) { $db->query($q); } $db->commit(); } catch (\Exception $e) { $db->rollback(); throw $e; } }
/** * @param Configuration $configuration * @param Schema $from * @param Schema $to * * @return string */ public function up(Configuration $configuration, Schema $from, Schema $to) { return $this->build($configuration, $from->getMigrateToSql($to, $configuration->getConnection()->getDatabasePlatform())); }
/** * @param Schema $fromSchema * @param Schema $toSchema * @return string[] */ public function getSqlDiffToMigrate(Schema $fromSchema, Schema $toSchema) { return $fromSchema->getMigrateToSql($toSchema, $this->platform); }