-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move new database creation into own script
- Loading branch information
Showing
5 changed files
with
56 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import sys | ||
from pathlib import Path | ||
|
||
import sys_vars | ||
from flask import Flask | ||
|
||
# We have to add the app path to the path to get the db schema | ||
sys.path.insert(0, Path(__file__).parent.parent.as_posix()) | ||
|
||
from src.core.database.schema import db | ||
|
||
|
||
def create_app() -> Flask: | ||
"""Dummy Flask instance used for database management.""" | ||
app = Flask(__name__) | ||
app.config["SECRET_KEY"] = "secret tunnel" | ||
|
||
db_path = sys_vars.get_path("DB_PATH").resolve() | ||
with app.app_context(): | ||
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False | ||
app.config["SQLALCHEMY_DATABASE_URI"] = f"sqlite:///{db_path.as_posix()}" | ||
db.init_app(app) | ||
return app |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import sys | ||
from pathlib import Path | ||
|
||
from alembic import command | ||
from alembic.config import Config | ||
from sqlalchemy import inspect | ||
|
||
# We have to add the app path to the path to get the db | ||
sys.path.insert(0, Path(__file__).parent.parent.as_posix()) | ||
|
||
|
||
from db.dummy_db import create_app | ||
from src.core.database.schema import db | ||
|
||
|
||
def create_database() -> None: | ||
"""Create a brand new copy of the database.""" | ||
app = create_app() | ||
with app.app_context(): | ||
# Create the database tables if needed | ||
if not bool(inspect(db.engine).get_table_names()): | ||
print("Creating new database...") # noqa: T201 | ||
db.create_all() | ||
|
||
# Tell Alembic this is a new database and | ||
# we don't need to update it to a newer schema | ||
alembic_cfg = Config("alembic.ini") | ||
command.stamp(alembic_cfg, "head") | ||
|
||
|
||
if __name__ == "__main__": | ||
create_database() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters