Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
- Added testing Model
- Fix for beckenrode#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 committed Nov 20, 2016
1 parent d89e009 commit f33d209
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# Created by .ignore support plugin (hsz.mobi)
!.gitignore
.idea/
*.bak
30 changes: 24 additions & 6 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 All @@ -316,7 +334,7 @@ def export_schema(table_schema, tree):
first_foreign_created = False

for key in tbl.foreignKeys:
if key.name != '':
if key.name != '' and hasattr(key.index, 'name'):
index_name = key.index.name
foreign_key = key.columns[0].name

Expand Down
Binary file added test/Test.mwb
Binary file not shown.

0 comments on commit f33d209

Please sign in to comment.