Esempio n. 1
0
 /**
  * Migrate table structure changes to database
  *
  * @return bool
  * @throws \Doctrine\DBAL\Schema\SchemaException
  * @throws \Spot\Exception
  */
 public function migrate()
 {
     // Mapper knows currently set entity
     $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 \Doctrine\DBAL\Schema\Schema($tableObjects);
     $tableColumns = $tableObject->getColumns();
     $tableExists = !empty($tableColumns);
     if ($tableExists) {
         // Update existing table
         $existingTable = $schema->getTable($table);
         $newSchema = $this->migrateCreateSchema();
         $queries = $schema->getMigrateToSql($newSchema, $connection->getDatabasePlatform());
     } else {
         // Create new table
         $newSchema = $this->migrateCreateSchema();
         $queries = $newSchema->toSql($connection->getDatabasePlatform());
     }
     // Execute resulting queries
     $lastResult = false;
     foreach ($queries as $sql) {
         $lastResult = $connection->exec($sql);
     }
     return $lastResult;
 }
Esempio n. 2
0
File: DBAL.php Progetto: cti/storage
 public function convert(\Cti\Storage\Schema $inputSchema)
 {
     $schema = new \Doctrine\DBAL\Schema\Schema();
     foreach ($inputSchema->getModels() as $model) {
         $table = $schema->createTable($model->getName());
         foreach ($model->getProperties() as $property) {
             $params = array('comment' => $property->getComment(), 'notnull' => $property->getRequired());
             $type = $property->getType();
             if ($type === 'char') {
                 $type = 'string';
                 $params['length'] = 1;
             }
             $table->addColumn($property->getName(), $type, $params);
         }
         $table->setPrimaryKey($model->getPk());
         foreach ($model->getIndexes() as $index) {
             $table->addIndex($index->getFields());
         }
     }
     foreach ($inputSchema->getModels() as $model) {
         $table = $schema->getTable($model->getName());
         foreach ($model->getOutReferences() as $reference) {
             if ($inputSchema->getModel($reference->getDestination())->getBehaviour("log")) {
                 continue;
             }
             $destination = $schema->getTable($reference->getDestination());
             $foreignProperties = array();
             foreach ($reference->getProperties() as $property) {
                 $foreignProperties[] = $property->getForeignName();
             }
             $localProperties = array_keys($reference->getProperties());
             $table->addForeignKeyConstraint($destination, $localProperties, $foreignProperties);
         }
     }
     foreach ($inputSchema->getSequences() as $sequence) {
         $schema->createSequence($sequence->getName());
     }
     return $schema;
 }