-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathMySQLDialect.php
More file actions
67 lines (56 loc) · 2.14 KB
/
MySQLDialect.php
File metadata and controls
67 lines (56 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php namespace DBDiff\SQLGen\Dialect;
class MySQLDialect implements SQLDialectInterface {
public function quote(string $name): string {
return '`' . str_replace('`', '``', $name) . '`';
}
public function getDriver(): string {
return 'mysql';
}
public function isMySQLOnly(): bool {
return true;
}
public function dropIndex(string $table, string $key): string {
$t = $this->quote($table);
if ($key === 'PRIMARY') {
return "ALTER TABLE $t DROP PRIMARY KEY;";
}
$k = $this->quote($key);
return "ALTER TABLE $t DROP INDEX $k;";
}
public function dropTrigger(string $trigger, string $table): string {
return "DROP TRIGGER IF EXISTS " . $this->quote($trigger) . ";";
}
/**
* MySQL uses ADD without the COLUMN keyword for backwards-compat.
* Both forms are valid MySQL SQL; the shorter form matches pre-existing
* migration baselines and expected test files.
*/
public function addColumn(string $table, string $colDef): string {
$t = $this->quote($table);
return "ALTER TABLE $t ADD $colDef;";
}
public function dropColumn(string $table, string $col): string {
$t = $this->quote($table);
$c = $this->quote($col);
return "ALTER TABLE $t DROP $c;";
}
public function changeColumn(string $table, string $col, string $newDef): string {
$t = $this->quote($table);
$c = $this->quote($col);
// MySQL CHANGE keeps the same column name; the newDef already contains
// the backtick-quoted name as the first token.
return "ALTER TABLE $t CHANGE $c $newDef;";
}
/**
* MySQL requires DROP FOREIGN KEY for FK constraints.
* Detects the constraint type from the schema DDL fragment.
*/
public function dropConstraint(string $table, string $name, string $schema): string {
$t = $this->quote($table);
$n = $this->quote($name);
if (stripos($schema, 'FOREIGN KEY') !== false) {
return "ALTER TABLE $t DROP FOREIGN KEY $n;";
}
return "ALTER TABLE $t DROP CONSTRAINT $n;";
}
}