Skip to content

Commit

Permalink
review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
weronikasosnowskaseqera committed Nov 28, 2023
1 parent 87e812c commit 0cde59d
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 14 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ In addition, the migration files can be written in Groovy in order to run script
Only requirement is that migration files follow the pattern `V99__Some_name.[sql|groovy]`, where
`99` can be any integer value.

### Fixed and amended files

Beside files matching pattern mentioned above there is also possibility of creating `.fixed.` or `.amended.`,
example: `V99__Some_name.[fixed|amended].[sql|groovy]`.
These can be useful when the original migration file was somehow broken. `.fixed.` file will be applied when original file was already applied,
and `.amended.` file will be applied on clean instance where original file wasn't yet applied. These two files should be always created together,
meaning that there can't be `.fixed.` file without `.amended.` file.


## Get started

Expand Down
22 changes: 13 additions & 9 deletions src/main/java/io/seqera/migtool/MigTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,10 @@ else if( locations.startsWith(SOURCE_FILE)) {
else {
throw new IllegalArgumentException("Invalid locations prefix: " + locations);
}

log.debug("Scanned {} migration files, {} amended files and {} fixed files", migrationEntries.size(), amendedEntries.size(), fixedEntries.size());
if (amendedEntries.size() != fixedEntries.size())
throw new IllegalStateException("Sum of fixed and amended files doesn't match. For each fixed file there has to be created amended file.");
}

private void addEntry(MigRecord entry) {
Expand Down Expand Up @@ -407,16 +410,17 @@ protected void applyMigration(MigRecord entry) throws SQLException {

protected void applyMigrationFix(MigRecord entry) throws SQLException {
MigRecord fix = findFixedRecord(entry);
if (fix != null) {
log.info("Detected fix file. Attempt to apply {}", fix.script);
if (checkMigrated(fix)) {
log.info("DB migration fix already applied: {} {}", fix.rank, fix.script);
return;
}
log.info("DB migration fix {} {} ..", fix.rank, fix.script);
int delta = migrate(fix);
log.info("DB migration fix performed: {} {} - execution time {}ms {}", fix.rank, fix.script, delta, fix.statements);
if (fix == null)
return;

log.info("Detected fix file. Attempt to apply {}", fix.script);
if (checkMigrated(fix)) {
log.info("DB migration fix already applied: {} {}", fix.rank, fix.script);
return;
}
log.info("DB migration fix {} {} ..", fix.rank, fix.script);
int delta = migrate(fix);
log.info("DB migration fix performed: {} {} - execution time {}ms {}", fix.rank, fix.script, delta, fix.statements);
}

private int migrate(MigRecord entry) throws SQLException {
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/db/mysql/V01__mysql1.amended.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
create table AMENDED ( col1 varchar(1) );
create table AMENDED ( col1 varchar(1) );
2 changes: 1 addition & 1 deletion src/test/resources/db/mysql/V01__mysql1.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
create table XXX ( col1 varchar(1) );
create table XXX ( col1 varchar(1) );
2 changes: 1 addition & 1 deletion src/test/resources/db/mysql/V02__mysql2.fixed.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
create table FIXED ( col2 varchar(2) );
create table FIXED ( col2 varchar(2) );
3 changes: 1 addition & 2 deletions src/test/resources/db/mysql/V02__mysql2.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
create table YYY ( col2 varchar(2) );

create table ZZZ ( col3 varchar(3) );;

create table ZZZ ( col3 varchar(3) );

0 comments on commit 0cde59d

Please sign in to comment.