Skip to content

Commit 135169d

Browse files
author
Colin Viebrock
committed
clean up #37
1 parent 7faea1f commit 135169d

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

src/MigrationParser.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,19 @@ public function makeMigration()
124124
$constraints = trim(implode($eol . $indent12, $this->formatConstraints())) . $eol;
125125
$tableCollationAndCharset = trim(implode($eol . $indent12, $this->formatTableCollationAndCharset())) . $eol;
126126
$extras = trim(implode($eol . $indent8, $this->formatExtras())) . $eol;
127-
$foreign = trim(implode($eol . $indent12, $this->formatForeign())) . $eol;
128-
$foreignDrop = trim(implode($eol . $indent12, $this->formatForeignDrop())) . $eol;
129-
if ($this->hasForeign == "true") {
130-
$className = 'AddFkTo' . $this->studly($this->tableName) . 'Table';
127+
128+
if ($this->hasForeign === "true") {
129+
$foreign = trim(implode($eol . $indent12, $this->formatForeign())) . $eol;
130+
$foreignDrop = trim(implode($eol . $indent12, $this->formatForeignDrop())) . $eol;
131+
$className = 'AddForeignKeyTo' . $this->studly($this->tableName) . 'Table';
131132
$output = file_get_contents(__DIR__ . '/foreign_key.stub');
132133
$output = str_replace(
133134
[
134135
':VERSION:',
135136
'DummyClass',
136137
'DummyTable',
137138
"// foreign\n",
138-
"// foreignDrop\n",
139+
"// foreignDrop\n",
139140
],
140141
[
141142
$this->version,
@@ -190,7 +191,6 @@ public function formatForeign()
190191
foreach ($rows as $row) {
191192
list($table, $colName, $constName, $refTable, $refColumnName) = explode("\t",
192193
$row, 5);
193-
$fields[] = '$table->unsignedBigInteger(\''. trim($colName) .'\')->change();';
194194
$fields[] = '$table->foreign(\''. trim($colName) .'\')->references(\''. trim($refColumnName) .'\')->on(\''. trim($refTable) .'\');';
195195
}
196196
return array_filter($fields);
@@ -206,7 +206,6 @@ public function formatForeignDrop()
206206
foreach ($rows as $row) {
207207
list($table, $colName, $constName, $refTable, $refColumnName) = explode("\t",
208208
$row, 5);
209-
//$table->dropForeign('local_table_foreign_id_foreign');
210209
$fields[] = '$table->dropForeign(\''. trim($table) .'_'. trim($colName) .'_foreign\');';
211210
}
212211
return array_filter($fields);
@@ -312,7 +311,6 @@ public function buildStructure()
312311
}
313312

314313
// look for id primary key
315-
316314
if (
317315
array_key_exists('id', $this->structure)
318316
&& $this->structure['id']['method'] === 'integer'
@@ -395,7 +393,7 @@ public function formatStructure()
395393
}
396394

397395
// If isn't empty, set the comment
398-
if ($data['comment'] !== '') {
396+
if ($data['comment'] !== '' || $data['comment'] !== null) {
399397
$temp .= '->comment(\'' . addslashes($data['comment']) . '\')';
400398
}
401399

@@ -445,7 +443,7 @@ public function buildKeys()
445443
$primaryColumn = reset($primary['columns']);
446444
$field = $this->structure[$primaryColumn];
447445
// and that column is an "increments" field ...
448-
if (isset($field['autoIncrement']) && $field['autoIncrement'] == '1') {
446+
if (isset($field['args']['autoIncrement']) && $field['args']['autoIncrement'] === 'true') {
449447
// then don't build the primary key, since Laravel takes care of it
450448
unset($this->keys['PRIMARY']);
451449
}

src/parse.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<?php
2+
23
require_once(__DIR__ . '/MigrationParser.php');
34
$tableName = $argv[1];
45
$hasForeign = "false";
6+
57
if (isset($argv[2])) {
6-
$hasForeign = "true";
8+
$hasForeign = "true";
79
}
810

911
$m = new MigrationParser(

src/parse.sh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,14 @@ do
190190
# end loop through tables
191191
done
192192

193+
# ensure timestamps for any foreign key migrations occur after creation migrations
194+
sleep 1
195+
193196
# Loop through tables
194197
for table in "${tables[@]}"
195198
do
196199
# get the table foreign key
197-
198-
echo "SELECT
200+
echo "SELECT
199201
TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME
200202
FROM
201203
INFORMATION_SCHEMA.KEY_COLUMN_USAGE
@@ -208,9 +210,9 @@ do
208210

209211
hasForeignKey=`cat "$SP_BUNDLE_PATH/rowsForeignStructure.tsv" | grep -v "TABLE_NAME" | wc -l | awk '{print $1}'`;
210212
if [ $hasForeignKey != 0 ]; then
211-
FILENAME=$(date "+%Y_%m_%d_%H%M%S-0_add_fk_to_${table}_table.php")
213+
FILENAME=$(date "+%Y_%m_%d_%H%M%S_add_foreign_key_to_${table}_table.php")
212214
/usr/bin/php "$SP_BUNDLE_PATH/parse.php" "${table}" "foreignkey" > $DESTDIR/$FILENAME
213-
echo "<p>Migration Foreing Key saved: <a href=\"SP-REVEAL-FILE://$DESTDIR/$FILENAME\">$FILENAME</a></p>"
215+
echo "<p>Migration for foreign key saved: <a href=\"SP-REVEAL-FILE://$DESTDIR/$FILENAME\">$FILENAME</a></p>"
214216
# clean up
215217
clear_temp
216218
fi

0 commit comments

Comments
 (0)