forked from liquibase/liquibase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RowCountPrecondition.java
102 lines (81 loc) · 3.1 KB
/
RowCountPrecondition.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
package liquibase.precondition.core;
import liquibase.Scope;
import liquibase.changelog.ChangeSet;
import liquibase.changelog.DatabaseChangeLog;
import liquibase.changelog.visitor.ChangeExecListener;
import liquibase.database.Database;
import liquibase.exception.PreconditionErrorException;
import liquibase.exception.PreconditionFailedException;
import liquibase.exception.ValidationErrors;
import liquibase.exception.Warnings;
import liquibase.executor.ExecutorService;
import liquibase.precondition.AbstractPrecondition;
import liquibase.statement.core.TableRowCountStatement;
import liquibase.util.StringUtil;
public class RowCountPrecondition extends AbstractPrecondition {
private String catalogName;
private String schemaName;
private String tableName;
private Integer expectedRows;
public String getCatalogName() {
return catalogName;
}
public void setCatalogName(String catalogName) {
this.catalogName = catalogName;
}
public String getSchemaName() {
return schemaName;
}
public void setSchemaName(String schemaName) {
this.schemaName = StringUtil.trimToNull(schemaName);
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public Integer getExpectedRows() {
return expectedRows;
}
public void setExpectedRows(Integer expectedRows) {
this.expectedRows = expectedRows;
}
@Override
public Warnings warn(Database database) {
return new Warnings();
}
@Override
public ValidationErrors validate(Database database) {
ValidationErrors validationErrors = new ValidationErrors();
validationErrors.checkRequiredField("tableName", tableName);
validationErrors.checkRequiredField("expectedRows", expectedRows);
return validationErrors;
}
@Override
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet, ChangeExecListener changeExecListener)
throws PreconditionFailedException, PreconditionErrorException {
try {
TableRowCountStatement statement = new TableRowCountStatement(catalogName, schemaName, tableName);
int result = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database).queryForInt(statement);
if (result != expectedRows) {
throw new PreconditionFailedException(getFailureMessage(result, expectedRows), changeLog, this);
}
} catch (PreconditionFailedException e) {
throw e;
} catch (Exception e) {
throw new PreconditionErrorException(e, changeLog, this);
}
}
protected String getFailureMessage(int result, int expectedRows) {
return "Table "+tableName+" does not have the expected row count of "+expectedRows+". It contains "+result+" rows";
}
@Override
public String getSerializedObjectNamespace() {
return STANDARD_CHANGELOG_NAMESPACE;
}
@Override
public String getName() {
return "rowCount";
}
}