diff --git a/subunit2sql/migrations/versions/1679b5bc102c_add_subsecond_columns_to_test_runs_table.mysql_upgrade.sql b/subunit2sql/migrations/versions/1679b5bc102c_add_subsecond_columns_to_test_runs_table.mysql_upgrade.sql new file mode 100644 index 0000000..c66b5ca --- /dev/null +++ b/subunit2sql/migrations/versions/1679b5bc102c_add_subsecond_columns_to_test_runs_table.mysql_upgrade.sql @@ -0,0 +1,16 @@ +-- +-- This file is necessary because of an issue with a large MySQL DB having +-- issues with the alter table to add a column +-- + +CREATE TABLE test_runs_migration LIKE test_runs; +ALTER TABLE test_runs_migration ADD COLUMN start_time_microsecond INTEGER default 0; +ALTER TABLE test_runs_migration ADD COLUMN stop_time_microsecond INTEGER default 0; +LOCK TABLES test_runs write, test_runs_migration write, test_run_metadata write; +INSERT INTO test_runs_migration SELECT id, test_id, run_id, status, start_time, stop_time, MICROSECOND(start_time), MICROSECOND(stop_time) from test_runs; +ALTER TABLE test_run_metadata drop foreign key test_run_metadata_ibfk_1; +UNLOCK TABLES; +-- race condition here - but at worst you'll lose a microsecond of data as rename is very fast and atomic +RENAME TABLE test_runs to test_runs_old, test_runs_migration to test_runs; +ALTER TABLE test_run_metadata ADD CONSTRAINT test_run_metadata_ibfk_1 FOREIGN KEY(test_run_id) REFERENCES test_runs(id); +DROP TABLE test_runs_old; diff --git a/subunit2sql/migrations/versions/1679b5bc102c_add_subsecond_columns_to_test_runs_table.py b/subunit2sql/migrations/versions/1679b5bc102c_add_subsecond_columns_to_test_runs_table.py index 0a463e8..bf292ce 100644 --- a/subunit2sql/migrations/versions/1679b5bc102c_add_subsecond_columns_to_test_runs_table.py +++ b/subunit2sql/migrations/versions/1679b5bc102c_add_subsecond_columns_to_test_runs_table.py @@ -24,6 +24,9 @@ Create Date: 2015-02-27 18:39:13.275801 revision = '1679b5bc102c' down_revision = '5332fe255095' +import os + +from alembic import context from alembic import op from oslo.config import cfg from oslo.db.sqlalchemy import utils as db_utils @@ -37,22 +40,28 @@ CONF = cfg.CONF def upgrade(): - op.add_column('test_runs', sa.Column('start_time_microsecond', - sa.Integer(), default=0)) - op.add_column('test_runs', sa.Column('stop_time_microsecond', - sa.Integer(), default=0)) - if not CONF.disable_microsecond_data_migration: - session = db_api.get_session() - query = db_utils.model_query(models.TestRun, session).values( - models.TestRun.id, models.TestRun.start_time, - models.TestRun.stop_time) - for test_run in query: - start_micro = test_run[1].microsecond - stop_micro = test_run[2].microsecond - values = {'start_time_microsecond': start_micro, - 'stop_time_microsecond': stop_micro} - db_api.update_test_run(values, test_run[0], session) - session.close() + sql_path = os.path.realpath(__file__).split('.')[0] + '.mysql_upgrade.sql' + migration_context = context.get_context() + if migration_context.dialect.name == 'mysql': + with open(sql_path, 'r') as sql_file: + op.execute(sql_file.read()) + else: + op.add_column('test_runs', sa.Column('start_time_microsecond', + sa.Integer(), default=0)) + op.add_column('test_runs', sa.Column('stop_time_microsecond', + sa.Integer(), default=0)) + if not CONF.disable_microsecond_data_migration: + session = db_api.get_session() + query = db_utils.model_query(models.TestRun, session).values( + models.TestRun.id, models.TestRun.start_time, + models.TestRun.stop_time) + for test_run in query: + start_micro = test_run[1].microsecond + stop_micro = test_run[2].microsecond + values = {'start_time_microsecond': start_micro, + 'stop_time_microsecond': stop_micro} + db_api.update_test_run(values, test_run[0], session) + session.close() def downgrade():