Tony CHEMIT pushed to branch develop at ultreiaio / ird-observe
Commits:
-
86160984
by Tony Chemit at 2024-01-18T09:25:25+01:00
-
5457da89
by Tony Chemit at 2024-01-18T09:25:27+01:00
-
58e4b573
by Tony Chemit at 2024-01-18T09:25:30+01:00
10 changed files:
- 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:
| ... | ... | @@ -23,7 +23,7 @@ |
| 23 | 23 | <parent>
|
| 24 | 24 | <groupId>io.ultreia.maven</groupId>
|
| 25 | 25 | <artifactId>pom</artifactId>
|
| 26 | - <version>2023.59</version>
|
|
| 26 | + <version>2024.01</version>
|
|
| 27 | 27 | </parent>
|
| 28 | 28 | <groupId>fr.ird.observe</groupId>
|
| 29 | 29 | <artifactId>ird-observe</artifactId>
|
| ... | ... | @@ -261,7 +261,7 @@ public class MigrationVersionResourceExecutor implements AutoCloseable { |
| 261 | 261 | return classLoader;
|
| 262 | 262 | }
|
| 263 | 263 | |
| 264 | - protected boolean isPG() {
|
|
| 264 | + public boolean isPG() {
|
|
| 265 | 265 | return PGTopiaSqlDllSupportImpl.CLASSIFIER.equals(classifier);
|
| 266 | 266 | }
|
| 267 | 267 |
| 1 | +package fr.ird.observe.toolkit.maven.plugin.persistence;
|
|
| 2 | + |
|
| 3 | +/*-
|
|
| 4 | + * #%L
|
|
| 5 | + * ObServe Toolkit :: Maven plugin
|
|
| 6 | + * %%
|
|
| 7 | + * Copyright (C) 2008 - 2024 IRD, Ultreia.io
|
|
| 8 | + * %%
|
|
| 9 | + * This program is free software: you can redistribute it and/or modify
|
|
| 10 | + * it under the terms of the GNU General Public License as
|
|
| 11 | + * published by the Free Software Foundation, either version 3 of the
|
|
| 12 | + * License, or (at your option) any later version.
|
|
| 13 | + *
|
|
| 14 | + * This program is distributed in the hope that it will be useful,
|
|
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
| 17 | + * GNU General Public License for more details.
|
|
| 18 | + *
|
|
| 19 | + * You should have received a copy of the GNU General Public
|
|
| 20 | + * License along with this program. If not, see
|
|
| 21 | + * <http://www.gnu.org/licenses/gpl-3.0.html>.
|
|
| 22 | + * #L%
|
|
| 23 | + */
|
|
| 24 | + |
|
| 25 | +/**
|
|
| 26 | + * Created at 12/01/2024.
|
|
| 27 | + *
|
|
| 28 | + * @author Tony Chemit - dev@tchemit.fr
|
|
| 29 | + * @since 9.3.0
|
|
| 30 | + */
|
|
| 31 | +public class DropTableTask {
|
|
| 32 | + |
|
| 33 | + private String schemaName;
|
|
| 34 | + private String[] tableName;
|
|
| 35 | + |
|
| 36 | + public String getSchemaName() {
|
|
| 37 | + return schemaName;
|
|
| 38 | + }
|
|
| 39 | + |
|
| 40 | + public void setSchemaName(String schemaName) {
|
|
| 41 | + this.schemaName = schemaName;
|
|
| 42 | + }
|
|
| 43 | + |
|
| 44 | + public String[] getTableName() {
|
|
| 45 | + return tableName;
|
|
| 46 | + }
|
|
| 47 | + |
|
| 48 | + public void setTableName(String[] tableName) {
|
|
| 49 | + this.tableName = tableName;
|
|
| 50 | + }
|
|
| 51 | +} |
| 1 | +package fr.ird.observe.toolkit.maven.plugin.persistence;
|
|
| 2 | + |
|
| 3 | +/*-
|
|
| 4 | + * #%L
|
|
| 5 | + * ObServe Toolkit :: Maven plugin
|
|
| 6 | + * %%
|
|
| 7 | + * Copyright (C) 2008 - 2024 IRD, Ultreia.io
|
|
| 8 | + * %%
|
|
| 9 | + * This program is free software: you can redistribute it and/or modify
|
|
| 10 | + * it under the terms of the GNU General Public License as
|
|
| 11 | + * published by the Free Software Foundation, either version 3 of the
|
|
| 12 | + * License, or (at your option) any later version.
|
|
| 13 | + *
|
|
| 14 | + * This program is distributed in the hope that it will be useful,
|
|
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
| 17 | + * GNU General Public License for more details.
|
|
| 18 | + *
|
|
| 19 | + * You should have received a copy of the GNU General Public
|
|
| 20 | + * License along with this program. If not, see
|
|
| 21 | + * <http://www.gnu.org/licenses/gpl-3.0.html>.
|
|
| 22 | + * #L%
|
|
| 23 | + */
|
|
| 24 | + |
|
| 25 | +import java.util.LinkedList;
|
|
| 26 | +import java.util.List;
|
|
| 27 | + |
|
| 28 | +/**
|
|
| 29 | + * Created at 12/01/2024.
|
|
| 30 | + *
|
|
| 31 | + * @author Tony Chemit - dev@tchemit.fr
|
|
| 32 | + * @since 9.3.0
|
|
| 33 | + */
|
|
| 34 | +public class DropTableTasks {
|
|
| 35 | + private String prefix;
|
|
| 36 | + private List<DropTableTask> tasks = List.of();
|
|
| 37 | + |
|
| 38 | + public String getPrefix() {
|
|
| 39 | + return prefix;
|
|
| 40 | + }
|
|
| 41 | + |
|
| 42 | + public void setPrefix(String prefix) {
|
|
| 43 | + this.prefix = prefix;
|
|
| 44 | + }
|
|
| 45 | + |
|
| 46 | + public List<DropTableTask> getTasks() {
|
|
| 47 | + return tasks;
|
|
| 48 | + }
|
|
| 49 | + |
|
| 50 | + public void setTasks(List<DropTableTask> tasks) {
|
|
| 51 | + this.tasks = tasks;
|
|
| 52 | + }
|
|
| 53 | + |
|
| 54 | + public List<String> toSql(boolean h2) {
|
|
| 55 | + String addCascadeClause = h2 ? "" : " CASCADE";
|
|
| 56 | + List<String> statements = new LinkedList<>();
|
|
| 57 | + for (DropTableTask task : getTasks()) {
|
|
| 58 | + String schemaName = task.getSchemaName();
|
|
| 59 | + for (String name : task.getTableName()) {
|
|
| 60 | + String tableName = name.toLowerCase();
|
|
| 61 | + statements.add(String.format("DROP TABLE %s.%s%s;", schemaName, tableName, addCascadeClause));
|
|
| 62 | + }
|
|
| 63 | + }
|
|
| 64 | + return statements;
|
|
| 65 | + }
|
|
| 66 | +} |
| 1 | +package fr.ird.observe.toolkit.maven.plugin.persistence;
|
|
| 2 | + |
|
| 3 | +/*-
|
|
| 4 | + * #%L
|
|
| 5 | + * ObServe Toolkit :: Maven plugin
|
|
| 6 | + * %%
|
|
| 7 | + * Copyright (C) 2008 - 2024 IRD, Ultreia.io
|
|
| 8 | + * %%
|
|
| 9 | + * This program is free software: you can redistribute it and/or modify
|
|
| 10 | + * it under the terms of the GNU General Public License as
|
|
| 11 | + * published by the Free Software Foundation, either version 3 of the
|
|
| 12 | + * License, or (at your option) any later version.
|
|
| 13 | + *
|
|
| 14 | + * This program is distributed in the hope that it will be useful,
|
|
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
| 17 | + * GNU General Public License for more details.
|
|
| 18 | + *
|
|
| 19 | + * You should have received a copy of the GNU General Public
|
|
| 20 | + * License along with this program. If not, see
|
|
| 21 | + * <http://www.gnu.org/licenses/gpl-3.0.html>.
|
|
| 22 | + * #L%
|
|
| 23 | + */
|
|
| 24 | + |
|
| 25 | +/**
|
|
| 26 | + * Created at 12/01/2024.
|
|
| 27 | + *
|
|
| 28 | + * @author Tony Chemit - dev@tchemit.fr
|
|
| 29 | + * @since
|
|
| 30 | + */
|
|
| 31 | +public class ExtractSchemaTask {
|
|
| 32 | + |
|
| 33 | + private String schemaName;
|
|
| 34 | + private String prefix;
|
|
| 35 | + |
|
| 36 | + public String getSchemaName() {
|
|
| 37 | + return schemaName;
|
|
| 38 | + }
|
|
| 39 | + |
|
| 40 | + public void setSchemaName(String schemaName) {
|
|
| 41 | + this.schemaName = schemaName;
|
|
| 42 | + }
|
|
| 43 | + |
|
| 44 | + public String getPrefix() {
|
|
| 45 | + return prefix;
|
|
| 46 | + }
|
|
| 47 | + |
|
| 48 | + public void setPrefix(String prefix) {
|
|
| 49 | + this.prefix = prefix;
|
|
| 50 | + }
|
|
| 51 | +} |
| 1 | +package fr.ird.observe.toolkit.maven.plugin.persistence;
|
|
| 2 | + |
|
| 3 | +/*-
|
|
| 4 | + * #%L
|
|
| 5 | + * ObServe Toolkit :: Maven plugin
|
|
| 6 | + * %%
|
|
| 7 | + * Copyright (C) 2008 - 2024 IRD, Ultreia.io
|
|
| 8 | + * %%
|
|
| 9 | + * This program is free software: you can redistribute it and/or modify
|
|
| 10 | + * it under the terms of the GNU General Public License as
|
|
| 11 | + * published by the Free Software Foundation, either version 3 of the
|
|
| 12 | + * License, or (at your option) any later version.
|
|
| 13 | + *
|
|
| 14 | + * This program is distributed in the hope that it will be useful,
|
|
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
| 17 | + * GNU General Public License for more details.
|
|
| 18 | + *
|
|
| 19 | + * You should have received a copy of the GNU General Public
|
|
| 20 | + * License along with this program. If not, see
|
|
| 21 | + * <http://www.gnu.org/licenses/gpl-3.0.html>.
|
|
| 22 | + * #L%
|
|
| 23 | + */
|
|
| 24 | + |
|
| 25 | +/**
|
|
| 26 | + * Created at 12/01/2024.
|
|
| 27 | + *
|
|
| 28 | + * @author Tony Chemit - dev@tchemit.fr
|
|
| 29 | + * @since 9.3.0
|
|
| 30 | + */
|
|
| 31 | +public class ExtractTableTask {
|
|
| 32 | + |
|
| 33 | + private String schemaName;
|
|
| 34 | + private String prefix;
|
|
| 35 | + private String[] tableName;
|
|
| 36 | + |
|
| 37 | + public String getSchemaName() {
|
|
| 38 | + return schemaName;
|
|
| 39 | + }
|
|
| 40 | + |
|
| 41 | + public void setSchemaName(String schemaName) {
|
|
| 42 | + this.schemaName = schemaName;
|
|
| 43 | + }
|
|
| 44 | + |
|
| 45 | + public String getPrefix() {
|
|
| 46 | + return prefix;
|
|
| 47 | + }
|
|
| 48 | + |
|
| 49 | + public void setPrefix(String prefix) {
|
|
| 50 | + this.prefix = prefix;
|
|
| 51 | + }
|
|
| 52 | + |
|
| 53 | + public String[] getTableName() {
|
|
| 54 | + return tableName;
|
|
| 55 | + }
|
|
| 56 | + |
|
| 57 | + public void setTableName(String[] tableName) {
|
|
| 58 | + this.tableName = tableName;
|
|
| 59 | + }
|
|
| 60 | +} |
| ... | ... | @@ -96,14 +96,14 @@ public class GenerateMigrationScriptRunner extends PersistenceRunner { |
| 96 | 96 | }
|
| 97 | 97 | PersistenceScriptHelper.write(scriptPath, context, String.format("empty-%%s-%s", scriptSuffix), "", true);
|
| 98 | 98 | |
| 99 | - for (MigrationScriptTask.ExtractSchemaTask task : task.getSchemaTasks()) {
|
|
| 99 | + for (ExtractSchemaTask task : task.getSchemaTasks()) {
|
|
| 100 | 100 | String prefix = task.getPrefix();
|
| 101 | 101 | String schemaName = task.getSchemaName();
|
| 102 | 102 | String baseName = String.format("%s_%%s-%s-%s", prefix, schemaName, scriptSuffix);
|
| 103 | 103 | ScriptSplitter.SplitSimpleResult filterOnSchema = ScriptSplitter.filterSchema(context, schemaName);
|
| 104 | 104 | PersistenceScriptHelper.write(scriptPath, filterOnSchema, baseName, "-schema", false);
|
| 105 | 105 | }
|
| 106 | - for (MigrationScriptTask.ExtractTableTask task : task.getTableTasks()) {
|
|
| 106 | + for (ExtractTableTask task : task.getTableTasks()) {
|
|
| 107 | 107 | String prefix = task.getPrefix();
|
| 108 | 108 | String schemaName = task.getSchemaName();
|
| 109 | 109 | for (String name : task.getTableName()) {
|
| ... | ... | @@ -113,6 +113,13 @@ public class GenerateMigrationScriptRunner extends PersistenceRunner { |
| 113 | 113 | PersistenceScriptHelper.write(scriptPath, filterOnSchema, baseName, "-table", false);
|
| 114 | 114 | }
|
| 115 | 115 | }
|
| 116 | + for (DropTableTasks task : task.getDropTableTasks()) {
|
|
| 117 | + String baseName = String.format("%s_drop-tasks-%s", Objects.requireNonNull(task.getPrefix()), scriptSuffix);
|
|
| 118 | + PersistenceScriptHelper.writeScript(scriptPath.resolve(baseName), task.toSql(h2));
|
|
| 119 | + }
|
|
| 120 | + for (RenameForeignKeyTasks task : task.getRenameForeignKeyTasks()) {
|
|
| 121 | + String baseName = String.format("%s_rename-foreign-key-%s", Objects.requireNonNull(task.getPrefix()), scriptSuffix);
|
|
| 122 | + PersistenceScriptHelper.writeScript(scriptPath.resolve(baseName), task.toSql(h2));
|
|
| 123 | + }
|
|
| 116 | 124 | }
|
| 117 | - |
|
| 118 | 125 | } |
| ... | ... | @@ -23,6 +23,7 @@ package fr.ird.observe.toolkit.maven.plugin.persistence; |
| 23 | 23 | */
|
| 24 | 24 | |
| 25 | 25 | import java.util.List;
|
| 26 | +import java.util.Objects;
|
|
| 26 | 27 | |
| 27 | 28 | /**
|
| 28 | 29 | * Created on 24/11/2021.
|
| ... | ... | @@ -34,59 +35,8 @@ public class MigrationScriptTask { |
| 34 | 35 | |
| 35 | 36 | private List<ExtractSchemaTask> schemaTasks = List.of();
|
| 36 | 37 | private List<ExtractTableTask> tableTasks = List.of();
|
| 37 | - |
|
| 38 | - public static class ExtractSchemaTask {
|
|
| 39 | - |
|
| 40 | - private String schemaName;
|
|
| 41 | - private String prefix;
|
|
| 42 | - |
|
| 43 | - public String getSchemaName() {
|
|
| 44 | - return schemaName;
|
|
| 45 | - }
|
|
| 46 | - |
|
| 47 | - public void setSchemaName(String schemaName) {
|
|
| 48 | - this.schemaName = schemaName;
|
|
| 49 | - }
|
|
| 50 | - |
|
| 51 | - public String getPrefix() {
|
|
| 52 | - return prefix;
|
|
| 53 | - }
|
|
| 54 | - |
|
| 55 | - public void setPrefix(String prefix) {
|
|
| 56 | - this.prefix = prefix;
|
|
| 57 | - }
|
|
| 58 | - }
|
|
| 59 | - |
|
| 60 | - public static class ExtractTableTask {
|
|
| 61 | - |
|
| 62 | - private String schemaName;
|
|
| 63 | - private String prefix;
|
|
| 64 | - private String[] tableName;
|
|
| 65 | - |
|
| 66 | - public String getSchemaName() {
|
|
| 67 | - return schemaName;
|
|
| 68 | - }
|
|
| 69 | - |
|
| 70 | - public void setSchemaName(String schemaName) {
|
|
| 71 | - this.schemaName = schemaName;
|
|
| 72 | - }
|
|
| 73 | - |
|
| 74 | - public String getPrefix() {
|
|
| 75 | - return prefix;
|
|
| 76 | - }
|
|
| 77 | - |
|
| 78 | - public void setPrefix(String prefix) {
|
|
| 79 | - this.prefix = prefix;
|
|
| 80 | - }
|
|
| 81 | - |
|
| 82 | - public String[] getTableName() {
|
|
| 83 | - return tableName;
|
|
| 84 | - }
|
|
| 85 | - |
|
| 86 | - public void setTableName(String[] tableName) {
|
|
| 87 | - this.tableName = tableName;
|
|
| 88 | - }
|
|
| 89 | - }
|
|
| 38 | + private List<DropTableTasks> dropTableTasks = List.of();
|
|
| 39 | + private List<RenameForeignKeyTasks> renameForeignKeyTasks = List.of();
|
|
| 90 | 40 | |
| 91 | 41 | public List<ExtractSchemaTask> getSchemaTasks() {
|
| 92 | 42 | return schemaTasks;
|
| ... | ... | @@ -103,4 +53,20 @@ public class MigrationScriptTask { |
| 103 | 53 | public void setTableTasks(List<ExtractTableTask> tableTasks) {
|
| 104 | 54 | this.tableTasks = tableTasks;
|
| 105 | 55 | }
|
| 56 | + |
|
| 57 | + public List<DropTableTasks> getDropTableTasks() {
|
|
| 58 | + return dropTableTasks;
|
|
| 59 | + }
|
|
| 60 | + |
|
| 61 | + public void setDropTableTasks(List<DropTableTasks> dropTableTasks) {
|
|
| 62 | + this.dropTableTasks = Objects.requireNonNull(dropTableTasks);
|
|
| 63 | + }
|
|
| 64 | + |
|
| 65 | + public List<RenameForeignKeyTasks> getRenameForeignKeyTasks() {
|
|
| 66 | + return renameForeignKeyTasks;
|
|
| 67 | + }
|
|
| 68 | + |
|
| 69 | + public void setRenameForeignKeyTasks(List<RenameForeignKeyTasks> renameForeignKeyTasks) {
|
|
| 70 | + this.renameForeignKeyTasks = renameForeignKeyTasks;
|
|
| 71 | + }
|
|
| 106 | 72 | } |
| 1 | +package fr.ird.observe.toolkit.maven.plugin.persistence;
|
|
| 2 | + |
|
| 3 | +/*-
|
|
| 4 | + * #%L
|
|
| 5 | + * ObServe Toolkit :: Maven plugin
|
|
| 6 | + * %%
|
|
| 7 | + * Copyright (C) 2008 - 2024 IRD, Ultreia.io
|
|
| 8 | + * %%
|
|
| 9 | + * This program is free software: you can redistribute it and/or modify
|
|
| 10 | + * it under the terms of the GNU General Public License as
|
|
| 11 | + * published by the Free Software Foundation, either version 3 of the
|
|
| 12 | + * License, or (at your option) any later version.
|
|
| 13 | + *
|
|
| 14 | + * This program is distributed in the hope that it will be useful,
|
|
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
| 17 | + * GNU General Public License for more details.
|
|
| 18 | + *
|
|
| 19 | + * You should have received a copy of the GNU General Public
|
|
| 20 | + * License along with this program. If not, see
|
|
| 21 | + * <http://www.gnu.org/licenses/gpl-3.0.html>.
|
|
| 22 | + * #L%
|
|
| 23 | + */
|
|
| 24 | + |
|
| 25 | +/**
|
|
| 26 | + * Created at 12/01/2024.
|
|
| 27 | + *
|
|
| 28 | + * @author Tony Chemit - dev@tchemit.fr
|
|
| 29 | + * @since 9.3.0
|
|
| 30 | + */
|
|
| 31 | +public class RenameForeignKeyTask {
|
|
| 32 | + private static final String RENAME_FOREIGN_KEY = "-- Rename foreign key %1$s.%2$s.%3$s → %4$s\n" +
|
|
| 33 | + "ALTER TABLE %1$s.%2$s ADD COLUMN %4$s VARCHAR(255);\n" +
|
|
| 34 | + "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" +
|
|
| 35 | + "CREATE INDEX idx_%1$s_%2$s_%4$s ON %1$s.%2$s(%4$s);\n" +
|
|
| 36 | + "UPDATE %1$s.%2$s SET %4$s = REPLACE(set, '.%3$s', '.%4$s');\n" +
|
|
| 37 | + "ALTER TABLE %1$s.%2$s ALTER COLUMN %4$s SET NOT NULL;\n" +
|
|
| 38 | + "ALTER TABLE %1$s.%2$s DROP COLUMN %3$s%5$s;";
|
|
| 39 | + 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" +
|
|
| 40 | + "ALTER TABLE %1$s.%2$s ADD COLUMN %4$s VARCHAR(255);\n" +
|
|
| 41 | + "ALTER TABLE %1$s.%2$s ADD COLUMN %4$s_idx INTEGER;\n" +
|
|
| 42 | + "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" +
|
|
| 43 | + "CREATE INDEX idx_%1$s_%2$s_%4$s ON %1$s.%2$s(%4$s);\n" +
|
|
| 44 | + "UPDATE %1$s.%2$s SET %4$s = REPLACE(set, '.%3$s', '.%4$s'), %4$s_idx = %3$s_idx;\n" +
|
|
| 45 | + "ALTER TABLE %1$s.%2$s ALTER COLUMN %4$s SET NOT NULL;\n" +
|
|
| 46 | + "ALTER TABLE %1$s.%2$s ALTER COLUMN %4$s_idx SET NOT NULL;\n" +
|
|
| 47 | + "ALTER TABLE %1$s.%2$s DROP COLUMN %3$s%5$s;\n" +
|
|
| 48 | + "ALTER TABLE %1$s.%2$s DROP COLUMN %3$s_idx%5$s;";
|
|
| 49 | + private final String schemaName;
|
|
| 50 | + private final String tableName;
|
|
| 51 | + private final boolean useIndex;
|
|
| 52 | + |
|
| 53 | + public RenameForeignKeyTask(String schemaName, String tableName, boolean useIndex) {
|
|
| 54 | + this.schemaName = schemaName;
|
|
| 55 | + this.tableName = tableName;
|
|
| 56 | + this.useIndex = useIndex;
|
|
| 57 | + }
|
|
| 58 | + |
|
| 59 | + public String getSchemaName() {
|
|
| 60 | + return schemaName;
|
|
| 61 | + }
|
|
| 62 | + |
|
| 63 | + public String getTableName() {
|
|
| 64 | + return tableName;
|
|
| 65 | + }
|
|
| 66 | + |
|
| 67 | + public boolean isUseIndex() {
|
|
| 68 | + return useIndex;
|
|
| 69 | + }
|
|
| 70 | + |
|
| 71 | + public String toSql(boolean h2, String oldColumnName, String newColumnName) {
|
|
| 72 | + return String.format(isUseIndex() ? RENAME_FOREIGN_KEY_WITH_IDX : RENAME_FOREIGN_KEY, getSchemaName(), getTableName(), oldColumnName, newColumnName, h2 ? "" : " CASCADE");
|
|
| 73 | + }
|
|
| 74 | +} |
| 1 | +package fr.ird.observe.toolkit.maven.plugin.persistence;
|
|
| 2 | + |
|
| 3 | +/*-
|
|
| 4 | + * #%L
|
|
| 5 | + * ObServe Toolkit :: Maven plugin
|
|
| 6 | + * %%
|
|
| 7 | + * Copyright (C) 2008 - 2024 IRD, Ultreia.io
|
|
| 8 | + * %%
|
|
| 9 | + * This program is free software: you can redistribute it and/or modify
|
|
| 10 | + * it under the terms of the GNU General Public License as
|
|
| 11 | + * published by the Free Software Foundation, either version 3 of the
|
|
| 12 | + * License, or (at your option) any later version.
|
|
| 13 | + *
|
|
| 14 | + * This program is distributed in the hope that it will be useful,
|
|
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
| 17 | + * GNU General Public License for more details.
|
|
| 18 | + *
|
|
| 19 | + * You should have received a copy of the GNU General Public
|
|
| 20 | + * License along with this program. If not, see
|
|
| 21 | + * <http://www.gnu.org/licenses/gpl-3.0.html>.
|
|
| 22 | + * #L%
|
|
| 23 | + */
|
|
| 24 | + |
|
| 25 | +import java.util.LinkedList;
|
|
| 26 | +import java.util.List;
|
|
| 27 | + |
|
| 28 | +/**
|
|
| 29 | + * Created at 12/01/2024.
|
|
| 30 | + *
|
|
| 31 | + * @author Tony Chemit - dev@tchemit.fr
|
|
| 32 | + * @since 9.3.0
|
|
| 33 | + */
|
|
| 34 | +public class RenameForeignKeyTasks {
|
|
| 35 | + private final String prefix;
|
|
| 36 | + private final String oldColumnName;
|
|
| 37 | + private final String newColumnName;
|
|
| 38 | + private List<RenameForeignKeyTask> tasks = List.of();
|
|
| 39 | + |
|
| 40 | + public RenameForeignKeyTasks(String prefix, String oldColumnName, String newColumnName) {
|
|
| 41 | + this.prefix = prefix;
|
|
| 42 | + this.oldColumnName = oldColumnName;
|
|
| 43 | + this.newColumnName = newColumnName;
|
|
| 44 | + }
|
|
| 45 | + |
|
| 46 | + public String getPrefix() {
|
|
| 47 | + return prefix;
|
|
| 48 | + }
|
|
| 49 | + |
|
| 50 | + public String getOldColumnName() {
|
|
| 51 | + return oldColumnName;
|
|
| 52 | + }
|
|
| 53 | + |
|
| 54 | + public String getNewColumnName() {
|
|
| 55 | + return newColumnName;
|
|
| 56 | + }
|
|
| 57 | + |
|
| 58 | + public List<RenameForeignKeyTask> getTasks() {
|
|
| 59 | + return tasks;
|
|
| 60 | + }
|
|
| 61 | + |
|
| 62 | + public void setTasks(List<RenameForeignKeyTask> tasks) {
|
|
| 63 | + this.tasks = tasks;
|
|
| 64 | + }
|
|
| 65 | + |
|
| 66 | + public List<String> toSql(boolean h2) {
|
|
| 67 | + List<String> statements = new LinkedList<>();
|
|
| 68 | + for (RenameForeignKeyTask task : getTasks()) {
|
|
| 69 | + statements.add(task.toSql(h2, getOldColumnName(), getNewColumnName()));
|
|
| 70 | + }
|
|
| 71 | + return statements;
|
|
| 72 | + }
|
|
| 73 | +} |