Skip to content

Commit d5e5301

Browse files
committed
Added db connectivity & alembic
1 parent a3dab09 commit d5e5301

File tree

21 files changed

+530
-9
lines changed

21 files changed

+530
-9
lines changed

.idea/dataSources.xml

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/mrmat-python-api-fastapi.iml

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/runConfigurations/db_revision.xml

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/runConfigurations/db_upgrade__head_.xml

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/runConfigurations/run.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

migrations/README

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Generic single-database configuration.

migrations/alembic.ini

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# A generic, single database configuration.
2+
3+
[alembic]
4+
# template used to generate migration files
5+
# file_template = %%(rev)s_%%(slug)s
6+
7+
# set to 'true' to run the environment during
8+
# the 'revision' command, regardless of autogenerate
9+
# revision_environment = false
10+
11+
script_location = migrations
12+
prepend_sys_path = .
13+
14+
# Logging configuration
15+
[loggers]
16+
keys = root,sqlalchemy,alembic
17+
18+
[handlers]
19+
keys = console
20+
21+
[formatters]
22+
keys = generic
23+
24+
[logger_root]
25+
level = WARN
26+
handlers = console
27+
qualname =
28+
29+
[logger_sqlalchemy]
30+
level = WARN
31+
handlers =
32+
qualname = sqlalchemy.engine
33+
34+
[logger_alembic]
35+
level = INFO
36+
handlers =
37+
qualname = alembic
38+
39+
[handler_console]
40+
class = StreamHandler
41+
args = (sys.stderr,)
42+
level = NOTSET
43+
formatter = generic
44+
45+
[formatter_generic]
46+
format = %(levelname)-5.5s [%(name)s] %(message)s
47+
datefmt = %H:%M:%S

migrations/env.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# MIT License
2+
#
3+
# Copyright (c) 2021 MrMat
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in
13+
# all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
from __future__ import with_statement
24+
25+
import logging
26+
from logging.config import fileConfig
27+
from alembic import context
28+
from sqlalchemy import engine_from_config, pool
29+
30+
config = context.config
31+
fileConfig(config.config_file_name)
32+
logger = logging.getLogger('alembic.env')
33+
34+
from mrmat_python_api_fastapi import Base, app_config
35+
target_metadata = Base.metadata
36+
config.set_main_option('sqlalchemy.url', app_config.db_url)
37+
38+
39+
def run_migrations_offline():
40+
url = config.get_main_option("sqlalchemy.url")
41+
context.configure(
42+
url=url, target_metadata=target_metadata, literal_binds=True
43+
)
44+
with context.begin_transaction():
45+
context.run_migrations()
46+
47+
48+
def run_migrations_online():
49+
configuration = config.get_section(config.config_ini_section)
50+
configuration['sqlalchemy.url'] = app_config.db_url
51+
connectable = engine_from_config(configuration, prefix='sqlalchemy.', poolclass=pool.NullPool)
52+
with connectable.connect() as connection:
53+
context.configure(
54+
connection=connection, target_metadata=target_metadata, compare_type=True
55+
)
56+
with context.begin_transaction():
57+
context.run_migrations()
58+
59+
# db revision -m newest --autogenerate
60+
61+
62+
if context.is_offline_mode():
63+
run_migrations_offline()
64+
else:
65+
run_migrations_online()

migrations/script.py.mako

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""${message}
2+
3+
Revision ID: ${up_revision}
4+
Revises: ${down_revision | comma,n}
5+
Create Date: ${create_date}
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
${imports if imports else ""}
11+
12+
# revision identifiers, used by Alembic.
13+
revision = ${repr(up_revision)}
14+
down_revision = ${repr(down_revision)}
15+
branch_labels = ${repr(branch_labels)}
16+
depends_on = ${repr(depends_on)}
17+
18+
19+
def upgrade():
20+
${upgrades if upgrades else "pass"}
21+
22+
23+
def downgrade():
24+
${downgrades if downgrades else "pass"}

0 commit comments

Comments
 (0)