diff options
author | rubenwardy <rw@rubenwardy.com> | 2020-07-09 01:11:50 +0100 |
---|---|---|
committer | rubenwardy <rw@rubenwardy.com> | 2020-07-09 01:11:50 +0100 |
commit | 2617c53abfc3b1a1af10a88794e17bb88affd61d (patch) | |
tree | 53691f2eff9fad39709b29d5da095abb493601e6 /migrations | |
parent | bbf1143090a4b70b3226547d243b54685f8c6af2 (diff) | |
download | cheatdb-2617c53abfc3b1a1af10a88794e17bb88affd61d.tar.xz |
Add downloads column to Package
Fixes #200
Diffstat (limited to 'migrations')
-rw-r--r-- | migrations/versions/c141a63b2487_.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/migrations/versions/c141a63b2487_.py b/migrations/versions/c141a63b2487_.py new file mode 100644 index 0000000..571eef6 --- /dev/null +++ b/migrations/versions/c141a63b2487_.py @@ -0,0 +1,38 @@ +"""empty message + +Revision ID: c141a63b2487 +Revises: cb6ab141c522 +Create Date: 2020-07-09 00:05:39.845465 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy import orm, func +from app.models import Package, PackageRelease + + +# revision identifiers, used by Alembic. +revision = 'c141a63b2487' +down_revision = 'cb6ab141c522' +branch_labels = None +depends_on = None + + +def upgrade(): + op.add_column('package', sa.Column('downloads', sa.Integer(), nullable=False, server_default="0")) + + bind = op.get_bind() + session = orm.Session(bind=bind) + + for package in session.query(Package).all(): + downloads_result = session.query(func.sum(PackageRelease.downloads)).filter_by(package_id=package.id).one_or_none() + downloads = 0 if not downloads_result or not downloads_result[0] else downloads_result[0] + package.downloads = downloads + + session.commit() + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('package', 'downloads') + # ### end Alembic commands ### |