Add settings table

This commit is contained in:
Keith Edmunds 2021-03-26 15:28:22 +00:00
parent f1805138f6
commit a6de739c74
3 changed files with 73 additions and 1 deletions

View File

@ -36,6 +36,35 @@ session = Session()
# Database classes
class Settings(Base):
__tablename__ = 'settings'
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String(32), nullable=False, unique=True)
f_datetime = Column(DateTime, default=None, nullable=True)
f_int = Column(Integer, default=None, nullable=True)
f_string = Column(String(128), default=None, nullable=True)
@classmethod
def get_int(cls, name):
try:
int_setting = session.query(cls).filter(
cls.name == name).one()
except NoResultFound:
int_setting = Settings()
int_setting.name = name
int_setting.f_int = None
session.add(int_setting)
session.commit()
return int_setting
def update(self, data):
for key, value in data.items():
assert hasattr(self, key)
setattr(self, key, value)
session.commit()
class Tracks(Base):
__tablename__ = 'tracks'

View File

@ -1,3 +1,6 @@
import sys
import os
from logging.config import fileConfig
from sqlalchemy import engine_from_config
@ -17,7 +20,11 @@ fileConfig(config.config_file_name)
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
from model import Base
# https://stackoverflow.com/questions/32032940/how-to-import-the-own-model-into-myproject-alembic-env-py
path = os.path.dirname(os.path.dirname(__file__))
sys.path.insert(0, path)
sys.path.insert(0, os.path.join(path, "app"))
from app.model import Base
target_metadata = Base.metadata
# other values from the config, defined by the needs of env.py,
# can be acquired:

View File

@ -0,0 +1,36 @@
"""Add settings table
Revision ID: b0983648595e
Revises: 1bc727e5e87f
Create Date: 2021-03-26 13:33:41.994508
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'b0983648595e'
down_revision = '1bc727e5e87f'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('settings',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('name', sa.String(length=32), nullable=False),
sa.Column('f_datetime', sa.DateTime(), nullable=True),
sa.Column('f_int', sa.Integer(), nullable=True),
sa.Column('f_string', sa.String(length=128), nullable=True),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('name')
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('settings')
# ### end Alembic commands ###