Tony CHEMIT pushed to branch develop at ultreiaio / ird-observe

Commits:

4 changed files:

Changes:

  • toolkit/maven-plugin/src/main/java/fr/ird/observe/maven/plugins/toolbox/ExecuteRunnerMojo.java
    ... ... @@ -33,6 +33,7 @@ import java.io.File;
    33 33
     import java.net.URLClassLoader;
    
    34 34
     import java.nio.file.Path;
    
    35 35
     import java.util.List;
    
    36
    +import java.util.Map;
    
    36 37
     
    
    37 38
     /**
    
    38 39
      * To execute the given
    
    ... ... @@ -76,11 +77,15 @@ public class ExecuteRunnerMojo extends ToolboxMojoSupport {
    76 77
         @Parameter(defaultValue = "${project.build.directory}/generated-sources/java", required = true)
    
    77 78
         private File targetRoot;
    
    78 79
     
    
    80
    +    @Parameter
    
    81
    +    private Map<String, String> extraProperties;
    
    82
    +
    
    79 83
         public static abstract class MojoRunnable implements Runnable {
    
    80 84
     
    
    81 85
             protected I18nKeySet getterFile;
    
    82 86
             protected Log log;
    
    83 87
             private boolean force;
    
    88
    +        private Map<String, String> extraProperties;
    
    84 89
     
    
    85 90
             public I18nKeySet getGetterFile() {
    
    86 91
                 return getterFile;
    
    ... ... @@ -105,6 +110,14 @@ public class ExecuteRunnerMojo extends ToolboxMojoSupport {
    105 110
             public void setForce(boolean force) {
    
    106 111
                 this.force = force;
    
    107 112
             }
    
    113
    +
    
    114
    +        public Map<String, String> getExtraProperties() {
    
    115
    +            return extraProperties;
    
    116
    +        }
    
    117
    +
    
    118
    +        public void setExtraProperties(Map<String, String> extraProperties) {
    
    119
    +            this.extraProperties = extraProperties;
    
    120
    +        }
    
    108 121
         }
    
    109 122
     
    
    110 123
         @Override
    
    ... ... @@ -136,6 +149,7 @@ public class ExecuteRunnerMojo extends ToolboxMojoSupport {
    136 149
                     MojoRunnable o = (MojoRunnable) urlClassLoader.loadClass(runnerType).getConstructor().newInstance();
    
    137 150
     
    
    138 151
                     o.setGetterFile(getterFile);
    
    152
    +                o.setExtraProperties(extraProperties);
    
    139 153
                     o.setLog(getLog());
    
    140 154
                     o.run();
    
    141 155
                 }
    

  • toolkit/maven-plugin/src/main/java/fr/ird/observe/maven/plugins/toolbox/persistence/ExtractSchema.java
    1
    +package fr.ird.observe.maven.plugins.toolbox.persistence;
    
    2
    +
    
    3
    +/*-
    
    4
    + * #%L
    
    5
    + * ObServe Toolkit :: Maven plugin
    
    6
    + * %%
    
    7
    + * Copyright (C) 2008 - 2021 IRD, Code Lutin, 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 fr.ird.observe.maven.plugins.toolbox.ExecuteRunnerMojo;
    
    26
    +import org.nuiton.topia.persistence.script.TopiaSqlScript;
    
    27
    +import org.nuiton.topia.service.migration.resources.MigrationVersionResourceProvider;
    
    28
    +import org.nuiton.version.Version;
    
    29
    +
    
    30
    +import java.io.File;
    
    31
    +import java.io.IOException;
    
    32
    +import java.nio.file.Files;
    
    33
    +import java.nio.file.Path;
    
    34
    +import java.nio.file.StandardCopyOption;
    
    35
    +import java.util.LinkedList;
    
    36
    +import java.util.List;
    
    37
    +
    
    38
    +/**
    
    39
    + * To extract a single schema DDL into a specific sql file.
    
    40
    + * <p>
    
    41
    + * Created on 20/01/2021.
    
    42
    + *
    
    43
    + * @author Tony Chemit - dev@tchemit.fr
    
    44
    + * @since 8.0.5
    
    45
    + */
    
    46
    +public class ExtractSchema extends ExecuteRunnerMojo.MojoRunnable {
    
    47
    +
    
    48
    +    @Override
    
    49
    +    public void run() {
    
    50
    +
    
    51
    +        Path targetDirectory = new File(System.getProperty("compile.target.directory")).toPath();
    
    52
    +        Path sourceDirectory = new File(System.getProperty("compile.source.directory")).toPath();
    
    53
    +
    
    54
    +        String schemaName = getExtraProperties().get("schemaName");
    
    55
    +
    
    56
    +        Version modelVersion = MigrationVersionResourceProvider.get().getLastVersion();
    
    57
    +
    
    58
    +        Path dbRootPath = sourceDirectory.getParent().resolve("resources").resolve("db").resolve("migration").resolve(modelVersion.getVersion());
    
    59
    +        Path targetDbRootPath = targetDirectory.resolve("db").resolve("migration").resolve(modelVersion.getVersion());
    
    60
    +        try {
    
    61
    +            processScript(true, dbRootPath.resolve("observe_full-schema-H2.sql"), targetDbRootPath, schemaName);
    
    62
    +            processScript(false, dbRootPath.resolve("observe_full-schema-PG.sql"), targetDbRootPath, schemaName);
    
    63
    +        } catch (Exception e) {
    
    64
    +            throw new IllegalStateException(e);
    
    65
    +        }
    
    66
    +    }
    
    67
    +
    
    68
    +    protected void processScript(boolean h2, Path incomingFullSchema, Path targetDbRootPath, String schemaName) throws IOException {
    
    69
    +        Path pathFullSchema = incomingFullSchema.getParent().resolve(incomingFullSchema.toFile().getName().replace("-schema-", "-schema-" + schemaName+"-"));
    
    70
    +        log.info(String.format("Generating schema %s schema [%s] (from %s).", schemaName, pathFullSchema.toFile().getName(), incomingFullSchema));
    
    71
    +        TopiaSqlScript script = TopiaSqlScript.of(incomingFullSchema);
    
    72
    +        Path pathCreateSchema = pathFullSchema.getParent().resolve(pathFullSchema.toFile().getName().replace("full", "create"));
    
    73
    +        Path pathFinalizeSchema = pathFullSchema.getParent().resolve(pathFullSchema.toFile().getName().replace("full", "finalize"));
    
    74
    +        List<String> createSchemaStatements = new LinkedList<>();
    
    75
    +        List<String> finalizeSchemaStatements = new LinkedList<>();
    
    76
    +        script.getLocation().forEach(statement -> {
    
    77
    +            String trim = statement.toLowerCase().trim();
    
    78
    +            if (trim.startsWith("create schema " + schemaName) || trim.startsWith("create table " + schemaName)) {
    
    79
    +                if (!h2) {
    
    80
    +                    statement = statement.replace("data blob", "data OID");
    
    81
    +                }
    
    82
    +                createSchemaStatements.add(statement);
    
    83
    +            } else {
    
    84
    +                if (trim.startsWith("alter table " + schemaName) || trim.startsWith("create index idx_" + schemaName)) {
    
    85
    +                    finalizeSchemaStatements.add(statement);
    
    86
    +                }
    
    87
    +            }
    
    88
    +        });
    
    89
    +
    
    90
    +        int createStatementCount = createSchemaStatements.size();
    
    91
    +        int finalizeStatementCount = finalizeSchemaStatements.size();
    
    92
    +
    
    93
    +        log.info(String.format("Generated create schema (%d statements) at %s.", createStatementCount, pathCreateSchema));
    
    94
    +        Files.write(pathCreateSchema, createSchemaStatements);
    
    95
    +        copyToTarget(pathCreateSchema, targetDbRootPath);
    
    96
    +
    
    97
    +        log.info(String.format("Generated finalize schema (%d statements) at %s.", finalizeStatementCount, pathFinalizeSchema));
    
    98
    +        Files.write(pathFinalizeSchema, finalizeSchemaStatements);
    
    99
    +        copyToTarget(pathFinalizeSchema, targetDbRootPath);
    
    100
    +    }
    
    101
    +
    
    102
    +    private void copyToTarget(Path source, Path targetDbRootPath) throws IOException {
    
    103
    +        Path target = targetDbRootPath.resolve(source.toFile().getName());
    
    104
    +        Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
    
    105
    +        getLog().debug(String.format("Copy from %s → %s", source.toFile().getName(), target));
    
    106
    +    }
    
    107
    +
    
    108
    +}

  • toolkit/maven-plugin/src/main/java/fr/ird/observe/maven/plugins/toolbox/persistence/ExtractTable.java
    1
    +package fr.ird.observe.maven.plugins.toolbox.persistence;
    
    2
    +
    
    3
    +/*-
    
    4
    + * #%L
    
    5
    + * ObServe Toolkit :: Maven plugin
    
    6
    + * %%
    
    7
    + * Copyright (C) 2008 - 2021 IRD, Code Lutin, 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 fr.ird.observe.entities.ObserveTopiaApplicationContextSupport;
    
    26
    +import fr.ird.observe.entities.ObserveTopiaConfiguration;
    
    27
    +import fr.ird.observe.entities.ObserveTopiaConfigurationFactory;
    
    28
    +import fr.ird.observe.maven.plugins.toolbox.ExecuteRunnerMojo;
    
    29
    +import fr.ird.observe.services.configuration.topia.ObserveDataSourceConfigurationTopiaH2;
    
    30
    +import org.nuiton.topia.persistence.script.TopiaSqlScript;
    
    31
    +import org.nuiton.topia.service.migration.resources.MigrationVersionResourceProvider;
    
    32
    +import org.nuiton.version.Version;
    
    33
    +
    
    34
    +import java.io.File;
    
    35
    +import java.io.IOException;
    
    36
    +import java.lang.reflect.InvocationTargetException;
    
    37
    +import java.nio.file.Files;
    
    38
    +import java.nio.file.Path;
    
    39
    +import java.nio.file.StandardCopyOption;
    
    40
    +import java.util.LinkedList;
    
    41
    +import java.util.List;
    
    42
    +import java.util.function.BiFunction;
    
    43
    +
    
    44
    +/**
    
    45
    + * to extract a single table DDL into a specific sql file.
    
    46
    + * Created on 20/01/2021.
    
    47
    + *
    
    48
    + * @author Tony Chemit - dev@tchemit.fr
    
    49
    + * @since 8.0.5
    
    50
    + */
    
    51
    +public class ExtractTable extends ExecuteRunnerMojo.MojoRunnable {
    
    52
    +
    
    53
    +    @Override
    
    54
    +    public void run() {
    
    55
    +
    
    56
    +        Path targetDirectory = new File(System.getProperty("compile.target.directory")).toPath();
    
    57
    +        Path sourceDirectory = new File(System.getProperty("compile.source.directory")).toPath();
    
    58
    +
    
    59
    +        String schemaName = getExtraProperties().get("schemaName");
    
    60
    +        String tableName = getExtraProperties().get("tableName");
    
    61
    +
    
    62
    +        Version modelVersion = MigrationVersionResourceProvider.get().getLastVersion();
    
    63
    +
    
    64
    +        Path dbRootPath = sourceDirectory.getParent().resolve("resources").resolve("db").resolve("migration").resolve(modelVersion.getVersion());
    
    65
    +        Path targetDbRootPath = targetDirectory.resolve("db").resolve("migration").resolve(modelVersion.getVersion());
    
    66
    +        try {
    
    67
    +            processScript(true, dbRootPath.resolve("observe_full-schema-H2.sql"), targetDbRootPath, schemaName, tableName);
    
    68
    +            processScript(false, dbRootPath.resolve("observe_full-schema-PG.sql"), targetDbRootPath, schemaName, tableName);
    
    69
    +        } catch (Exception e) {
    
    70
    +            throw new IllegalStateException(e);
    
    71
    +        }
    
    72
    +    }
    
    73
    +
    
    74
    +    protected void processScript(boolean h2, Path incomingFullSchema, Path targetDbRootPath, String schemaName, String tableName) throws IOException {
    
    75
    +        String suffix = schemaName + "." + tableName;
    
    76
    +        Path pathFullSchema = incomingFullSchema.getParent().resolve(incomingFullSchema.toFile().getName().replace("-schema-", "-table-" + suffix+"-"));
    
    77
    +        log.info(String.format("Generating table %s.%s schema [%s] (from %s).", schemaName, tableName, pathFullSchema.toFile().getName(), incomingFullSchema));
    
    78
    +        TopiaSqlScript script = TopiaSqlScript.of(incomingFullSchema);
    
    79
    +        Path pathCreateSchema = pathFullSchema.getParent().resolve(pathFullSchema.toFile().getName().replace("full", "create"));
    
    80
    +        Path pathFinalizeSchema = pathFullSchema.getParent().resolve(pathFullSchema.toFile().getName().replace("full", "finalize"));
    
    81
    +        List<String> createSchemaStatements = new LinkedList<>();
    
    82
    +        List<String> finalizeSchemaStatements = new LinkedList<>();
    
    83
    +
    
    84
    +        String suffix2 = schemaName + "_" + tableName;
    
    85
    +        script.getLocation().forEach(statement -> {
    
    86
    +            String trim = statement.toLowerCase().trim();
    
    87
    +            if (trim.startsWith("create table " + suffix)) {
    
    88
    +                if (!h2) {
    
    89
    +                    statement = statement.replace("data blob", "data OID");
    
    90
    +                }
    
    91
    +                createSchemaStatements.add(statement);
    
    92
    +            } else {
    
    93
    +                if (trim.startsWith("alter table " + suffix) || trim.startsWith("create index idx_" + suffix2)) {
    
    94
    +                    finalizeSchemaStatements.add(statement);
    
    95
    +                }
    
    96
    +            }
    
    97
    +        });
    
    98
    +
    
    99
    +        int createStatementCount = createSchemaStatements.size();
    
    100
    +        int finalizeStatementCount = finalizeSchemaStatements.size();
    
    101
    +
    
    102
    +        log.info(String.format("Generated create table (%d statements) at %s.", createStatementCount, pathCreateSchema));
    
    103
    +        Files.write(pathCreateSchema, createSchemaStatements);
    
    104
    +        copyToTarget(pathCreateSchema, targetDbRootPath);
    
    105
    +
    
    106
    +        log.info(String.format("Generated finalize table (%d statements) at %s.", finalizeStatementCount, pathFinalizeSchema));
    
    107
    +        Files.write(pathFinalizeSchema, finalizeSchemaStatements);
    
    108
    +        copyToTarget(pathFinalizeSchema, targetDbRootPath);
    
    109
    +    }
    
    110
    +
    
    111
    +    private void copyToTarget(Path source, Path targetDbRootPath) throws IOException {
    
    112
    +        Path target = targetDbRootPath.resolve(source.toFile().getName());
    
    113
    +        Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
    
    114
    +        getLog().debug(String.format("Copy from %s → %s", source.toFile().getName(), target));
    
    115
    +    }
    
    116
    +}

  • 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 {
    115 115
             log.info(String.format("Generated full schema (%d statements) at %s.", fullStatementCount, pathFullSchema));
    
    116 116
             copyToTarget(pathFullSchema, targetDbRootPath);
    
    117 117
     
    
    118
    -        log.info(String.format("Generated create schema (%d statements) at %s.", createStatementCount, pathFullSchema));
    
    118
    +        log.info(String.format("Generated create schema (%d statements) at %s.", createStatementCount, pathCreateSchema));
    
    119 119
             Files.write(pathCreateSchema, createSchemaStatements);
    
    120 120
             copyToTarget(pathCreateSchema, targetDbRootPath);
    
    121 121
     
    
    122
    -        log.info(String.format("Generated finalize schema (%d statements) at %s.", finalizeStatementCount, pathFullSchema));
    
    122
    +        log.info(String.format("Generated finalize schema (%d statements) at %s.", finalizeStatementCount, pathFinalizeSchema));
    
    123 123
             Files.write(pathFinalizeSchema, finalizeSchemaStatements);
    
    124 124
             copyToTarget(pathFinalizeSchema, targetDbRootPath);
    
    125 125
         }