-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
AlterSequenceChange.java
175 lines (145 loc) · 5.56 KB
/
AlterSequenceChange.java
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
package liquibase.change.core;
import liquibase.change.*;
import liquibase.database.Database;
import liquibase.snapshot.SnapshotGeneratorFactory;
import liquibase.statement.SqlStatement;
import liquibase.statement.core.AlterSequenceStatement;
import liquibase.structure.core.Sequence;
import java.math.BigInteger;
/**
* Modifies properties of an existing sequence. StartValue is not allowed since we cannot alter the starting sequence number
*/
@DatabaseChange(name="alterSequence", description = "Alter properties of an existing sequence", priority = ChangeMetaData.PRIORITY_DEFAULT, appliesTo = "sequence")
public class AlterSequenceChange extends AbstractChange {
private String catalogName;
private String schemaName;
private String sequenceName;
private BigInteger incrementBy;
private BigInteger maxValue;
private BigInteger minValue;
private Boolean ordered;
private BigInteger cacheSize;
private Boolean cycle;
private String dataType;
@DatabaseChangeProperty(mustEqualExisting ="sequence.catalog", since = "3.0")
public String getCatalogName() {
return catalogName;
}
public void setCatalogName(String catalogName) {
this.catalogName = catalogName;
}
@DatabaseChangeProperty(mustEqualExisting ="sequence.schema")
public String getSchemaName() {
return schemaName;
}
public void setSchemaName(String schemaName) {
this.schemaName = schemaName;
}
@DatabaseChangeProperty(mustEqualExisting = "sequence")
public String getSequenceName() {
return sequenceName;
}
public void setSequenceName(String sequenceName) {
this.sequenceName = sequenceName;
}
@DatabaseChangeProperty(description = "New amount the sequence should increment by")
public BigInteger getIncrementBy() {
return incrementBy;
}
public void setIncrementBy(BigInteger incrementBy) {
this.incrementBy = incrementBy;
}
@DatabaseChangeProperty(description = "New maximum value for the sequence")
public BigInteger getMaxValue() {
return maxValue;
}
public void setMaxValue(BigInteger maxValue) {
this.maxValue = maxValue;
}
@DatabaseChangeProperty(description = "New minimum value for the sequence")
public BigInteger getMinValue() {
return minValue;
}
public void setMinValue(BigInteger minValue) {
this.minValue = minValue;
}
@DatabaseChangeProperty(description = "Does the sequence need to be guaranteed to be generated in the order of request?")
public Boolean isOrdered() {
return ordered;
}
public void setOrdered(Boolean ordered) {
this.ordered = ordered;
}
@DatabaseChangeProperty(description = "Change the cache size?")
public BigInteger getCacheSize() {
return cacheSize;
}
public void setCacheSize(BigInteger cacheSize) {
this.cacheSize = cacheSize;
}
public Boolean getCycle() {
return cycle;
}
public void setCycle(Boolean cycle) {
this.cycle = cycle;
}
public void setDataType(String dataType) {
this.dataType = dataType;
}
@DatabaseChangeProperty(description = "Data type of the sequence")
public String getDataType() {
return dataType;
}
@Override
public SqlStatement[] generateStatements(Database database) {
return new SqlStatement[] {
new AlterSequenceStatement(getCatalogName(), getSchemaName(), getSequenceName())
.setIncrementBy(getIncrementBy())
.setMaxValue(getMaxValue())
.setMinValue(getMinValue())
.setCacheSize(getCacheSize())
.setCycle(getCycle())
.setOrdered(isOrdered())
.setDataType(getDataType())
};
}
@Override
public ChangeStatus checkStatus(Database database) {
ChangeStatus result = new ChangeStatus();
try {
Sequence sequence = SnapshotGeneratorFactory.getInstance().createSnapshot(new Sequence(getCatalogName(), getSchemaName(), getSequenceName()), database);
if (sequence == null) {
return result.unknown("Sequence " + getSequenceName() + " does not exist");
}
if (getIncrementBy() != null) {
result.assertCorrect(getIncrementBy().equals(sequence.getIncrementBy()), "Increment by has a different value");
}
if (getMinValue() != null) {
result.assertCorrect(getMinValue().equals(sequence.getMinValue()), "Min Value is different");
}
if (getMaxValue() != null) {
result.assertCorrect(getMaxValue().equals(sequence.getMaxValue()), "Max Value is different");
}
if (isOrdered() != null) {
result.assertCorrect(isOrdered().equals(sequence.getOrdered()), "Max Value is different");
}
if (getCacheSize() != null) {
result.assertCorrect(getCacheSize().equals(sequence.getCacheSize()), "Cache size is different");
}
if (getDataType() != null) {
result.assertCorrect(getDataType().equals(sequence.getDataType()), "Data type is different");
}
} catch (Exception e) {
return result.unknown(e);
}
return result;
}
@Override
public String getConfirmationMessage() {
return "Sequence " + getSequenceName() + " altered";
}
@Override
public String getSerializedObjectNamespace() {
return STANDARD_CHANGELOG_NAMESPACE;
}
}