Repository navigation
Expand file tree
/
Copy pathenv.py
More file actions
65 lines (52 loc) · 2.1 KB
/
Copy pathenv.py
File metadata and controls
65 lines (52 loc) · 2.1 KB
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
56
57
58
59
60
61
62
63
64
65
import os, json
from dotenv import load_dotenv
from logging.config import fileConfig
from sqlalchemy import create_engine, pool
from sqlalchemy.engine import URL
from alembic import context
load_dotenv()
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# Interpret the config file for Python logging.
if config.config_file_name is not None:
fileConfig(config.config_file_name)
# Host, port and database from the JSON config; credentials from the environment
with open("/etc/pythonrestapi_config.json", "r") as f:
json_config = json.load(f)
db_url = URL.create(
drivername="postgresql+psycopg",
username=os.environ["DB_USERNAME"],
password=os.environ["DB_PASSWORD"], # URL.create escapes special characters itself
host=json_config["DB_HOST"],
port=int(json_config.get("DB_PORT", 5432)),
database=json_config["DB_DATABASE"],
)
# add your model's MetaData object here for 'autogenerate' support.
# The model imports register their tables on Base.metadata - keep them.
from src.models.AuthorModel import AuthorModel
from src.models.BookModel import BookModel
from src.models.UserModel import UserModel
from src.models.base import Base
target_metadata = Base.metadata
def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode (emit SQL without a DB connection)."""
context.configure(
url=db_url.render_as_string(hide_password=False),
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online() -> None:
"""Run migrations in 'online' mode (against a live connection)."""
connectable = create_engine(db_url, poolclass=pool.NullPool)
with connectable.connect() as connection:
context.configure(connection=connection, target_metadata=target_metadata)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()