Skip to content

Commit 91aa75a

Browse files
author
Colin Viebrock
committed
Merge branch 'master' of https://github.com/rootrus/sequel-pro-laravel-export into rootrus-master
2 parents 30ffa69 + c55cdf8 commit 91aa75a

File tree

4 files changed

+190
-34
lines changed

4 files changed

+190
-34
lines changed

src/MigrationParser.php

Lines changed: 116 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ class MigrationParser
7676
*/
7777
protected $tableCharsetAndCollationFile;
7878

79+
/**
80+
* @var string
81+
*/
82+
protected $foreignStructureFile;
83+
84+
/**
85+
* @var string
86+
*/
87+
protected $hasForeign;
88+
7989
/**
8090
* MigrationParser constructor.
8191
*
@@ -84,14 +94,18 @@ class MigrationParser
8494
* @param string $keysFile
8595
* @param string $constraintsFile
8696
* @param string $tableCharsetAndCollationFile
97+
* @param string $foreignStructureFile
98+
* @param string $hasForeign
8799
*/
88-
public function __construct($tableName, $structureFile, $keysFile, $constraintsFile, $tableCharsetAndCollationFile)
100+
public function __construct($tableName, $structureFile, $keysFile, $constraintsFile, $tableCharsetAndCollationFile, $foreignStructureFile, $hasForeign)
89101
{
90102
$this->tableName = $tableName;
91103
$this->structureFile = $structureFile;
92104
$this->keysFile = $keysFile;
93105
$this->constraintsFile = $constraintsFile;
94106
$this->tableCharsetAndCollationFile = $tableCharsetAndCollationFile;
107+
$this->foreignStructureFile = $foreignStructureFile;
108+
$this->hasForeign = $hasForeign;
95109
}
96110

97111
public function makeMigration()
@@ -110,40 +124,94 @@ public function makeMigration()
110124
$constraints = trim(implode($eol . $indent12, $this->formatConstraints())) . $eol;
111125
$tableCollationAndCharset = trim(implode($eol . $indent12, $this->formatTableCollationAndCharset())) . $eol;
112126
$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';
131+
$output = file_get_contents(__DIR__ . '/foreign_key.stub');
132+
$output = str_replace(
133+
[
134+
':VERSION:',
135+
'DummyClass',
136+
'DummyTable',
137+
"// foreign\n",
138+
"// foreignDrop\n",
139+
],
140+
[
141+
$this->version,
142+
$className,
143+
$this->tableName,
144+
$foreign,
145+
$foreignDrop,
146+
],
147+
$output
148+
);
149+
} else {
150+
$className = 'Create' . $this->studly($this->tableName) . 'Table';
151+
$output = file_get_contents(__DIR__ . '/create.stub');
152+
$output = str_replace(
153+
[
154+
':VERSION:',
155+
'DummyClass',
156+
'DummyTable',
157+
"// structure\n",
158+
"// keys\n",
159+
"// constraints\n",
160+
"// tableCollationAndCharset\n",
161+
"// extras\n",
162+
],
163+
[
164+
$this->version,
165+
$className,
166+
$this->tableName,
167+
$structure,
168+
$keys,
169+
$constraints,
170+
$tableCollationAndCharset,
171+
$extras,
172+
],
173+
$output
174+
);
175+
}
113176

114-
$output = file_get_contents(__DIR__ . '/create.stub');
115-
116-
$className = 'Create' . $this->studly($this->tableName) . 'Table';
117-
118-
$output = str_replace(
119-
[
120-
':VERSION:',
121-
'DummyClass',
122-
'DummyTable',
123-
"// structure\n",
124-
"// keys\n",
125-
"// constraints\n",
126-
"// tableCollationAndCharset\n",
127-
"// extras\n",
128-
],
129-
[
130-
$this->version,
131-
$className,
132-
$this->tableName,
133-
$structure,
134-
$keys,
135-
$constraints,
136-
$tableCollationAndCharset,
137-
$extras,
138-
],
139-
$output
140-
);
141177

142178
$output = preg_replace("/^(\s*\R){2,}/m", "\n", $output);
143179

144180
return $output;
145181
}
146182

183+
public function formatForeign()
184+
{
185+
$fields = [];
186+
187+
$rows = file($this->foreignStructureFile);
188+
array_shift($rows);
189+
190+
foreach ($rows as $row) {
191+
list($table, $colName, $constName, $refTable, $refColumnName) = explode("\t",
192+
$row, 5);
193+
$fields[] = '$table->unsignedBigInteger(\''. trim($colName) .'\')->change();';
194+
$fields[] = '$table->foreign(\''. trim($colName) .'\')->references(\''. trim($refColumnName) .'\')->on(\''. trim($refTable) .'\');';
195+
}
196+
return array_filter($fields);
197+
}
198+
199+
public function formatForeignDrop()
200+
{
201+
$fields = [];
202+
203+
$rows = file($this->foreignStructureFile);
204+
array_shift($rows);
205+
206+
foreach ($rows as $row) {
207+
list($table, $colName, $constName, $refTable, $refColumnName) = explode("\t",
208+
$row, 5);
209+
//$table->dropForeign('local_table_foreign_id_foreign');
210+
$fields[] = '$table->dropForeign(\''. trim($table) .'_'. trim($colName) .'_foreign\');';
211+
}
212+
return array_filter($fields);
213+
}
214+
147215
protected function studly($value)
148216
{
149217
$value = ucwords(str_replace(['-', '_'], ' ', $value));
@@ -242,6 +310,24 @@ public function buildStructure()
242310
$this->structure['remember_token']['nullable'] = false;
243311
$this->structure['remember_token']['field'] = null;
244312
}
313+
314+
// look for id primary key
315+
316+
if (
317+
array_key_exists('id', $this->structure)
318+
&& $this->structure['id']['method'] === 'integer'
319+
&& $this->structure['id']['autoIncrement'] === true
320+
&& $this->structure['id']['args'] === null
321+
) {
322+
$this->structure['id']['method'] = 'id';
323+
$this->structure['id']['args'] = null;
324+
$this->structure['id']['default'] = null;
325+
$this->structure['id']['nullable'] = false;
326+
$this->structure['id']['field'] = null;
327+
$this->structure['id']['comment'] = null;
328+
$this->structure['id']['autoIncrement'] = null;
329+
$this->structure['id']['unsigned'] = null;
330+
}
245331
}
246332

247333
public function formatStructure()
@@ -359,7 +445,7 @@ public function buildKeys()
359445
$primaryColumn = reset($primary['columns']);
360446
$field = $this->structure[$primaryColumn];
361447
// and that column is an "increments" field ...
362-
if (isset($field['args']['autoIncrement']) && $field['args']['autoIncrement'] === 'true') {
448+
if (isset($field['autoIncrement']) && $field['autoIncrement'] == '1') {
363449
// then don't build the primary key, since Laravel takes care of it
364450
unset($this->keys['PRIMARY']);
365451
}
@@ -375,7 +461,7 @@ public function formatKeys()
375461
$columns = $this->escapeArray($data['columns']);
376462

377463
if ($field === 'PRIMARY') {
378-
$fields[$field] = sprintf('$table->primary(%s);', $columns);
464+
//$fields[$field] = sprintf('$table->primary(%s);', $columns);
379465
} else {
380466
$fields[$field] = sprintf('$table->%s(%s, \'%s\');',
381467
$data['method'],

src/foreign_key.stub

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
/**
8+
* Migration auto-generated by Sequel Pro Laravel Export (:VERSION:)
9+
* @see https://github.com/cviebrock/sequel-pro-laravel-export
10+
*/
11+
class DummyClass extends Migration
12+
{
13+
/**
14+
* Run the migrations.
15+
*
16+
* @return void
17+
*/
18+
public function up()
19+
{
20+
Schema::table('DummyTable', function (Blueprint $table) {
21+
// foreign
22+
});
23+
// extras
24+
}
25+
26+
/**
27+
* Reverse the migrations.
28+
*
29+
* @return void
30+
*/
31+
public function down()
32+
{
33+
Schema::table('DummyTable', function (Blueprint $table) {
34+
// foreignDrop
35+
});
36+
}
37+
}

src/parse.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
<?php
2-
32
require_once(__DIR__ . '/MigrationParser.php');
4-
53
$tableName = $argv[1];
4+
$hasForeign = "false";
5+
if (isset($argv[2])) {
6+
$hasForeign = "true";
7+
}
68

79
$m = new MigrationParser(
810
$tableName,
911
__DIR__ . '/rowsStructure.tsv',
1012
__DIR__ . '/rowsKeys.tsv',
1113
__DIR__ . '/rowsConstraints.tsv',
12-
__DIR__ . '/rowsTableCharsetAndCollation.tsv'
14+
__DIR__ . '/rowsTableCharsetAndCollation.tsv',
15+
__DIR__ . '/rowsForeignStructure.tsv',
16+
$hasForeign
1317
);
1418

1519
echo $m->makeMigration();

src/parse.sh

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ do
117117
# get character set and collation name
118118
echo "
119119
SELECT
120-
CHARACTER_SET_NAME, TABLE_COLLATION
120+
CHARACTER_SET_NAME, TABLE_COLLATION
121121
FROM
122122
information_schema.TABLES, information_schema.COLLATION_CHARACTER_SET_APPLICABILITY
123123
WHERE
@@ -179,6 +179,7 @@ do
179179
echo -n "" > "$SP_BUNDLE_PATH/rowsConstraints.tsv"
180180
fi
181181

182+
182183
# process the results and save to the desktop
183184
FILENAME=$(date "+%Y_%m_%d_%H%M%S_create_${table}_table.php")
184185
/usr/bin/php "$SP_BUNDLE_PATH/parse.php" "${table}" > $DESTDIR/$FILENAME
@@ -189,5 +190,33 @@ do
189190
# end loop through tables
190191
done
191192

193+
# Loop through tables
194+
for table in "${tables[@]}"
195+
do
196+
# get the table foreign key
197+
198+
echo "SELECT
199+
TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME
200+
FROM
201+
INFORMATION_SCHEMA.KEY_COLUMN_USAGE
202+
WHERE
203+
REFERENCED_TABLE_SCHEMA = '${SP_SELECTED_DATABASE}' AND TABLE_NAME = '${table}';" > "$SP_QUERY_FILE"
204+
205+
# execute and save the table structure result
206+
execute_sql
207+
cp $SP_QUERY_RESULT_FILE "$SP_BUNDLE_PATH/rowsForeignStructure.tsv"
208+
209+
hasForeignKey=`cat "$SP_BUNDLE_PATH/rowsForeignStructure.tsv" | grep -v "TABLE_NAME" | wc -l | awk '{print $1}'`;
210+
if [ $hasForeignKey != 0 ]; then
211+
FILENAME=$(date "+%Y_%m_%d_%H%M%S-0_add_fk_to_${table}_table.php")
212+
/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>"
214+
# clean up
215+
clear_temp
216+
fi
217+
clear_temp
218+
# end loop through tables
219+
done
220+
192221
echo "<button onclick=\"window.system.closeHTMLOutputWindow()\">Close</button>"
193222
exit $SP_BUNDLE_EXIT_SHOW_AS_HTML

0 commit comments

Comments
 (0)