| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
import abc |
| 18 |
import functools |
| 19 |
import logging |
| 20 |
import pprint |
| 21 |
import re |
| 22 |
|
| 23 |
import alembic |
| 24 |
import alembic.autogenerate |
| 25 |
import alembic.migration |
| 26 |
import sqlalchemy |
| 27 |
import sqlalchemy.exc |
| 28 |
import sqlalchemy.sql.expression as expr |
| 29 |
import sqlalchemy.types as types |
| 30 |
|
| 31 |
from oslo_db.sqlalchemy import provision |
| 32 |
from oslo_db.sqlalchemy import utils |
| 33 |
|
| 34 |
LOG = logging.getLogger(__name__) |
| 35 |
|
| 36 |
|
| 37 |
class ModelsMigrationsSync(metaclass=abc.ABCMeta): |
| 38 |
"""A helper class for comparison of DB migration scripts and models. |
| 39 |
|
| 40 |
It's intended to be inherited by test cases in target projects. They have |
| 41 |
to provide implementations for methods used internally in the test (as |
| 42 |
we have no way to implement them here). |
| 43 |
|
| 44 |
test_model_sync() will run migration scripts for the engine provided and |
| 45 |
then compare the given metadata to the one reflected from the database. |
| 46 |
The difference between MODELS and MIGRATION scripts will be printed and |
| 47 |
the test will fail, if the difference is not empty. The return value is |
| 48 |
really a list of actions, that should be performed in order to make the |
| 49 |
current database schema state (i.e. migration scripts) consistent with |
| 50 |
models definitions. It's left up to developers to analyze the output and |
| 51 |
decide whether the models definitions or the migration scripts should be |
| 52 |
modified to make them consistent. |
| 53 |
|
| 54 |
Output:: |
| 55 |
|
| 56 |
[( |
| 57 |
'add_table', |
| 58 |
description of the table from models |
| 59 |
), |
| 60 |
( |
| 61 |
'remove_table', |
| 62 |
description of the table from database |
| 63 |
), |
| 64 |
( |
| 65 |
'add_column', |
| 66 |
schema, |
| 67 |
table name, |
| 68 |
column description from models |
| 69 |
), |
| 70 |
( |
| 71 |
'remove_column', |
| 72 |
schema, |
| 73 |
table name, |
| 74 |
column description from database |
| 75 |
), |
| 76 |
( |
| 77 |
'add_index', |
| 78 |
description of the index from models |
| 79 |
), |
| 80 |
( |
| 81 |
'remove_index', |
| 82 |
description of the index from database |
| 83 |
), |
| 84 |
( |
| 85 |
'add_constraint', |
| 86 |
description of constraint from models |
| 87 |
), |
| 88 |
( |
| 89 |
'remove_constraint, |
| 90 |
description of constraint from database |
| 91 |
), |
| 92 |
( |
| 93 |
'modify_nullable', |
| 94 |
schema, |
| 95 |
table name, |
| 96 |
column name, |
| 97 |
{ |
| 98 |
'existing_type': type of the column from database, |
| 99 |
'existing_server_default': default value from database |
| 100 |
}, |
| 101 |
nullable from database, |
| 102 |
nullable from models |
| 103 |
), |
| 104 |
( |
| 105 |
'modify_type', |
| 106 |
schema, |
| 107 |
table name, |
| 108 |
column name, |
| 109 |
{ |
| 110 |
'existing_nullable': database nullable, |
| 111 |
'existing_server_default': default value from database |
| 112 |
}, |
| 113 |
database column type, |
| 114 |
type of the column from models |
| 115 |
), |
| 116 |
( |
| 117 |
'modify_default', |
| 118 |
schema, |
| 119 |
table name, |
| 120 |
column name, |
| 121 |
{ |
| 122 |
'existing_nullable': database nullable, |
| 123 |
'existing_type': type of the column from database |
| 124 |
}, |
| 125 |
connection column default value, |
| 126 |
default from models |
| 127 |
)] |
| 128 |
|
| 129 |
Method include_object() can be overridden to exclude some tables from |
| 130 |
comparison (e.g. migrate_repo). |
| 131 |
|
| 132 |
""" |
| 133 |
|
| 134 |
@abc.abstractmethod |
| 135 |
def db_sync(self, engine): |
| 136 |
"""Run migration scripts with the given engine instance. |
| 137 |
|
| 138 |
This method must be implemented in subclasses and run migration scripts |
| 139 |
for a DB the given engine is connected to. |
| 140 |
|
| 141 |
""" |
| 142 |
|
| 143 |
@abc.abstractmethod |
| 144 |
def get_engine(self): |
| 145 |
"""Return the engine instance to be used when running tests. |
| 146 |
|
| 147 |
This method must be implemented in subclasses and return an engine |
| 148 |
instance to be used when running tests. |
| 149 |
|
| 150 |
""" |
| 151 |
|
| 152 |
@abc.abstractmethod |
| 153 |
def get_metadata(self): |
| 154 |
"""Return the metadata instance to be used for schema comparison. |
| 155 |
|
| 156 |
This method must be implemented in subclasses and return the metadata |
| 157 |
instance attached to the BASE model. |
| 158 |
|
| 159 |
""" |
| 160 |
|
| 161 |
def include_object(self, object_, name, type_, reflected, compare_to): |
| 162 |
"""Return True for objects that should be compared. |
| 163 |
|
| 164 |
:param object_: a SchemaItem object such as a Table or Column object |
| 165 |
:param name: the name of the object |
| 166 |
:param type_: a string describing the type of object (e.g. "table") |
| 167 |
:param reflected: True if the given object was produced based on |
| 168 |
table reflection, False if it's from a local |
| 169 |
MetaData object |
| 170 |
:param compare_to: the object being compared against, if available, |
| 171 |
else None |
| 172 |
|
| 173 |
""" |
| 174 |
|
| 175 |
return True |
| 176 |
|
| 177 |
def compare_type(self, ctxt, insp_col, meta_col, insp_type, meta_type): |
| 178 |
"""Return True if types are different, False if not. |
| 179 |
|
| 180 |
Return None to allow the default implementation to compare these types. |
| 181 |
|
| 182 |
:param ctxt: alembic MigrationContext instance |
| 183 |
:param insp_col: reflected column |
| 184 |
:param meta_col: column from model |
| 185 |
:param insp_type: reflected column type |
| 186 |
:param meta_type: column type from model |
| 187 |
|
| 188 |
""" |
| 189 |
|
| 190 |
|
| 191 |
BOOLEAN_METADATA = (types.BOOLEAN, types.Boolean) |
| 192 |
BOOLEAN_SQL = BOOLEAN_METADATA + (types.INTEGER, types.Integer) |
| 193 |
|
| 194 |
if issubclass(type(meta_type), BOOLEAN_METADATA): |
| 195 |
return not issubclass(type(insp_type), BOOLEAN_SQL) |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
if isinstance(meta_type, types.Variant): |
| 200 |
orig_type = meta_col.type |
| 201 |
impl_type = meta_type.load_dialect_impl(ctxt.dialect) |
| 202 |
meta_col.type = impl_type |
| 203 |
try: |
| 204 |
return self.compare_type(ctxt, insp_col, meta_col, insp_type, |
| 205 |
impl_type) |
| 206 |
finally: |
| 207 |
meta_col.type = orig_type |
| 208 |
|
| 209 |
return ctxt.impl.compare_type(insp_col, meta_col) |
| 210 |
|
| 211 |
def compare_server_default(self, ctxt, ins_col, meta_col, |
| 212 |
insp_def, meta_def, rendered_meta_def): |
| 213 |
"""Compare default values between model and db table. |
| 214 |
|
| 215 |
Return True if the defaults are different, False if not, or None to |
| 216 |
allow the default implementation to compare these defaults. |
| 217 |
|
| 218 |
:param ctxt: alembic MigrationContext instance |
| 219 |
:param insp_col: reflected column |
| 220 |
:param meta_col: column from model |
| 221 |
:param insp_def: reflected column default value |
| 222 |
:param meta_def: column default value from model |
| 223 |
:param rendered_meta_def: rendered column default value (from model) |
| 224 |
|
| 225 |
""" |
| 226 |
return self._compare_server_default(ctxt.bind, meta_col, insp_def, |
| 227 |
meta_def) |
| 228 |
|
| 229 |
@utils.DialectFunctionDispatcher.dispatch_for_dialect("*") |
| 230 |
def _compare_server_default(bind, meta_col, insp_def, meta_def): |
| 231 |
pass |
| 232 |
|
| 233 |
@_compare_server_default.dispatch_for('mysql') |
| 234 |
def _compare_server_default(bind, meta_col, insp_def, meta_def): |
| 235 |
if isinstance(meta_col.type, sqlalchemy.Boolean): |
| 236 |
if meta_def is None or insp_def is None: |
| 237 |
return meta_def != insp_def |
| 238 |
insp_def = insp_def.strip("'") |
| 239 |
return not ( |
| 240 |
isinstance(meta_def.arg, expr.True_) and insp_def == "1" or |
| 241 |
isinstance(meta_def.arg, expr.False_) and insp_def == "0" |
| 242 |
) |
| 243 |
|
| 244 |
if isinstance(meta_col.type, sqlalchemy.String): |
| 245 |
if meta_def is None or insp_def is None: |
| 246 |
return meta_def != insp_def |
| 247 |
insp_def = re.sub(r"^'|'$", "", insp_def) |
| 248 |
return meta_def.arg != insp_def |
| 249 |
|
| 250 |
def filter_metadata_diff(self, diff): |
| 251 |
"""Filter changes before assert in test_models_sync(). |
| 252 |
|
| 253 |
Allow subclasses to whitelist/blacklist changes. By default, no |
| 254 |
filtering is performed, changes are returned as is. |
| 255 |
|
| 256 |
:param diff: a list of differences (see `compare_metadata()` docs for |
| 257 |
details on format) |
| 258 |
:returns: a list of differences |
| 259 |
""" |
| 260 |
return diff |
| 261 |
|
| 262 |
def test_models_sync(self): |
| 263 |
|
| 264 |
engine = self.get_engine() |
| 265 |
backend = provision.Backend(engine.name, engine.url) |
| 266 |
self.addCleanup(functools.partial(backend.drop_all_objects, engine)) |
| 267 |
|
| 268 |
|
| 269 |
self.db_sync(self.get_engine()) |
| 270 |
|
| 271 |
with self.get_engine().connect() as conn: |
| 272 |
opts = { |
| 273 |
'include_object': self.include_object, |
| 274 |
'compare_type': self.compare_type, |
| 275 |
'compare_server_default': self.compare_server_default, |
| 276 |
} |
| 277 |
mc = alembic.migration.MigrationContext.configure(conn, opts=opts) |
| 278 |
|
| 279 |
|
| 280 |
diff = self.filter_metadata_diff( |
| 281 |
alembic.autogenerate.compare_metadata(mc, self.get_metadata())) |
| 282 |
if diff: |
| 283 |
msg = pprint.pformat(diff, indent=2, width=20) |
| 284 |
self.fail( |
| 285 |
"Models and migration scripts aren't in sync:\n%s" % msg) |