reorganization
- Had to reorganize so that Alembic's env.py could reference the tables in both local environment and a docker environment without manually modifying the python sys path. Maybe there is a better way idk
This commit is contained in:
parent
3670d1522f
commit
a101447eeb
22 changed files with 77 additions and 64 deletions
1
src/alembic/README
Normal file
1
src/alembic/README
Normal file
|
|
@ -0,0 +1 @@
|
|||
Generic single-database configuration.
|
||||
81
src/alembic/env.py
Normal file
81
src/alembic/env.py
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
from logging.config import fileConfig
|
||||
|
||||
from sqlalchemy import engine_from_config
|
||||
from sqlalchemy import pool
|
||||
|
||||
from alembic import context
|
||||
|
||||
# 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.
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
# add your model's MetaData object here
|
||||
# for 'autogenerate' support
|
||||
# from myapp import mymodel
|
||||
# target_metadata = mymodel.Base.metadata
|
||||
from src.bot.models import TableNotification, TableSteamItem, TableGiveaway, Base
|
||||
|
||||
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
src/alembic/script.py.mako
Normal file
24
src/alembic/script.py.mako
Normal file
|
|
@ -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"}
|
||||
|
|
@ -0,0 +1,70 @@
|
|||
"""init
|
||||
|
||||
Revision ID: 1da33402b659
|
||||
Revises:
|
||||
Create Date: 2022-05-19 15:50:19.539401
|
||||
|
||||
"""
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '1da33402b659'
|
||||
down_revision = None
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table('notification',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('type', sa.String(length=50), nullable=False),
|
||||
sa.Column('message', sa.String(length=300), nullable=False),
|
||||
sa.Column('medium', sa.String(length=50), nullable=False),
|
||||
sa.Column('success', sa.Boolean(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'),
|
||||
nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'),
|
||||
nullable=True),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_table('steam_item',
|
||||
sa.Column('steam_id', sa.String(length=15), nullable=False),
|
||||
sa.Column('game_name', sa.String(length=200), nullable=False),
|
||||
sa.Column('steam_url', sa.String(length=100), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'),
|
||||
nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'),
|
||||
nullable=True),
|
||||
sa.PrimaryKeyConstraint('steam_id')
|
||||
)
|
||||
op.create_table('giveaway',
|
||||
sa.Column('giveaway_id', sa.String(length=10), nullable=False),
|
||||
sa.Column('steam_id', sa.Integer(), nullable=False),
|
||||
sa.Column('giveaway_uri', sa.String(length=200), nullable=False),
|
||||
sa.Column('user', sa.String(length=40), nullable=False),
|
||||
sa.Column('giveaway_created_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column('giveaway_ended_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column('cost', sa.Integer(), nullable=False),
|
||||
sa.Column('copies', sa.Integer(), nullable=False),
|
||||
sa.Column('contributor_level', sa.Integer(), nullable=False),
|
||||
sa.Column('entered', sa.Boolean(), nullable=False),
|
||||
sa.Column('won', sa.Boolean(), nullable=False),
|
||||
sa.Column('game_entries', sa.Integer(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'),
|
||||
nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'),
|
||||
nullable=True),
|
||||
sa.ForeignKeyConstraint(['steam_id'], ['steam_item.steam_id'], ),
|
||||
sa.PrimaryKeyConstraint('giveaway_id', 'steam_id')
|
||||
)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_table('giveaway')
|
||||
op.drop_table('steam_item')
|
||||
op.drop_table('notification')
|
||||
# ### end Alembic commands ###
|
||||
|
|
@ -1,9 +1,9 @@
|
|||
from configparser import ConfigParser
|
||||
from random import randint, randrange
|
||||
|
||||
import log
|
||||
from .log import get_logger
|
||||
|
||||
logger = log.get_logger(__name__)
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class ConfigException(Exception):
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
import os
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
import sqlalchemy
|
||||
|
|
@ -8,18 +9,15 @@ from sqlalchemy import func
|
|||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy_utils import database_exists
|
||||
|
||||
import log
|
||||
from models import TableNotification, TableGiveaway, TableSteamItem
|
||||
from .log import get_logger
|
||||
from .models import TableNotification, TableGiveaway, TableSteamItem
|
||||
|
||||
logger = log.get_logger(__name__)
|
||||
engine = None
|
||||
logger = get_logger(__name__)
|
||||
engine = sqlalchemy.create_engine(f"{os.getenv('BOT_DB_URL', 'sqlite:///./config/sqlite.db')}", echo=False)
|
||||
engine.connect()
|
||||
|
||||
|
||||
def create_engine(db_url: str):
|
||||
global engine
|
||||
if not engine:
|
||||
engine = sqlalchemy.create_engine(db_url, echo=False)
|
||||
engine.connect()
|
||||
return engine
|
||||
|
||||
|
||||
|
|
@ -49,9 +47,6 @@ def run_migrations(script_location: str, db_url: str) -> None:
|
|||
command.upgrade(alembic_cfg, 'head')
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class NotificationHelper:
|
||||
|
||||
@classmethod
|
||||
|
|
@ -7,11 +7,11 @@ from bs4 import BeautifulSoup
|
|||
from requests.adapters import HTTPAdapter
|
||||
from urllib3.util import Retry
|
||||
|
||||
import log
|
||||
from database import NotificationHelper, GiveawayHelper
|
||||
from giveaway import Giveaway
|
||||
from .log import get_logger
|
||||
from .database import NotificationHelper, GiveawayHelper
|
||||
from .giveaway import Giveaway
|
||||
|
||||
logger = log.get_logger(__name__)
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class SteamGiftsException(Exception):
|
||||
|
|
@ -1,8 +1,8 @@
|
|||
import re
|
||||
import log
|
||||
from .log import get_logger
|
||||
import time
|
||||
|
||||
logger = log.get_logger(__name__)
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class Giveaway:
|
||||
|
|
@ -7,10 +7,10 @@ from time import sleep
|
|||
|
||||
from dateutil import tz
|
||||
|
||||
import log
|
||||
from enter_giveaways import EnterGiveaways
|
||||
from .log import get_logger
|
||||
from .enter_giveaways import EnterGiveaways
|
||||
|
||||
logger = log.get_logger(__name__)
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class GiveawayThread(threading.Thread):
|
||||
|
|
@ -1,11 +1,12 @@
|
|||
import logging
|
||||
import os
|
||||
from logging.handlers import RotatingFileHandler
|
||||
|
||||
# log info level logs to stdout and debug to debug file
|
||||
|
||||
log_format = "%(levelname)s %(asctime)s - %(message)s"
|
||||
logging.basicConfig(
|
||||
handlers=[RotatingFileHandler('../config/debug.log', maxBytes=500000, backupCount=10)],
|
||||
handlers=[RotatingFileHandler(f"{os.getenv('BOT_CONFIG_DIR', './config')}/debug.log", maxBytes=500000, backupCount=10)],
|
||||
level=logging.DEBUG,
|
||||
format=log_format)
|
||||
|
||||
|
|
@ -14,7 +15,7 @@ console_output.setLevel(logging.INFO)
|
|||
console_format = logging.Formatter(log_format)
|
||||
console_output.setFormatter(console_format)
|
||||
|
||||
info_log_file = RotatingFileHandler('../config/info.log', maxBytes=100000, backupCount=10)
|
||||
info_log_file = RotatingFileHandler(f"{os.getenv('BOT_CONFIG_DIR', './config')}/info.log", maxBytes=100000, backupCount=10)
|
||||
info_log_file.setLevel(logging.INFO)
|
||||
info_log_format = logging.Formatter(log_format)
|
||||
info_log_file.setFormatter(info_log_format)
|
||||
|
|
@ -1,10 +1,10 @@
|
|||
import http.client
|
||||
import urllib
|
||||
|
||||
import log
|
||||
from database import NotificationHelper
|
||||
from .log import get_logger
|
||||
from .database import NotificationHelper
|
||||
|
||||
logger = log.get_logger(__name__)
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class Notification:
|
||||
|
|
@ -1,17 +1,18 @@
|
|||
import os
|
||||
from time import sleep
|
||||
|
||||
import log
|
||||
from config_reader import ConfigReader, ConfigException
|
||||
from enter_giveaways import SteamGiftsException
|
||||
from giveaway_thread import GiveawayThread
|
||||
from notification import Notification
|
||||
from database import run_migrations, create_engine
|
||||
from webserver_thread import WebServerThread
|
||||
from .log import get_logger
|
||||
from .config_reader import ConfigReader, ConfigException
|
||||
from .enter_giveaways import SteamGiftsException
|
||||
from .giveaway_thread import GiveawayThread
|
||||
from .notification import Notification
|
||||
from .database import run_migrations, create_engine
|
||||
from .webserver_thread import WebServerThread
|
||||
|
||||
logger = log.get_logger(__name__)
|
||||
config_file_name = '../config/config.ini'
|
||||
db_url = 'sqlite:///../config/sqlite.db'
|
||||
alembic_migration_files = '../alembic'
|
||||
logger = get_logger(__name__)
|
||||
config_file_name = f"{os.getenv('BOT_CONFIG_DIR', './config')}/config.ini"
|
||||
db_url = f"{os.getenv('BOT_DB_URL', 'sqlite:///./config/sqlite.db')}"
|
||||
alembic_migration_files = os.getenv('BOT_ALEMBIC_CONFIG_DIR', './src/alembic')
|
||||
|
||||
|
||||
def run():
|
||||
|
|
@ -59,19 +60,22 @@ def run():
|
|||
exit(-1)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
def entry():
|
||||
logger.info("""
|
||||
-------------------------------------------------------------------------------------
|
||||
_____ _ _ __ _ ____ _
|
||||
/ ____|| | (_) / _|| | | _ \ | |
|
||||
| (___ | |_ ___ __ _ _ __ ___ __ _ _ | |_ | |_ ___ | |_) | ___ | |_
|
||||
\___ \ | __|/ _ \ / _` || '_ ` _ \ / _` || || _|| __|/ __| | _ < / _ \ | __|
|
||||
____) || |_| __/| (_| || | | | | || (_| || || | | |_ \__ \ | |_) || (_) || |_
|
||||
|_____/ \__|\___| \__,_||_| |_| |_| \__, ||_||_| \__||___/ |____/ \___/ \__|
|
||||
__/ |
|
||||
|___/
|
||||
-------------------------------------------------------------------------------------
|
||||
""")
|
||||
-------------------------------------------------------------------------------------
|
||||
_____ _ _ __ _ ____ _
|
||||
/ ____|| | (_) / _|| | | _ \ | |
|
||||
| (___ | |_ ___ __ _ _ __ ___ __ _ _ | |_ | |_ ___ | |_) | ___ | |_
|
||||
\___ \ | __|/ _ \ / _` || '_ ` _ \ / _` || || _|| __|/ __| | _ < / _ \ | __|
|
||||
____) || |_| __/| (_| || | | | | || (_| || || | | |_ \__ \ | |_) || (_) || |_
|
||||
|_____/ \__|\___| \__,_||_| |_| |_| \__, ||_||_| \__||___/ |____/ \___/ \__|
|
||||
__/ |
|
||||
|___/
|
||||
-------------------------------------------------------------------------------------
|
||||
""")
|
||||
run_migrations(alembic_migration_files, db_url)
|
||||
create_engine(db_url)
|
||||
run()
|
||||
|
||||
if __name__ == '__main__':
|
||||
entry()
|
||||
|
|
@ -1,12 +1,11 @@
|
|||
import threading
|
||||
from threading import Thread
|
||||
from time import sleep
|
||||
|
||||
from flask_basicauth import BasicAuth
|
||||
|
||||
import log
|
||||
from .log import get_logger
|
||||
|
||||
logger = log.get_logger(__name__)
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class WebServerThread(threading.Thread):
|
||||
Loading…
Add table
Add a link
Reference in a new issue