Skip to content

Commit

Permalink
Prepare Mysql 8.x migration, initial script update & flyway repair
Browse files Browse the repository at this point in the history
- Update the initial setup script to quote the now reserved keyword "group"
- Repair Flyway table to support Mysql 8 change if necessary. This is needed since the change to the initial setup script modifies the checksum. Because of the checksum change trying to run Flyway.migrate() will fail, and the server won't start.

Note that if the baseline of the Mojito instance is different for example because of the migration to springboot 2.x, the flyway repair code may never run, which is fine.
  • Loading branch information
aurambaj committed Jul 27, 2023
1 parent a05fa52 commit 9adbc87
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
29 changes: 28 additions & 1 deletion webapp/src/main/java/com/box/l10n/mojito/FlyWayConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ public FlywayMigrationStrategy cleanMigrateStrategy() {
flyway.migrate();
logger.info("Flyway migrate() finished");
} catch (FlywayException fe) {
tryToMigrateIfSpringMigration(flyway, fe);
if (flyway.info().current().getVersion().getVersion().equals("63")) {
tryToMigrateIfMysql8Migration(flyway, fe);
} else {
tryToMigrateIfSpringMigration(flyway, fe);
}
}
}
};
Expand Down Expand Up @@ -122,6 +126,29 @@ String getBaselineVersionFromOldFlywayTable() {
return baselineVersion;
}

/**
* To migrate to Mysql 8, we need to escape new reserved keyword "groups" in
* V1__Initial_Setup.sql, which in turns changes the Flyway MD5. If the Flyway migrate() fails for
* that specific reason we just repair the schema, i.e. just change the MD5 for version 1. *
*
* @param flyway
* @param fe
*/
void tryToMigrateIfMysql8Migration(Flyway flyway, FlywayException fe) {
if (fe.getMessage()
.contains(
"Migration checksum mismatch for migration version 1\n"
+ "-> Applied to database : 1443976515\n"
+ "-> Resolved locally : -998267617")) {

logger.info("Flyway repair()");
flyway.repair();
logger.info("Flyway repair() finished");
} else {
throw fe;
}
}

/**
* Additional check to avoid cleaning a critical environment by mistake. This is a weak check
* since a failure to perform the query will show that the DB is not protected.
Expand Down
8 changes: 4 additions & 4 deletions webapp/src/main/resources/db/migration/V1__Initial_Setup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ create table asset_text_unit_to_tm_text_unit (id bigint not null auto_increment,
create table authority (id bigint not null auto_increment, created_date datetime, last_modified_date datetime, authority varchar(255), created_by_user_id bigint, user_id bigint not null, primary key (id));
create table group_authorities (id bigint not null auto_increment, created_date datetime, last_modified_date datetime, authority varchar(255) not null, created_by_user_id bigint, group_id bigint not null, primary key (id));
create table group_members (id bigint not null auto_increment, created_date datetime, last_modified_date datetime, created_by_user_id bigint, group_id bigint not null, username bigint not null, primary key (id));
create table groups (id bigint not null auto_increment, created_date datetime, last_modified_date datetime, group_name varchar(255) not null, created_by_user_id bigint, primary key (id));
create table `groups` (id bigint not null auto_increment, created_date datetime, last_modified_date datetime, group_name varchar(255) not null, created_by_user_id bigint, primary key (id));
create table locale (id bigint not null auto_increment, bcp47_tag varchar(255) not null, primary key (id));
create table pollable_task (id bigint not null auto_increment, created_date datetime, last_modified_date datetime, error_message longtext, error_stacks longtext, expected_sub_task_number integer not null, finished_date datetime, message longtext, name varchar(255) not null, timeout bigint, created_by_user_id bigint, parent_task_id bigint, primary key (id));
create table repository (id bigint not null auto_increment, created_date datetime, last_modified_date datetime, description varchar(255), drop_exporter_type varchar(255), name varchar(255) not null, created_by_user_id bigint, repository_statistic_id bigint, tm_id bigint, primary key (id));
Expand Down Expand Up @@ -66,11 +66,11 @@ alter table asset_text_unit_to_tm_text_unit add constraint FK__ASSET_TEXT_UNIT_T
alter table authority add constraint FK__AUTHORITY__USER__ID foreign key (created_by_user_id) references user (id);
alter table authority add constraint FK__AUTHORITY__USER__USER_ID foreign key (user_id) references user (id);
alter table group_authorities add constraint FK__GROUP_AUTHORITY__USER__ID foreign key (created_by_user_id) references user (id);
alter table group_authorities add constraint FK__GROUP_AUTHORITY__GROUP__ID foreign key (group_id) references groups (id);
alter table group_authorities add constraint FK__GROUP_AUTHORITY__GROUP__ID foreign key (group_id) references `groups` (id);
alter table group_members add constraint FK__GROUP_MEMBER__USER__ID foreign key (created_by_user_id) references user (id);
alter table group_members add constraint FK__GROUP_MEMBER__GROUP__ID foreign key (group_id) references groups (id);
alter table group_members add constraint FK__GROUP_MEMBER__GROUP__ID foreign key (group_id) references `groups` (id);
alter table group_members add constraint FK__GROUP_MEMBER__USER__USERNAME foreign key (username) references user (id);
alter table groups add constraint FK__GROUP__USER__ID foreign key (created_by_user_id) references user (id);
alter table `groups` add constraint FK__GROUP__USER__ID foreign key (created_by_user_id) references user (id);
alter table pollable_task add constraint FK__POLLABLE_TASK__USER__ID foreign key (created_by_user_id) references user (id);
alter table pollable_task add constraint FK__POLLABLE_TASK__POLLABLE_TASK__ID foreign key (parent_task_id) references pollable_task (id);
alter table repository add constraint FK__REPOSITORY__USER__ID foreign key (created_by_user_id) references user (id);
Expand Down

0 comments on commit 9adbc87

Please sign in to comment.