|
2 | 2 |
|
3 | 3 | import io.avaje.applog.AppLog; |
4 | 4 | import io.ebean.migration.MigrationConfig; |
| 5 | +import io.ebean.migration.MigrationContext; |
5 | 6 | import io.ebean.migration.MigrationException; |
6 | 7 | import io.ebean.migration.MigrationResource; |
7 | 8 |
|
@@ -35,51 +36,63 @@ public MigrationEngine(MigrationConfig migrationConfig, boolean checkStateOnly) |
35 | 36 |
|
36 | 37 | /** |
37 | 38 | * Run the migrations if there are any that need running. |
| 39 | + * |
| 40 | + * @param connection the connection to run on. Note the connection will be closed. |
38 | 41 | */ |
39 | 42 | public List<MigrationResource> run(Connection connection) { |
40 | 43 | try { |
41 | | - long startMs = System.currentTimeMillis(); |
42 | | - LocalMigrationResources resources = new LocalMigrationResources(migrationConfig); |
43 | | - if (!resources.readResources() && !resources.readInitResources()) { |
44 | | - log.log(DEBUG, "no migrations to check"); |
45 | | - return emptyList(); |
46 | | - } |
47 | | - long splitMs = System.currentTimeMillis() - startMs; |
48 | | - final var platform = derivePlatform(migrationConfig, connection); |
49 | | - final var firstCheck = new FirstCheck(migrationConfig, connection, platform); |
50 | | - if (fastMode && firstCheck.fastModeCheck(resources.versions())) { |
51 | | - long checkMs = System.currentTimeMillis() - startMs; |
52 | | - log.log(INFO, "DB migrations completed in {0}ms - totalMigrations:{1} readResources:{2}ms", checkMs, firstCheck.count(), splitMs); |
53 | | - return emptyList(); |
54 | | - } |
55 | | - // ensure running with autoCommit false |
56 | | - setAutoCommitFalse(connection); |
57 | | - |
58 | | - final MigrationTable table = initialiseMigrationTable(firstCheck, connection); |
59 | | - try { |
60 | | - List<MigrationResource> result = runMigrations(table, resources.versions()); |
61 | | - connection.commit(); |
62 | | - if (!checkStateOnly) { |
63 | | - long commitMs = System.currentTimeMillis(); |
64 | | - log.log(INFO, "DB migrations completed in {0}ms - executed:{1} totalMigrations:{2} mode:{3}", (commitMs - startMs), table.count(), table.size(), table.mode()); |
65 | | - int countNonTransactional = table.runNonTransactional(); |
66 | | - if (countNonTransactional > 0) { |
67 | | - log.log(INFO, "Non-transactional DB migrations completed in {0}ms - executed:{1}", (System.currentTimeMillis() - commitMs), countNonTransactional); |
68 | | - } |
| 44 | + return run(new DefaultMigrationContext(migrationConfig, connection)); |
| 45 | + } finally { |
| 46 | + close(connection); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Run the migrations if there are any that need running. (Does not close connection) |
| 52 | + */ |
| 53 | + public List<MigrationResource> run(MigrationContext context) { |
| 54 | + |
| 55 | + long startMs = System.currentTimeMillis(); |
| 56 | + LocalMigrationResources resources = new LocalMigrationResources(migrationConfig); |
| 57 | + if (!resources.readResources() && !resources.readInitResources()) { |
| 58 | + log.log(DEBUG, "no migrations to check"); |
| 59 | + return emptyList(); |
| 60 | + } |
| 61 | + |
| 62 | + var connection = context.connection(); |
| 63 | + long splitMs = System.currentTimeMillis() - startMs; |
| 64 | + final var platform = derivePlatform(migrationConfig, connection); |
| 65 | + final var firstCheck = new FirstCheck(migrationConfig, context, platform); |
| 66 | + if (fastMode && firstCheck.fastModeCheck(resources.versions())) { |
| 67 | + long checkMs = System.currentTimeMillis() - startMs; |
| 68 | + log.log(INFO, "DB migrations completed in {0}ms - totalMigrations:{1} readResources:{2}ms", checkMs, firstCheck.count(), splitMs); |
| 69 | + return emptyList(); |
| 70 | + } |
| 71 | + // ensure running with autoCommit false |
| 72 | + setAutoCommitFalse(connection); |
| 73 | + |
| 74 | + final MigrationTable table = initialiseMigrationTable(firstCheck, connection); |
| 75 | + try { |
| 76 | + List<MigrationResource> result = runMigrations(table, resources.versions()); |
| 77 | + connection.commit(); |
| 78 | + if (!checkStateOnly) { |
| 79 | + long commitMs = System.currentTimeMillis(); |
| 80 | + log.log(INFO, "DB migrations completed in {0}ms - executed:{1} totalMigrations:{2} mode:{3}", (commitMs - startMs), table.count(), table.size(), table.mode()); |
| 81 | + int countNonTransactional = table.runNonTransactional(); |
| 82 | + if (countNonTransactional > 0) { |
| 83 | + log.log(INFO, "Non-transactional DB migrations completed in {0}ms - executed:{1}", (System.currentTimeMillis() - commitMs), countNonTransactional); |
69 | 84 | } |
70 | | - return result; |
71 | | - } catch (MigrationException e) { |
72 | | - rollback(connection); |
73 | | - throw e; |
74 | | - } catch (Throwable e) { |
75 | | - log.log(ERROR, "Perform rollback due to DB migration error", e); |
76 | | - rollback(connection); |
77 | | - throw new MigrationException("Error running DB migrations", e); |
78 | | - } finally { |
79 | | - table.unlockMigrationTable(); |
80 | 85 | } |
| 86 | + return result; |
| 87 | + } catch (MigrationException e) { |
| 88 | + rollback(connection); |
| 89 | + throw e; |
| 90 | + } catch (Throwable e) { |
| 91 | + log.log(ERROR, "Perform rollback due to DB migration error", e); |
| 92 | + rollback(connection); |
| 93 | + throw new MigrationException("Error running DB migrations", e); |
81 | 94 | } finally { |
82 | | - close(connection); |
| 95 | + table.unlockMigrationTable(); |
83 | 96 | } |
84 | 97 | } |
85 | 98 |
|
|
0 commit comments