2014-09-01 17:01:13 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2014-09-02 16:31:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-09-01 17:01:13 +02:00
|
|
|
def import_sqlite(project_id, sqlite_file):
|
2014-09-02 16:31:02 +02:00
|
|
|
print "This is import_sqlite speaking!"
|
|
|
|
print project_id
|
|
|
|
print sqlite_file
|
2014-09-03 15:24:48 +02:00
|
|
|
print "Importing data..."
|
2014-09-02 16:31:02 +02:00
|
|
|
|
|
|
|
from sqlalchemy import *
|
|
|
|
from sqlalchemy.schema import CreateSchema
|
2014-09-03 15:24:48 +02:00
|
|
|
from sqlalchemy.ext.automap import automap_base
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
|
|
|
|
# Initialize sqlite part
|
|
|
|
sqlite_engine = create_engine('sqlite+pysqlite:///project_import_9.ctt4', echo=True)
|
|
|
|
sqlite_session = Session(sqlite_engine)
|
|
|
|
sqlite_meta = MetaData(bind=sqlite_engine)
|
|
|
|
sqlite_meta.reflect()
|
|
|
|
|
|
|
|
# Initialize postresql part
|
|
|
|
psql_engine = create_engine("postgresql://citavi_mapper:foobar2000@localhost:5432/citavi_mapper")
|
|
|
|
psql_session = Session(psql_engine)
|
|
|
|
psql_engine.execute(CreateSchema('alchemytest')) # TODO: Catch Exception/Warning/Whatever here
|
|
|
|
psql_meta = MetaData(bind=psql_engine, schema='alchemytest')
|
|
|
|
|
|
|
|
"""
|
|
|
|
# Reflect the origin - maybe psql_meta.reflect() is enough?
|
|
|
|
sqlite_autobase = automap_base()
|
|
|
|
sqlite_autobase.prepare(sqlite_engine, reflect=True)
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Now that sh- super stuff is reflected and i can go on, using the Table instances.
|
|
|
|
for table in sqlite_meta.sorted_tables:
|
|
|
|
table_name = str(table)
|
|
|
|
columns = table.columns.items()
|
|
|
|
for column_tuple in columns:
|
|
|
|
column_name = column_tuple[0]
|
|
|
|
actual_sqlalchemy_column = column_tuple[1]
|
|
|
|
column_type = str(vars(actual_sqlalchemy_column)['type'])
|
|
|
|
print table_name + "." + column_name + ': ' + column_type
|
|
|
|
print
|
|
|
|
print
|
|
|
|
|
|
|
|
# Shove it up into postgresql's ... you know.
|
|
|
|
psql_meta.create_all(psql_engine)
|
|
|
|
|
|
|
|
#sqlite_meta.reflect()
|
|
|
|
|
2014-09-02 16:31:02 +02:00
|
|
|
#meta.tables['Reference']
|
|
|
|
#meta.tables['ReferenceAuthor'].select().execute().fetchall()
|
|
|
|
|
|
|
|
# Now we need to "convert" unsupported types :-/
|
2014-09-03 15:24:48 +02:00
|
|
|
"""
|
2014-09-02 16:31:02 +02:00
|
|
|
for table in meta.sorted_tables:
|
|
|
|
table_name = str(table)
|
|
|
|
columns = table.columns.items()
|
|
|
|
for column_tuple in columns:
|
|
|
|
column_name = column_tuple[0]
|
|
|
|
actual_sqlalchemy_column = column_tuple[1]
|
|
|
|
column_type = str(vars(actual_sqlalchemy_column)['type'])
|
|
|
|
print table_name + "." + column_name + ': ' + column_type
|
|
|
|
print
|
|
|
|
print
|
2014-09-03 15:24:48 +02:00
|
|
|
"""
|
2014-09-02 16:31:02 +02:00
|
|
|
|
|
|
|
""" TODO: since DATETIME (sqlite3) does not map onto TIMESTAMP / DATE (postgresql), some mapping/migrating is required! """
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-09-01 17:01:13 +02:00
|
|
|
pass
|