Tony CHEMIT pushed to branch develop at ultreiaio / ird-observe Commits: 01819545 by Tony Chemit at 2024-01-18T10:23:06+01:00 update pom - - - - - 1640c13a by Tony Chemit at 2024-01-18T10:23:06+01:00 [toolkit-maven-plugin] Add more actions in GenerateMigrationScriptRunner - - - - - d4cb0767 by Tony Chemit at 2024-01-18T10:23:06+01:00 [toolkit-persistence] Open MigrationVersionResourceExecutor.isPg method - - - - - 11 changed files: - client/datasource/editor/spi/pom.xml - pom.xml - toolkit/persistence/src/main/java/org/nuiton/topia/service/migration/resources/MigrationVersionResourceExecutor.java - + toolkit/plugin/src/main/java/fr/ird/observe/toolkit/maven/plugin/persistence/DropTableTask.java - + toolkit/plugin/src/main/java/fr/ird/observe/toolkit/maven/plugin/persistence/DropTableTasks.java - + toolkit/plugin/src/main/java/fr/ird/observe/toolkit/maven/plugin/persistence/ExtractSchemaTask.java - + toolkit/plugin/src/main/java/fr/ird/observe/toolkit/maven/plugin/persistence/ExtractTableTask.java - toolkit/plugin/src/main/java/fr/ird/observe/toolkit/maven/plugin/persistence/GenerateMigrationScriptRunner.java - toolkit/plugin/src/main/java/fr/ird/observe/toolkit/maven/plugin/persistence/MigrationScriptTask.java - + toolkit/plugin/src/main/java/fr/ird/observe/toolkit/maven/plugin/persistence/RenameForeignKeyTask.java - + toolkit/plugin/src/main/java/fr/ird/observe/toolkit/maven/plugin/persistence/RenameForeignKeyTasks.java Changes: ===================================== client/datasource/editor/spi/pom.xml ===================================== @@ -154,6 +154,18 @@ <scope>test</scope> </dependency> </dependencies> + <build> + <pluginManagement> + <plugins> + <plugin> + <artifactId>maven-plugin-plugin</artifactId> + <configuration> + <goalPrefix>client-datasource-editor-spi</goalPrefix> + </configuration> + </plugin> + </plugins> + </pluginManagement> + </build> <profiles> <profile> <id>reporting</id> ===================================== pom.xml ===================================== @@ -23,7 +23,7 @@ <parent> <groupId>io.ultreia.maven</groupId> <artifactId>pom</artifactId> - <version>2023.59</version> + <version>2024.01</version> </parent> <groupId>fr.ird.observe</groupId> <artifactId>ird-observe</artifactId> ===================================== toolkit/persistence/src/main/java/org/nuiton/topia/service/migration/resources/MigrationVersionResourceExecutor.java ===================================== @@ -261,7 +261,7 @@ public class MigrationVersionResourceExecutor implements AutoCloseable { return classLoader; } - protected boolean isPG() { + public boolean isPG() { return PGTopiaSqlDllSupportImpl.CLASSIFIER.equals(classifier); } ===================================== toolkit/plugin/src/main/java/fr/ird/observe/toolkit/maven/plugin/persistence/DropTableTask.java ===================================== @@ -0,0 +1,51 @@ +package fr.ird.observe.toolkit.maven.plugin.persistence; + +/*- + * #%L + * ObServe Toolkit :: Maven plugin + * %% + * Copyright (C) 2008 - 2024 IRD, Ultreia.io + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +/** + * Created at 12/01/2024. + * + * @author Tony Chemit - dev@tchemit.fr + * @since 9.3.0 + */ +public class DropTableTask { + + private String schemaName; + private String[] tableName; + + public String getSchemaName() { + return schemaName; + } + + public void setSchemaName(String schemaName) { + this.schemaName = schemaName; + } + + public String[] getTableName() { + return tableName; + } + + public void setTableName(String[] tableName) { + this.tableName = tableName; + } +} ===================================== toolkit/plugin/src/main/java/fr/ird/observe/toolkit/maven/plugin/persistence/DropTableTasks.java ===================================== @@ -0,0 +1,66 @@ +package fr.ird.observe.toolkit.maven.plugin.persistence; + +/*- + * #%L + * ObServe Toolkit :: Maven plugin + * %% + * Copyright (C) 2008 - 2024 IRD, Ultreia.io + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +import java.util.LinkedList; +import java.util.List; + +/** + * Created at 12/01/2024. + * + * @author Tony Chemit - dev@tchemit.fr + * @since 9.3.0 + */ +public class DropTableTasks { + private String prefix; + private List<DropTableTask> tasks = List.of(); + + public String getPrefix() { + return prefix; + } + + public void setPrefix(String prefix) { + this.prefix = prefix; + } + + public List<DropTableTask> getTasks() { + return tasks; + } + + public void setTasks(List<DropTableTask> tasks) { + this.tasks = tasks; + } + + public List<String> toSql(boolean h2) { + String addCascadeClause = h2 ? "" : " CASCADE"; + List<String> statements = new LinkedList<>(); + for (DropTableTask task : getTasks()) { + String schemaName = task.getSchemaName(); + for (String name : task.getTableName()) { + String tableName = name.toLowerCase(); + statements.add(String.format("DROP TABLE %s.%s%s;", schemaName, tableName, addCascadeClause)); + } + } + return statements; + } +} ===================================== toolkit/plugin/src/main/java/fr/ird/observe/toolkit/maven/plugin/persistence/ExtractSchemaTask.java ===================================== @@ -0,0 +1,51 @@ +package fr.ird.observe.toolkit.maven.plugin.persistence; + +/*- + * #%L + * ObServe Toolkit :: Maven plugin + * %% + * Copyright (C) 2008 - 2024 IRD, Ultreia.io + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +/** + * Created at 12/01/2024. + * + * @author Tony Chemit - dev@tchemit.fr + * @since + */ +public class ExtractSchemaTask { + + private String schemaName; + private String prefix; + + public String getSchemaName() { + return schemaName; + } + + public void setSchemaName(String schemaName) { + this.schemaName = schemaName; + } + + public String getPrefix() { + return prefix; + } + + public void setPrefix(String prefix) { + this.prefix = prefix; + } +} ===================================== toolkit/plugin/src/main/java/fr/ird/observe/toolkit/maven/plugin/persistence/ExtractTableTask.java ===================================== @@ -0,0 +1,60 @@ +package fr.ird.observe.toolkit.maven.plugin.persistence; + +/*- + * #%L + * ObServe Toolkit :: Maven plugin + * %% + * Copyright (C) 2008 - 2024 IRD, Ultreia.io + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +/** + * Created at 12/01/2024. + * + * @author Tony Chemit - dev@tchemit.fr + * @since 9.3.0 + */ +public class ExtractTableTask { + + private String schemaName; + private String prefix; + private String[] tableName; + + public String getSchemaName() { + return schemaName; + } + + public void setSchemaName(String schemaName) { + this.schemaName = schemaName; + } + + public String getPrefix() { + return prefix; + } + + public void setPrefix(String prefix) { + this.prefix = prefix; + } + + public String[] getTableName() { + return tableName; + } + + public void setTableName(String[] tableName) { + this.tableName = tableName; + } +} ===================================== toolkit/plugin/src/main/java/fr/ird/observe/toolkit/maven/plugin/persistence/GenerateMigrationScriptRunner.java ===================================== @@ -96,14 +96,14 @@ public class GenerateMigrationScriptRunner extends PersistenceRunner { } PersistenceScriptHelper.write(scriptPath, context, String.format("empty-%%s-%s", scriptSuffix), "", true); - for (MigrationScriptTask.ExtractSchemaTask task : task.getSchemaTasks()) { + for (ExtractSchemaTask task : task.getSchemaTasks()) { String prefix = task.getPrefix(); String schemaName = task.getSchemaName(); String baseName = String.format("%s_%%s-%s-%s", prefix, schemaName, scriptSuffix); ScriptSplitter.SplitSimpleResult filterOnSchema = ScriptSplitter.filterSchema(context, schemaName); PersistenceScriptHelper.write(scriptPath, filterOnSchema, baseName, "-schema", false); } - for (MigrationScriptTask.ExtractTableTask task : task.getTableTasks()) { + for (ExtractTableTask task : task.getTableTasks()) { String prefix = task.getPrefix(); String schemaName = task.getSchemaName(); for (String name : task.getTableName()) { @@ -113,6 +113,13 @@ public class GenerateMigrationScriptRunner extends PersistenceRunner { PersistenceScriptHelper.write(scriptPath, filterOnSchema, baseName, "-table", false); } } + for (DropTableTasks task : task.getDropTableTasks()) { + String baseName = String.format("%s_drop-tasks-%s", Objects.requireNonNull(task.getPrefix()), scriptSuffix); + PersistenceScriptHelper.writeScript(scriptPath.resolve(baseName), task.toSql(h2)); + } + for (RenameForeignKeyTasks task : task.getRenameForeignKeyTasks()) { + String baseName = String.format("%s_rename-foreign-key-%s", Objects.requireNonNull(task.getPrefix()), scriptSuffix); + PersistenceScriptHelper.writeScript(scriptPath.resolve(baseName), task.toSql(h2)); + } } - } ===================================== toolkit/plugin/src/main/java/fr/ird/observe/toolkit/maven/plugin/persistence/MigrationScriptTask.java ===================================== @@ -23,6 +23,7 @@ package fr.ird.observe.toolkit.maven.plugin.persistence; */ import java.util.List; +import java.util.Objects; /** * Created on 24/11/2021. @@ -34,59 +35,8 @@ public class MigrationScriptTask { private List<ExtractSchemaTask> schemaTasks = List.of(); private List<ExtractTableTask> tableTasks = List.of(); - - public static class ExtractSchemaTask { - - private String schemaName; - private String prefix; - - public String getSchemaName() { - return schemaName; - } - - public void setSchemaName(String schemaName) { - this.schemaName = schemaName; - } - - public String getPrefix() { - return prefix; - } - - public void setPrefix(String prefix) { - this.prefix = prefix; - } - } - - public static class ExtractTableTask { - - private String schemaName; - private String prefix; - private String[] tableName; - - public String getSchemaName() { - return schemaName; - } - - public void setSchemaName(String schemaName) { - this.schemaName = schemaName; - } - - public String getPrefix() { - return prefix; - } - - public void setPrefix(String prefix) { - this.prefix = prefix; - } - - public String[] getTableName() { - return tableName; - } - - public void setTableName(String[] tableName) { - this.tableName = tableName; - } - } + private List<DropTableTasks> dropTableTasks = List.of(); + private List<RenameForeignKeyTasks> renameForeignKeyTasks = List.of(); public List<ExtractSchemaTask> getSchemaTasks() { return schemaTasks; @@ -103,4 +53,20 @@ public class MigrationScriptTask { public void setTableTasks(List<ExtractTableTask> tableTasks) { this.tableTasks = tableTasks; } + + public List<DropTableTasks> getDropTableTasks() { + return dropTableTasks; + } + + public void setDropTableTasks(List<DropTableTasks> dropTableTasks) { + this.dropTableTasks = Objects.requireNonNull(dropTableTasks); + } + + public List<RenameForeignKeyTasks> getRenameForeignKeyTasks() { + return renameForeignKeyTasks; + } + + public void setRenameForeignKeyTasks(List<RenameForeignKeyTasks> renameForeignKeyTasks) { + this.renameForeignKeyTasks = renameForeignKeyTasks; + } } ===================================== toolkit/plugin/src/main/java/fr/ird/observe/toolkit/maven/plugin/persistence/RenameForeignKeyTask.java ===================================== @@ -0,0 +1,74 @@ +package fr.ird.observe.toolkit.maven.plugin.persistence; + +/*- + * #%L + * ObServe Toolkit :: Maven plugin + * %% + * Copyright (C) 2008 - 2024 IRD, Ultreia.io + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +/** + * Created at 12/01/2024. + * + * @author Tony Chemit - dev@tchemit.fr + * @since 9.3.0 + */ +public class RenameForeignKeyTask { + private static final String RENAME_FOREIGN_KEY = "-- Rename foreign key %1$s.%2$s.%3$s → %4$s\n" + + "ALTER TABLE %1$s.%2$s ADD COLUMN %4$s VARCHAR(255);\n" + + "ALTER TABLE %1$s.%2$s ADD CONSTRAINT fk_%1$s_%2$s_%4$s FOREIGN KEY (%4$s) REFERENCES %1$s.%4$s;\n" + + "CREATE INDEX idx_%1$s_%2$s_%4$s ON %1$s.%2$s(%4$s);\n" + + "UPDATE %1$s.%2$s SET %4$s = REPLACE(set, '.%3$s', '.%4$s');\n" + + "ALTER TABLE %1$s.%2$s ALTER COLUMN %4$s SET NOT NULL;\n" + + "ALTER TABLE %1$s.%2$s DROP COLUMN %3$s%5$s;"; + private static final String RENAME_FOREIGN_KEY_WITH_IDX = "-- Rename foreign key %1$s.%2$s.%3$s → %4$s (and %3$s_idx → %4$s_idx)\n" + + "ALTER TABLE %1$s.%2$s ADD COLUMN %4$s VARCHAR(255);\n" + + "ALTER TABLE %1$s.%2$s ADD COLUMN %4$s_idx INTEGER;\n" + + "ALTER TABLE %1$s.%2$s ADD CONSTRAINT fk_%1$s_%2$s_%4$s FOREIGN KEY (%4$s) REFERENCES %1$s.%4$s;\n" + + "CREATE INDEX idx_%1$s_%2$s_%4$s ON %1$s.%2$s(%4$s);\n" + + "UPDATE %1$s.%2$s SET %4$s = REPLACE(set, '.%3$s', '.%4$s'), %4$s_idx = %3$s_idx;\n" + + "ALTER TABLE %1$s.%2$s ALTER COLUMN %4$s SET NOT NULL;\n" + + "ALTER TABLE %1$s.%2$s ALTER COLUMN %4$s_idx SET NOT NULL;\n" + + "ALTER TABLE %1$s.%2$s DROP COLUMN %3$s%5$s;\n" + + "ALTER TABLE %1$s.%2$s DROP COLUMN %3$s_idx%5$s;"; + private final String schemaName; + private final String tableName; + private final boolean useIndex; + + public RenameForeignKeyTask(String schemaName, String tableName, boolean useIndex) { + this.schemaName = schemaName; + this.tableName = tableName; + this.useIndex = useIndex; + } + + public String getSchemaName() { + return schemaName; + } + + public String getTableName() { + return tableName; + } + + public boolean isUseIndex() { + return useIndex; + } + + public String toSql(boolean h2, String oldColumnName, String newColumnName) { + return String.format(isUseIndex() ? RENAME_FOREIGN_KEY_WITH_IDX : RENAME_FOREIGN_KEY, getSchemaName(), getTableName(), oldColumnName, newColumnName, h2 ? "" : " CASCADE"); + } +} ===================================== toolkit/plugin/src/main/java/fr/ird/observe/toolkit/maven/plugin/persistence/RenameForeignKeyTasks.java ===================================== @@ -0,0 +1,73 @@ +package fr.ird.observe.toolkit.maven.plugin.persistence; + +/*- + * #%L + * ObServe Toolkit :: Maven plugin + * %% + * Copyright (C) 2008 - 2024 IRD, Ultreia.io + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +import java.util.LinkedList; +import java.util.List; + +/** + * Created at 12/01/2024. + * + * @author Tony Chemit - dev@tchemit.fr + * @since 9.3.0 + */ +public class RenameForeignKeyTasks { + private final String prefix; + private final String oldColumnName; + private final String newColumnName; + private List<RenameForeignKeyTask> tasks = List.of(); + + public RenameForeignKeyTasks(String prefix, String oldColumnName, String newColumnName) { + this.prefix = prefix; + this.oldColumnName = oldColumnName; + this.newColumnName = newColumnName; + } + + public String getPrefix() { + return prefix; + } + + public String getOldColumnName() { + return oldColumnName; + } + + public String getNewColumnName() { + return newColumnName; + } + + public List<RenameForeignKeyTask> getTasks() { + return tasks; + } + + public void setTasks(List<RenameForeignKeyTask> tasks) { + this.tasks = tasks; + } + + public List<String> toSql(boolean h2) { + List<String> statements = new LinkedList<>(); + for (RenameForeignKeyTask task : getTasks()) { + statements.add(task.toSql(h2, getOldColumnName(), getNewColumnName())); + } + return statements; + } +} View it on GitLab: https://gitlab.com/ultreiaio/ird-observe/-/compare/58e4b57332eb2d0c0e3516d52... -- View it on GitLab: https://gitlab.com/ultreiaio/ird-observe/-/compare/58e4b57332eb2d0c0e3516d52... You're receiving this email because of your account on gitlab.com.
participants (1)
-
Tony CHEMIT (@tchemit)