Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPgDiff.py
More file actions
Latest commit
205 lines (171 loc) · 10.1 KB
/
Copy pathPgDiff.py
File metadata and controls
205 lines (171 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
importargparse
importlogging
from ..helpers.WriterimportWriter
from ..loaders.PgDumpLoaderimportPgDumpLoader
from ..diff.PgDiffUtilsimportPgDiffUtils
from .SearchPathHelperimportSearchPathHelper
from ..diff.PgDiffTablesimportPgDiffTables
from ..diff.PgDiffTriggersimportPgDiffTriggers
from ..diff.PgDiffViewsimportPgDiffViews
from ..diff.PgDiffConstraintsimportPgDiffConstraints
from ..diff.PgDiffIndexesimportPgDiffIndexes
from ..diff.PgDiffSequencesimportPgDiffSequences
from ..diff.PgDiffFunctionsimportPgDiffFunctions
classPgDiff(object):
@staticmethod
defcreate_diff(writer, arguments):
old_database=PgDumpLoader.loadDatabaseSchema(arguments.old_dump)
new_database=PgDumpLoader.loadDatabaseSchema(arguments.new_dump)
PgDiff.diff_database_schemas(writer, arguments, old_database, new_database)
@staticmethod
defdiff_database_schemas(writer, arguments, old_database, new_database):
ifarguments.addTransaction:
writer.writeln("START TRANSACTION;")
if (old_database.commentisNone
andnew_database.commentisnotNone
orold_database.commentisnotNone
andnew_database.commentisnotNone
andold_database.comment!=new_database.comment):
writer.write("COMMENT ON DATABASE current_database() IS ")
writer.write(new_database.comment)
writer.writeln(";")
elifold_database.commentisnotNoneandnew_database.commentisNone:
writer.writeln("COMMENT ON DATABASE current_database() IS NULL;")
PgDiff.drop_old_schemas(writer, old_database, new_database)
PgDiff.create_new_schemas(writer, old_database, new_database)
PgDiff.update_schemas(writer, arguments, old_database, new_database)
ifarguments.addTransaction:
writer.writeln("COMMIT TRANSACTION;")
# if (arguments.isOutputIgnoredStatements()) {
# if (!oldDatabase.getIgnoredStatements().isEmpty()) {
# writer.println();
# writer.print("/* ");
# writer.println(Resources.getString(
# "OriginalDatabaseIgnoredStatements"));
# for (final String statement :
# oldDatabase.getIgnoredStatements()) {
# writer.println();
# writer.println(statement);
# }
# writer.println("*/");
# }
# if (!newDatabase.getIgnoredStatements().isEmpty()) {
# writer.println();
# writer.print("/* ");
# writer.println(Resources.getString("NewDatabaseIgnoredStatements"));
# for (final String statement :
# newDatabase.getIgnoredStatements()) {
# writer.println();
# writer.println(statement);
# }
# writer.println("*/");
# }
# }
@staticmethod
defdrop_old_schemas(writer, old_database, new_database):
foroldSchemaNameinold_database.schemas:
ifnew_database.getSchema(oldSchemaName) isNone:
writer.writeln("DROP SCHEMA %s CASCADE;"%PgDiffUtils.getQuotedName(oldSchemaName))
@staticmethod
defcreate_new_schemas(writer, old_database, new_database):
fornewSchemaNameinnew_database.schemas:
ifold_database.getSchema(newSchemaName) isNone:
writer.writeln(new_database.schemas[newSchemaName].getCreationSQL())
@staticmethod
defupdate_schemas(writer, arguments, old_database, new_database):
# We set search path if more than one schemas or it's name is not public
set_search_path=len(new_database.schemas) >1ornew_database.schemas.itervalues().next().name!="public"
fornewSchemaNameinnew_database.schemas:
ifset_search_path:
search_path_helper=SearchPathHelper("SET search_path = %s, pg_catalog;"%
PgDiffUtils.getQuotedName(newSchemaName, True))
else:
search_path_helper=SearchPathHelper(None)
old_schema=old_database.schemas.get(newSchemaName)
new_schema=new_database.schemas[newSchemaName]
ifold_schemaisnotNone:
if (old_schema.commentisNone
andnew_schema.commentisnotNone
orold_schema.commentisnotNone
andnew_schema.commentisnotNone
andold_schema.comment!=new_schema.comment):
writer.write("COMMENT ON SCHEMA ")
writer.write(PgDiffUtils.getQuotedName(new_schema.name))
writer.write(" IS ")
writer.write(new_schema.comment)
writer.writeln(';')
elifold_schema.commentisnotNoneandnew_schema.commentisNone:
writer.write("COMMENT ON SCHEMA ")
writer.write(PgDiffUtils.getQuotedName(new_schema.name))
writer.writeln(" IS NULL;")
PgDiffTriggers.dropTriggers(writer, old_schema, new_schema, search_path_helper)
PgDiffFunctions.dropFunctions(writer, arguments, old_schema, new_schema, search_path_helper)
PgDiffViews.dropViews(writer, old_schema, new_schema, search_path_helper)
PgDiffConstraints.dropConstraints(writer, old_schema, new_schema, True, search_path_helper)
PgDiffConstraints.dropConstraints(writer, old_schema, new_schema, False, search_path_helper)
PgDiffIndexes.dropIndexes(writer, old_schema, new_schema, search_path_helper)
# # PgDiffTables.dropClusters(oldSchema, newSchema, search_path_helper)
PgDiffTables.dropTables(writer, old_schema, new_schema, search_path_helper)
PgDiffSequences.dropSequences(writer, old_schema, new_schema, search_path_helper)
PgDiffSequences.createSequences(writer, old_schema, new_schema, search_path_helper)
PgDiffSequences.alterSequences(writer, arguments, old_schema, new_schema, search_path_helper)
PgDiffTables.createTables(writer, old_schema, new_schema, search_path_helper)
PgDiffTables.alterTables(writer, arguments, old_schema, new_schema, search_path_helper)
PgDiffSequences.alterCreatedSequences(writer, old_schema, new_schema, search_path_helper)
PgDiffFunctions.createFunctions(writer, arguments, old_schema, new_schema, search_path_helper)
PgDiffConstraints.createConstraints(writer, old_schema, new_schema, True, search_path_helper)
PgDiffConstraints.createConstraints(writer, old_schema, new_schema, False, search_path_helper)
PgDiffIndexes.createIndexes(writer, old_schema, new_schema, search_path_helper)
# # PgDiffTables.createClusters(oldSchema, newSchema, search_path_helper)
PgDiffTriggers.createTriggers(writer, old_schema, new_schema, search_path_helper)
PgDiffViews.createViews(writer, old_schema, new_schema, search_path_helper)
PgDiffViews.alterViews(writer, old_schema, new_schema, search_path_helper)
PgDiffFunctions.alterComments(writer, old_schema, new_schema, search_path_helper)
PgDiffConstraints.alterComments(writer, old_schema, new_schema, search_path_helper)
PgDiffIndexes.alterComments(writer, old_schema, new_schema, search_path_helper)
PgDiffTriggers.alterComments(writer, old_schema, new_schema, search_path_helper)
classLogLevelAction(argparse.Action):
def__call__(self, parser, namespace, values, option_string=None):
ifvalues=='DEBUG':
setattr(namespace, self.dest, logging.DEBUG)
elifvalues=='INFO':
setattr(namespace, self.dest, logging.INFO)
elifvalues=='WARNING':
setattr(namespace, self.dest, logging.WARNING)
elifvalues=='ERROR':
setattr(namespace, self.dest, logging.ERROR)
elifvalues=='CRITICAL':
setattr(namespace, self.dest, logging.CRITICAL)
if__name__=="__main__":
parser=argparse.ArgumentParser(prog='PgDiffPy', usage='python PgDiff.py [options] <old_dump> <new_dump>')
parser.add_argument('old_dump')
parser.add_argument('new_dump')
parser.add_argument('--add-transaction', dest='addTransaction', action='store_true',
help="Adds START TRANSACTION and COMMIT TRANSACTION to the generated diff file")
parser.add_argument('--add-defaults', dest='addDefaults', action='store_true',
help="adds DEFAULT ... in case new column has NOT NULL constraint but no default value "
"(the default value is dropped later)")
parser.add_argument('--ignore-start-with', dest='ignoreStartWith', action='store_false',
help="ignores START WITH modifications on SEQUENCEs (default is not to ignore these changes)")
parser.add_argument('--ignore-function-whitespace', dest='ignoreFunctionWhitespace', action='store_true',
help="ignores multiple spaces and new lines when comparing content of functions\n\
\t- WARNING: this may cause functions to appear to be same in cases they are\n\
\tnot, so use this feature only if you know what you are doing")
parser.add_argument('--loglevel', dest='loglevel', action=LogLevelAction
, choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL']
, default=logging.ERROR, help="")
arguments=parser.parse_args()
logging.basicConfig(format=u'%(filename)s:%(lineno)d [%(levelname)s] %(message)s'
, level=arguments.loglevel)
writer=Writer()
try:
PgDiff.create_diff(writer, arguments)
print(writer)
exceptExceptionase:
ifarguments.loglevel==logging.DEBUG:
importsys
importtraceback
traceback.print_exception(*sys.exc_info())
else:
print('Error: %s'%e)
exit(1)