All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fr.ird.observe.toolkit.maven.plugin.tck.MigrateTckDatabases Maven / Gradle / Ivy

package fr.ird.observe.toolkit.maven.plugin.tck;

/*-
 * #%L
 * ObServe Toolkit :: Maven plugin
 * %%
 * Copyright (C) 2017 - 2022 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
 * .
 * #L%
 */

import fr.ird.observe.dto.db.configuration.topia.ObserveDataSourceConfigurationTopiaH2;
import fr.ird.observe.entities.ToolkitTopiaApplicationContextSupport;
import fr.ird.observe.test.DatabaseName;
import fr.ird.observe.toolkit.maven.plugin.PersistenceRunner;
import io.ultreia.java4all.util.Version;
import org.nuiton.topia.persistence.script.TopiaSqlScript;

import java.nio.file.Path;
import java.util.Objects;

/**
 * Created on 22/11/2021.
 *
 * @author Tony Chemit - [email protected]
 * @since 5.0.55
 */
public class MigrateTckDatabases extends PersistenceRunner {
    protected Path archiveFile;

    public void setArchiveFile(Path archiveFile) {
        this.archiveFile = archiveFile;
    }

    @Override
    public void init() {
        super.init();
        Objects.requireNonNull(previousModelVersion);
        Objects.requireNonNull(archiveFile);
    }

    @Override
    public void run() {

        Path exploded = explodeArchive(archiveFile);
        Path sources = getTemporaryPath().resolve("sources");

        migrateVersion(sources, exploded, Version.valueOf("7.6"), DatabaseName.empty, DatabaseName.referential);
        migrateVersion(sources, exploded, previousModelVersion, DatabaseName.values());
    }

    protected void migrateVersion(Path sources, Path exploded, Version version, DatabaseName... databaseNames) {
        createForVersion(version, exploded, sources, databaseNames);
        for (DatabaseName databaseName : databaseNames) {
            migrateDatabase(databaseName, version, sources);
        }
    }

    protected void migrateDatabase(DatabaseName tckDbName, Version previousModelVersion, Path sources) {

        Path dbPath = sources.resolve(previousModelVersion.getVersion());

        ObserveDataSourceConfigurationTopiaH2 configuration = createConfiguration(getTemporaryPath().resolve("db").resolve(previousModelVersion.getValidName()).resolve(tckDbName.name()));

        String sqlFilename = tckDbName.name() + ".sql.gz";
        Path backupFile = getTemporaryPath().resolve(previousModelVersion.getValidName() + "-backup-" + sqlFilename.replace(".gz", ""));
        try (ToolkitTopiaApplicationContextSupport applicationContext = createTopiaApplicationContext(configuration)) {

            log.info(String.format("[%S] migrate from version %s to %s", tckDbName, previousModelVersion, modelVersion));
            applicationContext.executeSqlStatements(TopiaSqlScript.of(dbPath.resolve(sqlFilename)));
            applicationContext.migrate();

            log.info(String.format("[%S] backup to %s", tckDbName, backupFile));
            applicationContext.backupSane(backupFile, false);
        }
        checkDatabase(tckDbName, previousModelVersion, backupFile);
    }

    protected void checkDatabase(DatabaseName tckDbName, Version previousModelVersion, Path backupFile) {

        ObserveDataSourceConfigurationTopiaH2 checkConfiguration = createConfiguration(getTemporaryPath().resolve("db").resolve(previousModelVersion.getValidName() + "-" + modelVersion.getValidName()).resolve(tckDbName.name()));
        try (ToolkitTopiaApplicationContextSupport applicationContext = createTopiaApplicationContext(checkConfiguration)) {

            log.info(String.format("[%S] Check - loading new database with version %s", tckDbName, modelVersion));

            applicationContext.executeSqlStatements(TopiaSqlScript.of(backupFile));
            Path checkBackupFile = getTemporaryPath().resolve("check-backup-" + backupFile.toFile().getName().replace(".sql.gz", "-check.sql"));
            applicationContext.backup(checkBackupFile.toFile(), false);
            log.info(String.format("[%S] Check - backup to %s", tckDbName, checkBackupFile));
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy