From 6dea49b7d836e5809ce8514a1c5c13aed429824e Mon Sep 17 00:00:00 2001 From: Robert Goldmann Date: Mon, 22 Apr 2024 22:34:35 +0200 Subject: [PATCH] #91 - fix database migration --- .../versions/41c4f10f2564_planned_tours.py | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/sporttracker/migrations/versions/41c4f10f2564_planned_tours.py b/sporttracker/migrations/versions/41c4f10f2564_planned_tours.py index e4fbf72..6e5ece3 100644 --- a/sporttracker/migrations/versions/41c4f10f2564_planned_tours.py +++ b/sporttracker/migrations/versions/41c4f10f2564_planned_tours.py @@ -8,6 +8,7 @@ import sqlalchemy as sa from alembic import op +from sqlalchemy import Inspector from sqlalchemy.dialects import postgresql # revision identifiers, used by Alembic. @@ -18,20 +19,24 @@ def upgrade(): - op.create_table( - 'planned_tour', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('type', postgresql.ENUM(name='tracktype', create_type=False), nullable=True), - sa.Column('name', sa.String(), nullable=False), - sa.Column('last_edit_date', sa.DateTime(), nullable=False), - sa.Column('gpxFileName', sa.String(), nullable=True), - sa.Column('user_id', sa.Integer(), nullable=False), - sa.ForeignKeyConstraint( - ['user_id'], - ['user.id'], - ), - sa.PrimaryKeyConstraint('id'), - ) + inspector = Inspector.from_engine(op.get_bind().engine) + tableNames = inspector.get_table_names() + + if 'planned_tour' not in tableNames: + op.create_table( + 'planned_tour', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('type', postgresql.ENUM(name='tracktype', create_type=False), nullable=True), + sa.Column('name', sa.String(), nullable=False), + sa.Column('last_edit_date', sa.DateTime(), nullable=False), + sa.Column('gpxFileName', sa.String(), nullable=True), + sa.Column('user_id', sa.Integer(), nullable=False), + sa.ForeignKeyConstraint( + ['user_id'], + ['user.id'], + ), + sa.PrimaryKeyConstraint('id'), + ) def downgrade():