11
11
import ch .ergon .adam .core .prepost .MigrationScriptProvider ;
12
12
import ch .ergon .adam .core .prepost .MigrationStep ;
13
13
import ch .ergon .adam .core .prepost .MigrationStepExecutor ;
14
+ import com .google .common .collect .Sets ;
14
15
import org .slf4j .Logger ;
15
16
import org .slf4j .LoggerFactory ;
16
17
17
18
import java .io .*;
18
19
import java .nio .file .Files ;
19
20
import java .nio .file .Path ;
21
+ import java .util .Collection ;
20
22
import java .util .List ;
23
+ import java .util .Set ;
21
24
22
25
import static ch .ergon .adam .core .prepost .MigrationStep .POSTMIGRATION_ALWAYS ;
23
26
import static ch .ergon .adam .core .prepost .MigrationStep .POSTMIGRATION_INIT ;
27
30
import static ch .ergon .adam .core .prepost .MigrationStep .PREMIGRATION_ONCE ;
28
31
import static ch .ergon .adam .core .prepost .db_schema_version .DbSchemaVersionSource .SCHEMA_VERSION_TABLE_NAME ;
29
32
import static com .google .common .collect .Lists .newArrayList ;
33
+ import static com .google .common .collect .Sets .newHashSet ;
30
34
import static java .lang .ClassLoader .getSystemResourceAsStream ;
31
35
import static java .lang .String .format ;
32
36
@@ -49,6 +53,8 @@ public class Adam {
49
53
private boolean allowUnknownDBVersion = false ;
50
54
private boolean allowNonForwardMigration = false ;
51
55
private boolean migrateSameVersion = false ;
56
+ private Collection <String > includes ;
57
+ private Collection <String > excludes ;
52
58
53
59
54
60
public static Adam usingGitRepo (String referenceSchemaUrl , String targetUrl , String targetVersion , File migrationScriptPath , File gitRepo ) throws IOException {
@@ -163,7 +169,7 @@ public void execute() throws IOException {
163
169
MigrationStepExecutor executor = new MigrationStepExecutor (migrationScriptProvider , targetExecutor );
164
170
executor .executeStep (PREMIGRATION_ALWAYS );
165
171
executor .executeStep (PREMIGRATION_ONCE );
166
- SchemaMigrator .migrate (referenceUrl , targetUrl );
172
+ SchemaMigrator .migrate (referenceUrl , targetUrl , getMigrationConfig () );
167
173
executor .executeStep (POSTMIGRATION_ONCE );
168
174
executor .executeStep (POSTMIGRATION_ALWAYS );
169
175
} else {
@@ -172,7 +178,7 @@ public void execute() throws IOException {
172
178
if (isDbInit ) {
173
179
executor .executeStep (PREMIGRATION_INIT );
174
180
}
175
- SchemaMigrator .migrate (referenceUrl , targetUrl );
181
+ SchemaMigrator .migrate (referenceUrl , targetUrl , getMigrationConfig () );
176
182
if (isDbInit ) {
177
183
executor .executeStep (POSTMIGRATION_INIT );
178
184
}
@@ -190,6 +196,17 @@ public void execute() throws IOException {
190
196
191
197
}
192
198
199
+ private MigrationConfiguration getMigrationConfig () {
200
+ MigrationConfiguration migrationConfiguration = new MigrationConfiguration ();
201
+ Set <String > excludeList = newHashSet (SCHEMA_VERSION_TABLE_NAME );
202
+ if (excludes != null ) {
203
+ excludeList .addAll (excludes );
204
+ }
205
+ migrationConfiguration .setObjectNameExcludeList (excludeList );
206
+ migrationConfiguration .setObjectNameIncludeList (includes );
207
+ return migrationConfiguration ;
208
+ }
209
+
193
210
private void logExecutionOrder (MigrationScriptProvider migrationScriptProvider ) {
194
211
logger .info ("The following scripts will be executed in given order:" );
195
212
logExecutionOrderForStep (migrationScriptProvider , PREMIGRATION_ALWAYS );
@@ -219,23 +236,23 @@ private void ensureSchemaVersionTable(String targetUrl) {
219
236
}
220
237
221
238
private void ensureNoInProgressMigrations (SqlExecutor sqlExecutor ) {
222
- Object result = sqlExecutor .queryResult (format ("SELECT COUNT(1) FROM %s WHERE execution_completed_at IS NULL" , SCHEMA_VERSION_TABLE_NAME ));
223
- if (! (result .equals ( 0L ) || result . equals ( 0 )) ) {
239
+ Object result = sqlExecutor .queryResult (format ("SELECT COUNT(1) FROM \" %s \" WHERE \" execution_completed_at\" IS NULL" , SCHEMA_VERSION_TABLE_NAME ));
240
+ if (Integer . parseInt (result .toString ()) != 0 ) {
224
241
throw new RuntimeException ("There is an unfinished migration in [" + SCHEMA_VERSION_TABLE_NAME + "]" );
225
242
}
226
243
}
227
244
228
245
private String getDbSchemaVersion (SqlExecutor sqlExecutor ) {
229
- Object result = sqlExecutor .queryResult (format ("SELECT target_version FROM %s ORDER BY execution_started_at DESC" , SCHEMA_VERSION_TABLE_NAME ));
246
+ Object result = sqlExecutor .queryResult (format ("SELECT \" target_version\" FROM \" %s \" ORDER BY \" execution_started_at\" DESC" , SCHEMA_VERSION_TABLE_NAME ));
230
247
return result == null ? null : result .toString ();
231
248
}
232
249
233
250
private void createSchemaVersionEntry (SqlExecutor sqlExecutor , String fromVersion , String toVersion ) {
234
- sqlExecutor .queryResult (format ("INSERT INTO %s ( execution_started_at, source_version, target_version) VALUES (CURRENT_TIMESTAMP, ?, ?)" , SCHEMA_VERSION_TABLE_NAME ), fromVersion , toVersion );
251
+ sqlExecutor .queryResult (format ("INSERT INTO \" %s \" ( \" execution_started_at\" , \" source_version\" , \" target_version\" ) VALUES (CURRENT_TIMESTAMP, ?, ?)" , SCHEMA_VERSION_TABLE_NAME ), fromVersion , toVersion );
235
252
}
236
253
237
254
private void completeSchemaVersionEntry (SqlExecutor sqlExecutor ) {
238
- sqlExecutor .queryResult (format ("UPDATE %s SET execution_completed_at = CURRENT_TIMESTAMP WHERE execution_completed_at IS NULL" , SCHEMA_VERSION_TABLE_NAME ));
255
+ sqlExecutor .queryResult (format ("UPDATE \" %s \" SET \" execution_completed_at\" = CURRENT_TIMESTAMP WHERE \" execution_completed_at\" IS NULL" , SCHEMA_VERSION_TABLE_NAME ));
239
256
}
240
257
241
258
public void setAllowUnknownDBVersion (boolean allowUnknownDBVersion ) {
@@ -261,4 +278,12 @@ public boolean isAllowNonForwardMigration() {
261
278
public void setAllowNonForwardMigration (boolean allowNonForwardMigration ) {
262
279
this .allowNonForwardMigration = allowNonForwardMigration ;
263
280
}
281
+
282
+ public void setIncludes (Collection <String > includes ) {
283
+ this .includes = includes ;
284
+ }
285
+
286
+ public void setExcludes (Collection <String > excludes ) {
287
+ this .excludes = excludes ;
288
+ }
264
289
}
0 commit comments