-
Notifications
You must be signed in to change notification settings - Fork 22
/
database.py
55 lines (40 loc) · 1.75 KB
/
database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import os
from ooi_data.postgres.model import MetadataBase
from ooi_data.postgres.model.preload import preload_tables
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session
from sqlalchemy.orm import sessionmaker
here = os.path.abspath(os.path.dirname(__file__))
PRELOAD_DATABASE_SCRIPT_FILE_PATH = os.path.join(here, "preload_database.sql")
def create_in_memory_engine():
engine = create_engine('sqlite://')
engine.connect()
script = get_preload_database_script_as_string()
if script:
sqlite_connection = engine.raw_connection().connection
sqlite_connection.executescript(script)
sqlite_connection.commit()
else:
MetadataBase.metadata.create_all(bind=engine, tables=preload_tables)
return engine
def create_scoped_session(engine):
return scoped_session(sessionmaker(autocommit=False, autoflush=False, bind=engine))
def create_engine_from_url(url):
if url is None:
return create_in_memory_engine()
return create_engine(url)
def generate_script_from_preload_database(connection):
delete_preload_database_script()
# Dump the SQLite database to a script
with open(PRELOAD_DATABASE_SCRIPT_FILE_PATH, "w") as sqlFile:
for line in connection.iterdump():
sqlFile.write((line + '\n').encode('utf8'))
def delete_preload_database_script():
# Delete the preload database script if it exists
if os.path.isfile(PRELOAD_DATABASE_SCRIPT_FILE_PATH):
os.remove(PRELOAD_DATABASE_SCRIPT_FILE_PATH)
def get_preload_database_script_as_string():
# Read the entire SQL script into a string
if os.path.exists(PRELOAD_DATABASE_SCRIPT_FILE_PATH):
with open(PRELOAD_DATABASE_SCRIPT_FILE_PATH, "r") as sqlFile:
return sqlFile.read()