Skip to content

Commit bc1c76d

Browse files
committed
Merge pull request #1 from mdhgriffiths/master
Added foreign key and on update CURRENT_TIMESTAMP support
2 parents 3d7b2a3 + 332b293 commit bc1c76d

1 file changed

Lines changed: 52 additions & 3 deletions

File tree

mysql2phinx.php

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
'name' => $argv[1],
2323
'user' => $argv[2],
2424
'pass' => $argv[3],
25-
'host' => $argc === 5 ? $argv[6] : '127.0.0.1',
25+
'host' => $argc === 5 ? $argv[6] : 'localhost',
2626
'port' => $argc === 6 ? $argv[5] : '3306'
2727
);
2828

@@ -61,8 +61,8 @@ function getTableMigration($table, $mysqli, $indent)
6161
}
6262
}
6363

64-
if ($indexes = getIndexMigrations(getIndexes($table, $mysqli), $indent + 1)) {
65-
$output[] = $indexes;
64+
if ($foreign_keys = getForeignKeysMigrations(getForeignKeys($table, $mysqli), $indent + 1)) {
65+
$output[] = $foreign_keys;
6666
}
6767

6868
$output[] = $ind . ' ->create();';
@@ -112,6 +112,19 @@ function getIndexMigrations($indexes, $indent)
112112
return implode(PHP_EOL, $output);
113113
}
114114

115+
function getForeignKeysMigrations($foreign_keys, $indent)
116+
{
117+
$ind = getIndentation($indent);
118+
$output = [];
119+
foreach ($foreign_keys as $foreign_key) {
120+
$output[] = $ind . "->addForeignKey('" . $foreign_key['COLUMN_NAME'] . "', '" . $foreign_key['REFERENCED_TABLE_NAME'] . "', '" . $foreign_key['REFERENCED_COLUMN_NAME'] . "', array("
121+
. "'delete' => '" . str_replace(' ', '_', $foreign_key['DELETE_RULE']) . "',"
122+
. "'update' => '" . str_replace(' ', '_', $foreign_key['UPDATE_RULE']) . "'"
123+
. "))";
124+
}
125+
return implode(PHP_EOL, $output);
126+
}
127+
115128
/* ---- */
116129

117130
function getMySQLColumnType($columndata)
@@ -177,6 +190,11 @@ function getPhinxColumnAttibutes($phinxtype, $columndata)
177190
$attributes[] = '\'default\' => ' . $default;
178191
}
179192

193+
// on update CURRENT_TIMESTAMP
194+
if ($columndata['Extra'] === 'on update CURRENT_TIMESTAMP') {
195+
$attributes[] = '\'update\' => \'CURRENT_TIMESTAMP\'';
196+
}
197+
180198
// limit / length
181199
$limit = 0;
182200
switch (getMySQLColumnType($columndata)) {
@@ -244,6 +262,37 @@ function getIndexes($table, $mysqli)
244262
return $res->fetch_all(MYSQLI_ASSOC);
245263
}
246264

265+
function getForeignKeys($table, $mysqli)
266+
{
267+
$res = $mysqli->query("SELECT
268+
cols.TABLE_NAME,
269+
cols.COLUMN_NAME,
270+
refs.REFERENCED_TABLE_NAME,
271+
refs.REFERENCED_COLUMN_NAME,
272+
cRefs.UPDATE_RULE,
273+
cRefs.DELETE_RULE
274+
FROM INFORMATION_SCHEMA.COLUMNS as cols
275+
LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS refs
276+
ON refs.TABLE_SCHEMA=cols.TABLE_SCHEMA
277+
AND refs.REFERENCED_TABLE_SCHEMA=cols.TABLE_SCHEMA
278+
AND refs.TABLE_NAME=cols.TABLE_NAME
279+
AND refs.COLUMN_NAME=cols.COLUMN_NAME
280+
LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS cons
281+
ON cons.TABLE_SCHEMA=cols.TABLE_SCHEMA
282+
AND cons.TABLE_NAME=cols.TABLE_NAME
283+
AND cons.CONSTRAINT_NAME=refs.CONSTRAINT_NAME
284+
LEFT JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS cRefs
285+
ON cRefs.CONSTRAINT_SCHEMA=cols.TABLE_SCHEMA
286+
AND cRefs.CONSTRAINT_NAME=refs.CONSTRAINT_NAME
287+
WHERE
288+
cols.TABLE_NAME = '" . $table . "'
289+
AND cols.TABLE_SCHEMA = DATABASE()
290+
AND refs.REFERENCED_TABLE_NAME IS NOT NULL
291+
AND cons.CONSTRAINT_TYPE = 'FOREIGN KEY'
292+
;");
293+
return $res->fetch_all(MYSQLI_ASSOC);
294+
}
295+
247296
function getIndentation($level)
248297
{
249298
return str_repeat(' ', $level);

0 commit comments

Comments
 (0)