import sqlalchemy.dialects.mssql.base
from sqlalchemy import create_engine, MetaData, Boolean, DATETIME
from database import conn_string

source_dsn = {'user': 'sa', 'pass': 'Mffu3011#', 'server': 'GC-SERVER1\\GLOBALCUBE', 'database': 'OPTIMA', 'driver': 'mssql'}
source_schema = 'import'

target_dsn = {'user': 'root', 'pass': '', 'server': 'localhost', 'database': 'OPTIMA', 'driver': 'mysql'}
target_schema = ''

source_db = create_engine(conn_string(source_dsn))
source_meta = MetaData()
source_meta.reflect(bind=source_db, schema=source_schema)
# print('\n'.join(source_meta.tables.keys()))
target_db = create_engine(conn_string(target_dsn))
source_meta.bind = target_db

for table in source_meta.sorted_tables:
    table.schema = None

    for col in table.c:
        if getattr(col.type, 'collation', None) is not None:
            col.type.collation = None
        if type(col.type) is sqlalchemy.dialects.mssql.base.BIT:
            col.type = Boolean()
        if type(col.type) is DATETIME:
            # print(table.name + '.' + col.name)
            col.server_default = None
        if type(col.type) is sqlalchemy.dialects.mssql.base.SMALLDATETIME:
            col.type = DATETIME()

source_meta.create_all()