| 1 |
# Copyright (C) 2012-2013 Yahoo! Inc. All Rights Reserved. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
"""initial_logbook_details_tables |
| 16 |
|
| 17 |
Revision ID: 1cea328f0f65 |
| 18 |
Revises: None |
| 19 |
Create Date: 2013-08-23 11:41:49.207087 |
| 20 |
|
| 21 |
""" |
| 22 |
|
| 23 |
# revision identifiers, used by Alembic. |
| 24 |
revision = '1cea328f0f65' |
| 25 |
down_revision = None |
| 26 |
|
| 27 |
import logging |
| 28 |
|
| 29 |
from alembic import op |
| 30 |
import sqlalchemy as sa |
| 31 |
|
| 32 |
from taskflow.persistence.backends.sqlalchemy import tables |
| 33 |
|
| 34 |
LOG = logging.getLogger(__name__) |
| 35 |
|
| 36 |
|
| 37 |
def _get_indexes(): |
| 38 |
# Ensure all uuids are indexed since they are what is typically looked |
| 39 |
# up and fetched, so attempt to ensure that that is done quickly. |
| 40 |
indexes = [ |
| 41 |
{ |
| 42 |
'index_name': 'logbook_uuid_idx', |
| 43 |
'table_name': 'logbooks', |
| 44 |
'columns': ['uuid'], |
| 45 |
}, |
| 46 |
{ |
| 47 |
'index_name': 'flowdetails_uuid_idx', |
| 48 |
'table_name': 'flowdetails', |
| 49 |
'columns': ['uuid'], |
| 50 |
}, |
| 51 |
{ |
| 52 |
'index_name': 'taskdetails_uuid_idx', |
| 53 |
'table_name': 'taskdetails', |
| 54 |
'columns': ['uuid'], |
| 55 |
}, |
| 56 |
] |
| 57 |
return indexes |
| 58 |
|
| 59 |
|
| 60 |
def _get_foreign_keys(): |
| 61 |
f_keys = [ |
| 62 |
# Flow details uuid -> logbook parent uuid |
| 63 |
{ |
| 64 |
'constraint_name': 'flowdetails_ibfk_1', |
| 65 |
'source_table': 'flowdetails', |
| 66 |
'referent_table': 'logbooks', |
| 67 |
'local_cols': ['parent_uuid'], |
| 68 |
'remote_cols': ['uuid'], |
| 69 |
'ondelete': 'CASCADE', |
| 70 |
}, |
| 71 |
# Task details uuid -> flow details parent uuid |
| 72 |
{ |
| 73 |
'constraint_name': 'taskdetails_ibfk_1', |
| 74 |
'source_table': 'taskdetails', |
| 75 |
'referent_table': 'flowdetails', |
| 76 |
'local_cols': ['parent_uuid'], |
| 77 |
'remote_cols': ['uuid'], |
| 78 |
'ondelete': 'CASCADE', |
| 79 |
}, |
| 80 |
] |
| 81 |
return f_keys |
| 82 |
|
| 83 |
|
| 84 |
def upgrade(): |
| 85 |
op.create_table('logbooks', |
| 86 |
sa.Column('created_at', sa.DateTime), |
| 87 |
sa.Column('updated_at', sa.DateTime), |
| 88 |
sa.Column('meta', sa.Text(), nullable=True), |
| 89 |
sa.Column('name', sa.String(length=tables.NAME_LENGTH), |
| 90 |
nullable=True), |
| 91 |
sa.Column('uuid', sa.String(length=tables.UUID_LENGTH), |
| 92 |
primary_key=True, nullable=False), |
| 93 |
mysql_engine='InnoDB', |
| 94 |
mysql_charset='utf8') |
| 95 |
op.create_table('flowdetails', |
| 96 |
sa.Column('created_at', sa.DateTime), |
| 97 |
sa.Column('updated_at', sa.DateTime), |
| 98 |
sa.Column('parent_uuid', |
| 99 |
sa.String(length=tables.UUID_LENGTH)), |
| 100 |
sa.Column('meta', sa.Text(), nullable=True), |
| 101 |
sa.Column('state', sa.String(length=tables.STATE_LENGTH), |
| 102 |
nullable=True), |
| 103 |
sa.Column('name', sa.String(length=tables.NAME_LENGTH), |
| 104 |
nullable=True), |
| 105 |
sa.Column('uuid', sa.String(length=tables.UUID_LENGTH), |
| 106 |
primary_key=True, nullable=False), |
| 107 |
mysql_engine='InnoDB', |
| 108 |
mysql_charset='utf8') |
| 109 |
op.create_table('taskdetails', |
| 110 |
sa.Column('created_at', sa.DateTime), |
| 111 |
sa.Column('updated_at', sa.DateTime), |
| 112 |
sa.Column('parent_uuid', |
| 113 |
sa.String(length=tables.UUID_LENGTH)), |
| 114 |
sa.Column('meta', sa.Text(), nullable=True), |
| 115 |
sa.Column('name', sa.String(length=tables.NAME_LENGTH), |
| 116 |
nullable=True), |
| 117 |
sa.Column('results', sa.Text(), nullable=True), |
| 118 |
sa.Column('version', |
| 119 |
sa.String(length=tables.VERSION_LENGTH), |
| 120 |
nullable=True), |
| 121 |
sa.Column('stacktrace', sa.Text(), nullable=True), |
| 122 |
sa.Column('exception', sa.Text(), nullable=True), |
| 123 |
sa.Column('state', sa.String(length=tables.STATE_LENGTH), |
| 124 |
nullable=True), |
| 125 |
sa.Column('uuid', sa.String(length=tables.UUID_LENGTH), |
| 126 |
primary_key=True, nullable=False), |
| 127 |
mysql_engine='InnoDB', |
| 128 |
mysql_charset='utf8') |
| 129 |
try: |
| 130 |
for fkey_descriptor in _get_foreign_keys(): |
| 131 |
op.create_foreign_key(**fkey_descriptor) |
| 132 |
except NotImplementedError as e: |
| 133 |
LOG.warning("Foreign keys are not supported: %s", e) |
| 134 |
try: |
| 135 |
for index_descriptor in _get_indexes(): |
| 136 |
op.create_index(**index_descriptor) |
| 137 |
except NotImplementedError as e: |
| 138 |
LOG.warning("Indexes are not supported: %s", e) |
| 139 |
|
| 140 |
|
| 141 |
def downgrade(): |
| 142 |
for table in ['logbooks', 'flowdetails', 'taskdetails']: |
| 143 |
op.drop_table(table) |