Hard code the sql for mysql on migration 1679b5bc102

Due to some issues with trying to execute migration 1679b5bc102 with
the alembic operations on the production infra subunit2sql DB this
commit adds some explicit SQL commands to achieve the same end goal,
but bypass the issues.

Co-Authored-By: Monty Taylor <mordred@inaugust.com>
Change-Id: Ie7fee27dd4eaa5cbdb24a0578274439227e9f1bb
This commit is contained in:
Matthew Treinish
2015-03-16 15:37:36 -04:00
parent dd271423c2
commit 86b867521e
2 changed files with 41 additions and 16 deletions

View File

@@ -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;

View File

@@ -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():