Repository: fossasia/open-event-server

Issue:
Handle invalid data while change data types during migrations
**Related error:**

```
sqlalchemy.exc.IntegrityError: (psycopg2.IntegrityError) column "pdf_url" contains null values
[SQL: 'ALTER TABLE ticket_holders ADD COLUMN pdf_url VARCHAR NOT NULL']
```

Due to this, migration are not running on `api.eventyay.com`.

Ensure existing data (valid or not) is handled properly and also ensure columns have proper null setting. (for example here, make pdf_url nullable.)
Similarly check and ensure other columns as well


**Full log attached for reference:**

```
2017-06-28T05:49:05.921467228Z     run_migrations_online()
2017-06-28T05:49:05.921470119Z   File "migrations/env.py", line 151, in run_migrations_online
2017-06-28T05:49:05.921473075Z     context.run_migrations()
2017-06-28T05:49:05.921475912Z   File "<string>", line 8, in run_migrations
2017-06-28T05:49:05.921479088Z   File "/usr/local/lib/python2.7/site-packages/alembic/runtime/environment.py", line 817, in run_migrations
2017-06-28T05:49:05.922610188Z     self.get_context().run_migrations(**kw)
2017-06-28T05:49:05.922642643Z   File "/usr/local/lib/python2.7/site-packages/alembic/runtime/migration.py", line 329, in run_migrations
2017-06-28T05:49:05.923243091Z     step.migration_fn(**kw)
2017-06-28T05:49:05.923274018Z   File "/opev/open_event/migrations/versions/c6b183975be9_.py", line 20, in upgrade
2017-06-28T05:49:05.923338965Z     op.add_column('ticket_holders', sa.Column('pdf_url', sa.String(), nullable=False))
2017-06-28T05:49:05.923364414Z   File "<string>", line 8, in add_column
2017-06-28T05:49:05.923427981Z   File "<string>", line 3, in add_column
2017-06-28T05:49:05.923478561Z   File "/usr/local/lib/python2.7/site-packages/alembic/operations/ops.py", line 1551, in add_column
2017-06-28T05:49:05.926653441Z     return operations.invoke(op)
2017-06-28T05:49:05.926712969Z   File "/usr/local/lib/python2.7/site-packages/alembic/operations/base.py", line 318, in invoke
2017-06-28T05:49:05.927353504Z     return fn(self, operation)
2017-06-28T05:49:05.927386093Z   File "/usr/local/lib/python2.7/site-packages/alembic/operations/toimpl.py", line 123, in add_column
2017-06-28T05:49:05.927969743Z     schema=schema
2017-06-28T05:49:05.927998827Z   File "/usr/local/lib/python2.7/site-packages/alembic/ddl/impl.py", line 172, in add_column
2017-06-28T05:49:05.92861825Z     self._exec(base.AddColumn(table_name, column, schema=schema))
2017-06-28T05:49:05.92864919Z   File "/usr/local/lib/python2.7/site-packages/alembic/ddl/impl.py", line 118, in _exec
2017-06-28T05:49:05.928742734Z     return conn.execute(construct, *multiparams, **params)
2017-06-28T05:49:05.928786028Z   File "/usr/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 945, in execute
2017-06-28T05:49:05.929323557Z     return meth(self, multiparams, params)
2017-06-28T05:49:05.929333376Z   File "/usr/local/lib/python2.7/site-packages/sqlalchemy/sql/ddl.py", line 68, in _execute_on_connection
2017-06-28T05:49:05.929336744Z     return connection._execute_ddl(self, multiparams, params)
2017-06-28T05:49:05.929339861Z   File "/usr/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1002, in _execute_ddl
2017-06-28T05:49:05.929512493Z     compiled
2017-06-28T05:49:05.929538983Z   File "/usr/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1189, in _execute_context
2017-06-28T05:49:05.929768208Z     context)
2017-06-28T05:49:05.929810133Z   File "/usr/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1402, in _handle_dbapi_exception
2017-06-28T05:49:05.930067186Z     exc_info
2017-06-28T05:49:05.930093798Z   File "/usr/local/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause
2017-06-28T05:49:05.930194023Z     reraise(type(exception), exception, tb=exc_tb, cause=cause)
2017-06-28T05:49:05.930237111Z   File "/usr/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
2017-06-28T05:49:05.931176835Z     context)
2017-06-28T05:49:05.931187737Z   File "/usr/local/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 470, in do_execute
2017-06-28T05:49:05.931191369Z     cursor.execute(statement, parameters)
2017-06-28T05:49:05.931334485Z sqlalchemy.exc.IntegrityError: (psycopg2.IntegrityError) column "pdf_url" contains null values
2017-06-28T05:49:05.931342263Z  [SQL: 'ALTER TABLE ticket_holders ADD COLUMN pdf_url VARCHAR NOT NULL']
```




Discussion:

Resolving patch:
diff --git a/migrations/versions/c6b183975be9_.py b/migrations/versions/c6b183975be9_.py
--- a/migrations/versions/c6b183975be9_.py
+++ b/migrations/versions/c6b183975be9_.py
@@ -17,7 +17,7 @@
 
 def upgrade():
     # ### commands auto generated by Alembic - please adjust! ###
-    op.add_column('ticket_holders', sa.Column('pdf_url', sa.String(), nullable=False))
+    op.add_column('ticket_holders', sa.Column('pdf_url', sa.String(), nullable=True))
     # ### end Alembic commands ###
 
 


Test patch: