39
39
40
40
import javax .sql .DataSource ;
41
41
import java .sql .Connection ;
42
+ import java .util .Map ;
42
43
43
44
public final class DataSourceUtils {
44
45
@@ -47,7 +48,6 @@ public final class DataSourceUtils {
47
48
private static final int MIGRATION_MAX_RETRIES = 10 ;
48
49
private static final int MIGRATION_RETRY_DELAY = 10000 ;
49
50
50
-
51
51
public static DataSource createDataSource (DatabaseConfiguration cfg ,
52
52
String poolName ,
53
53
String username ,
@@ -70,11 +70,28 @@ public static DataSource createDataSource(DatabaseConfiguration cfg,
70
70
}
71
71
72
72
public static void migrateDb (DataSource ds , DatabaseChangeLogProvider p ) {
73
+ migrateDb (ds , p , null );
74
+ }
75
+
76
+ /**
77
+ * Migrate a database using the provided changelog and Liquibase parameters.
78
+ *
79
+ * @param dataSource datasource to use for migration
80
+ * @param changeLogProvider provider of the changelog
81
+ * @param changeLogParams Liquibase parameters to use during the migration
82
+ */
83
+ public static void migrateDb (DataSource dataSource ,
84
+ DatabaseChangeLogProvider changeLogProvider ,
85
+ Map <String , Object > changeLogParams ) {
86
+
73
87
int retries = MIGRATION_MAX_RETRIES ;
74
88
for (int i = 0 ; i < retries ; i ++) {
75
- try (Connection c = ds .getConnection ()) {
76
- log .info ("get -> performing '{}' migration..." , p );
77
- migrateDb (c , p .getChangeLogPath (), p .getChangeLogTable (), p .getLockTable ());
89
+ try (Connection c = dataSource .getConnection ()) {
90
+ log .info ("get -> performing '{}' migration..." , changeLogProvider );
91
+ String logPath = changeLogProvider .getChangeLogPath ();
92
+ String logTable = changeLogProvider .getChangeLogTable ();
93
+ String lockTable = changeLogProvider .getLockTable ();
94
+ migrateDb (c , logPath , logTable , lockTable , changeLogParams );
78
95
log .info ("get -> done" );
79
96
break ;
80
97
} catch (Exception e ) {
@@ -93,17 +110,6 @@ public static void migrateDb(DataSource ds, DatabaseChangeLogProvider p) {
93
110
}
94
111
}
95
112
96
- private static void migrateDb (Connection conn , String logPath , String logTable , String lockTable ) throws Exception {
97
- LogFactory .getInstance ().setDefaultLoggingLevel (LogLevel .WARNING );
98
-
99
- Database db = DatabaseFactory .getInstance ().findCorrectDatabaseImplementation (new JdbcConnection (conn ));
100
- db .setDatabaseChangeLogTableName (logTable );
101
- db .setDatabaseChangeLogLockTableName (lockTable );
102
-
103
- Liquibase lb = new Liquibase (logPath , new ClassLoaderResourceAccessor (), db );
104
- lb .update ((String ) null );
105
- }
106
-
107
113
public static Configuration createJooqConfiguration (DataSource ds ) {
108
114
Settings settings = new Settings ();
109
115
settings .setRenderSchema (false );
@@ -115,6 +121,27 @@ public static Configuration createJooqConfiguration(DataSource ds) {
115
121
.set (SQLDialect .POSTGRES );
116
122
}
117
123
124
+ private static void migrateDb (Connection conn ,
125
+ String logPath ,
126
+ String logTable ,
127
+ String lockTable ,
128
+ Map <String , Object > params ) throws Exception {
129
+
130
+ LogFactory .getInstance ().setDefaultLoggingLevel (LogLevel .WARNING );
131
+
132
+ Database db = DatabaseFactory .getInstance ().findCorrectDatabaseImplementation (new JdbcConnection (conn ));
133
+ db .setDatabaseChangeLogTableName (logTable );
134
+ db .setDatabaseChangeLogLockTableName (lockTable );
135
+
136
+ Liquibase lb = new Liquibase (logPath , new ClassLoaderResourceAccessor (), db );
137
+
138
+ if (params != null ) {
139
+ params .forEach (lb ::setChangeLogParameter );
140
+ }
141
+
142
+ lb .update ((String ) null );
143
+ }
144
+
118
145
private DataSourceUtils () {
119
146
}
120
147
}
0 commit comments