diff --git a/tests/resources/example_data.tar.gz b/tests/resources/example_data.tar.gz index 43a343d1..7d81c5cb 100644 Binary files a/tests/resources/example_data.tar.gz and b/tests/resources/example_data.tar.gz differ diff --git a/tests/test_assets.py b/tests/test_assets.py index d0819863..d9340b30 100644 --- a/tests/test_assets.py +++ b/tests/test_assets.py @@ -1313,6 +1313,15 @@ class TestAssetDBVersioning(ZiplineTestCase): def test_downgrade(self): # Attempt to downgrade a current assets db all the way down to v0 conn = self.engine.connect() + + # first downgrade to v3 + downgrade(self.engine, 3) + metadata = sa.MetaData(conn) + metadata.reflect(bind=self.engine) + check_version_info(metadata.tables['version_info'], 3) + self.assertFalse('exchange_full' in metadata.tables) + + # now go all the way to v0 downgrade(self.engine, 0) # Verify that the db version is now 0 diff --git a/zipline/assets/asset_db_migrations.py b/zipline/assets/asset_db_migrations.py index 0ea453b1..22d2180d 100644 --- a/zipline/assets/asset_db_migrations.py +++ b/zipline/assets/asset_db_migrations.py @@ -148,7 +148,6 @@ def _downgrade_v2(op): # Execute batch op to allow column modification in SQLite with op.batch_alter_table('equities') as batch_op: - batch_op.drop_column('auto_close_date') # Recreate indices after batch @@ -195,7 +194,7 @@ def _downgrade_v3(op): ) op.drop_table('equities') op.rename_table('_new_equities', 'equities') - # we need to make sure the indicies have the proper names after the rename + # we need to make sure the indices have the proper names after the rename op.create_index( 'ix_equities_company_symbol', 'equities', @@ -206,3 +205,25 @@ def _downgrade_v3(op): 'equities', ['fuzzy_symbol'], ) + + +@downgrades(4) +def _downgrade_v4(op): + """ + Downgrades assets db by copying the `exchange_full` column to `exchange`, + then dropping the `exchange_full` column. + """ + op.drop_index('ix_equities_fuzzy_symbol') + op.drop_index('ix_equities_company_symbol') + + op.execute("UPDATE equities SET exchange = exchange_full") + + with op.batch_alter_table('equities') as batch_op: + batch_op.drop_column('exchange_full') + + op.create_index('ix_equities_fuzzy_symbol', + table_name='equities', + columns=['fuzzy_symbol']) + op.create_index('ix_equities_company_symbol', + table_name='equities', + columns=['company_symbol']) diff --git a/zipline/assets/asset_db_schema.py b/zipline/assets/asset_db_schema.py index 3cff1668..6499c45f 100644 --- a/zipline/assets/asset_db_schema.py +++ b/zipline/assets/asset_db_schema.py @@ -6,7 +6,7 @@ import sqlalchemy as sa # assets database # NOTE: When upgrading this remember to add a downgrade in: # .asset_db_migrations -ASSET_DB_VERSION = 3 +ASSET_DB_VERSION = 4 def generate_asset_db_metadata(bind=None): @@ -55,6 +55,7 @@ def _equities_table_schema(metadata): sa.Column('first_traded', sa.Integer), sa.Column('auto_close_date', sa.Integer), sa.Column('exchange', sa.Text), + sa.Column('exchange_full', sa.Text) ) diff --git a/zipline/assets/asset_writer.py b/zipline/assets/asset_writer.py index d11df1c0..b1259169 100644 --- a/zipline/assets/asset_writer.py +++ b/zipline/assets/asset_writer.py @@ -41,7 +41,10 @@ _equities_defaults = { 'end_date': 2 ** 62 - 1, 'first_traded': None, 'auto_close_date': None, + # the canonical exchange name, like "NYSE" 'exchange': None, + # optional, something like "New York Stock Exchange" + 'exchange_full': None, } # Default values for the futures DataFrame