1
1
# -*- coding: utf-8 -*-
2
2
from copy import deepcopy
3
3
4
- from .util import TablesInfo , DiffResult , InspectorFactory , CompareResult
4
+ from .util import (
5
+ TablesInfo , DiffResult , InspectorFactory , CompareResult , IgnoreManager
6
+ )
5
7
6
8
7
- def compare (left_uri , right_uri , ignore_tables = None ):
9
+ def compare (left_uri , right_uri , ignores = None , ignores_sep = None ):
8
10
"""Compare two databases, given two URIs.
9
11
10
- Compare two databases, given two URIs and a (possibly empty) set of
11
- tables to ignore during the comparison.
12
+ Compare two databases, ignoring whatever is specified in `ignores`.
12
13
13
14
The ``info`` dict has this structure::
14
15
@@ -63,26 +64,34 @@ def compare(left_uri, right_uri, ignore_tables=None):
63
64
64
65
:param string left_uri: The URI for the first (left) database.
65
66
:param string right_uri: The URI for the second (right) database.
66
- :param set ignore_tables:
67
- A set of string values to be excluded from both databases (if
68
- present) when doing the comparison. String matching is case
69
- sensitive.
67
+ :param iterable ignores:
68
+ A list of strings in the format:
69
+ * `table-name`
70
+ * `table-name.identifier.name`
71
+
72
+ If a table name is specified, the whole table is excluded from
73
+ comparison. If a complete clause is specified, then only the
74
+ specified element is excluded from comparison. `identifier` is one
75
+ of (`col`, `pk`, `fk`, `idx`) and name is the name of the element
76
+ to be excluded from the comparison.
77
+ :param string ignores_sep:
78
+ Separator to be used to spilt the `ignores` clauses.
70
79
:return:
71
80
A :class:`~.util.CompareResult` object with ``info`` and
72
81
``errors`` dicts populated with the comparison result.
73
82
"""
74
- if ignore_tables is None :
75
- ignore_tables = set ()
83
+ ignore_manager = IgnoreManager (ignores , separator = ignores_sep )
76
84
77
85
left_inspector , right_inspector = _get_inspectors (left_uri , right_uri )
78
86
79
87
tables_info = _get_tables_info (
80
- left_inspector , right_inspector , ignore_tables )
88
+ left_inspector , right_inspector , ignore_manager . ignore_tables )
81
89
82
90
info = _get_info_dict (left_uri , right_uri , tables_info )
83
91
84
92
info ['tables_data' ] = _get_tables_data (
85
- tables_info .common , left_inspector , right_inspector )
93
+ tables_info .common , left_inspector , right_inspector , ignore_manager
94
+ )
86
95
87
96
errors = _compile_errors (info )
88
97
result = _make_result (info , errors )
@@ -157,32 +166,53 @@ def _get_info_dict(left_uri, right_uri, tables_info):
157
166
return info
158
167
159
168
160
- def _get_tables_data (tables_common , left_inspector , right_inspector ):
169
+ def _get_tables_data (
170
+ tables_common , left_inspector , right_inspector , ignore_manager
171
+ ):
161
172
tables_data = {}
162
173
163
174
for table_name in tables_common :
164
175
table_data = _get_table_data (
165
- left_inspector , right_inspector , table_name )
176
+ left_inspector , right_inspector , table_name , ignore_manager
177
+ )
166
178
tables_data [table_name ] = table_data
167
179
168
180
return tables_data
169
181
170
182
171
- def _get_table_data (left_inspector , right_inspector , table_name ):
183
+ def _get_table_data (
184
+ left_inspector , right_inspector , table_name , ignore_manager
185
+ ):
172
186
table_data = {}
173
187
174
188
# foreign keys
175
189
table_data ['foreign_keys' ] = _get_foreign_keys_info (
176
- left_inspector , right_inspector , table_name )
190
+ left_inspector ,
191
+ right_inspector ,
192
+ table_name ,
193
+ ignore_manager .get (table_name , 'fk' )
194
+ )
177
195
178
196
table_data ['primary_keys' ] = _get_primary_keys_info (
179
- left_inspector , right_inspector , table_name )
197
+ left_inspector ,
198
+ right_inspector ,
199
+ table_name ,
200
+ ignore_manager .get (table_name , 'pk' )
201
+ )
180
202
181
203
table_data ['indexes' ] = _get_indexes_info (
182
- left_inspector , right_inspector , table_name )
204
+ left_inspector ,
205
+ right_inspector ,
206
+ table_name ,
207
+ ignore_manager .get (table_name , 'idx' )
208
+ )
183
209
184
210
table_data ['columns' ] = _get_columns_info (
185
- left_inspector , right_inspector , table_name )
211
+ left_inspector ,
212
+ right_inspector ,
213
+ table_name ,
214
+ ignore_manager .get (table_name , 'col' )
215
+ )
186
216
187
217
return table_data
188
218
@@ -225,10 +255,15 @@ def _diff_dicts(left, right):
225
255
)._asdict ()
226
256
227
257
228
- def _get_foreign_keys_info (left_inspector , right_inspector , table_name ):
258
+ def _get_foreign_keys_info (
259
+ left_inspector , right_inspector , table_name , ignores
260
+ ):
229
261
left_fk_list = _get_foreign_keys (left_inspector , table_name )
230
262
right_fk_list = _get_foreign_keys (right_inspector , table_name )
231
263
264
+ left_fk_list = _discard_ignores_by_name (left_fk_list , ignores )
265
+ right_fk_list = _discard_ignores_by_name (right_fk_list , ignores )
266
+
232
267
# process into dict
233
268
left_fk = dict ((elem ['name' ], elem ) for elem in left_fk_list )
234
269
right_fk = dict ((elem ['name' ], elem ) for elem in right_fk_list )
@@ -240,10 +275,15 @@ def _get_foreign_keys(inspector, table_name):
240
275
return inspector .get_foreign_keys (table_name )
241
276
242
277
243
- def _get_primary_keys_info (left_inspector , right_inspector , table_name ):
278
+ def _get_primary_keys_info (
279
+ left_inspector , right_inspector , table_name , ignores
280
+ ):
244
281
left_pk_list = _get_primary_keys (left_inspector , table_name )
245
282
right_pk_list = _get_primary_keys (right_inspector , table_name )
246
283
284
+ left_pk_list = _discard_ignores (left_pk_list , ignores )
285
+ right_pk_list = _discard_ignores (right_pk_list , ignores )
286
+
247
287
# process into dict
248
288
left_pk = dict ((elem , elem ) for elem in left_pk_list )
249
289
right_pk = dict ((elem , elem ) for elem in right_pk_list )
@@ -255,10 +295,13 @@ def _get_primary_keys(inspector, table_name):
255
295
return inspector .get_primary_keys (table_name )
256
296
257
297
258
- def _get_indexes_info (left_inspector , right_inspector , table_name ):
298
+ def _get_indexes_info (left_inspector , right_inspector , table_name , ignores ):
259
299
left_index_list = _get_indexes (left_inspector , table_name )
260
300
right_index_list = _get_indexes (right_inspector , table_name )
261
301
302
+ left_index_list = _discard_ignores_by_name (left_index_list , ignores )
303
+ right_index_list = _discard_ignores_by_name (right_index_list , ignores )
304
+
262
305
# process into dict
263
306
left_index = dict ((elem ['name' ], elem ) for elem in left_index_list )
264
307
right_index = dict ((elem ['name' ], elem ) for elem in right_index_list )
@@ -270,10 +313,13 @@ def _get_indexes(inspector, table_name):
270
313
return inspector .get_indexes (table_name )
271
314
272
315
273
- def _get_columns_info (left_inspector , right_inspector , table_name ):
316
+ def _get_columns_info (left_inspector , right_inspector , table_name , ignores ):
274
317
left_columns_list = _get_columns (left_inspector , table_name )
275
318
right_columns_list = _get_columns (right_inspector , table_name )
276
319
320
+ left_columns_list = _discard_ignores_by_name (left_columns_list , ignores )
321
+ right_columns_list = _discard_ignores_by_name (right_columns_list , ignores )
322
+
277
323
# process into dict
278
324
left_columns = dict ((elem ['name' ], elem ) for elem in left_columns_list )
279
325
right_columns = dict ((elem ['name' ], elem ) for elem in right_columns_list )
@@ -289,6 +335,14 @@ def _get_columns(inspector, table_name):
289
335
return inspector .get_columns (table_name )
290
336
291
337
338
+ def _discard_ignores_by_name (items , ignores ):
339
+ return [item for item in items if item ['name' ] not in ignores ]
340
+
341
+
342
+ def _discard_ignores (items , ignores ):
343
+ return [item for item in items if item not in ignores ]
344
+
345
+
292
346
def _process_types (column_dict ):
293
347
for column in column_dict :
294
348
column_dict [column ]['type' ] = _process_type (
0 commit comments