Skip to content

Commit

Permalink
Bugfix: Overwrite database by default
Browse files Browse the repository at this point in the history
  • Loading branch information
stroitzsch committed Mar 4, 2019
1 parent caf2c8b commit 903fc9a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
5 changes: 3 additions & 2 deletions cobmo/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@


def connect_database(
data_path=os.path.join(os.path.dirname(os.path.normpath(__file__)), '..', 'data')
data_path=os.path.join(os.path.dirname(os.path.normpath(__file__)), '..', 'data'),
overwrite_database=True
):
# Create database, if none
if not os.path.isfile(os.path.join(data_path, 'data.sqlite')):
if overwrite_database or not os.path.isfile(os.path.join(data_path, 'data.sqlite')):
cobmo.utils.create_database(
sqlite_path=os.path.join(data_path, 'data.sqlite'),
sql_path=os.path.join(data_path, 'data.sqlite.schema.sql'),
Expand Down
16 changes: 11 additions & 5 deletions cobmo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ def create_database(
"""
Create SQLITE database from SQL (schema) file and CSV files
"""
# Remove old SQLITE database file, if any
if os.path.isfile(sqlite_path):
os.remove(sqlite_path)

# Create SQLITE database (schema) from SQL file
# Connect SQLITE database (creates file, if none)
conn = sqlite3.connect(sqlite_path)
cursor = conn.cursor()

# Remove old data, if any
cursor.executescript("""
PRAGMA writable_schema = 1;
DELETE FROM sqlite_master WHERE type IN ('table', 'index', 'trigger');
PRAGMA writable_schema = 0;
VACUUM;
""")

# Recreate SQLITE database (schema) from SQL file
cursor.executescript(open(sql_path, 'r').read())
conn.commit()

Expand Down

0 comments on commit 903fc9a

Please sign in to comment.