Skip to content

Commit 6b10f1f

Browse files
committed
Update export-laravel-5-migrations.py
1 parent a490f1f commit 6b10f1f

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

export-laravel-5-migrations.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ def export_schema(out, schema, is_main_schema):
2929
if len(schema.tables) == 0:
3030
return
3131

32+
foreign_keys = {};
33+
migration_tables = [];
34+
3235
for tbl in schema.tables:
36+
migration_tables.append(tbl.name)
3337
out.write('<?php\n')
3438
out.write('\n')
3539
out.write('use Illuminate\Database\Schema\Blueprint;\n')
@@ -101,7 +105,38 @@ def export_schema(out, schema, is_main_schema):
101105
out.write(";")
102106
out.write('\n')
103107

108+
first_foreign_created = 0
109+
for fkey in tbl.foreignKeys:
110+
if fkey.name != '':
111+
if fkey.referencedColumns[0].owner.name in migration_tables:
112+
if first_foreign_created == 0:
113+
out.write('\n')
114+
first_foreign_created = 1
115+
out.write(" $table->foreign('" + fkey.columns[0].name + "')->references('" + fkey.referencedColumns[0].name + "')->on('" + fkey.referencedColumns[0].owner.name + "');")
116+
out.write('\n')
117+
else:
118+
if fkey.referencedColumns[0].owner.name not in foreign_keys:
119+
foreign_keys[fkey.referencedColumns[0].owner.name] = []
120+
foreign_keys[fkey.referencedColumns[0].owner.name].append({'table':fkey.columns[0].owner.name, 'name':fkey.columns[0].name, 'referenced_table':fkey.referencedColumns[0].owner.name, 'referenced_name':fkey.referencedColumns[0].name})
121+
104122
out.write(" });\n")
123+
124+
for fkey, fval in foreign_keys.iteritems():
125+
if fkey == tbl.name:
126+
keyed_tables = []
127+
schema_table = 0
128+
for item in fval:
129+
if item['table'] not in keyed_tables:
130+
keyed_tables.append(item['table'])
131+
if schema_table == 0:
132+
out.write('\n')
133+
out.write(" Schema::table('" + item['table'] + "', function (Blueprint $table) {\n")
134+
schema_table = 1
135+
out.write(" $table->foreign('" + item['name'] + "')->references('" + item['referenced_name'] + "')->on('" + item['referenced_table'] + "');\n")
136+
if schema_table == 1:
137+
out.write(" });\n")
138+
out.write('\n')
139+
105140
out.write(' }\n')
106141
out.write('\n')
107142
out.write(' /**\n')
@@ -111,6 +146,34 @@ def export_schema(out, schema, is_main_schema):
111146
out.write(' */\n')
112147
out.write(' public function down()\n')
113148
out.write(' {\n')
149+
150+
first_foreign_created = 0
151+
for fkey in tbl.foreignKeys:
152+
if fkey.name != '':
153+
if fkey.referencedColumns[0].owner.name in migration_tables:
154+
if first_foreign_created == 0:
155+
out.write('\n')
156+
first_foreign_created = 1
157+
out.write(" $table->dropForeign(['" + fkey.columns[0].name + "']);\n")
158+
if first_foreign_created == 1:
159+
out.write('\n')
160+
161+
for fkey, fval in foreign_keys.iteritems():
162+
if fkey == tbl.name:
163+
keyed_tables = []
164+
schema_table = 0
165+
for item in fval:
166+
if item['table'] not in keyed_tables:
167+
keyed_tables.append(item['table'])
168+
if schema_table == 0:
169+
out.write('\n')
170+
out.write(" Schema::table('" + item['table'] + "', function (Blueprint $table) {\n")
171+
schema_table = 1
172+
out.write(" $table->dropForeign(['" + item['name'] + "']);\n")
173+
if schema_table == 1:
174+
out.write(" });\n")
175+
out.write('\n')
176+
114177
out.write(" Schema::drop('" + tbl.name + "');\n")
115178
out.write(' }\n')
116179
out.write('}\n\n\n')

0 commit comments

Comments
 (0)