Skip to content

Commit

Permalink
Feature framework (#1)
Browse files Browse the repository at this point in the history
* added skeleton

* formed docker

* alembic

* tables

* tables

* added cli
  • Loading branch information
andribas404 authored Aug 17, 2021
1 parent 62b04c8 commit 02dd097
Show file tree
Hide file tree
Showing 30 changed files with 812 additions and 31 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@

.env
.coverage
docker-compose.override.yml
/notebooks
/.run
89 changes: 89 additions & 0 deletions alembic.ini
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 = tom_calculator/alembic

# 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 = driver://user:pass@localhost/dbname


[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 REVISION_SCRIPT_FILENAME

# 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
Empty file added config-test.yml
Empty file.
3 changes: 3 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
db:
dsn: postgresql+psycopg2://tom:jae2Ahfe@postgres:5432/tomdb
async_dsn: postgresql+asyncpg://tom:jae2Ahfe@postgres:5432/tomdb
6 changes: 6 additions & 0 deletions data/discounts.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
amount,rate
1000,3
5000,5
7000,7
10000,10
50000,15
6 changes: 6 additions & 0 deletions data/taxes.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
state_name,rate
UT,6.85
NV,8
TX,6.25
AL,4
CA,8.25
31 changes: 7 additions & 24 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
version: "3.7"

# logging option
x-logging:
&logging
driver: "syslog"
# driver: "json-file"

services:
app:
image: tom-calculator/app:latest
build:
context: .
dockerfile: ./docker/web/Dockerfile
volumes:
- .:/app
working_dir: /app
env_file:
- .env
command: python run
logging:
<< : *logging
options:
tag: tom-calculator__app
# TODO: move to Dockerfile
environment:
TOM_CONFIG: "/app/config.yml"
TOM_DATA: "/app/data"
command: python tom_calculator/main.py
restart: always
ports:
- "18000:80"
Expand All @@ -31,16 +20,10 @@ services:
build:
context: ./docker/postgres
dockerfile: Dockerfile
environment:
POSTGRES_USER: tom
POSTGRES_PASSWORD: password
POSTGRES_DB: tom
env_file:
- .env
volumes:
- tom-calculator__postgres:/var/lib/postgresql/data
logging:
<< : *logging
options:
tag: tom-calculator__postgres
restart: always
ports:
- "18001:5432"
Expand Down
10 changes: 9 additions & 1 deletion docker/postgres/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
FROM postgres:13.1
COPY test.sql /docker-entrypoint-initdb.d
COPY initdb.d /docker-entrypoint-initdb.d

RUN echo "ru_RU.UTF-8 UTF-8" > /etc/locale.gen && \
echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen && \
locale-gen

ENV LANG=en_US.UTF-8 \
LANGUAGE=en_US:en \
LC_ALL=en_US.UTF-8
4 changes: 4 additions & 0 deletions docker/postgres/initdb.d/01_extensions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash
psql --dbname template1 --username "postgres" <<-EOSQL
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
EOSQL
7 changes: 7 additions & 0 deletions docker/postgres/initdb.d/02_create.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash
psql --dbname postgres --username "postgres" <<-EOSQL
CREATE USER ${TOM_USER} WITH PASSWORD '${TOM_USER_PASSWORD}';
CREATE USER ${TOM_USER_TEST} WITH PASSWORD '${TOM_USER_TEST_PASSWORD}';
CREATE DATABASE ${TOM_DBNAME} WITH OWNER = ${TOM_USER};
CREATE DATABASE ${TOM_DBNAME_TEST} WITH OWNER = ${TOM_USER_TEST};
EOSQL
1 change: 0 additions & 1 deletion docker/postgres/test.sql

This file was deleted.

3 changes: 2 additions & 1 deletion docker/web/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ RUN apt-get update \
locales \
# python setup
&& pip install --upgrade pip \
&& pip install --no-cache-dir -e /app \
&& pip install --no-cache-dir -e /app/[dev] \
# clean
&& rm -rf /var/lib/apt/lists/* \
&& apt-get -qq --allow-remove-essential remove gcc \
&& apt-get -qq autoremove \
&& apt-get clean \
&& echo "ru_RU.UTF-8 UTF-8" > /etc/locale.gen \
&& echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen \
&& locale-gen

Expand Down
2 changes: 1 addition & 1 deletion entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env bash
pip install -e .
pip install -e .[dev]
exec "$@";
12 changes: 12 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
[flake8]
max-line-length = 120
exclude =
notebooks
tom_calculator/alembic

[mypy]
ignore_missing_imports = True
plugins = sqlalchemy.ext.mypy.plugin

[coverage:run]
source = tom_calculator
omit =
tom_calculator/alembic/*

[coverage:report]
fail_under = 75
31 changes: 28 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,32 @@
)

install_requires = [
'alembic==1.6.5',
'async_exit_stack==1.0.1',
'async_generator==1.10',
'asyncpg==0.24.0',
'dependency-injector==4.35.2',
'fastapi==0.68.0',
'psycopg2==2.9.1',
'requests==2.25.1',
'sqlalchemy==1.4.22',
'typer==0.3.2',
'uvicorn[standard]==0.13.4',
'uvloop==0.16.0',
]

dev_require = [
test_require = [
'flake8==3.9.2',
'mypy==0.910',
'pytest-asyncio==0.15.1',
'pytest-cov==2.12.1',
'pytest-sugar==0.9.4',
'pytest==6.2.4',
'sqlalchemy[mypy]==1.4.22',
]

dev_require = [
'pydevd-pycharm~=211.7142.13',
'jupyter==1.0.0',
]

setup(
Expand All @@ -29,5 +45,14 @@
packages=find_packages(include=['tom_calculator']),
python_requires='>=3.9',
install_requires=install_requires,
extras_require={'dev': dev_require},
extras_require={
'test': test_require,
'dev': dev_require,
},
entry_points={
'console_scripts': [
'tom-calculator = tom_calculator.cli:app',
],
},

)
Empty file added tom_calculator/__init__.py
Empty file.
1 change: 1 addition & 0 deletions tom_calculator/alembic/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Generic single-database configuration.
80 changes: 80 additions & 0 deletions tom_calculator/alembic/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from logging.config import fileConfig

from sqlalchemy import engine_from_config
from sqlalchemy import pool

from alembic import context
from tom_calculator.application import app
from tom_calculator.database import Base

# 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.
# This line sets up loggers basically.
fileConfig(config.config_file_name)

config.set_main_option("sqlalchemy.url", app.container.config.db.dsn())
# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = Base.metadata

# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.


def run_migrations_offline():
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)

with context.begin_transaction():
context.run_migrations()


def run_migrations_online():
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
connectable = engine_from_config(
config.get_section(config.config_ini_section),
prefix="sqlalchemy.",
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()
24 changes: 24 additions & 0 deletions tom_calculator/alembic/script.py.mako
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""${message}

Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}

"""
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}

# revision identifiers, used by Alembic.
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}


def upgrade():
${upgrades if upgrades else "pass"}


def downgrade():
${downgrades if downgrades else "pass"}
Loading

0 comments on commit 02dd097

Please sign in to comment.