# Buggy code
def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "user_detail",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("fullname", sa.String()),
        sa.Column("avatar", sa.String()),
        sa.Column("contact", sa.String()),
        sa.Column("facebook", sa.String()),
        sa.Column("twitter", sa.String()),
        sa.Column("user_id", sa.Integer(), nullable=True),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["user.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )


# Traceback (sqlalchemy.exc.ProgrammingError)
INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade  -> 30ca70296a1c, empty message
INFO  [alembic.runtime.migration] Running upgrade 30ca70296a1c -> 202b15987640, empty message
INFO  [alembic.runtime.migration] Running upgrade  -> 072706cab017, empty message
INFO  [alembic.runtime.migration] Running upgrade 072706cab017 -> 3b55af20871a, empty message
INFO  [alembic.runtime.migration] Running upgrade 3b55af20871a, 202b15987640 -> 3750bc39e4de, empty message
INFO  [alembic.runtime.migration] Running upgrade 3750bc39e4de -> c3676a5f81de, empty message
INFO  [alembic.runtime.migration] Running upgrade c3676a5f81de -> dbb79ff12e45, empty message
INFO  [alembic.runtime.migration] Running upgrade dbb79ff12e45 -> 98166ca15055, empty message
INFO  [alembic.runtime.migration] Running upgrade 98166ca15055 -> 6097b0a20149, empty message
Traceback (most recent call last):
File "manage.py", line 21, in <module>
manager.run()
File "/app/.heroku/python/lib/python2.7/site-packages/flask_script/__init__.py", line 412, in run
result = self.handle(sys.argv[0], sys.argv[1:])
File "/app/.heroku/python/lib/python2.7/site-packages/flask_script/__init__.py", line 383, in handle
res = handle(*args, **config)
File "/app/.heroku/python/lib/python2.7/site-packages/flask_script/commands.py", line 216, in __call__
return self.run(*args, **kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/flask_migrate/__init__.py", line 235, in upgrade
command.upgrade(config, revision, sql=sql, tag=tag)
File "/app/.heroku/python/lib/python2.7/site-packages/alembic/command.py", line 174, in upgrade
script.run_env()
File "/app/.heroku/python/lib/python2.7/site-packages/alembic/script/base.py", line 397, in run_env
util.load_python_file(self.dir, 'env.py')
File "/app/.heroku/python/lib/python2.7/site-packages/alembic/util/pyfiles.py", line 93, in load_python_file
module = load_module_py(module_id, path)
File "/app/.heroku/python/lib/python2.7/site-packages/alembic/util/compat.py", line 79, in load_module_py
mod = imp.load_source(module_id, path, fp)
File "migrations/env.py", line 88, in <module>
run_migrations_online()
File "migrations/env.py", line 81, in run_migrations_online
context.run_migrations()
File "<string>", line 8, in run_migrations
File "/app/.heroku/python/lib/python2.7/site-packages/alembic/runtime/environment.py", line 797, in run_migrations
self.get_context().run_migrations(**kw)
File "/app/.heroku/python/lib/python2.7/site-packages/alembic/runtime/migration.py", line 312, in run_migrations
step.migration_fn(**kw)
File "/app/migrations/versions/6097b0a20149_.py", line 29, in upgrade
sa.PrimaryKeyConstraint('id')
File "<string>", line 8, in create_table
File "<string>", line 3, in create_table
File "/app/.heroku/python/lib/python2.7/site-packages/alembic/operations/ops.py", line 1098, in create_table
return operations.invoke(op)
File "/app/.heroku/python/lib/python2.7/site-packages/alembic/operations/base.py", line 318, in invoke
return fn(self, operation)
File "/app/.heroku/python/lib/python2.7/site-packages/alembic/operations/toimpl.py", line 101, in create_table
operations.impl.create_table(table)
File "/app/.heroku/python/lib/python2.7/site-packages/alembic/ddl/impl.py", line 194, in create_table
self._exec(schema.CreateTable(table))
File "/app/.heroku/python/lib/python2.7/site-packages/alembic/ddl/impl.py", line 118, in _exec
return conn.execute(construct, *multiparams, **params)
File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 914, in execute
return meth(self, multiparams, params)
File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/sql/ddl.py", line 68, in _execute_on_connection
return connection._execute_ddl(self, multiparams, params)
File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 968, in _execute_ddl
compiled
File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1146, in _execute_context
context)
File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1341, in _handle_dbapi_exception
exc_info
File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 202, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1139, in _execute_context
context)
File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 450, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) relation "user_detail" already exists
[SQL: '\nCREATE TABLE user_detail (\n\tid SERIAL NOT NULL, \n\tfullname VARCHAR, \n\tavatar VARCHAR, \n\tcontact VARCHAR, \n\tfacebook VARCHAR, \n\ttwitter VARCHAR, \n\tuser_id INTEGER, \n\tPRIMARY KEY (id), \n\tFOREIGN KEY(user_id) REFERENCES "user" (id)\n)\n\n']

# Fixed code
def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "user_detail",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("fullname", sa.String(), nullable=True),
        sa.Column("avatar", sa.String(), nullable=True),
        sa.Column("contact", sa.String(), nullable=True),
        sa.Column("facebook", sa.String(), nullable=True),
        sa.Column("twitter", sa.String(), nullable=True),
        sa.Column("user_id", sa.Integer(), nullable=True),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["user.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.add_column("events", sa.Column("closing_datetime", sa.DateTime(), nullable=True))