|
19 | 19 | import liquibase.database.jvm.JdbcConnection; |
20 | 20 | import liquibase.exception.LiquibaseException; |
21 | 21 | import liquibase.resource.ClassLoaderResourceAccessor; |
| 22 | +import liquibase.resource.FileSystemResourceAccessor; |
| 23 | +import liquibase.resource.ResourceAccessor; |
22 | 24 |
|
23 | 25 | import javax.sql.DataSource; |
| 26 | +import java.io.File; |
24 | 27 | import java.sql.Connection; |
25 | 28 | import java.sql.SQLException; |
26 | 29 | import java.util.Objects; |
|
30 | 33 | public final class LiquibasePreparer implements DatabasePreparer { |
31 | 34 |
|
32 | 35 | private final String location; |
| 36 | + private final ResourceAccessor accessor; |
33 | 37 | private final Contexts contexts; |
34 | 38 |
|
35 | 39 | public static LiquibasePreparer forClasspathLocation(String location) { |
36 | | - return new LiquibasePreparer(location, new Contexts()); |
| 40 | + return forClasspathLocation(location, null); |
37 | 41 | } |
| 42 | + |
38 | 43 | public static LiquibasePreparer forClasspathLocation(String location, Contexts contexts) { |
39 | | - return new LiquibasePreparer(location, contexts); |
| 44 | + return new LiquibasePreparer(location, new ClassLoaderResourceAccessor(), contexts); |
| 45 | + } |
| 46 | + |
| 47 | + public static LiquibasePreparer forFile(File file) { |
| 48 | + return forFile(file, null); |
| 49 | + } |
| 50 | + |
| 51 | + public static LiquibasePreparer forFile(File file, Contexts contexts) { |
| 52 | + if (file == null) |
| 53 | + throw new IllegalArgumentException("Missing file"); |
| 54 | + File dir = file.getParentFile(); |
| 55 | + if (dir == null) |
| 56 | + throw new IllegalArgumentException("Cannot get parent dir from file"); |
| 57 | + |
| 58 | + return new LiquibasePreparer(file.getName(), new FileSystemResourceAccessor(dir), contexts); |
40 | 59 | } |
41 | 60 |
|
42 | | - private LiquibasePreparer(String location, Contexts contexts) { |
| 61 | + private LiquibasePreparer(String location, ResourceAccessor accessor, Contexts contexts) { |
43 | 62 | this.location = location; |
44 | | - this.contexts = contexts; |
| 63 | + this.accessor = accessor; |
| 64 | + this.contexts = contexts != null ? contexts : new Contexts(); |
45 | 65 | } |
46 | 66 |
|
47 | 67 | @Override |
48 | 68 | public void prepare(DataSource ds) throws SQLException { |
49 | | - Connection connection = null; |
50 | | - try { |
51 | | - connection = ds.getConnection(); |
| 69 | + try (Connection connection = ds.getConnection()) { |
52 | 70 | Database database = getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection)); |
53 | | - Liquibase liquibase = new Liquibase(location, new ClassLoaderResourceAccessor(), database); |
| 71 | + Liquibase liquibase = new Liquibase(location, accessor, database); |
54 | 72 | liquibase.update(contexts); |
55 | 73 | } catch (LiquibaseException e) { |
56 | 74 | throw new SQLException(e); |
57 | | - } finally { |
58 | | - if (connection != null) { |
59 | | - connection.rollback(); |
60 | | - connection.close(); |
61 | | - } |
62 | 75 | } |
63 | 76 | } |
64 | 77 |
|
65 | 78 | @Override |
66 | | - public boolean equals(Object obj) { |
67 | | - return obj instanceof LiquibasePreparer && Objects.equals(location, ((LiquibasePreparer) obj).location); |
| 79 | + public boolean equals(Object o) { |
| 80 | + if (this == o) return true; |
| 81 | + if (o == null || getClass() != o.getClass()) return false; |
| 82 | + LiquibasePreparer that = (LiquibasePreparer) o; |
| 83 | + return Objects.equals(location, that.location) |
| 84 | + && Objects.equals(accessor, that.accessor) |
| 85 | + && Objects.equals(contexts.getContexts(), that.contexts.getContexts()); |
68 | 86 | } |
69 | 87 |
|
70 | 88 | @Override |
71 | 89 | public int hashCode() { |
72 | | - return Objects.hashCode(location); |
| 90 | + return Objects.hash(location, accessor, contexts.getContexts()); |
73 | 91 | } |
74 | 92 | } |
0 commit comments