-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPgDiff.py
205 lines (171 loc) · 10.1 KB
/
PgDiff.py
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
import argparse
import logging
from ..helpers.Writer import Writer
from ..loaders.PgDumpLoader import PgDumpLoader
from ..diff.PgDiffUtils import PgDiffUtils
from .SearchPathHelper import SearchPathHelper
from ..diff.PgDiffTables import PgDiffTables
from ..diff.PgDiffTriggers import PgDiffTriggers
from ..diff.PgDiffViews import PgDiffViews
from ..diff.PgDiffConstraints import PgDiffConstraints
from ..diff.PgDiffIndexes import PgDiffIndexes
from ..diff.PgDiffSequences import PgDiffSequences
from ..diff.PgDiffFunctions import PgDiffFunctions
class PgDiff(object):
@staticmethod
def create_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
def diff_database_schemas(writer, arguments, old_database, new_database):
if arguments.addTransaction:
writer.writeln("START TRANSACTION;")
if (old_database.comment is None
and new_database.comment is not None
or old_database.comment is not None
and new_database.comment is not None
and old_database.comment != new_database.comment):
writer.write("COMMENT ON DATABASE current_database() IS ")
writer.write(new_database.comment)
writer.writeln(";")
elif old_database.comment is not None and new_database.comment is None:
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)
if arguments.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
def drop_old_schemas(writer, old_database, new_database):
for oldSchemaName in old_database.schemas:
if new_database.getSchema(oldSchemaName) is None:
writer.writeln("DROP SCHEMA %s CASCADE;" % PgDiffUtils.getQuotedName(oldSchemaName))
@staticmethod
def create_new_schemas(writer, old_database, new_database):
for newSchemaName in new_database.schemas:
if old_database.getSchema(newSchemaName) is None:
writer.writeln(new_database.schemas[newSchemaName].getCreationSQL())
@staticmethod
def update_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) > 1 or new_database.schemas.itervalues().next().name != "public"
for newSchemaName in new_database.schemas:
if set_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]
if old_schema is not None:
if (old_schema.comment is None
and new_schema.comment is not None
or old_schema.comment is not None
and new_schema.comment is not None
and old_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(';')
elif old_schema.comment is not None and new_schema.comment is None:
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)
class LogLevelAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
if values == 'DEBUG':
setattr(namespace, self.dest, logging.DEBUG)
elif values == 'INFO':
setattr(namespace, self.dest, logging.INFO)
elif values == 'WARNING':
setattr(namespace, self.dest, logging.WARNING)
elif values == 'ERROR':
setattr(namespace, self.dest, logging.ERROR)
elif values == '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)
except Exception as e:
if arguments.loglevel == logging.DEBUG:
import sys
import traceback
traceback.print_exception(*sys.exc_info())
else:
print('Error: %s' % e)
exit(1)