Skip to content

Commit

Permalink
Fixes and Updates (#20)
Browse files Browse the repository at this point in the history
* Updates

- Added testing Model
- Fix for #18
- Fixed issues with 'CURRENT_TIMESTAMP', 'NULL ON UPDATE CURRENT_TIMESTAMP',  'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'
- Check for non existing column types
- Fixed index keys, primary key is no longer included
- Added .gitignore
  • Loading branch information
VeeeneX authored Nov 20, 2016
1 parent f0929ab commit 7829358
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
!.gitignore
.idea/
*.bak
28 changes: 23 additions & 5 deletions export-laravel-5-migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ class Create{tableNameCamelCase}Table extends Migration
)
@ModuleInfo.export(grt.INT, grt.classes.db_Catalog)
def generate_laravel5_migration(cat):

def create_tree(table_schema):
tree = {}
for tbl in sorted(table_schema.tables, key=lambda table: table.name):
Expand Down Expand Up @@ -225,7 +224,16 @@ def export_schema(table_schema, tree):

primary_key = [col for col in tbl.indices if col.isPrimary == 1]
primary_key = primary_key[0] if len(primary_key) > 0 else None
primary_col = primary_key.columns[0].referencedColumn

if hasattr(primary_key, 'columns'):
primary_col = primary_key.columns[0].referencedColumn
else:
primary_col = None

default_time_values = ['CURRENT_TIMESTAMP',
'NULL ON UPDATE CURRENT_TIMESTAMP',
'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'
]

for col in tbl.columns:
if (col.name == 'created_at' or col.name == 'updated_at') and (
Expand All @@ -250,6 +258,11 @@ def export_schema(table_schema, tree):
col_type = "INCREMENTS"

col_data = '\''

# Continue if type is not in dictionary
if col_type not in typesDict:
continue

if typesDict[col_type] == 'char':
if col.length > -1:
col_data = '\', %s' % (str(col.length))
Expand Down Expand Up @@ -278,9 +291,14 @@ def export_schema(table_schema, tree):
migrations[ti].append('->nullable()')

if col.defaultValue != '' and col.defaultValueIsNull != 0:
migrations[ti].append('->default(NULL)')
migrations[ti].append('->default(null)')
elif col.defaultValue != '':
migrations[ti].append('->default(%s)' % col.defaultValue)
default_value = col.defaultValue.replace("'", "")

if default_value in default_time_values:
migrations[ti].append("->default(DB::raw('{}'))".format(default_value))
else:
migrations[ti].append("->default('{}')".format(default_value))

if col.comment != '':
migrations[ti].append("->comment('{comment}')".format(comment=col.comment))
Expand All @@ -294,7 +312,7 @@ def export_schema(table_schema, tree):
index_type = index.owner.indexType.lower()
key = index.referencedColumn.name

if col != primary_col and index_type != "primary":
if (col != primary_col and index_type != "primary") and index_type != "index":
indexes[index_type].append(key)

for index_type in indexes:
Expand Down
Binary file added test/Test.mwb
Binary file not shown.

0 comments on commit 7829358

Please sign in to comment.