Tony CHEMIT pushed to branch develop at ultreiaio / ird-observe Commits: 064d69ff by Tony Chemit at 2021-01-20T21:13:20+01:00 Introduce a way to extract schema or table from generated schema (help to create new safe migration for v9) - - - - - 4 changed files: - toolkit/maven-plugin/src/main/java/fr/ird/observe/maven/plugins/toolbox/ExecuteRunnerMojo.java - + toolkit/maven-plugin/src/main/java/fr/ird/observe/maven/plugins/toolbox/persistence/ExtractSchema.java - + toolkit/maven-plugin/src/main/java/fr/ird/observe/maven/plugins/toolbox/persistence/ExtractTable.java - toolkit/maven-plugin/src/main/java/fr/ird/observe/maven/plugins/toolbox/persistence/GenerateEmptyDatabases.java Changes: ===================================== toolkit/maven-plugin/src/main/java/fr/ird/observe/maven/plugins/toolbox/ExecuteRunnerMojo.java ===================================== @@ -33,6 +33,7 @@ import java.io.File; import java.net.URLClassLoader; import java.nio.file.Path; import java.util.List; +import java.util.Map; /** * To execute the given @@ -76,11 +77,15 @@ public class ExecuteRunnerMojo extends ToolboxMojoSupport { @Parameter(defaultValue = "${project.build.directory}/generated-sources/java", required = true) private File targetRoot; + @Parameter + private Map<String, String> extraProperties; + public static abstract class MojoRunnable implements Runnable { protected I18nKeySet getterFile; protected Log log; private boolean force; + private Map<String, String> extraProperties; public I18nKeySet getGetterFile() { return getterFile; @@ -105,6 +110,14 @@ public class ExecuteRunnerMojo extends ToolboxMojoSupport { public void setForce(boolean force) { this.force = force; } + + public Map<String, String> getExtraProperties() { + return extraProperties; + } + + public void setExtraProperties(Map<String, String> extraProperties) { + this.extraProperties = extraProperties; + } } @Override @@ -136,6 +149,7 @@ public class ExecuteRunnerMojo extends ToolboxMojoSupport { MojoRunnable o = (MojoRunnable) urlClassLoader.loadClass(runnerType).getConstructor().newInstance(); o.setGetterFile(getterFile); + o.setExtraProperties(extraProperties); o.setLog(getLog()); o.run(); } ===================================== toolkit/maven-plugin/src/main/java/fr/ird/observe/maven/plugins/toolbox/persistence/ExtractSchema.java ===================================== @@ -0,0 +1,108 @@ +package fr.ird.observe.maven.plugins.toolbox.persistence; + +/*- + * #%L + * ObServe Toolkit :: Maven plugin + * %% + * Copyright (C) 2008 - 2021 IRD, Code Lutin, 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 fr.ird.observe.maven.plugins.toolbox.ExecuteRunnerMojo; +import org.nuiton.topia.persistence.script.TopiaSqlScript; +import org.nuiton.topia.service.migration.resources.MigrationVersionResourceProvider; +import org.nuiton.version.Version; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; +import java.util.LinkedList; +import java.util.List; + +/** + * To extract a single schema DDL into a specific sql file. + * <p> + * Created on 20/01/2021. + * + * @author Tony Chemit - dev@tchemit.fr + * @since 8.0.5 + */ +public class ExtractSchema extends ExecuteRunnerMojo.MojoRunnable { + + @Override + public void run() { + + Path targetDirectory = new File(System.getProperty("compile.target.directory")).toPath(); + Path sourceDirectory = new File(System.getProperty("compile.source.directory")).toPath(); + + String schemaName = getExtraProperties().get("schemaName"); + + Version modelVersion = MigrationVersionResourceProvider.get().getLastVersion(); + + Path dbRootPath = sourceDirectory.getParent().resolve("resources").resolve("db").resolve("migration").resolve(modelVersion.getVersion()); + Path targetDbRootPath = targetDirectory.resolve("db").resolve("migration").resolve(modelVersion.getVersion()); + try { + processScript(true, dbRootPath.resolve("observe_full-schema-H2.sql"), targetDbRootPath, schemaName); + processScript(false, dbRootPath.resolve("observe_full-schema-PG.sql"), targetDbRootPath, schemaName); + } catch (Exception e) { + throw new IllegalStateException(e); + } + } + + protected void processScript(boolean h2, Path incomingFullSchema, Path targetDbRootPath, String schemaName) throws IOException { + Path pathFullSchema = incomingFullSchema.getParent().resolve(incomingFullSchema.toFile().getName().replace("-schema-", "-schema-" + schemaName+"-")); + log.info(String.format("Generating schema %s schema [%s] (from %s).", schemaName, pathFullSchema.toFile().getName(), incomingFullSchema)); + TopiaSqlScript script = TopiaSqlScript.of(incomingFullSchema); + Path pathCreateSchema = pathFullSchema.getParent().resolve(pathFullSchema.toFile().getName().replace("full", "create")); + Path pathFinalizeSchema = pathFullSchema.getParent().resolve(pathFullSchema.toFile().getName().replace("full", "finalize")); + List<String> createSchemaStatements = new LinkedList<>(); + List<String> finalizeSchemaStatements = new LinkedList<>(); + script.getLocation().forEach(statement -> { + String trim = statement.toLowerCase().trim(); + if (trim.startsWith("create schema " + schemaName) || trim.startsWith("create table " + schemaName)) { + if (!h2) { + statement = statement.replace("data blob", "data OID"); + } + createSchemaStatements.add(statement); + } else { + if (trim.startsWith("alter table " + schemaName) || trim.startsWith("create index idx_" + schemaName)) { + finalizeSchemaStatements.add(statement); + } + } + }); + + int createStatementCount = createSchemaStatements.size(); + int finalizeStatementCount = finalizeSchemaStatements.size(); + + log.info(String.format("Generated create schema (%d statements) at %s.", createStatementCount, pathCreateSchema)); + Files.write(pathCreateSchema, createSchemaStatements); + copyToTarget(pathCreateSchema, targetDbRootPath); + + log.info(String.format("Generated finalize schema (%d statements) at %s.", finalizeStatementCount, pathFinalizeSchema)); + Files.write(pathFinalizeSchema, finalizeSchemaStatements); + copyToTarget(pathFinalizeSchema, targetDbRootPath); + } + + private void copyToTarget(Path source, Path targetDbRootPath) throws IOException { + Path target = targetDbRootPath.resolve(source.toFile().getName()); + Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING); + getLog().debug(String.format("Copy from %s → %s", source.toFile().getName(), target)); + } + +} ===================================== toolkit/maven-plugin/src/main/java/fr/ird/observe/maven/plugins/toolbox/persistence/ExtractTable.java ===================================== @@ -0,0 +1,116 @@ +package fr.ird.observe.maven.plugins.toolbox.persistence; + +/*- + * #%L + * ObServe Toolkit :: Maven plugin + * %% + * Copyright (C) 2008 - 2021 IRD, Code Lutin, 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 fr.ird.observe.entities.ObserveTopiaApplicationContextSupport; +import fr.ird.observe.entities.ObserveTopiaConfiguration; +import fr.ird.observe.entities.ObserveTopiaConfigurationFactory; +import fr.ird.observe.maven.plugins.toolbox.ExecuteRunnerMojo; +import fr.ird.observe.services.configuration.topia.ObserveDataSourceConfigurationTopiaH2; +import org.nuiton.topia.persistence.script.TopiaSqlScript; +import org.nuiton.topia.service.migration.resources.MigrationVersionResourceProvider; +import org.nuiton.version.Version; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; +import java.util.LinkedList; +import java.util.List; +import java.util.function.BiFunction; + +/** + * to extract a single table DDL into a specific sql file. + * Created on 20/01/2021. + * + * @author Tony Chemit - dev@tchemit.fr + * @since 8.0.5 + */ +public class ExtractTable extends ExecuteRunnerMojo.MojoRunnable { + + @Override + public void run() { + + Path targetDirectory = new File(System.getProperty("compile.target.directory")).toPath(); + Path sourceDirectory = new File(System.getProperty("compile.source.directory")).toPath(); + + String schemaName = getExtraProperties().get("schemaName"); + String tableName = getExtraProperties().get("tableName"); + + Version modelVersion = MigrationVersionResourceProvider.get().getLastVersion(); + + Path dbRootPath = sourceDirectory.getParent().resolve("resources").resolve("db").resolve("migration").resolve(modelVersion.getVersion()); + Path targetDbRootPath = targetDirectory.resolve("db").resolve("migration").resolve(modelVersion.getVersion()); + try { + processScript(true, dbRootPath.resolve("observe_full-schema-H2.sql"), targetDbRootPath, schemaName, tableName); + processScript(false, dbRootPath.resolve("observe_full-schema-PG.sql"), targetDbRootPath, schemaName, tableName); + } catch (Exception e) { + throw new IllegalStateException(e); + } + } + + protected void processScript(boolean h2, Path incomingFullSchema, Path targetDbRootPath, String schemaName, String tableName) throws IOException { + String suffix = schemaName + "." + tableName; + Path pathFullSchema = incomingFullSchema.getParent().resolve(incomingFullSchema.toFile().getName().replace("-schema-", "-table-" + suffix+"-")); + log.info(String.format("Generating table %s.%s schema [%s] (from %s).", schemaName, tableName, pathFullSchema.toFile().getName(), incomingFullSchema)); + TopiaSqlScript script = TopiaSqlScript.of(incomingFullSchema); + Path pathCreateSchema = pathFullSchema.getParent().resolve(pathFullSchema.toFile().getName().replace("full", "create")); + Path pathFinalizeSchema = pathFullSchema.getParent().resolve(pathFullSchema.toFile().getName().replace("full", "finalize")); + List<String> createSchemaStatements = new LinkedList<>(); + List<String> finalizeSchemaStatements = new LinkedList<>(); + + String suffix2 = schemaName + "_" + tableName; + script.getLocation().forEach(statement -> { + String trim = statement.toLowerCase().trim(); + if (trim.startsWith("create table " + suffix)) { + if (!h2) { + statement = statement.replace("data blob", "data OID"); + } + createSchemaStatements.add(statement); + } else { + if (trim.startsWith("alter table " + suffix) || trim.startsWith("create index idx_" + suffix2)) { + finalizeSchemaStatements.add(statement); + } + } + }); + + int createStatementCount = createSchemaStatements.size(); + int finalizeStatementCount = finalizeSchemaStatements.size(); + + log.info(String.format("Generated create table (%d statements) at %s.", createStatementCount, pathCreateSchema)); + Files.write(pathCreateSchema, createSchemaStatements); + copyToTarget(pathCreateSchema, targetDbRootPath); + + log.info(String.format("Generated finalize table (%d statements) at %s.", finalizeStatementCount, pathFinalizeSchema)); + Files.write(pathFinalizeSchema, finalizeSchemaStatements); + copyToTarget(pathFinalizeSchema, targetDbRootPath); + } + + private void copyToTarget(Path source, Path targetDbRootPath) throws IOException { + Path target = targetDbRootPath.resolve(source.toFile().getName()); + Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING); + getLog().debug(String.format("Copy from %s → %s", source.toFile().getName(), target)); + } +} ===================================== toolkit/maven-plugin/src/main/java/fr/ird/observe/maven/plugins/toolbox/persistence/GenerateEmptyDatabases.java ===================================== @@ -115,11 +115,11 @@ public class GenerateEmptyDatabases extends ExecuteRunnerMojo.MojoRunnable { log.info(String.format("Generated full schema (%d statements) at %s.", fullStatementCount, pathFullSchema)); copyToTarget(pathFullSchema, targetDbRootPath); - log.info(String.format("Generated create schema (%d statements) at %s.", createStatementCount, pathFullSchema)); + log.info(String.format("Generated create schema (%d statements) at %s.", createStatementCount, pathCreateSchema)); Files.write(pathCreateSchema, createSchemaStatements); copyToTarget(pathCreateSchema, targetDbRootPath); - log.info(String.format("Generated finalize schema (%d statements) at %s.", finalizeStatementCount, pathFullSchema)); + log.info(String.format("Generated finalize schema (%d statements) at %s.", finalizeStatementCount, pathFinalizeSchema)); Files.write(pathFinalizeSchema, finalizeSchemaStatements); copyToTarget(pathFinalizeSchema, targetDbRootPath); } View it on GitLab: https://gitlab.com/ultreiaio/ird-observe/-/commit/064d69ff2e0a7044e4b28cce37... -- View it on GitLab: https://gitlab.com/ultreiaio/ird-observe/-/commit/064d69ff2e0a7044e4b28cce37... You're receiving this email because of your account on gitlab.com.
participants (1)
-
Tony CHEMIT