public function testDropForeignKeyGetSql() { $foreignKey = new ForeignKey(new Table('foo'), new Table('bar')); $foreignKey->setColumns('column_foo'); $dropConstraintCommand = new DropConstraintCommand(); $dropConstraintCommand->setConstraint($foreignKey); $this->assertEquals('ALTER TABLE public.foo DROP CONSTRAINT foo_column_foo_fkey;', $dropConstraintCommand->getSql()); }
public function setUp() { $this->clearDatabase(); $columns = array(new StringColumn('name', array('not_null' => true, 'default' => 'foo', 'limit' => 150)), new IntegerColumn('bar_id')); $foreignKey = new ForeignKey(new Table('foo'), new Table('bar')); $foreignKey->setColumns('bar_id'); $foreignKey->setReferencedColumns('bar_id'); $constraints = array($foreignKey, new Unique('bar_id', new Table('foo'))); $this->createTable('bar'); $this->createTable('foo', $columns, $constraints); }
/** * Load contsraints to table. * * @param Table $table A table. * * @return void */ private function loadConstraints(Table $table) { foreach ($this->getConstraints() as $constraint) { switch ($constraint['constraint_type']) { case 'FOREIGN KEY': // TODO Find a better way to define foreign key $foreignKey = new ForeignKey(new Table($constraint['table_name']), new Table($constraint['column_name'])); $foreignKey->setColumns($constraint['references_table']); $foreignKey->setReferencedColumns($constraint['references_field']); $table->addConstraint($foreignKey); break; case 'PRIMARY KEY': $table->addConstraint(new PrimaryKey($constraint['column_name'], $table)); break; case 'UNIQUE': $table->addConstraint(new Unique($constraint['column_name'], new Table($constraint['table_name']))); break; } } }
/** * Drop a foreign key. * * @param string $referenceTable Referenced table name. * @param string $referenceColumns Columns of referenced table. * @param array $options Optional options. * * @return TableApi Self. */ public function dropForeignKey($referenceTable, $referenceColumns, array $options = []) { $schema = isset($options['schema']) ? new Schema($options['schema']) : null; $foreignKey = new ForeignKey($this, new Table($referenceTable, $schema)); $foreignKey->setColumns($referenceColumns); $foreignKey->setReferencedColumns($referenceColumns); $this->actions[] = function () use($foreignKey) { return $this->manipulation->drop($foreignKey); }; return $this; }