-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPgDiffSequences.py
More file actions
117 lines (90 loc) · 4.68 KB
/
PgDiffSequences.py
File metadata and controls
117 lines (90 loc) · 4.68 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
from PgDiffUtils import PgDiffUtils
class PgDiffSequences(object):
@staticmethod
def dropSequences(writer, oldSchema, newSchema, searchPathHelper):
if oldSchema is None:
return
# Drop sequences that do not exist in new schema
for sequenceName in oldSchema.sequences:
if sequenceName not in newSchema.sequences:
searchPathHelper.outputSearchPath(writer)
writer.writeln(oldSchema.sequences[sequenceName].getDropSQL())
@staticmethod
def createSequences(writer, oldSchema, newSchema, searchPathHelper):
# Add new sequences
for sequenceName in newSchema.sequences:
if oldSchema is None or sequenceName not in oldSchema.sequences:
searchPathHelper.outputSearchPath(writer)
writer.writeln(newSchema.sequences[sequenceName].getCreationSQL())
@staticmethod
def alterSequences(writer, arguments, oldSchema, newSchema, searchPathHelper):
if oldSchema is None:
return
for newSequenceName in newSchema.sequences:
oldSequence = oldSchema.sequences.get(newSequenceName)
newSequence = newSchema.sequences[newSequenceName]
if oldSequence is None:
continue
sbSQL = []
oldIncrement = oldSequence.increment
newIncrement = newSequence.increment
if (newIncrement is not None and oldIncrement != newIncrement):
sbSQL.append("\n\tINCREMENT BY ")
sbSQL.append(newIncrement)
oldMinValue = oldSequence.minValue
newMinValue = newSequence.minValue
if (newMinValue is None and oldMinValue is not None):
sbSQL.append("\n\tNO MINVALUE")
elif (newMinValue is not None and newMinValue != oldMinValue):
sbSQL.append("\n\tMINVALUE ")
sbSQL.append(newMinValue)
oldMaxValue = oldSequence.maxValue
newMaxValue = newSequence.maxValue
if (newMaxValue is None and oldMaxValue is not None):
sbSQL.append("\n\tNO MAXVALUE")
elif (newMaxValue is not None and newMaxValue != oldMaxValue):
sbSQL.append("\n\tMAXVALUE ")
sbSQL.append(newMaxValue)
if not arguments.ignoreStartWith:
oldStart = oldSequence.startWith
newStart = newSequence.startWith
if (newStart is not None and newStart != oldStart):
sbSQL.append("\n\tRESTART WITH ")
sbSQL.append(newStart)
oldCache = oldSequence.cache
newCache = newSequence.cache
if (newCache is not None and newCache != oldCache):
sbSQL.append("\n\tCACHE ")
sbSQL.append(newCache)
oldCycle = oldSequence.cycle
newCycle = newSequence.cycle
if (oldCycle and not newCycle):
sbSQL.append("\n\tNO CYCLE")
elif (not oldCycle and newCycle):
sbSQL.append("\n\tCYCLE")
oldOwnedBy = oldSequence.ownedBy
newOwnedBy = newSequence.ownedBy
if (newOwnedBy is not None and newOwnedBy != oldOwnedBy):
sbSQL.append("\n\tOWNED BY ")
sbSQL.append(newOwnedBy)
if len(sbSQL):
searchPathHelper.outputSearchPath(writer)
writer.writeln("ALTER SEQUENCE %s%s;" % (PgDiffUtils.getQuotedName(newSequence.name), ''.join(sbSQL)))
if (oldSequence.comment is None and newSequence.comment is not None
or oldSequence.comment is not None
and newSequence.comment is not None
and oldSequence.comment != newSequence.comment):
searchPathHelper.outputSearchPath(writer)
writer.writeln("COMMENT ON SEQUENCE %s IS %s;" % (PgDiffUtils.getQuotedName(newSequence.name), newSequence.comment))
elif (oldSequence.comment is not None and newSequence.comment is None):
searchPathHelper.outputSearchPath(writer)
writer.writeln("COMMENT ON SEQUENCE %s IS NULL;" % PgDiffUtils.getQuotedName(newSequence.name))
@staticmethod
def alterCreatedSequences(writer, oldSchema, newSchema, searchPathHelper):
# Alter created sequences
for sequenceName, sequence in newSchema.sequences.items():
if ((oldSchema is None or sequenceName not in oldSchema.sequences)
and sequence.ownedBy is not None
and len(sequence.ownedBy) > 0):
searchPathHelper.outputSearchPath(writer)
writer.writeln(sequence.getOwnedBySQL())