|
1 |
| -package org.javalite.db_migrator; |
2 |
| - |
3 |
| -import org.apache.maven.plugin.logging.Log; |
4 |
| - |
5 |
| -import java.sql.SQLException; |
6 |
| -import java.util.ArrayList; |
7 |
| -import java.util.Collection; |
8 |
| -import java.util.Date; |
9 |
| -import java.util.List; |
10 |
| - |
11 |
| -import static org.javalite.db_migrator.DbUtils.connection; |
12 |
| -import static org.javalite.db_migrator.DbUtils.databaseType; |
13 |
| - |
14 |
| -public class MigrationManager { |
15 |
| - |
16 |
| - private DatabaseType dbType; |
17 |
| - private VersionStrategy versionStrategy = new VersionStrategy(); |
18 |
| - private MigrationResolver migrationResolver; |
19 |
| - |
20 |
| - public MigrationManager(String migrationLocation) throws SQLException { |
21 |
| - this.dbType = determineDatabaseType(); |
22 |
| - migrationResolver = new MigrationResolver(migrationLocation); |
23 |
| - } |
24 |
| - |
25 |
| - /** |
26 |
| - * Validates whether the database is currently up-to-date. |
27 |
| - * |
28 |
| - * @return true if the database is up-to-date, false if it is not or is unversioned |
29 |
| - */ |
30 |
| - public boolean validate() { |
31 |
| - return getPendingMigrations().isEmpty(); |
32 |
| - } |
33 |
| - |
34 |
| - /** |
35 |
| - * Returns pending migrations. |
36 |
| - * |
37 |
| - * @return a sorted set of pending migrations |
38 |
| - */ |
39 |
| - public List<Migration> getPendingMigrations() { |
40 |
| - List<String> appliedMigrations = getAppliedMigrationVersions(); |
41 |
| - |
42 |
| - List<Migration> allMigrations = migrationResolver.resolve(); |
43 |
| - List<Migration> pendingMigrations = new ArrayList<Migration>(); |
44 |
| - for (Migration migration : allMigrations) { |
45 |
| - if(!appliedMigrations.contains(migration.getVersion())){ |
46 |
| - pendingMigrations.add(migration); |
47 |
| - } |
48 |
| - } |
49 |
| - return pendingMigrations; |
50 |
| - } |
51 |
| - |
52 |
| - /** |
53 |
| - * Migrates the database to the latest version, enabling migrations if necessary. |
54 |
| - */ |
55 |
| - public void migrate(Log log, String encoding) { |
56 |
| - |
57 |
| - if (!versionTableExists()) { |
58 |
| - createSchemaVersionTable(); |
59 |
| - } |
60 |
| - |
61 |
| - final Collection<Migration> pendingMigrations = getPendingMigrations(); |
62 |
| - |
63 |
| - if (pendingMigrations.isEmpty()) { |
64 |
| - log.info("No new migrations are found"); |
65 |
| - return; |
66 |
| - } |
67 |
| - log.info("Migrating database, applying " + pendingMigrations.size() + " migration(s)"); |
68 |
| - Migration currentMigration = null; |
69 |
| - |
70 |
| - try { |
71 |
| - connection().setAutoCommit(false); |
72 |
| - for (Migration migration : pendingMigrations) { |
73 |
| - currentMigration = migration; |
74 |
| - log.info("Running migration " + currentMigration.getName()); |
75 |
| - long start = System.currentTimeMillis(); |
76 |
| - |
77 |
| - currentMigration.migrate(encoding); |
78 |
| - versionStrategy.recordMigration(currentMigration.getVersion(), new Date(start), (start - System.currentTimeMillis())); |
79 |
| - connection().commit(); |
80 |
| - } |
81 |
| - } catch (Exception e) { |
82 |
| - try{connection().rollback();}catch(Exception ex){throw new MigrationException(e);} |
83 |
| - assert currentMigration != null; |
84 |
| - throw new MigrationException("Migration for version " + currentMigration.getVersion() + " failed, rolling back and terminating migration.", e); |
85 |
| - } |
86 |
| - log.info("Migrated database"); |
87 |
| - } |
88 |
| - |
89 |
| - protected DatabaseType determineDatabaseType() throws SQLException { |
90 |
| - return databaseType(connection().getMetaData().getURL()); |
91 |
| - |
92 |
| - } |
93 |
| - |
94 |
| - protected boolean versionTableExists() { |
95 |
| - return versionStrategy.versionTableExists(); |
96 |
| - } |
97 |
| - |
98 |
| - public void createSchemaVersionTable() { |
99 |
| - versionStrategy.createSchemaVersionTable(dbType); |
100 |
| - } |
101 |
| - |
102 |
| - protected List<String> getAppliedMigrationVersions() { |
103 |
| - return versionStrategy.getAppliedMigrations(); |
104 |
| - } |
105 |
| -} |
| 1 | +package org.javalite.db_migrator; |
| 2 | + |
| 3 | +import org.apache.maven.plugin.logging.Log; |
| 4 | + |
| 5 | +import java.sql.SQLException; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Collection; |
| 8 | +import java.util.Date; |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +import static org.javalite.db_migrator.DbUtils.connection; |
| 12 | +import static org.javalite.db_migrator.DbUtils.databaseType; |
| 13 | + |
| 14 | +public class MigrationManager { |
| 15 | + |
| 16 | + private DatabaseType dbType; |
| 17 | + private VersionStrategy versionStrategy = new VersionStrategy(); |
| 18 | + private MigrationResolver migrationResolver; |
| 19 | + |
| 20 | + public MigrationManager(String migrationLocation) throws SQLException { |
| 21 | + this.dbType = determineDatabaseType(); |
| 22 | + migrationResolver = new MigrationResolver(migrationLocation); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Validates whether the database is currently up-to-date. |
| 27 | + * |
| 28 | + * @return true if the database is up-to-date, false if it is not or is unversioned |
| 29 | + */ |
| 30 | + public boolean validate() { |
| 31 | + return getPendingMigrations().isEmpty(); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Returns pending migrations. |
| 36 | + * |
| 37 | + * @return a sorted set of pending migrations |
| 38 | + */ |
| 39 | + public List<Migration> getPendingMigrations() { |
| 40 | + List<String> appliedMigrations = getAppliedMigrationVersions(); |
| 41 | + |
| 42 | + List<Migration> allMigrations = migrationResolver.resolve(); |
| 43 | + List<Migration> pendingMigrations = new ArrayList<Migration>(); |
| 44 | + for (Migration migration : allMigrations) { |
| 45 | + if(!appliedMigrations.contains(migration.getVersion())){ |
| 46 | + pendingMigrations.add(migration); |
| 47 | + } |
| 48 | + } |
| 49 | + return pendingMigrations; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Migrates the database to the latest version, enabling migrations if necessary. |
| 54 | + */ |
| 55 | + public void migrate(Log log, String encoding) { |
| 56 | + |
| 57 | + createSchemaVersionTable(); |
| 58 | + |
| 59 | + final Collection<Migration> pendingMigrations = getPendingMigrations(); |
| 60 | + |
| 61 | + if (pendingMigrations.isEmpty()) { |
| 62 | + log.info("No new migrations are found"); |
| 63 | + return; |
| 64 | + } |
| 65 | + log.info("Migrating database, applying " + pendingMigrations.size() + " migration(s)"); |
| 66 | + Migration currentMigration = null; |
| 67 | + |
| 68 | + try { |
| 69 | + connection().setAutoCommit(false); |
| 70 | + for (Migration migration : pendingMigrations) { |
| 71 | + currentMigration = migration; |
| 72 | + log.info("Running migration " + currentMigration.getName()); |
| 73 | + long start = System.currentTimeMillis(); |
| 74 | + |
| 75 | + currentMigration.migrate(encoding); |
| 76 | + versionStrategy.recordMigration(currentMigration.getVersion(), new Date(start), (start - System.currentTimeMillis())); |
| 77 | + connection().commit(); |
| 78 | + } |
| 79 | + } catch (Exception e) { |
| 80 | + try{connection().rollback();}catch(Exception ex){throw new MigrationException(e);} |
| 81 | + assert currentMigration != null; |
| 82 | + throw new MigrationException("Migration for version " + currentMigration.getVersion() + " failed, rolling back and terminating migration.", e); |
| 83 | + } |
| 84 | + log.info("Migrated database"); |
| 85 | + } |
| 86 | + |
| 87 | + protected DatabaseType determineDatabaseType() throws SQLException { |
| 88 | + return databaseType(connection().getMetaData().getURL()); |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + protected boolean versionTableExists() { |
| 93 | + return versionStrategy.versionTableExists(); |
| 94 | + } |
| 95 | + |
| 96 | + public void createSchemaVersionTable() { |
| 97 | + if (!versionTableExists()) { |
| 98 | + versionStrategy.createSchemaVersionTable(dbType); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + protected List<String> getAppliedMigrationVersions() { |
| 103 | + return versionStrategy.getAppliedMigrations(); |
| 104 | + } |
| 105 | +} |
0 commit comments