1 | |
package liquibase.precondition.core; |
2 | |
|
3 | |
import liquibase.changelog.ChangeSet; |
4 | |
import liquibase.changelog.DatabaseChangeLog; |
5 | |
import liquibase.database.Database; |
6 | |
import liquibase.exception.DatabaseException; |
7 | |
import liquibase.exception.PreconditionErrorException; |
8 | |
import liquibase.exception.PreconditionFailedException; |
9 | |
import liquibase.exception.ValidationErrors; |
10 | |
import liquibase.exception.Warnings; |
11 | |
import liquibase.precondition.Precondition; |
12 | |
import liquibase.snapshot.DatabaseSnapshot; |
13 | |
import liquibase.snapshot.DatabaseSnapshotGeneratorFactory; |
14 | |
import liquibase.util.StringUtils; |
15 | |
|
16 | 8 | public class SequenceExistsPrecondition implements Precondition { |
17 | |
private String schemaName; |
18 | |
private String sequenceName; |
19 | |
|
20 | |
public String getSchemaName() { |
21 | 0 | return schemaName; |
22 | |
} |
23 | |
|
24 | |
public void setSchemaName(String schemaName) { |
25 | 0 | this.schemaName = StringUtils.trimToNull(schemaName); |
26 | 0 | } |
27 | |
|
28 | |
public String getSequenceName() { |
29 | 0 | return sequenceName; |
30 | |
} |
31 | |
|
32 | |
public void setSequenceName(String sequenceName) { |
33 | 0 | this.sequenceName = sequenceName; |
34 | 0 | } |
35 | |
|
36 | |
@Override |
37 | |
public Warnings warn(Database database) { |
38 | 0 | return new Warnings(); |
39 | |
} |
40 | |
|
41 | |
@Override |
42 | |
public ValidationErrors validate(Database database) { |
43 | 0 | return new ValidationErrors(); |
44 | |
} |
45 | |
|
46 | |
@Override |
47 | |
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet) |
48 | |
throws PreconditionFailedException, PreconditionErrorException { |
49 | |
DatabaseSnapshot snapshot; |
50 | |
try { |
51 | 0 | snapshot = DatabaseSnapshotGeneratorFactory.getInstance().createSnapshot(database, getSchemaName(), null); |
52 | 0 | } catch (DatabaseException e) { |
53 | 0 | throw new PreconditionErrorException(e, changeLog, this); |
54 | 0 | } |
55 | 0 | if (snapshot.getSequence(getSequenceName()) == null) { |
56 | 0 | throw new PreconditionFailedException("Sequence " |
57 | |
+ database.escapeSequenceName(getSchemaName(), getSequenceName()) + " does not exist", changeLog, |
58 | |
this); |
59 | |
} |
60 | 0 | } |
61 | |
|
62 | |
@Override |
63 | |
public String getName() { |
64 | 8 | return "sequenceExists"; |
65 | |
} |
66 | |
} |