V0.2.0
All checks were successful
Build Docker Image / build (push) Successful in 12m39s

This commit is contained in:
2025-02-13 17:46:15 -08:00
parent 9544b0415c
commit b7e89d9c22
17 changed files with 455 additions and 24 deletions

83
migrations/env.py Normal file
View File

@@ -0,0 +1,83 @@
from logging.config import fileConfig
from sqlalchemy import engine_from_config
from sqlalchemy import pool, create_engine
from alembic import context
from app.models import Base
from app.config import Config
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# Set the database URL from our app config
config.set_main_option('sqlalchemy.url', Config.SQLALCHEMY_DATABASE_URI)
# 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
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() -> None:
"""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() -> None:
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
# Use create_engine() directly with our URL
connectable = create_engine(Config.SQLALCHEMY_DATABASE_URI)
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()

26
migrations/script.py.mako Normal file
View File

@@ -0,0 +1,26 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
# revision identifiers, used by Alembic.
revision: str = ${repr(up_revision)}
down_revision: Union[str, None] = ${repr(down_revision)}
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
def upgrade() -> None:
${upgrades if upgrades else "pass"}
def downgrade() -> None:
${downgrades if downgrades else "pass"}

View File

@@ -0,0 +1,80 @@
"""add watch history columns
Revision ID: 8911624d0776
Revises:
Create Date: 2024-xx-xx xx:xx:xx.xxx
"""
from alembic import op
import sqlalchemy as sa
from datetime import datetime
from sqlalchemy.engine.reflection import Inspector
from sqlalchemy import text
# revision identifiers, used by Alembic.
revision = '8911624d0776'
down_revision = None
branch_labels = None
depends_on = None
def column_exists(table, column):
# Get inspector
conn = op.get_bind()
inspector = Inspector.from_engine(conn)
columns = [c['name'] for c in inspector.get_columns(table)]
return column in columns
def upgrade():
# Add new columns if they don't exist
with op.batch_alter_table('watch_history') as batch_op:
# Add category column
if not column_exists('watch_history', 'category'):
batch_op.add_column(sa.Column('category', sa.String(100), nullable=True))
# Add view_count column
if not column_exists('watch_history', 'view_count'):
batch_op.add_column(sa.Column('view_count', sa.Integer(), nullable=True))
# Add subscriber_count column
if not column_exists('watch_history', 'subscriber_count'):
batch_op.add_column(sa.Column('subscriber_count', sa.Integer(), nullable=True))
# Add thumbnail_url column
if not column_exists('watch_history', 'thumbnail_url'):
batch_op.add_column(sa.Column('thumbnail_url', sa.String(255), nullable=True))
# Add upload_date column
if not column_exists('watch_history', 'upload_date'):
batch_op.add_column(sa.Column('upload_date', sa.DateTime(), nullable=True))
# Backfill data
conn = op.get_bind()
conn.execute(text("""
UPDATE watch_history
SET category = COALESCE(category, 'Unknown'),
view_count = COALESCE(view_count, 0),
subscriber_count = COALESCE(subscriber_count, 0),
thumbnail_url = COALESCE(thumbnail_url, ''),
upload_date = COALESCE(upload_date, watch_date)
WHERE category IS NULL
OR view_count IS NULL
OR subscriber_count IS NULL
OR thumbnail_url IS NULL
OR upload_date IS NULL
"""))
def downgrade():
with op.batch_alter_table('watch_history') as batch_op:
if column_exists('watch_history', 'upload_date'):
batch_op.drop_column('upload_date')
if column_exists('watch_history', 'thumbnail_url'):
batch_op.drop_column('thumbnail_url')
if column_exists('watch_history', 'subscriber_count'):
batch_op.drop_column('subscriber_count')
if column_exists('watch_history', 'view_count'):
batch_op.drop_column('view_count')
if column_exists('watch_history', 'category'):
batch_op.drop_column('category')