public function makeTableRepresentation(DBAL\Schema\Table $table) { $table->addColumn('id', 'string', ['length' => 100]); $table->addColumn('datetime', 'datetime'); $table->addColumn('last_indexed', 'integer', ['length' => 50]); $table->addIndex(['id', 'datetime']); }
public function __construct(TableInformation $parent, \Doctrine\DBAL\Schema\Table $table, \Doctrine\DBAL\Schema\Column $column) { $this->table = $parent; foreach ($table->getForeignKeys() as $foreign) { if (in_array($column->getName(), $foreign->getColumns())) { $foreign_columns = $foreign->getForeignColumns(); $this->foreignTable = $foreign->getForeignTableName(); $this->foreignColumn = reset($foreign_columns); $this->isForeign = true; } } if ($primary_key = $table->getPrimaryKey()) { $this->isPrimary = in_array($column->getName(), $primary_key->getColumns()); } $this->name = $column->getName(); $this->type = $column->getType()->getName(); $this->length = $column->getLength(); $this->precision = $column->getPrecision(); $this->default = $column->getDefault(); $this->isNotNull = $column->getNotnull(); $this->isUnsigned = $column->getUnsigned(); $this->isFixed = $column->getFixed(); $this->isAutoIncrement = $column->getAutoincrement(); $this->comment = $column->getComment(); if ($this->type === \Doctrine\DBAL\Types\Type::BLOB) { $this->length = min($this->bytesFromIni('post_max_size'), $this->bytesFromIni('upload_max_filesize')); } }
public function testGenerateTableWithAutoincrement() { $table = new \Doctrine\DBAL\Schema\Table('autoinc_table'); $column = $table->addColumn('id', 'integer'); $column->setAutoincrement(true); $this->assertEquals(array('CREATE TABLE autoinc_table (id SERIAL NOT NULL)'), $this->_platform->getCreateTableSQL($table)); }
public function addColumns(\Doctrine\DBAL\Schema\Table $table, $columns) { foreach ($columns as $column) { $table->addColumn($column['name'], $column['type'], $column['options']); } return $table; }
public function testReservedColumnName() { $table = new Table("TABLE"); $column = $table->addColumn('table', 'string'); $this->validator->acceptColumn($table, $column); $this->assertEquals(array('Table TABLE column table keyword violations: MySQL'), $this->validator->getViolations()); }
/** * @param Table $localTable * @param ForeignKeyConstraint $fkConstraint */ public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint) { // Append the foreign key constraints SQL if ($this->_platform->supportsForeignKeyConstraints()) { $this->_createFkConstraintQueries = array_merge($this->_createFkConstraintQueries, (array) $this->_platform->getCreateForeignKeySQL($fkConstraint, $localTable->getName())); } }
/** * @test */ public function table_should_contain_binary_uuid_column() { $uuidColumn = $this->table->getColumn('uuid'); $this->assertEquals(16, $uuidColumn->getLength()); $this->assertEquals(Type::getType(Type::BINARY), $uuidColumn->getType()); $this->assertTrue($uuidColumn->getFixed()); }
/** * @param Table $table * @return bool */ public function isTableNotIgnored(Table $table) { if (!$this->config->hasTableConfig($table->getName())) { return true; } return !$this->config->getTableConfig($table->getName())->isTableIgnored(); }
private function createTable($name) { $table = new Schema\Table($this->tableName($name)); $table->addColumn('id', Type::INTEGER, ['autoincrement' => true, 'unsigned' => true]); $table->setPrimaryKey(['id']); return $table; }
/** * @param string $schema * @param Table $table * @param string $name * @param FieldDefinition $definition */ public function map(string $schema, Table $table, string $name, FieldDefinition $definition) { $table->addColumn($name, 'float', ['notnull' => !$definition->isNullable(), 'default' => $definition->defaultValue(), 'unique' => $definition->options()['unique'] ?? false]); if ($definition->options()['index'] ?? false) { $table->addIndex([$name]); } }
public function makeTableRepresentation(DBAL\Schema\Table $table) { $table->addColumn('id', 'string', ['length' => 100]); $table->addColumn('content', 'string', ['length' => $this->length]); $table->addColumn('last_indexed', 'integer', ['length' => 50]); $table->addIndex(['id', 'content']); }
/** * @group DDC-1337 * @return void */ public function testCreateTemporaryTableNotAutoCommitTransaction() { if ($this->_conn->getDatabasePlatform()->getName() == 'sqlanywhere' || $this->_conn->getDatabasePlatform()->getName() == 'oracle') { $this->markTestSkipped("Test does not work on Oracle and SQL Anywhere."); } $platform = $this->_conn->getDatabasePlatform(); $columnDefinitions = array("id" => array("type" => Type::getType("integer"), "notnull" => true)); $tempTable = $platform->getTemporaryTableName("my_temporary"); $createTempTableSQL = $platform->getCreateTemporaryTableSnippetSQL() . ' ' . $tempTable . ' (' . $platform->getColumnDeclarationListSQL($columnDefinitions) . ')'; $table = new Table("nontemporary"); $table->addColumn("id", "integer"); $table->setPrimaryKey(array('id')); foreach ($platform->getCreateTableSQL($table) as $sql) { $this->_conn->executeQuery($sql); } $this->_conn->beginTransaction(); $this->_conn->insert("nontemporary", array("id" => 1)); $this->_conn->exec($createTempTableSQL); $this->_conn->insert("nontemporary", array("id" => 2)); $this->_conn->rollback(); try { $this->_conn->exec($platform->getDropTemporaryTableSQL($tempTable)); } catch (\Exception $e) { } $rows = $this->_conn->fetchAll('SELECT * FROM nontemporary'); $this->assertEquals(array(), $rows, "In an event of an error this result has one row, because of an implicit commit."); }
/** * Exports create table SQL. * * @return string */ protected function exportCreateTable() { $table = new Table(self::TABLE_NAME, array(), array(), array(), false, array()); $table->addColumn('id', 'string', array('length' => 2, 'notnull' => true)); $table->setPrimaryKey(array('id')); $table->addColumn('value', 'string', array('length' => 64)); return array_pop($this->getConnection()->getDatabasePlatform()->getCreateTableSQL($table, AbstractPlatform::CREATE_INDEXES)) . ';' . PHP_EOL; }
/** * @param string $schema * @param Table $table * @param string $name * @param FieldDefinition $definition * * @throws DoctrineStorageException */ public function map(string $schema, Table $table, string $name, FieldDefinition $definition) { if (!$definition instanceof AssociationFieldDefinition) { throw DoctrineStorageException::invalidDefinition(AssociationFieldDefinition::class, $definition); } $table->addColumn($name, 'guid', ['notnull' => !$definition->isNullable(), 'default' => $definition->defaultValue(), 'length' => $definition->options()['length'] ?? null, 'unique' => $definition->options()['unique'] ?? false]); $table->addForeignKeyConstraint($this->tableName($schema, $definition->typeSchema()->name()), [$name], ['id']); }
/** * Get the table's schema object. * * @param Schema $schema * @param string $tableName * * @return \Doctrine\DBAL\Schema\Table */ public function buildTable(Schema $schema, $tableName) { $this->table = $schema->createTable($tableName); $this->tableName = $this->table->getName(); $this->addColumns(); $this->addIndexes(); $this->setPrimaryKey(); return $this->table; }
/** * @param DBALTable $KeyTarget Foreign Key (Column: KeySource Name) * @param DBALTable $KeySource Foreign Data (Column: Id) */ public function addForeignKey(DBALTable &$KeyTarget, DBALTable $KeySource) { if (!$this->Database->hasColumn($KeyTarget->getName(), $KeySource->getName())) { $KeyTarget->addColumn($KeySource->getName(), 'bigint'); if ($this->Database->getPlatform()->supportsForeignKeyConstraints()) { $KeyTarget->addForeignKeyConstraint($KeySource, array($KeySource->getName()), array('Id')); } } }
/** * @param Table $table */ protected function _addTable(Table $table) { $tableName = strtolower($table->getName()); if (isset($this->_tables[$tableName])) { throw SchemaException::tableAlreadyExists($tableName); } $this->_tables[$tableName] = $table; $table->setSchemaConfig($this->_schemaConfig); }
/** * Renames a column * * @param Schema $schema * @param QueryBag $queries * @param Table $table * @param string $oldColumnName * @param string $newColumnName */ public function renameColumn(Schema $schema, QueryBag $queries, Table $table, $oldColumnName, $newColumnName) { $column = new Column(['column' => $table->getColumn($oldColumnName)]); $column->changeName($newColumnName); $diff = new TableDiff($table->getName()); $diff->renamedColumns = [$oldColumnName => $column]; $renameQuery = new SqlMigrationQuery($this->platform->getAlterTableSQL($diff)); $queries->addQuery($renameQuery); }
public function __construct(SchemaInformation $schema, \Doctrine\DBAL\Schema\Table $table) { $this->table = $table; $this->schema = $schema; $this->name = $table->getName(); foreach ($this->table->getColumns() as $column) { $this->columns[$column->getName()] = new ColumnInformation($this, $table, $column); } }
/** * Adds the missing indexes to the table * * @param \Doctrine\DBAL\Schema\Table $table Table object * @return \Doctrine\DBAL\Schema\Table Updated table object */ protected function addIndexes(\Doctrine\DBAL\Schema\Table $table) { $indexes = array('idx_ezpus_langid' => array('langid'), 'idx_ezpus_status_ln_fn' => array('status', 'lastname', 'firstname'), 'idx_ezpus_status_ad1_ad2' => array('status', 'address1', 'address2'), 'idx_ezpus_status_postal_city' => array('status', 'postal', 'city'), 'idx_ezpus_lastname' => array('lastname'), 'idx_ezpus_address1' => array('address1'), 'idx_ezpus_postal' => array('postal'), 'idx_ezpus_city' => array('city')); foreach ($indexes as $name => $def) { if ($table->hasIndex($name) === false) { $table->addIndex($def, $name); } } return $table; }
/** * @param Schema $schema * @param Table $table * @param string $keyName */ protected function setOwnerCascadeDelete(Schema $schema, Table $table, $keyName) { foreach ($table->getForeignKeys() as $foreignKey) { if ($foreignKey->getLocalColumns() == ['owner_id']) { $table->removeForeignKey($foreignKey->getName()); $table->addForeignKeyConstraint($schema->getTable('orocrm_contact'), ['owner_id'], ['id'], ['onDelete' => 'CASCADE', 'onUpdate' => null], $keyName); break; } } }
/** * @param Table $localTable * @param ForeignKeyConstraint $fkConstraint */ public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint) { if ($this->_addExplicitIndexForForeignKey) { $columns = $fkConstraint->getColumns(); if ($localTable->columnsAreIndexed($columns)) { return; } $localTable->addIndex($columns); } }
/** * Adds the missing indexes to the table * * @param \Doctrine\DBAL\Schema\Table $table Table object * @return \Doctrine\DBAL\Schema\Table Updated table object */ protected function addIndexes(\Doctrine\DBAL\Schema\Table $table) { $indexes = array('idx_msord_sid_cdate' => array('siteid', 'cdate'), 'idx_msord_sid_cmonth' => array('siteid', 'cmonth'), 'idx_msord_sid_cweek' => array('siteid', 'cweek'), 'idx_msord_sid_hour' => array('siteid', 'chour')); foreach ($indexes as $name => $def) { if ($table->hasIndex($name) === false) { $table->addIndex($def, $name); } } return $table; }
/** * Drops the tables and rebuilds them */ public function rebuildSchema() { $schemaManager = $this->conn->getSchemaManager(); $productTable = new Table('product'); $productTable->addColumn("id", "integer", array("unsigned" => true)); $productTable->addColumn("name", "string", array("length" => 255)); $productTable->addColumn('author_id', 'integer', array('notNull' => false)); $productTable->addColumn("description", "text", array('notNull' => false)); $productTable->addColumn("price", "decimal", array('scale' => 2, 'notNull' => false)); $productTable->addColumn("is_published", "boolean"); $productTable->addColumn('created_at', 'datetime'); $productTable->setPrimaryKey(array("id")); $schemaManager->dropAndCreateTable($productTable); $userTable = new Table('user'); $userTable->addColumn('id', 'integer', array('unsigned' => true, 'autoincrement' => true)); $userTable->setPrimaryKey(array('id')); $userTable->addColumn('username', 'string', array('length' => 32)); $userTable->addUniqueIndex(array('username')); $userTable->addColumn('password', 'string', array('length' => 255)); $userTable->addColumn('roles', 'string', array('length' => 255)); $userTable->addColumn('created_at', 'datetime'); // add an author_id to product $productTable->addForeignKeyConstraint($userTable, array('author_id'), array('id')); $schemaManager->dropAndCreateTable($userTable); }
protected function doExecute() { $this->step('Creating schema'); $configuration = new \Firenote\Configuration\Yaml($this->rootPath . 'config'); $app = new \Firenote\Application($configuration); $this->createDatabase($configuration); $schema = $app['db']->getSchemaManager(); if (!$schema instanceof \Doctrine\DBAL\Schema\AbstractSchemaManager) { throw new \Exception(); } if (!$schema->tablesExist('users')) { $this->writeln('<info>Creating table users ...</info>'); $users = new Table('users'); $users->addColumn('id', 'integer', array('unsigned' => true, 'autoincrement' => true)); $users->setPrimaryKey(array('id')); $users->addColumn('username', 'string', array('length' => 32)); $users->addUniqueIndex(array('username')); $users->addColumn('password', 'string', array('length' => 255)); $users->addColumn('roles', 'string', array('length' => 255)); $users->addColumn('avatar', 'string', array('length' => 512)); $schema->createTable($users); $this->writeln('<info>Adding admin user (admin/foo) ...</info>'); $this->writeln('<comment>Please change this dummy password !</comment>'); $app['db']->insert('users', array('username' => 'admin', 'password' => '5FZ2Z8QIkA7UTZ4BYkoC+GsReLf569mSKDsfods6LYQ8t+a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg==', 'roles' => 'ROLE_ADMIN', 'avatar' => '/assets/firenote/avatars/avatar2.png')); } else { $this->writeln('Nothing to do !'); } }
/** * @group DDC-1737 */ public function testClobNoAlterTable() { $tableOld = new Table("test"); $tableOld->addColumn('id', 'integer'); $tableOld->addColumn('description', 'string', array('length' => 65536)); $tableNew = clone $tableOld; $tableNew->setPrimaryKey(array('id')); $diff = $this->comparator->diffTable($tableOld, $tableNew); $sql = $this->platform->getAlterTableSQL($diff); $this->assertEquals(array('ALTER TABLE test ADD PRIMARY KEY (id)'), $sql); }
public function testListTableColumnsWithFixedStringTypeColumn() { $table = new Table('list_table_columns_char'); $table->addColumn('id', 'integer', array('notnull' => true)); $table->addColumn('test', 'string', array('fixed' => true)); $table->setPrimaryKey(array('id')); $this->_sm->dropAndCreateTable($table); $columns = $this->_sm->listTableColumns('list_table_columns_char'); $this->assertArrayHasKey('test', $columns); $this->assertTrue($columns['test']->getFixed()); }
/** * @group DBAL-255 */ public function testDropColumnConstraints() { $table = new Table('sqlsrv_drop_column'); $table->addColumn('id', 'integer'); $table->addColumn('todrop', 'decimal', array('default' => 10.2)); $this->_sm->createTable($table); $diff = new TableDiff('sqlsrv_drop_column', array(), array(), array(new Column('todrop', Type::getType('decimal')))); $this->_sm->alterTable($diff); $columns = $this->_sm->listTableColumns('sqlsrv_drop_column'); $this->assertEquals(1, count($columns)); }
public function testSearchPathSchemaChanges() { $table = new Table("dbal510tbl"); $table->addColumn('id', 'integer'); $table->setPrimaryKey(array('id')); $this->_conn->getSchemaManager()->createTable($table); $onlineTable = $this->_conn->getSchemaManager()->listTableDetails('dbal510tbl'); $comparator = new Comparator(); $diff = $comparator->diffTable($onlineTable, $table); $this->assertFalse($diff); }
/** * @group DBAL-132 */ public function testGenerateForeignKeySQL() { $tableOld = new Table("test"); $tableOld->addColumn('foo_id', 'integer'); $tableOld->addUnnamedForeignKeyConstraint('test_foreign', array('foo_id'), array('foo_id')); $sqls = array(); foreach ($tableOld->getForeignKeys() as $fk) { $sqls[] = $this->platform->getCreateForeignKeySQL($fk, $tableOld); } $this->assertEquals(array("ALTER TABLE test ADD CONSTRAINT FK_D87F7E0C8E48560F FOREIGN KEY (foo_id) REFERENCES test_foreign (foo_id)"), $sqls); }