Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
fix: migration of c058421705ec (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomBursch authored May 31, 2023
1 parent 2b50891 commit 3a5051f
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions migrations/versions/c058421705ec_.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
from datetime import datetime
from alembic import op
import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy import orm, inspect
from os import listdir
from os.path import isfile, join

from app.config import UPLOAD_FOLDER
from app.config import UPLOAD_FOLDER, db

DeclarativeBase = orm.declarative_base()

Expand Down Expand Up @@ -47,26 +47,30 @@ class User(DeclarativeBase):

def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('file',
sa.Column('filename', sa.String(), nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.Column('updated_at', sa.DateTime(), nullable=False),
sa.Column('created_by', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['created_by'], ['user.id'], name=op.f('fk_file_created_by_user')),
sa.PrimaryKeyConstraint('filename', name=op.f('pk_file')),
)
inspector = inspect(db.engine)
if not inspector.has_table("file"):
op.create_table('file',
sa.Column('filename', sa.String(), nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.Column('updated_at', sa.DateTime(), nullable=False),
sa.Column('created_by', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['created_by'], ['user.id'], name=op.f('fk_file_created_by_user')),
sa.PrimaryKeyConstraint('filename', name=op.f('pk_file')),
)
# ### end Alembic commands ###
bind = op.get_bind()
session = orm.Session(bind=bind)

filesInUploadFolder = [f for f in listdir(UPLOAD_FOLDER) if isfile(join(UPLOAD_FOLDER, f))]

try:
filesInUploadFolder = [f for f in listdir(UPLOAD_FOLDER) if isfile(join(UPLOAD_FOLDER, f))]
files = [File(filename=f) for f in filesInUploadFolder]

session.bulk_save_objects(files)
session.commit()
except Exception as e:
except FileNotFoundError as e:
session.rollback()
except BaseException as e:
session.rollback()
raise e

Expand Down

0 comments on commit 3a5051f

Please sign in to comment.