Repository: fossasia/open-event-server

Issue:
[Sentry]: (psycopg2.ProgrammingError) relation "export_jobs" already exists 
```
ProgrammingError: (psycopg2.ProgrammingError) relation "export_jobs" already exists
 [SQL: '\nCREATE TABLE export_jobs (\n\tid SERIAL NOT NULL, \n\ttask VARCHAR NOT NULL, \n\tstarts_at TIMESTAMP WITH TIME ZONE, \n\tuser_email VARCHAR, \n\tevent_id INTEGER, \n\tPRIMARY KEY (id), \n\tFOREIGN KEY(event_id) REFERENCES events (id) ON DELETE CASCADE\n)\n\n']
(25 additional frame(s) were not displayed)
...
  File "sqlalchemy/engine/base.py", line 1189, in _execute_context
    context)
  File "sqlalchemy/engine/base.py", line 1402, in _handle_dbapi_exception
    exc_info
  File "sqlalchemy/util/compat.py", line 203, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "sqlalchemy/engine/base.py", line 1182, in _execute_context
    context)
  File "sqlalchemy/engine/default.py", line 470, in do_execute
    cursor.execute(statement, parameters)
```


Discussion:

Resolving patch:
diff --git a/migrations/versions/bf3e8c2a01f6_.py b/migrations/versions/bf3e8c2a01f6_.py
--- a/migrations/versions/bf3e8c2a01f6_.py
+++ b/migrations/versions/bf3e8c2a01f6_.py
@@ -10,39 +10,51 @@
 import sqlalchemy as sa
 import sqlalchemy_utils
 
+from sqlalchemy import engine_from_config
 
 # revision identifiers, used by Alembic.
 revision = 'bf3e8c2a01f6'
 down_revision = 'dbb9670cd902'
 
 
+def table_exists(table):
+    config = op.get_context().config
+    engine = engine_from_config(
+        config.get_section(config.config_ini_section), prefix='sqlalchemy.')
+    return engine.dialect.has_table(engine, table)
+
+
 def upgrade():
     # commands auto generated by Alembic - please adjust! #
-    op.create_table('export_jobs',
-    sa.Column('id', sa.Integer(), nullable=False),
-    sa.Column('task', sa.String(), nullable=False),
-    sa.Column('starts_at', sa.DateTime(timezone=True), nullable=True),
-    sa.Column('user_email', sa.String(), nullable=True),
-    sa.Column('event_id', sa.Integer(), nullable=True),
-    sa.ForeignKeyConstraint(['event_id'], ['events.id'], ondelete='CASCADE'),
-    sa.PrimaryKeyConstraint('id')
-    )
-    op.create_table('stripe_authorizations',
-    sa.Column('id', sa.Integer(), nullable=False),
-    sa.Column('stripe_secret_key', sa.String(), nullable=True),
-    sa.Column('stripe_refresh_token', sa.String(), nullable=True),
-    sa.Column('stripe_publishable_key', sa.String(), nullable=True),
-    sa.Column('stripe_user_id', sa.String(), nullable=True),
-    sa.Column('stripe_email', sa.String(), nullable=True),
-    sa.Column('event_id', sa.Integer(), nullable=True),
-    sa.ForeignKeyConstraint(['event_id'], ['events.id'], ondelete='CASCADE'),
-    sa.PrimaryKeyConstraint('id')
-    )
+    if not table_exists('export_jobs'):
+        op.create_table('export_jobs',
+        sa.Column('id', sa.Integer(), nullable=False),
+        sa.Column('task', sa.String(), nullable=False),
+        sa.Column('starts_at', sa.DateTime(timezone=True), nullable=True),
+        sa.Column('user_email', sa.String(), nullable=True),
+        sa.Column('event_id', sa.Integer(), nullable=True),
+        sa.ForeignKeyConstraint(['event_id'], ['events.id'], ondelete='CASCADE'),
+        sa.PrimaryKeyConstraint('id')
+        )
+    if not table_exists('stripe_authorizations'):
+        op.create_table('stripe_authorizations',
+        sa.Column('id', sa.Integer(), nullable=False),
+        sa.Column('stripe_secret_key', sa.String(), nullable=True),
+        sa.Column('stripe_refresh_token', sa.String(), nullable=True),
+        sa.Column('stripe_publishable_key', sa.String(), nullable=True),
+        sa.Column('stripe_user_id', sa.String(), nullable=True),
+        sa.Column('stripe_email', sa.String(), nullable=True),
+        sa.Column('event_id', sa.Integer(), nullable=True),
+        sa.ForeignKeyConstraint(['event_id'], ['events.id'], ondelete='CASCADE'),
+        sa.PrimaryKeyConstraint('id')
+        )
     # end Alembic commands #
 
 
 def downgrade():
     # commands auto generated by Alembic - please adjust! #
-    op.drop_table('stripe_authorizations')
-    op.drop_table('export_jobs')
+    if table_exists('stripe_authorizations'):
+        op.drop_table('stripe_authorizations')
+    if table_exists('export_jobs'):
+        op.drop_table('export_jobs')
     # end Alembic commands #


Test patch: