Repository: rucio/rucio
Issue: alembic cli not working

Motivation
----------
On next branch, the alembic CLI seems to not work anymore with the upgrade from version 0.9 to 0.11.
```
alembic -c etc/alembic.ini history

 File "/usr/lib/python2.7/site-packages/alembic/util/langhelpers.py", line 83, in _name_error
    % (name, cls.__name__)
NameError: Can't invoke function 'get_context', as the proxy object has not yet been established for the Alembic 'EnvironmentContext' class.  Try placing this code inside a callable.
```


Modification
------------
Seems like all the schema declarations have to be moved to the upgrade/downgrade functions



Changed files:
lib/rucio/db/sqla/migrate_repo/versions/01eaf73ab656_add_new_rule_notification_state_progress.py
lib/rucio/db/sqla/migrate_repo/versions/21d6b9dc9961_add_mismatch_scheme_state_to_requests.py
lib/rucio/db/sqla/migrate_repo/versions/58c8b78301ab_rename_callback_to_message.py
lib/rucio/db/sqla/migrate_repo/versions/bb695f45c04_extend_request_state.py
lib/rucio/db/sqla/migrate_repo/versions/3c9df354071b_extend_waiting_request_state.py

diff --git a/lib/rucio/db/sqla/migrate_repo/versions/01eaf73ab656_add_new_rule_notification_state_progress.py b/lib/rucio/db/sqla/migrate_repo/versions/01eaf73ab656_add_new_rule_notification_state_progress.py
index d69e1a90c..01bd236ba 100644
--- a/lib/rucio/db/sqla/migrate_repo/versions/01eaf73ab656_add_new_rule_notification_state_progress.py
+++ b/lib/rucio/db/sqla/migrate_repo/versions/01eaf73ab656_add_new_rule_notification_state_progress.py
@@ -27,13 +27,12 @@ from alembic.op import create_check_constraint, drop_constraint, execute
 revision = '01eaf73ab656'
 down_revision = '90f47792bb76'
 
-schema = context.get_context().version_table_schema + '.' if context.get_context().version_table_schema else ''
-
 
 def upgrade():
     '''
     Upgrade the database to this revision
     '''
+    schema = context.get_context().version_table_schema + '.' if context.get_context().version_table_schema else ''
 
     if context.get_context().dialect.name in ['oracle', 'mysql']:
         drop_constraint('RULES_NOTIFICATION_CHK', 'rules', type_='check')
@@ -50,6 +49,7 @@ def downgrade():
     '''
     Downgrade the database to the previous revision
     '''
+    schema = context.get_context().version_table_schema + '.' if context.get_context().version_table_schema else ''
 
     if context.get_context().dialect.name in ['oracle', 'mysql']:
         drop_constraint(constraint_name='RULES_NOTIFICATION_CHK', table_name='rules', type_='check')
diff --git a/lib/rucio/db/sqla/migrate_repo/versions/21d6b9dc9961_add_mismatch_scheme_state_to_requests.py b/lib/rucio/db/sqla/migrate_repo/versions/21d6b9dc9961_add_mismatch_scheme_state_to_requests.py
index c748e2f59..7c11c48a3 100644
--- a/lib/rucio/db/sqla/migrate_repo/versions/21d6b9dc9961_add_mismatch_scheme_state_to_requests.py
+++ b/lib/rucio/db/sqla/migrate_repo/versions/21d6b9dc9961_add_mismatch_scheme_state_to_requests.py
@@ -27,8 +27,6 @@ from alembic.op import create_check_constraint, drop_constraint
 revision = '21d6b9dc9961'
 down_revision = '5f139f77382a'
 
-schema = context.get_context().version_table_schema + '.' if context.get_context().version_table_schema else ''
-
 
 def upgrade():
     '''
@@ -45,6 +43,7 @@ def downgrade():
     '''
     Downgrade the database to the previous revision
     '''
+    schema = context.get_context().version_table_schema + '.' if context.get_context().version_table_schema else ''
 
     if context.get_context().dialect.name in ['oracle', 'mysql']:
         drop_constraint('REQUESTS_STATE_CHK', 'requests', type_='check')
diff --git a/lib/rucio/db/sqla/migrate_repo/versions/3c9df354071b_extend_waiting_request_state.py b/lib/rucio/db/sqla/migrate_repo/versions/3c9df354071b_extend_waiting_request_state.py
index dfdab23e3..ea5186b76 100644
--- a/lib/rucio/db/sqla/migrate_repo/versions/3c9df354071b_extend_waiting_request_state.py
+++ b/lib/rucio/db/sqla/migrate_repo/versions/3c9df354071b_extend_waiting_request_state.py
@@ -28,8 +28,6 @@ from alembic.op import create_check_constraint, drop_constraint
 revision = '3c9df354071b'
 down_revision = '2edee4a83846'
 
-schema = context.get_context().version_table_schema if context.get_context().version_table_schema else ''
-
 
 def upgrade():
     '''
diff --git a/lib/rucio/db/sqla/migrate_repo/versions/58c8b78301ab_rename_callback_to_message.py b/lib/rucio/db/sqla/migrate_repo/versions/58c8b78301ab_rename_callback_to_message.py
index f27e33ea2..5a705d0cf 100644
--- a/lib/rucio/db/sqla/migrate_repo/versions/58c8b78301ab_rename_callback_to_message.py
+++ b/lib/rucio/db/sqla/migrate_repo/versions/58c8b78301ab_rename_callback_to_message.py
@@ -28,13 +28,12 @@ from alembic.op import (create_primary_key, create_check_constraint,
 revision = '58c8b78301ab'
 down_revision = '2b8e7bcb4783'
 
-schema = context.get_context().version_table_schema if context.get_context().version_table_schema else ''
-
 
 def upgrade():
     '''
     Upgrade the database to this revision
     '''
+    schema = context.get_context().version_table_schema if context.get_context().version_table_schema else ''
 
     if context.get_context().dialect.name in ['oracle', 'mysql']:
         drop_constraint('callbacks_pk', 'callbacks', type_='primary')
@@ -59,6 +58,7 @@ def downgrade():
     '''
     Downgrade the database to the previous revision
     '''
+    schema = context.get_context().version_table_schema if context.get_context().version_table_schema else ''
 
     if context.get_context().dialect.name in ['oracle', 'mysql']:
         drop_constraint('MESSAGES_EVENT_TYPE_NN', 'messages', type_='check')
diff --git a/lib/rucio/db/sqla/migrate_repo/versions/bb695f45c04_extend_request_state.py b/lib/rucio/db/sqla/migrate_repo/versions/bb695f45c04_extend_request_state.py
index 5981392c6..b3976fc0e 100644
--- a/lib/rucio/db/sqla/migrate_repo/versions/bb695f45c04_extend_request_state.py
+++ b/lib/rucio/db/sqla/migrate_repo/versions/bb695f45c04_extend_request_state.py
@@ -31,13 +31,12 @@ from alembic.op import (add_column, create_check_constraint,
 revision = 'bb695f45c04'
 down_revision = '3082b8cef557'
 
-schema = context.get_context().version_table_schema if context.get_context().version_table_schema else ''
-
 
 def upgrade():
     '''
     Upgrade the database to this revision
     '''
+    schema = context.get_context().version_table_schema if context.get_context().version_table_schema else ''
 
     if context.get_context().dialect.name in ['oracle', 'postgresql', 'mysql']:
         drop_constraint('REQUESTS_STATE_CHK', 'requests', type_='check')
@@ -51,6 +50,7 @@ def downgrade():
     '''
     Downgrade the database to the previous revision
     '''
+    schema = context.get_context().version_table_schema if context.get_context().version_table_schema else ''
 
     if context.get_context().dialect.name in ['oracle', 'mysql', 'postgresql']:
         drop_constraint('REQUESTS_STATE_CHK', 'requests', type_='check')