fix: use prefixed flyway table name#683
Conversation
Adds a prefix to the `flyway_schema_history` table resulting in `carbon_flyway_schema_history`. This also includes migration code to provide seamless migration for existing users from the old table to the new.
|
I couldn't get Checkstyle to work (Even with the self-compiled plugin), so I compiled & tested the plugin without it. There are a few things on my mind I'm unsure of how you want:
|
|
I'm not sure I would call this a fix, I think adding a note to the wiki that carbon expects to own the database it uses is enough. |
I agree this isn't a fix, but that's sort of completely dependent on what you consider the expected behavior. It's definitely not the prettiest piece of code. I guess my question to you is? Would you rather have the plugin occupy the Here's where I'm coming from: I'm open to getting this to a mergeable state or making that Wiki PR as suggested. Let me know which one you prefer! ❤️ |
There was a problem hiding this comment.
Pull request overview
This PR updates Carbon’s Flyway configuration to use a prefixed schema history table (carbon_flyway_schema_history) and adds a startup-time migration to move existing installations off the unprefixed flyway_schema_history table to prevent cross-plugin table name conflicts.
Changes:
- Add a manual migration step that copies
flyway_schema_historytocarbon_flyway_schema_history(when needed) and drops the old table. - Configure Flyway to use
carbon_flyway_schema_historygoing forward.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
| final String databaseType = metaData.getDatabaseProductName().toLowerCase(); | ||
|
|
| if (databaseType.contains("h2")) { | ||
| stmt.execute("CREATE TABLE \"carbon_flyway_schema_history\" AS SELECT * FROM \"flyway_schema_history\""); | ||
| stmt.execute("CREATE INDEX \"carbon_flyway_schema_history_s_idx\" ON \"carbon_flyway_schema_history\" (\"success\")"); | ||
| stmt.execute("DROP TABLE \"flyway_schema_history\""); | ||
| this.logger.info("Successfully migrated flyway_schema_history to carbon_flyway_schema_history"); |
| stmt.execute("CREATE INDEX carbon_flyway_schema_history_s_idx ON carbon_flyway_schema_history (success)"); | ||
| stmt.execute("DROP TABLE flyway_schema_history"); | ||
| this.logger.info("Successfully migrated flyway_schema_history to carbon_flyway_schema_history"); | ||
| } else if (databaseType.contains("postgresql")) { |
| final var metaData = connection.getMetaData(); | ||
|
|
||
| try ( | ||
| final var oldTableCheck = metaData.getTables(null, null, "flyway_schema_history", null); | ||
| final var newTableCheck = metaData.getTables(null, null, "carbon_flyway_schema_history", null) | ||
| ) { | ||
| final boolean oldTableExists = oldTableCheck.next(); | ||
| if (!oldTableExists) { | ||
| return; | ||
| } | ||
|
|
||
| final boolean newTableExists = newTableCheck.next(); | ||
| if (newTableExists) { | ||
| this.logger.debug("carbon_flyway_schema_history already exists, skipping migration"); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| try (final var stmt = connection.createStatement()) { | ||
| final String databaseType = metaData.getDatabaseProductName().toLowerCase(); | ||
|
|
||
| if (databaseType.contains("h2")) { | ||
| stmt.execute("CREATE TABLE \"carbon_flyway_schema_history\" AS SELECT * FROM \"flyway_schema_history\""); | ||
| stmt.execute("CREATE INDEX \"carbon_flyway_schema_history_s_idx\" ON \"carbon_flyway_schema_history\" (\"success\")"); | ||
| stmt.execute("DROP TABLE \"flyway_schema_history\""); | ||
| this.logger.info("Successfully migrated flyway_schema_history to carbon_flyway_schema_history"); | ||
| } else if (databaseType.contains("mysql") || databaseType.contains("mariadb")) { | ||
| stmt.execute("CREATE TABLE carbon_flyway_schema_history LIKE flyway_schema_history"); | ||
| stmt.execute("INSERT INTO carbon_flyway_schema_history SELECT * FROM flyway_schema_history"); | ||
| stmt.execute("CREATE INDEX carbon_flyway_schema_history_s_idx ON carbon_flyway_schema_history (success)"); | ||
| stmt.execute("DROP TABLE flyway_schema_history"); | ||
| this.logger.info("Successfully migrated flyway_schema_history to carbon_flyway_schema_history"); | ||
| } else if (databaseType.contains("postgresql")) { | ||
| stmt.execute("CREATE TABLE carbon_flyway_schema_history AS SELECT * FROM flyway_schema_history"); | ||
| stmt.execute("CREATE INDEX carbon_flyway_schema_history_s_idx ON carbon_flyway_schema_history (success)"); | ||
| stmt.execute("DROP TABLE flyway_schema_history"); | ||
| this.logger.info("Successfully migrated flyway_schema_history to carbon_flyway_schema_history"); | ||
| } else { | ||
| this.logger.warn("Unknown database type: {}. Skipping flyway schema history migration.", databaseType); | ||
| } |
Adds a prefix to the
flyway_schema_historytable, resulting in the new tablecarbon_flyway_schema_history. This also includes migration code to provide seamless migration for existing users from the old table to the new table.Currently, the migration works as follows: If
flyway_schema_historyexists andcarbon_flyway_schema_historydoesn't, then we migrate to the new table.Closes #679.
Testing
I've thoroughly tested this to ensure compatibility for all supported JDBC providers and existing instances of Carbon.