From 0cde59d5d4ed8ef61dc549dbf214fb0e54fc14e4 Mon Sep 17 00:00:00 2001 From: Weronika Sosnowska Date: Tue, 28 Nov 2023 13:58:44 +0100 Subject: [PATCH] review fixes --- README.md | 8 +++++++ src/main/java/io/seqera/migtool/MigTool.java | 22 +++++++++++-------- .../db/mysql/V01__mysql1.amended.sql | 2 +- src/test/resources/db/mysql/V01__mysql1.sql | 2 +- .../resources/db/mysql/V02__mysql2.fixed.sql | 2 +- src/test/resources/db/mysql/V02__mysql2.sql | 3 +-- 6 files changed, 25 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index ab7ad0d..662f488 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/main/java/io/seqera/migtool/MigTool.java b/src/main/java/io/seqera/migtool/MigTool.java index b812c28..867526b 100644 --- a/src/main/java/io/seqera/migtool/MigTool.java +++ b/src/main/java/io/seqera/migtool/MigTool.java @@ -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) { @@ -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 { diff --git a/src/test/resources/db/mysql/V01__mysql1.amended.sql b/src/test/resources/db/mysql/V01__mysql1.amended.sql index 191e804..de95c54 100644 --- a/src/test/resources/db/mysql/V01__mysql1.amended.sql +++ b/src/test/resources/db/mysql/V01__mysql1.amended.sql @@ -1 +1 @@ -create table AMENDED ( col1 varchar(1) ); \ No newline at end of file +create table AMENDED ( col1 varchar(1) ); diff --git a/src/test/resources/db/mysql/V01__mysql1.sql b/src/test/resources/db/mysql/V01__mysql1.sql index 514b4b3..abfaa74 100644 --- a/src/test/resources/db/mysql/V01__mysql1.sql +++ b/src/test/resources/db/mysql/V01__mysql1.sql @@ -1 +1 @@ -create table XXX ( col1 varchar(1) ); \ No newline at end of file +create table XXX ( col1 varchar(1) ); diff --git a/src/test/resources/db/mysql/V02__mysql2.fixed.sql b/src/test/resources/db/mysql/V02__mysql2.fixed.sql index 3248f0f..f6c0510 100644 --- a/src/test/resources/db/mysql/V02__mysql2.fixed.sql +++ b/src/test/resources/db/mysql/V02__mysql2.fixed.sql @@ -1 +1 @@ -create table FIXED ( col2 varchar(2) ); \ No newline at end of file +create table FIXED ( col2 varchar(2) ); diff --git a/src/test/resources/db/mysql/V02__mysql2.sql b/src/test/resources/db/mysql/V02__mysql2.sql index 3b38060..703cf9f 100644 --- a/src/test/resources/db/mysql/V02__mysql2.sql +++ b/src/test/resources/db/mysql/V02__mysql2.sql @@ -1,4 +1,3 @@ create table YYY ( col2 varchar(2) ); -create table ZZZ ( col3 varchar(3) );; - +create table ZZZ ( col3 varchar(3) );