Skip to content

Commit 90a2035

Browse files
committed
Add methods to ALTER TABLE with IF EXISTS
1 parent 7829b99 commit 90a2035

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

src/Definition/AlterTable.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,12 @@ public function dropColumn(string $name, bool $ifExists = false) : static
237237
return $this;
238238
}
239239

240+
public function dropColumnIfExists(string $name) : static
241+
{
242+
$this->sql['drop_columns'][$name] = true;
243+
return $this;
244+
}
245+
240246
protected function renderDropColumns() : ?string
241247
{
242248
if ( ! isset($this->sql['drop_columns'])) {
@@ -271,6 +277,12 @@ public function dropKey(string $name, bool $ifExists = false) : static
271277
return $this;
272278
}
273279

280+
public function dropKeyIfExists(string $name) : static
281+
{
282+
$this->sql['drop_keys'][$name] = true;
283+
return $this;
284+
}
285+
274286
protected function renderDropKeys() : ?string
275287
{
276288
if ( ! isset($this->sql['drop_keys'])) {
@@ -291,6 +303,12 @@ public function dropForeignKey(string $name, bool $ifExists = false) : static
291303
return $this;
292304
}
293305

306+
public function dropForeignKeyIfExists(string $name) : static
307+
{
308+
$this->sql['drop_foreign_keys'][$name] = true;
309+
return $this;
310+
}
311+
294312
protected function renderDropForeignKeys() : ?string
295313
{
296314
if ( ! isset($this->sql['drop_foreign_keys'])) {
@@ -311,6 +329,12 @@ public function dropConstraint(string $name, bool $ifExists = false) : static
311329
return $this;
312330
}
313331

332+
public function dropConstraintIfExists(string $name) : static
333+
{
334+
$this->sql['drop_constraints'][$name] = true;
335+
return $this;
336+
}
337+
314338
protected function renderDropConstraints() : ?string
315339
{
316340
if ( ! isset($this->sql['drop_constraints'])) {

tests/Definition/AlterTableTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ public function testModify() : void
9191
);
9292
}
9393

94+
public function testDropColumnIfExists() : void
95+
{
96+
$alterTable = $this->alterTable->table('t1')->dropColumnIfExists('foo');
97+
self::assertSame(
98+
"ALTER TABLE `t1`\n DROP COLUMN IF EXISTS `foo`",
99+
$alterTable->sql()
100+
);
101+
}
102+
94103
public function testDropColumns() : void
95104
{
96105
$alterTable = $this->alterTable->table('t1')->dropColumn('foo');
@@ -114,6 +123,15 @@ public function testDropPrimaryKey() : void
114123
);
115124
}
116125

126+
public function testDropKeyIfExists() : void
127+
{
128+
$alterTable = $this->alterTable->table('t1')->dropKeyIfExists('foo');
129+
self::assertSame(
130+
"ALTER TABLE `t1`\n DROP KEY IF EXISTS `foo`",
131+
$alterTable->sql()
132+
);
133+
}
134+
117135
public function testDropKeys() : void
118136
{
119137
$alterTable = $this->alterTable->table('t1')->dropKey('foo');
@@ -128,6 +146,15 @@ public function testDropKeys() : void
128146
);
129147
}
130148

149+
public function testDropForeignKeyIfExists() : void
150+
{
151+
$alterTable = $this->alterTable->table('t1')->dropForeignKeyIfExists('foo');
152+
self::assertSame(
153+
"ALTER TABLE `t1`\n DROP FOREIGN KEY IF EXISTS `foo`",
154+
$alterTable->sql()
155+
);
156+
}
157+
131158
public function testDropForeignKeys() : void
132159
{
133160
$alterTable = $this->alterTable->table('t1')->dropForeignKey('foo');
@@ -142,6 +169,15 @@ public function testDropForeignKeys() : void
142169
);
143170
}
144171

172+
public function testDropConstraintIfExists() : void
173+
{
174+
$alterTable = $this->alterTable->table('t1')->dropConstraintIfExists('foo');
175+
self::assertSame(
176+
"ALTER TABLE `t1`\n DROP CONSTRAINT IF EXISTS `foo`",
177+
$alterTable->sql()
178+
);
179+
}
180+
145181
public function testDropConstraints() : void
146182
{
147183
$alterTable = $this->alterTable->table('t1')->dropConstraint('foo');

0 commit comments

Comments
 (0)