forked from perara/wg-manager
-
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.
- Loading branch information
Showing
34 changed files
with
463 additions
and
365 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# A generic, single database configuration. | ||
|
||
[alembic] | ||
# path to migration scripts | ||
script_location = migrations | ||
|
||
# template used to generate migration files | ||
# file_template = %%(rev)s_%%(slug)s | ||
|
||
# sys.path path, will be prepended to sys.path if present. | ||
# defaults to the current working directory. | ||
prepend_sys_path = . | ||
|
||
# timezone to use when rendering the date | ||
# within the migration file as well as the filename. | ||
# string value is passed to dateutil.tz.gettz() | ||
# leave blank for localtime | ||
# timezone = | ||
|
||
# max length of characters to apply to the | ||
# "slug" field | ||
# truncate_slug_length = 40 | ||
|
||
# set to 'true' to run the environment during | ||
# the 'revision' command, regardless of autogenerate | ||
# revision_environment = false | ||
|
||
# set to 'true' to allow .pyc and .pyo files without | ||
# a source .py file to be detected as revisions in the | ||
# versions/ directory | ||
# sourceless = false | ||
|
||
# version location specification; this defaults | ||
# to alembic/versions. When using multiple version | ||
# directories, initial revisions must be specified with --version-path | ||
# version_locations = %(here)s/bar %(here)s/bat alembic/versions | ||
|
||
# the output encoding used when revision files | ||
# are written from script.py.mako | ||
# output_encoding = utf-8 | ||
|
||
sqlalchemy.url = sqlite:///database.db | ||
|
||
|
||
[post_write_hooks] | ||
# post_write_hooks defines scripts or Python functions that are run | ||
# on newly generated revision scripts. See the documentation for further | ||
# detail and examples | ||
|
||
# format using "black" - use the console_scripts runner, against the "black" entrypoint | ||
# hooks=black | ||
# black.type=console_scripts | ||
# black.entrypoint=black | ||
# black.options=-l 79 | ||
|
||
# Logging configuration | ||
[loggers] | ||
keys = root,sqlalchemy,alembic | ||
|
||
[handlers] | ||
keys = console | ||
|
||
[formatters] | ||
keys = generic | ||
|
||
[logger_root] | ||
level = WARN | ||
handlers = console | ||
qualname = | ||
|
||
[logger_sqlalchemy] | ||
level = WARN | ||
handlers = | ||
qualname = sqlalchemy.engine | ||
|
||
[logger_alembic] | ||
level = INFO | ||
handlers = | ||
qualname = alembic | ||
|
||
[handler_console] | ||
class = StreamHandler | ||
args = (sys.stderr,) | ||
level = NOTSET | ||
formatter = generic | ||
|
||
[formatter_generic] | ||
format = %(levelname)-5.5s [%(name)s] %(message)s | ||
datefmt = %H:%M:%S |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
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,21 @@ | ||
import sqlalchemy | ||
from sqlalchemy import MetaData | ||
from sqlalchemy.ext.declarative import declarative_base | ||
from sqlalchemy.orm import sessionmaker | ||
import const | ||
|
||
engine = sqlalchemy.create_engine( | ||
const.DATABASE_URL, connect_args={"check_same_thread": False} | ||
) | ||
|
||
|
||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) | ||
|
||
meta = MetaData(naming_convention={ | ||
"ix": "ix_%(column_0_label)s", | ||
"uq": "uq_%(table_name)s_%(column_0_name)s", | ||
"ck": "ck_%(table_name)s_%(column_0_name)s", | ||
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s", | ||
"pk": "pk_%(table_name)s" | ||
}) | ||
Base = declarative_base(metadata=meta) |
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,59 @@ | ||
import os | ||
|
||
import alembic.command | ||
from alembic.config import Config | ||
from sqlalchemy.orm import Session | ||
from sqlalchemy_utils import database_exists | ||
|
||
import middleware | ||
from database.database import engine, Base, SessionLocal | ||
from database import models | ||
from loguru import logger | ||
|
||
|
||
def perform_migrations(): | ||
logger.info("Performing migrations...") | ||
alembic_cfg = Config("alembic.ini") | ||
|
||
alembic_cfg.set_main_option('script_location', "migrations") | ||
alembic_cfg.set_main_option('sqlalchemy.url', str(engine.url)) | ||
alembic.command.upgrade(alembic_cfg, 'head') | ||
logger.info("Migrations done!") | ||
|
||
|
||
def setup_initial_database(): | ||
if not database_exists(engine.url): | ||
logger.info("Database does not exists. Creating initial database...") | ||
# Create database from metadata | ||
Base.metadata.create_all(engine) | ||
logger.info("Database creation done!") | ||
|
||
# Create default user | ||
_db: Session = SessionLocal() | ||
|
||
admin_exists = ( | ||
_db.query(models.User.id) | ||
.filter_by(role="admin") | ||
.first() | ||
) is not None | ||
|
||
if not admin_exists: | ||
logger.info("Admin user does not exists. Creating with env variables ADMIN_USERNAME, ADMIN_PASSWORD") | ||
ADMIN_USERNAME = os.getenv("ADMIN_USERNAME") | ||
ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD") | ||
|
||
if not ADMIN_USERNAME: | ||
raise RuntimeError("Database does not exist and the environment variable ADMIN_USERNAME is set") | ||
if not ADMIN_PASSWORD: | ||
raise RuntimeError("Database does not exist and the environment variable ADMIN_PASSWORD is set") | ||
|
||
_db.merge(models.User( | ||
username=ADMIN_USERNAME, | ||
password=middleware.get_password_hash(ADMIN_PASSWORD), | ||
full_name="Admin", | ||
role="admin", | ||
email="" | ||
)) | ||
|
||
_db.commit() | ||
_db.close() |
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
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
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 |
---|---|---|
@@ -1,4 +1 @@ | ||
This is a database migration repository. | ||
|
||
More information at | ||
http://code.google.com/p/sqlalchemy-migrate/ | ||
Generic single-database configuration. |
Oops, something went wrong.