Skip to content

Commit

Permalink
Add methods to ALTER TABLE with IF EXISTS
Browse files Browse the repository at this point in the history
  • Loading branch information
natanfelles committed May 16, 2022
1 parent 7829b99 commit 90a2035
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Definition/AlterTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,12 @@ public function dropColumn(string $name, bool $ifExists = false) : static
return $this;
}

public function dropColumnIfExists(string $name) : static
{
$this->sql['drop_columns'][$name] = true;
return $this;
}

protected function renderDropColumns() : ?string
{
if ( ! isset($this->sql['drop_columns'])) {
Expand Down Expand Up @@ -271,6 +277,12 @@ public function dropKey(string $name, bool $ifExists = false) : static
return $this;
}

public function dropKeyIfExists(string $name) : static
{
$this->sql['drop_keys'][$name] = true;
return $this;
}

protected function renderDropKeys() : ?string
{
if ( ! isset($this->sql['drop_keys'])) {
Expand All @@ -291,6 +303,12 @@ public function dropForeignKey(string $name, bool $ifExists = false) : static
return $this;
}

public function dropForeignKeyIfExists(string $name) : static
{
$this->sql['drop_foreign_keys'][$name] = true;
return $this;
}

protected function renderDropForeignKeys() : ?string
{
if ( ! isset($this->sql['drop_foreign_keys'])) {
Expand All @@ -311,6 +329,12 @@ public function dropConstraint(string $name, bool $ifExists = false) : static
return $this;
}

public function dropConstraintIfExists(string $name) : static
{
$this->sql['drop_constraints'][$name] = true;
return $this;
}

protected function renderDropConstraints() : ?string
{
if ( ! isset($this->sql['drop_constraints'])) {
Expand Down
36 changes: 36 additions & 0 deletions tests/Definition/AlterTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ public function testModify() : void
);
}

public function testDropColumnIfExists() : void
{
$alterTable = $this->alterTable->table('t1')->dropColumnIfExists('foo');
self::assertSame(
"ALTER TABLE `t1`\n DROP COLUMN IF EXISTS `foo`",
$alterTable->sql()
);
}

public function testDropColumns() : void
{
$alterTable = $this->alterTable->table('t1')->dropColumn('foo');
Expand All @@ -114,6 +123,15 @@ public function testDropPrimaryKey() : void
);
}

public function testDropKeyIfExists() : void
{
$alterTable = $this->alterTable->table('t1')->dropKeyIfExists('foo');
self::assertSame(
"ALTER TABLE `t1`\n DROP KEY IF EXISTS `foo`",
$alterTable->sql()
);
}

public function testDropKeys() : void
{
$alterTable = $this->alterTable->table('t1')->dropKey('foo');
Expand All @@ -128,6 +146,15 @@ public function testDropKeys() : void
);
}

public function testDropForeignKeyIfExists() : void
{
$alterTable = $this->alterTable->table('t1')->dropForeignKeyIfExists('foo');
self::assertSame(
"ALTER TABLE `t1`\n DROP FOREIGN KEY IF EXISTS `foo`",
$alterTable->sql()
);
}

public function testDropForeignKeys() : void
{
$alterTable = $this->alterTable->table('t1')->dropForeignKey('foo');
Expand All @@ -142,6 +169,15 @@ public function testDropForeignKeys() : void
);
}

public function testDropConstraintIfExists() : void
{
$alterTable = $this->alterTable->table('t1')->dropConstraintIfExists('foo');
self::assertSame(
"ALTER TABLE `t1`\n DROP CONSTRAINT IF EXISTS `foo`",
$alterTable->sql()
);
}

public function testDropConstraints() : void
{
$alterTable = $this->alterTable->table('t1')->dropConstraint('foo');
Expand Down

0 comments on commit 90a2035

Please sign in to comment.