Skip to content

Commit fbae3f7

Browse files
committed
Add database migrations
1 parent 4337f84 commit fbae3f7

7 files changed

+358
-0
lines changed

alembic.ini

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# A generic, single database configuration.
2+
3+
[alembic]
4+
# path to migration scripts
5+
# Use forward slashes (/) also on windows to provide an os agnostic path
6+
script_location = alembic
7+
8+
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
9+
# Uncomment the line below if you want the files to be prepended with date and time
10+
# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file
11+
# for all available tokens
12+
# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
13+
14+
# sys.path path, will be prepended to sys.path if present.
15+
# defaults to the current working directory.
16+
prepend_sys_path = .
17+
18+
# timezone to use when rendering the date within the migration file
19+
# as well as the filename.
20+
# If specified, requires the python>=3.9 or backports.zoneinfo library.
21+
# Any required deps can installed by adding `alembic[tz]` to the pip requirements
22+
# string value is passed to ZoneInfo()
23+
# leave blank for localtime
24+
# timezone =
25+
26+
# max length of characters to apply to the "slug" field
27+
# truncate_slug_length = 40
28+
29+
# set to 'true' to run the environment during
30+
# the 'revision' command, regardless of autogenerate
31+
# revision_environment = false
32+
33+
# set to 'true' to allow .pyc and .pyo files without
34+
# a source .py file to be detected as revisions in the
35+
# versions/ directory
36+
# sourceless = false
37+
38+
# version location specification; This defaults
39+
# to alembic/versions. When using multiple version
40+
# directories, initial revisions must be specified with --version-path.
41+
# The path separator used here should be the separator specified by "version_path_separator" below.
42+
# version_locations = %(here)s/bar:%(here)s/bat:alembic/versions
43+
44+
# version path separator; As mentioned above, this is the character used to split
45+
# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep.
46+
# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas.
47+
# Valid values for version_path_separator are:
48+
#
49+
# version_path_separator = :
50+
# version_path_separator = ;
51+
# version_path_separator = space
52+
# version_path_separator = newline
53+
version_path_separator = os # Use os.pathsep. Default configuration used for new projects.
54+
55+
# set to 'true' to search source files recursively
56+
# in each "version_locations" directory
57+
# new in Alembic version 1.10
58+
# recursive_version_locations = false
59+
60+
# the output encoding used when revision files
61+
# are written from script.py.mako
62+
# output_encoding = utf-8
63+
64+
sqlalchemy.url = sqlite:////tmp/configure_path_to_tinytown_database.db
65+
66+
67+
[post_write_hooks]
68+
# post_write_hooks defines scripts or Python functions that are run
69+
# on newly generated revision scripts. See the documentation for further
70+
# detail and examples
71+
72+
# format using "black" - use the console_scripts runner, against the "black" entrypoint
73+
# hooks = black
74+
# black.type = console_scripts
75+
# black.entrypoint = black
76+
# black.options = -l 79 REVISION_SCRIPT_FILENAME
77+
78+
# lint with attempts to fix using "ruff" - use the exec runner, execute a binary
79+
# hooks = ruff
80+
# ruff.type = exec
81+
# ruff.executable = %(here)s/.venv/bin/ruff
82+
# ruff.options = --fix REVISION_SCRIPT_FILENAME
83+
84+
# Logging configuration
85+
[loggers]
86+
keys = root,sqlalchemy,alembic
87+
88+
[handlers]
89+
keys = console
90+
91+
[formatters]
92+
keys = generic
93+
94+
[logger_root]
95+
level = WARNING
96+
handlers = console
97+
qualname =
98+
99+
[logger_sqlalchemy]
100+
level = WARNING
101+
handlers =
102+
qualname = sqlalchemy.engine
103+
104+
[logger_alembic]
105+
level = INFO
106+
handlers =
107+
qualname = alembic
108+
109+
[handler_console]
110+
class = StreamHandler
111+
args = (sys.stderr,)
112+
level = NOTSET
113+
formatter = generic
114+
115+
[formatter_generic]
116+
format = %(levelname)-5.5s [%(name)s] %(message)s
117+
datefmt = %H:%M:%S

alembic/README

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Generic single-database configuration.

alembic/env.py

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
from logging.config import fileConfig
2+
3+
from sqlalchemy import engine_from_config
4+
from sqlalchemy import pool
5+
6+
from alembic import context
7+
8+
import terroroftinytown.tracker.model
9+
10+
# this is the Alembic Config object, which provides
11+
# access to the values within the .ini file in use.
12+
config = context.config
13+
14+
# Interpret the config file for Python logging.
15+
# This line sets up loggers basically.
16+
if config.config_file_name is not None:
17+
fileConfig(config.config_file_name)
18+
19+
# add your model's MetaData object here
20+
# for 'autogenerate' support
21+
# from myapp import mymodel
22+
# target_metadata = mymodel.Base.metadata
23+
target_metadata = terroroftinytown.tracker.model.Base.metadata
24+
25+
# other values from the config, defined by the needs of env.py,
26+
# can be acquired:
27+
# my_important_option = config.get_main_option("my_important_option")
28+
# ... etc.
29+
30+
31+
def run_migrations_offline() -> None:
32+
"""Run migrations in 'offline' mode.
33+
34+
This configures the context with just a URL
35+
and not an Engine, though an Engine is acceptable
36+
here as well. By skipping the Engine creation
37+
we don't even need a DBAPI to be available.
38+
39+
Calls to context.execute() here emit the given string to the
40+
script output.
41+
42+
"""
43+
url = config.get_main_option("sqlalchemy.url")
44+
context.configure(
45+
url=url,
46+
target_metadata=target_metadata,
47+
literal_binds=True,
48+
dialect_opts={"paramstyle": "named"},
49+
)
50+
51+
with context.begin_transaction():
52+
context.run_migrations()
53+
54+
55+
def run_migrations_online() -> None:
56+
"""Run migrations in 'online' mode.
57+
58+
In this scenario we need to create an Engine
59+
and associate a connection with the context.
60+
61+
"""
62+
connectable = engine_from_config(
63+
config.get_section(config.config_ini_section, {}),
64+
prefix="sqlalchemy.",
65+
poolclass=pool.NullPool,
66+
)
67+
68+
with connectable.connect() as connection:
69+
context.configure(
70+
connection=connection, target_metadata=target_metadata
71+
)
72+
73+
with context.begin_transaction():
74+
context.run_migrations()
75+
76+
77+
if context.is_offline_mode():
78+
run_migrations_offline()
79+
else:
80+
run_migrations_online()

alembic/script.py.mako

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""${message}
2+
3+
Revision ID: ${up_revision}
4+
Revises: ${down_revision | comma,n}
5+
Create Date: ${create_date}
6+
7+
"""
8+
from typing import Sequence, Union
9+
10+
from alembic import op
11+
import sqlalchemy as sa
12+
${imports if imports else ""}
13+
14+
# revision identifiers, used by Alembic.
15+
revision: str = ${repr(up_revision)}
16+
down_revision: Union[str, None] = ${repr(down_revision)}
17+
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
18+
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
19+
20+
21+
def upgrade() -> None:
22+
${upgrades if upgrades else "pass"}
23+
24+
25+
def downgrade() -> None:
26+
${downgrades if downgrades else "pass"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
"""Initial tables
2+
3+
Revision ID: 95fe71fbb867
4+
Revises:
5+
Create Date: 2025-01-02 18:47:40.729944
6+
7+
"""
8+
from typing import Sequence, Union
9+
10+
from alembic import op
11+
import sqlalchemy as sa
12+
13+
14+
# revision identifiers, used by Alembic.
15+
revision: str = '95fe71fbb867'
16+
down_revision: Union[str, None] = None
17+
branch_labels: Union[str, Sequence[str], None] = None
18+
depends_on: Union[str, Sequence[str], None] = None
19+
20+
21+
def upgrade() -> None:
22+
# ### commands auto generated by Alembic - please adjust! ###
23+
op.create_table('blocked_users',
24+
sa.Column('username', sa.String(), nullable=False),
25+
sa.Column('note', sa.String(), nullable=True),
26+
sa.PrimaryKeyConstraint('username')
27+
)
28+
op.create_table('global_settings',
29+
sa.Column('key', sa.String(), nullable=False),
30+
sa.Column('value', terroroftinytown.tracker.model.JsonType(), nullable=True),
31+
sa.PrimaryKeyConstraint('key')
32+
)
33+
op.create_table('projects',
34+
sa.Column('name', sa.String(), nullable=False),
35+
sa.Column('min_version', sa.Integer(), nullable=False),
36+
sa.Column('min_client_version', sa.Integer(), nullable=False),
37+
sa.Column('alphabet', sa.String(), nullable=False),
38+
sa.Column('url_template', sa.String(), nullable=False),
39+
sa.Column('request_delay', sa.Float(), nullable=False),
40+
sa.Column('redirect_codes', terroroftinytown.tracker.model.JsonType(), nullable=False),
41+
sa.Column('no_redirect_codes', terroroftinytown.tracker.model.JsonType(), nullable=False),
42+
sa.Column('unavailable_codes', terroroftinytown.tracker.model.JsonType(), nullable=True),
43+
sa.Column('banned_codes', terroroftinytown.tracker.model.JsonType(), nullable=True),
44+
sa.Column('body_regex', sa.String(), nullable=True),
45+
sa.Column('location_anti_regex', sa.String(), nullable=True),
46+
sa.Column('method', sa.String(), nullable=False),
47+
sa.Column('enabled', sa.Boolean(), nullable=True),
48+
sa.Column('autoqueue', sa.Boolean(), nullable=True),
49+
sa.Column('num_count_per_item', sa.Integer(), nullable=False),
50+
sa.Column('max_num_items', sa.Integer(), nullable=False),
51+
sa.Column('lower_sequence_num', sa.Integer(), nullable=False),
52+
sa.Column('autorelease_time', sa.Integer(), nullable=True),
53+
sa.PrimaryKeyConstraint('name')
54+
)
55+
op.create_table('users',
56+
sa.Column('username', sa.String(), nullable=False),
57+
sa.Column('salt', sa.LargeBinary(), nullable=False),
58+
sa.Column('hash', sa.LargeBinary(), nullable=False),
59+
sa.PrimaryKeyConstraint('username')
60+
)
61+
op.create_table('items',
62+
sa.Column('id', sa.Integer(), nullable=False),
63+
sa.Column('project_id', sa.Integer(), nullable=False),
64+
sa.Column('lower_sequence_num', sa.Integer(), nullable=False),
65+
sa.Column('upper_sequence_num', sa.Integer(), nullable=False),
66+
sa.Column('datetime_claimed', sa.DateTime(), nullable=True),
67+
sa.Column('tamper_key', sa.String(), nullable=True),
68+
sa.Column('username', sa.String(), nullable=True),
69+
sa.Column('ip_address', sa.String(), nullable=True),
70+
sa.ForeignKeyConstraint(['project_id'], ['projects.name'], ),
71+
sa.PrimaryKeyConstraint('id')
72+
)
73+
op.create_table('results',
74+
sa.Column('id', sa.Integer(), nullable=False),
75+
sa.Column('project_id', sa.Integer(), nullable=False),
76+
sa.Column('shortcode', sa.String(), nullable=False),
77+
sa.Column('url', sa.String(), nullable=False),
78+
sa.Column('encoding', sa.String(), nullable=False),
79+
sa.Column('datetime', sa.DateTime(), nullable=True),
80+
sa.ForeignKeyConstraint(['project_id'], ['projects.name'], ),
81+
sa.PrimaryKeyConstraint('id')
82+
)
83+
op.create_index(op.f('ix_results_project_id'), 'results', ['project_id'], unique=False)
84+
op.create_table('error_reports',
85+
sa.Column('id', sa.Integer(), nullable=False),
86+
sa.Column('item_id', sa.Integer(), nullable=False),
87+
sa.Column('message', sa.String(), nullable=False),
88+
sa.Column('datetime', sa.DateTime(), nullable=False),
89+
sa.ForeignKeyConstraint(['item_id'], ['items.id'], ),
90+
sa.PrimaryKeyConstraint('id')
91+
)
92+
# ### end Alembic commands ###
93+
94+
95+
def downgrade() -> None:
96+
# ### commands auto generated by Alembic - please adjust! ###
97+
op.drop_table('error_reports')
98+
op.drop_index(op.f('ix_results_project_id'), table_name='results')
99+
op.drop_table('results')
100+
op.drop_table('items')
101+
op.drop_table('users')
102+
op.drop_table('projects')
103+
op.drop_table('global_settings')
104+
op.drop_table('blocked_users')
105+
# ### end Alembic commands ###
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""Add User.hash_format and Result.export columns
2+
3+
Revision ID: cd3f8e6affcc
4+
Revises: 95fe71fbb867
5+
Create Date: 2025-01-03 21:26:19.812712
6+
7+
"""
8+
from typing import Sequence, Union
9+
10+
from alembic import op
11+
import sqlalchemy as sa
12+
13+
14+
# revision identifiers, used by Alembic.
15+
revision: str = 'cd3f8e6affcc'
16+
down_revision: Union[str, None] = '95fe71fbb867'
17+
branch_labels: Union[str, Sequence[str], None] = None
18+
depends_on: Union[str, Sequence[str], None] = None
19+
20+
21+
def upgrade() -> None:
22+
op.add_column('users', sa.Column('hash_format', sa.String(), nullable=False, default='legacy'))
23+
op.add_column('results', sa.Column('export', sa.Boolean()))
24+
25+
26+
def downgrade() -> None:
27+
op.drop_column('users', 'hash_format')
28+
op.drop_column('results', 'export')

requirements-tracker.txt

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ wtforms-tornado @ git+https://github.com/chfoo/wtforms-tornado@96a8486f7be4f2894
44
wtforms==3.2.1
55
SQLAlchemy==2.0.36
66
internetarchive==5.0.5
7+
alembic==1.14.0

0 commit comments

Comments
 (0)