Tony CHEMIT pushed to branch develop-9.0.x at ultreiaio / ird-observe
Commits:
-
d1a3f027
by Tony Chemit at 2023-04-07T11:21:30+02:00
-
6540fc10
by Tony Chemit at 2023-04-07T13:44:30+02:00
15 changed files:
- − core/persistence/test/src/test/java/fr/ird/observe/persistence/avdth/data/AvdthAtlanticDataBuilderAllTest.java
- − core/persistence/test/src/test/java/fr/ird/observe/persistence/avdth/data/AvdthAtlanticDataBuilderByYearNotValidTest.java
- − core/persistence/test/src/test/java/fr/ird/observe/persistence/avdth/data/AvdthAtlanticDataBuilderByYearTest.java
- − core/persistence/test/src/test/java/fr/ird/observe/persistence/avdth/data/AvdthDataBuilderAllYearTestSupport.java
- − core/persistence/test/src/test/java/fr/ird/observe/persistence/avdth/data/AvdthIndianDataBuilderAllTest.java
- − core/persistence/test/src/test/java/fr/ird/observe/persistence/avdth/data/AvdthIndianDataBuilderByYearTest.java
- toolkit/persistence/src/main/java/org/nuiton/topia/persistence/jdbc/JdbcHelper.java
- toolkit/persistence/src/main/java/org/nuiton/topia/persistence/jdbc/JdbcSecurityHelper.java
- toolkit/persistence/src/main/java/org/nuiton/topia/persistence/jdbc/JdbcSecurityHelperH2.java
- toolkit/persistence/src/main/java/org/nuiton/topia/persistence/jdbc/JdbcSecurityHelperPostgres.java
- toolkit/persistence/src/main/java/org/nuiton/topia/service/migration/TopiaMigrationServiceContext.java
- toolkit/persistence/src/main/java/org/nuiton/topia/service/migration/TopiaMigrationServiceImpl.java
- toolkit/persistence/src/main/java/org/nuiton/topia/service/migration/version/MigrationServiceSqlHelper.java
- toolkit/persistence/src/main/java/org/nuiton/topia/service/migration/version/TMSVersion.java
- toolkit/persistence/src/main/java/org/nuiton/topia/service/sql/internal/consumer/AddVersionTableConsumer.java
Changes:
| 1 | -package fr.ird.observe.persistence.avdth.data;
|
|
| 2 | - |
|
| 3 | -/*-
|
|
| 4 | - * #%L
|
|
| 5 | - * ObServe Core :: Persistence :: Test
|
|
| 6 | - * %%
|
|
| 7 | - * Copyright (C) 2008 - 2023 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 fr.ird.observe.dto.ProtectedIdsCommon;
|
|
| 26 | -import org.junit.Ignore;
|
|
| 27 | -import org.junit.runners.Parameterized;
|
|
| 28 | - |
|
| 29 | -import java.nio.file.Path;
|
|
| 30 | - |
|
| 31 | -/**
|
|
| 32 | - * Created on 24/05/2021.
|
|
| 33 | - *
|
|
| 34 | - * @author Tony Chemit - dev@tchemit.fr
|
|
| 35 | - * @since 9.0.0
|
|
| 36 | - */
|
|
| 37 | -@Ignore
|
|
| 38 | -public class AvdthAtlanticDataBuilderAllTest extends AvdthDataBuilderAllYearTestSupport {
|
|
| 39 | - |
|
| 40 | - public static final Path ROOT_PATH = getPath("OA").resolve("all");
|
|
| 41 | - |
|
| 42 | - @Parameterized.Parameters(name = "{index}: {0}")
|
|
| 43 | - public static Iterable<?> data() {
|
|
| 44 | - return getDatabases(ROOT_PATH);
|
|
| 45 | - }
|
|
| 46 | - |
|
| 47 | - @Override
|
|
| 48 | - protected Path getRootPath() {
|
|
| 49 | - return ROOT_PATH;
|
|
| 50 | - }
|
|
| 51 | - |
|
| 52 | - @Override
|
|
| 53 | - protected String getOceanId() {
|
|
| 54 | - return ProtectedIdsCommon.COMMON_OCEAN_ATLANTIC;
|
|
| 55 | - }
|
|
| 56 | - |
|
| 57 | -} |
| 1 | -package fr.ird.observe.persistence.avdth.data;
|
|
| 2 | - |
|
| 3 | -/*-
|
|
| 4 | - * #%L
|
|
| 5 | - * ObServe Core :: Persistence :: Test
|
|
| 6 | - * %%
|
|
| 7 | - * Copyright (C) 2008 - 2023 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 fr.ird.observe.dto.ProtectedIdsCommon;
|
|
| 26 | -import fr.ird.observe.services.service.data.ps.AvdthDataImportResult;
|
|
| 27 | -import org.junit.Ignore;
|
|
| 28 | -import org.junit.runners.Parameterized;
|
|
| 29 | - |
|
| 30 | -import java.nio.file.Path;
|
|
| 31 | -import java.util.List;
|
|
| 32 | -import java.util.Set;
|
|
| 33 | - |
|
| 34 | -/**
|
|
| 35 | - * Only for debug some situations... will be removed soon.
|
|
| 36 | - * <p>
|
|
| 37 | - * Created on 24/05/2021.
|
|
| 38 | - *
|
|
| 39 | - * @author Tony Chemit - dev@tchemit.fr
|
|
| 40 | - * @since 9.0.0
|
|
| 41 | - */
|
|
| 42 | -@SuppressWarnings({"SpellCheckingInspection", "unused"})
|
|
| 43 | -@Ignore
|
|
| 44 | -public class AvdthAtlanticDataBuilderByYearNotValidTest extends AvdthDataBuilderByYearTestSupport {
|
|
| 45 | - |
|
| 46 | - public static final Path ROOT_PATH = getPath("OA");
|
|
| 47 | - |
|
| 48 | - public static final Set<String> NOT_VALID = Set.of(
|
|
| 49 | - "OA_1969_V35.mdb",
|
|
| 50 | - "OA_1970_V35.mdb",
|
|
| 51 | - "OA_1971_V35.mdb",
|
|
| 52 | - "OA_1972_V35.mdb",
|
|
| 53 | - "OA_1973_V35.mdb",
|
|
| 54 | - "OA_1974_V35.mdb",
|
|
| 55 | - "OA_1975_V35.mdb",
|
|
| 56 | - "OA_1976_V35.mdb",
|
|
| 57 | - "OA_1977_V35.mdb",
|
|
| 58 | - "OA_1978_V35.mdb",
|
|
| 59 | - "OA_1979_V35.mdb",
|
|
| 60 | - "OA_1980_V35.mdb",
|
|
| 61 | - "OA_1981_V35.mdb",
|
|
| 62 | - "OA_1982_V35.mdb",
|
|
| 63 | - "OA_1983_V35.mdb",
|
|
| 64 | - "OA_1984_V35.mdb",
|
|
| 65 | - "OA_1985_V35.mdb",
|
|
| 66 | - "OA_1986_V35.mdb",
|
|
| 67 | - "OA_1987_V35.mdb",
|
|
| 68 | - "OA_1988_V35.mdb",
|
|
| 69 | - "OA_1989_V35.mdb",
|
|
| 70 | - "OA_1990_V35.mdb",
|
|
| 71 | - "OA_1993_V35.mdb",
|
|
| 72 | - "OA_1995_V35.mdb",
|
|
| 73 | - "OA_1996_V35.mdb",
|
|
| 74 | - "OA_1997_V35.mdb",
|
|
| 75 | - "OA_1998_V35.mdb",
|
|
| 76 | - "OA_1999_V35.mdb",
|
|
| 77 | - "OA_2000_V35.mdb",
|
|
| 78 | - "OA_2001_V35.mdb",
|
|
| 79 | - "OA_2002_V35.mdb",
|
|
| 80 | - "OA_2003_V35.mdb",
|
|
| 81 | - "OA_2004_V35.mdb",
|
|
| 82 | - "OA_2005_V35.mdb",
|
|
| 83 | - "OA_2006_V35.mdb",
|
|
| 84 | - "OA_2010_V35.mdb",
|
|
| 85 | - "OA_2012_V35.mdb",
|
|
| 86 | - "OA_2013_V35.mdb");
|
|
| 87 | - |
|
| 88 | - // no link between CUVE and MAREE
|
|
| 89 | - public static final Set<String> MISSING_CUVE = Set.of(
|
|
| 90 | - "OA_1999_V35.mdb",
|
|
| 91 | - "OA_2000_V35.mdb",
|
|
| 92 | - "OA_2001_V35.mdb",
|
|
| 93 | - "OA_2002_V35.mdb",
|
|
| 94 | - "OA_2003_V35.mdb",
|
|
| 95 | - "OA_2004_V35.mdb",
|
|
| 96 | - "OA_2005_V35.mdb",
|
|
| 97 | - "OA_2006_V35.mdb",
|
|
| 98 | - "OA_2010_V35.mdb",
|
|
| 99 | - "OA_2012_V35.mdb",
|
|
| 100 | - "OA_2013_V35.mdb"
|
|
| 101 | - );
|
|
| 102 | - |
|
| 103 | - // no link between CUVE_CALEE and MAREE
|
|
| 104 | - public static final Set<String> MISSING_CUVE_CALEE = Set.of(
|
|
| 105 | - "OA_2000_V35.mdb",
|
|
| 106 | - "OA_2001_V35.mdb",
|
|
| 107 | - "OA_2002_V35.mdb",
|
|
| 108 | - "OA_2003_V35.mdb",
|
|
| 109 | - "OA_2004_V35.mdb",
|
|
| 110 | - "OA_2005_V35.mdb",
|
|
| 111 | - "OA_2006_V35.mdb",
|
|
| 112 | - "OA_2010_V35.mdb"
|
|
| 113 | - );
|
|
| 114 | - |
|
| 115 | - // no link between ECH_CALEE and MAREE
|
|
| 116 | - public static final Set<String> MISSING_ECH_CALEE = Set.of(
|
|
| 117 | - "OA_1969_V35.mdb",
|
|
| 118 | - "OA_1970_V35.mdb",
|
|
| 119 | - "OA_1971_V35.mdb",
|
|
| 120 | - "OA_1972_V35.mdb",
|
|
| 121 | - "OA_1973_V35.mdb",
|
|
| 122 | - "OA_1974_V35.mdb",
|
|
| 123 | - "OA_1975_V35.mdb",
|
|
| 124 | - "OA_1976_V35.mdb",
|
|
| 125 | - "OA_1977_V35.mdb",
|
|
| 126 | - "OA_1978_V35.mdb",
|
|
| 127 | - "OA_1979_V35.mdb",
|
|
| 128 | - "OA_1980_V35.mdb",
|
|
| 129 | - "OA_1981_V35.mdb",
|
|
| 130 | - "OA_1982_V35.mdb",
|
|
| 131 | - "OA_1983_V35.mdb",
|
|
| 132 | - "OA_1984_V35.mdb",
|
|
| 133 | - "OA_1985_V35.mdb",
|
|
| 134 | - "OA_1986_V35.mdb",
|
|
| 135 | - "OA_1987_V35.mdb",
|
|
| 136 | - "OA_1988_V35.mdb",
|
|
| 137 | - "OA_1989_V35.mdb",
|
|
| 138 | - "OA_1990_V35.mdb",
|
|
| 139 | - "OA_1998_V35.mdb",
|
|
| 140 | - "OA_1999_V35.mdb",
|
|
| 141 | - "OA_2000_V35.mdb",
|
|
| 142 | - "OA_2001_V35.mdb",
|
|
| 143 | - "OA_2002_V35.mdb",
|
|
| 144 | - "OA_2010_V35.mdb"
|
|
| 145 | - );
|
|
| 146 | - // no link between ECHANTILLON and MAREE
|
|
| 147 | - public static final Set<String> MISSING_ECHANTILLON = Set.of(
|
|
| 148 | - "OA_1969_V35.mdb",
|
|
| 149 | - "OA_1970_V35.mdb",
|
|
| 150 | - "OA_1971_V35.mdb",
|
|
| 151 | - "OA_1972_V35.mdb",
|
|
| 152 | - "OA_1973_V35.mdb",
|
|
| 153 | - "OA_1974_V35.mdb",
|
|
| 154 | - "OA_1975_V35.mdb",
|
|
| 155 | - "OA_1976_V35.mdb",
|
|
| 156 | - "OA_1977_V35.mdb",
|
|
| 157 | - "OA_1978_V35.mdb",
|
|
| 158 | - "OA_1979_V35.mdb",
|
|
| 159 | - "OA_1980_V35.mdb",
|
|
| 160 | - "OA_1981_V35.mdb",
|
|
| 161 | - "OA_1982_V35.mdb",
|
|
| 162 | - "OA_1983_V35.mdb",
|
|
| 163 | - "OA_1984_V35.mdb",
|
|
| 164 | - "OA_1985_V35.mdb",
|
|
| 165 | - "OA_1986_V35.mdb",
|
|
| 166 | - "OA_1987_V35.mdb",
|
|
| 167 | - "OA_1988_V35.mdb",
|
|
| 168 | - "OA_1989_V35.mdb",
|
|
| 169 | - "OA_1990_V35.mdb",
|
|
| 170 | - "OA_1998_V35.mdb",
|
|
| 171 | - "OA_1999_V35.mdb",
|
|
| 172 | - "OA_2000_V35.mdb",
|
|
| 173 | - "OA_2001_V35.mdb",
|
|
| 174 | - "OA_2002_V35.mdb",
|
|
| 175 | - "OA_2010_V35.mdb");
|
|
| 176 | - |
|
| 177 | - // Cound not find Sample.activity but there is some...
|
|
| 178 | - public static final Set<String> NO_MISSING = Set.of(
|
|
| 179 | - "OA_1993_V35.mdb",
|
|
| 180 | - "OA_1995_V35.mdb",
|
|
| 181 | - "OA_1996_V35.mdb",
|
|
| 182 | - "OA_1997_V35.mdb"
|
|
| 183 | - );
|
|
| 184 | - |
|
| 185 | - public static final String TEST_ADD_SAMPLE_ACTIVITY = "OA_1977_V35.mdb";
|
|
| 186 | - |
|
| 187 | - @Parameterized.Parameters(name = "{index}: {0}")
|
|
| 188 | - public static Iterable<?> data() {
|
|
| 189 | - List<String> databases = allDatabases(ROOT_PATH);
|
|
| 190 | - databases.removeIf(n -> !NOT_VALID.contains(n));
|
|
| 191 | -// databases.removeIf(n -> !MISSING_ECH_CALEE.contains(n));
|
|
| 192 | -// databases.removeIf(NO_MISSING::contains);
|
|
| 193 | -// databases.removeIf(MISSING_CUVE::contains);
|
|
| 194 | -// databases.removeIf(MISSING_CUVE_CALEE::contains);
|
|
| 195 | -// databases.removeIf(MISSING_ECHANTILLON::contains);
|
|
| 196 | -// databases.removeIf(MISSING_ECH_CALEE::contains);
|
|
| 197 | - return databases;
|
|
| 198 | - }
|
|
| 199 | - |
|
| 200 | - @Override
|
|
| 201 | - protected void checkNotValidResult(AvdthDataImportResult result) {
|
|
| 202 | - |
|
| 203 | - if (result.getNotReadResult().isEmpty()) {
|
|
| 204 | - super.checkNotValidResult(result);
|
|
| 205 | - }
|
|
| 206 | - }
|
|
| 207 | - |
|
| 208 | - @Override
|
|
| 209 | - protected Path getRootPath() {
|
|
| 210 | - return ROOT_PATH;
|
|
| 211 | - }
|
|
| 212 | - |
|
| 213 | - @Override
|
|
| 214 | - protected String getOceanId() {
|
|
| 215 | - return ProtectedIdsCommon.COMMON_OCEAN_ATLANTIC;
|
|
| 216 | - }
|
|
| 217 | -} |
| 1 | -package fr.ird.observe.persistence.avdth.data;
|
|
| 2 | - |
|
| 3 | -/*-
|
|
| 4 | - * #%L
|
|
| 5 | - * ObServe Core :: Persistence :: Test
|
|
| 6 | - * %%
|
|
| 7 | - * Copyright (C) 2008 - 2023 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 fr.ird.observe.dto.ProtectedIdsCommon;
|
|
| 26 | -import org.junit.runners.Parameterized;
|
|
| 27 | - |
|
| 28 | -import java.nio.file.Path;
|
|
| 29 | - |
|
| 30 | -/**
|
|
| 31 | - * Created on 24/05/2021.
|
|
| 32 | - *
|
|
| 33 | - * @author Tony Chemit - dev@tchemit.fr
|
|
| 34 | - * @since 9.0.0
|
|
| 35 | - */
|
|
| 36 | -public class AvdthAtlanticDataBuilderByYearTest extends AvdthDataBuilderByYearTestSupport {
|
|
| 37 | - |
|
| 38 | - public static final Path ROOT_PATH = getPath("OA");
|
|
| 39 | - |
|
| 40 | - @Parameterized.Parameters(name = "{index}: {0}")
|
|
| 41 | - public static Iterable<?> data() {
|
|
| 42 | - return getDatabases(ROOT_PATH);
|
|
| 43 | - }
|
|
| 44 | - |
|
| 45 | - @Override
|
|
| 46 | - protected Path getRootPath() {
|
|
| 47 | - return ROOT_PATH;
|
|
| 48 | - }
|
|
| 49 | - |
|
| 50 | - @Override
|
|
| 51 | - protected String getOceanId() {
|
|
| 52 | - return ProtectedIdsCommon.COMMON_OCEAN_ATLANTIC;
|
|
| 53 | - }
|
|
| 54 | -} |
| 1 | -package fr.ird.observe.persistence.avdth.data;
|
|
| 2 | - |
|
| 3 | -/*-
|
|
| 4 | - * #%L
|
|
| 5 | - * ObServe Core :: Persistence :: Test
|
|
| 6 | - * %%
|
|
| 7 | - * Copyright (C) 2008 - 2023 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 fr.ird.observe.persistence.avdth.AvdthFixtures;
|
|
| 26 | - |
|
| 27 | -import java.nio.file.Path;
|
|
| 28 | -import java.util.List;
|
|
| 29 | - |
|
| 30 | -/**
|
|
| 31 | - * Created on 28/10/2021.
|
|
| 32 | - *
|
|
| 33 | - * @author Tony Chemit - dev@tchemit.fr
|
|
| 34 | - * @since 9.0.0
|
|
| 35 | - */
|
|
| 36 | -public abstract class AvdthDataBuilderAllYearTestSupport extends AvdthDataBuilderTestSupport {
|
|
| 37 | - |
|
| 38 | - public static List<String> getDatabases(Path path) {
|
|
| 39 | - return allDatabases(path).subList(0, 1);
|
|
| 40 | - }
|
|
| 41 | - |
|
| 42 | - @Override
|
|
| 43 | - protected boolean doInlineImport() {
|
|
| 44 | - return false;
|
|
| 45 | - }
|
|
| 46 | - |
|
| 47 | - @Override
|
|
| 48 | - protected boolean forcePrepare() {
|
|
| 49 | - return AvdthFixtures.FORCE_PREPARE;
|
|
| 50 | - }
|
|
| 51 | - |
|
| 52 | - @Override
|
|
| 53 | - protected boolean forceImport() {
|
|
| 54 | - return false;
|
|
| 55 | - }
|
|
| 56 | -} |
| 1 | -package fr.ird.observe.persistence.avdth.data;
|
|
| 2 | - |
|
| 3 | -/*-
|
|
| 4 | - * #%L
|
|
| 5 | - * ObServe Core :: Persistence :: Test
|
|
| 6 | - * %%
|
|
| 7 | - * Copyright (C) 2008 - 2023 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 fr.ird.observe.dto.ProtectedIdsCommon;
|
|
| 26 | -import org.junit.Ignore;
|
|
| 27 | -import org.junit.runners.Parameterized;
|
|
| 28 | - |
|
| 29 | -import java.nio.file.Path;
|
|
| 30 | - |
|
| 31 | -/**
|
|
| 32 | - * Created on 24/05/2021.
|
|
| 33 | - *
|
|
| 34 | - * @author Tony Chemit - dev@tchemit.fr
|
|
| 35 | - * @since 9.0.0
|
|
| 36 | - */
|
|
| 37 | -@Ignore
|
|
| 38 | -public class AvdthIndianDataBuilderAllTest extends AvdthDataBuilderAllYearTestSupport {
|
|
| 39 | - |
|
| 40 | - public static final Path ROOT_PATH = getPath("OI").resolve("all");
|
|
| 41 | - |
|
| 42 | - @Parameterized.Parameters(name = "{index}: {0}")
|
|
| 43 | - public static Iterable<?> data() {
|
|
| 44 | - return getDatabases(ROOT_PATH);
|
|
| 45 | - }
|
|
| 46 | - |
|
| 47 | - @Override
|
|
| 48 | - protected Path getRootPath() {
|
|
| 49 | - return ROOT_PATH;
|
|
| 50 | - }
|
|
| 51 | - |
|
| 52 | - @Override
|
|
| 53 | - protected String getOceanId() {
|
|
| 54 | - return ProtectedIdsCommon.COMMON_OCEAN_INDIAN;
|
|
| 55 | - }
|
|
| 56 | -} |
| 1 | -package fr.ird.observe.persistence.avdth.data;
|
|
| 2 | - |
|
| 3 | -/*-
|
|
| 4 | - * #%L
|
|
| 5 | - * ObServe Core :: Persistence :: Test
|
|
| 6 | - * %%
|
|
| 7 | - * Copyright (C) 2008 - 2023 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 fr.ird.observe.dto.ProtectedIdsCommon;
|
|
| 26 | -import org.junit.runners.Parameterized;
|
|
| 27 | - |
|
| 28 | -import java.nio.file.Path;
|
|
| 29 | - |
|
| 30 | -/**
|
|
| 31 | - * Created on 24/05/2021.
|
|
| 32 | - *
|
|
| 33 | - * @author Tony Chemit - dev@tchemit.fr
|
|
| 34 | - * @since 9.0.0
|
|
| 35 | - */
|
|
| 36 | -public class AvdthIndianDataBuilderByYearTest extends AvdthDataBuilderByYearTestSupport {
|
|
| 37 | - |
|
| 38 | - public static final Path ROOT_PATH = getPath("OI");
|
|
| 39 | - |
|
| 40 | - @Parameterized.Parameters(name = "{index}: {0}")
|
|
| 41 | - public static Iterable<?> data() {
|
|
| 42 | - return getDatabases(ROOT_PATH);
|
|
| 43 | - }
|
|
| 44 | - |
|
| 45 | - @Override
|
|
| 46 | - protected Path getRootPath() {
|
|
| 47 | - return ROOT_PATH;
|
|
| 48 | - }
|
|
| 49 | - |
|
| 50 | - @Override
|
|
| 51 | - protected String getOceanId() {
|
|
| 52 | - return ProtectedIdsCommon.COMMON_OCEAN_INDIAN;
|
|
| 53 | - }
|
|
| 54 | -} |
| ... | ... | @@ -34,6 +34,8 @@ import java.sql.DriverManager; |
| 34 | 34 | import java.sql.PreparedStatement;
|
| 35 | 35 | import java.sql.ResultSet;
|
| 36 | 36 | import java.sql.SQLException;
|
| 37 | +import java.util.LinkedList;
|
|
| 38 | +import java.util.List;
|
|
| 37 | 39 | import java.util.Objects;
|
| 38 | 40 | |
| 39 | 41 | /**
|
| ... | ... | @@ -116,17 +118,38 @@ public abstract class JdbcHelper { |
| 116 | 118 | }
|
| 117 | 119 | |
| 118 | 120 | public String runSelectOnString(String sql) throws SQLException {
|
| 119 | - return runSelect(sql, resultSet -> resultSet.next() ? resultSet.getString(1) : null);
|
|
| 121 | + return runSelect(sql, resultSet -> resultSet.getString(1));
|
|
| 120 | 122 | }
|
| 121 | 123 | |
| 122 | 124 | public <T> T runSelect(String sql, SqlFunction<ResultSet, T> function) throws SQLException {
|
| 123 | 125 | try (Connection connection = openConnection()) {
|
| 124 | 126 | try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
|
| 125 | 127 | try (ResultSet resultSet = preparedStatement.executeQuery()) {
|
| 126 | - return function.apply(resultSet);
|
|
| 128 | + if (resultSet.next()) {
|
|
| 129 | + return function.apply(resultSet);
|
|
| 130 | + }
|
|
| 131 | + return null;
|
|
| 132 | + }
|
|
| 133 | + }
|
|
| 134 | + }
|
|
| 135 | + }
|
|
| 136 | + |
|
| 137 | + public <T> List<T> runMultipleSelect(String sql, SqlFunction<ResultSet, T> function) throws SQLException {
|
|
| 138 | + List<T> result = new LinkedList<>();
|
|
| 139 | + try (Connection connection = openConnection()) {
|
|
| 140 | + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
|
|
| 141 | + try (ResultSet resultSet = preparedStatement.executeQuery()) {
|
|
| 142 | + while (resultSet.next()) {
|
|
| 143 | + |
|
| 144 | + T row = function.apply(resultSet);
|
|
| 145 | + if (row != null) {
|
|
| 146 | + result.add(row);
|
|
| 147 | + }
|
|
| 148 | + }
|
|
| 127 | 149 | }
|
| 128 | 150 | }
|
| 129 | 151 | }
|
| 152 | + return result;
|
|
| 130 | 153 | }
|
| 131 | 154 | |
| 132 | 155 | public <T> T runOnMetadata(SqlFunction<DatabaseMetaData, T> function) throws SQLException {
|
| ... | ... | @@ -172,16 +172,16 @@ public interface JdbcSecurityHelper { |
| 172 | 172 | try {
|
| 173 | 173 | return getDefaultVersion();
|
| 174 | 174 | } catch (Exception e) {
|
| 175 | - // try on legacy
|
|
| 175 | + // try on legacy (migration may not been applied)
|
|
| 176 | 176 | return getLegacyVersion();
|
| 177 | 177 | }
|
| 178 | 178 | }
|
| 179 | 179 | |
| 180 | 180 | private Version getLegacyVersion() {
|
| 181 | - return MigrationServiceSqlHelper.LEGACY.getVersion(jdbcHelper()).map(TMSVersion::toVersion).orElse(Version.VZERO);
|
|
| 181 | + return MigrationServiceSqlHelper.LEGACY.getVersion(jdbcHelper()).map(TMSVersion::getVersion).orElse(Version.VZERO);
|
|
| 182 | 182 | }
|
| 183 | 183 | |
| 184 | 184 | private Version getDefaultVersion() {
|
| 185 | - return MigrationServiceSqlHelper.DEFAULT.getVersion(jdbcHelper()).map(TMSVersion::toVersion).orElse(Version.VZERO);
|
|
| 185 | + return MigrationServiceSqlHelper.DEFAULT.getVersion(jdbcHelper()).map(TMSVersion::getVersion).orElse(Version.VZERO);
|
|
| 186 | 186 | }
|
| 187 | 187 | } |
| ... | ... | @@ -31,7 +31,6 @@ import org.nuiton.topia.persistence.security.SecurityScriptHelper; |
| 31 | 31 | |
| 32 | 32 | import java.sql.ResultSet;
|
| 33 | 33 | import java.sql.SQLException;
|
| 34 | -import java.util.ArrayList;
|
|
| 35 | 34 | import java.util.Collection;
|
| 36 | 35 | import java.util.Collections;
|
| 37 | 36 | import java.util.HashSet;
|
| ... | ... | @@ -162,24 +161,21 @@ public class JdbcSecurityHelperH2 implements JdbcSecurityHelper { |
| 162 | 161 | Set<String> safeExtraTables = extraTables.stream().map(String::toLowerCase).collect(Collectors.toSet());
|
| 163 | 162 | try {
|
| 164 | 163 | |
| 165 | - return jdbcHelper.runSelect("SELECT TABLE_SCHEMA, TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'TABLE'", resultSet -> {
|
|
| 166 | - List<Pair<String, String>> result = new ArrayList<>();
|
|
| 167 | - while (resultSet.next()) {
|
|
| 168 | - String schemaName = resultSet.getString(1);
|
|
| 169 | - String tableName = resultSet.getString(2);
|
|
| 170 | - if (!safeExtraTables.contains(tableName.toLowerCase())) {
|
|
| 171 | - // if (POSTGIS_TABLES.contains(tableName)) {
|
|
| 172 | - // continue;
|
|
| 173 | - // }
|
|
| 174 | - if (schemaName == null || !safeSchemas.contains(schemaName.toLowerCase())) {
|
|
| 175 | - continue;
|
|
| 176 | - }
|
|
| 164 | + List<Pair<String, String>> pairs = jdbcHelper.runMultipleSelect("SELECT TABLE_SCHEMA, TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'TABLE'", resultSet -> {
|
|
| 165 | + String schemaName = resultSet.getString(1);
|
|
| 166 | + String tableName = resultSet.getString(2);
|
|
| 167 | + if (!safeExtraTables.contains(tableName.toLowerCase())) {
|
|
| 168 | + // if (POSTGIS_TABLES.contains(tableName)) {
|
|
| 169 | + // continue;
|
|
| 170 | + // }
|
|
| 171 | + if (schemaName == null || !safeSchemas.contains(schemaName.toLowerCase())) {
|
|
| 172 | + return null;
|
|
| 177 | 173 | }
|
| 178 | - result.add(Pair.of(schemaName, tableName));
|
|
| 179 | 174 | }
|
| 180 | - Collections.sort(result);
|
|
| 181 | - return result;
|
|
| 175 | + return Pair.of(schemaName, tableName);
|
|
| 182 | 176 | });
|
| 177 | + Collections.sort(pairs);
|
|
| 178 | + return pairs;
|
|
| 183 | 179 | } catch (Exception e) {
|
| 184 | 180 | throw new TopiaException(e);
|
| 185 | 181 | }
|
| ... | ... | @@ -188,14 +184,8 @@ public class JdbcSecurityHelperH2 implements JdbcSecurityHelper { |
| 188 | 184 | @Override
|
| 189 | 185 | public Set<String> getRoles() {
|
| 190 | 186 | try {
|
| 191 | - return jdbcHelper.runSelect("SELECT name FROM INFORMATION_SCHEMA.USERS;", resultSet -> {
|
|
| 192 | - Set<String> users = new LinkedHashSet<>();
|
|
| 193 | - while (resultSet.next()) {
|
|
| 194 | - String name = resultSet.getString(1);
|
|
| 195 | - users.add(name);
|
|
| 196 | - }
|
|
| 197 | - return users;
|
|
| 198 | - });
|
|
| 187 | + List<String> roles = jdbcHelper.runMultipleSelect("SELECT name FROM INFORMATION_SCHEMA.USERS;", resultSet -> resultSet.getString(1));
|
|
| 188 | + return Set.copyOf(roles);
|
|
| 199 | 189 | } catch (SQLException e) {
|
| 200 | 190 | throw new TopiaException(e);
|
| 201 | 191 | }
|
| ... | ... | @@ -204,7 +194,7 @@ public class JdbcSecurityHelperH2 implements JdbcSecurityHelper { |
| 204 | 194 | @Override
|
| 205 | 195 | public boolean isOwner() {
|
| 206 | 196 | try {
|
| 207 | - return jdbcHelper.runSelect("SELECT t.* FROM INFORMATION_SCHEMA.SCHEMATA t WHERE SCHEMA_NAME = 'PUBLIC' AND SCHEMA_OWNER = CURRENT_USER", ResultSet::next);
|
|
| 197 | + return jdbcHelper.runSelect("SELECT t.* FROM INFORMATION_SCHEMA.SCHEMATA t WHERE SCHEMA_NAME = 'PUBLIC' AND SCHEMA_OWNER = CURRENT_USER", r-> true);
|
|
| 208 | 198 | } catch (SQLException e) {
|
| 209 | 199 | throw new TopiaException(e);
|
| 210 | 200 | }
|
| ... | ... | @@ -213,7 +203,7 @@ public class JdbcSecurityHelperH2 implements JdbcSecurityHelper { |
| 213 | 203 | @Override
|
| 214 | 204 | public boolean isSuperUser() {
|
| 215 | 205 | try {
|
| 216 | - return jdbcHelper.runSelect("SELECT admin FROM INFORMATION_SCHEMA.USERS WHERE name = CURRENT_USER", resultSet -> resultSet.next() && resultSet.getBoolean(1));
|
|
| 206 | + return jdbcHelper.runSelect("SELECT admin FROM INFORMATION_SCHEMA.USERS WHERE name = CURRENT_USER", resultSet -> resultSet.getBoolean(1));
|
|
| 217 | 207 | } catch (SQLException e) {
|
| 218 | 208 | throw new TopiaException(e);
|
| 219 | 209 | }
|
| ... | ... | @@ -253,7 +253,7 @@ public class JdbcSecurityHelperPostgres implements JdbcSecurityHelper { |
| 253 | 253 | @Override
|
| 254 | 254 | public boolean isOwner() {
|
| 255 | 255 | try {
|
| 256 | - return jdbcHelper.runSelect("SELECT pg_catalog.pg_get_userbyid(d.datdba) FROM pg_catalog.pg_database d WHERE d.datname = (SELECT current_database())", resultSet -> resultSet.next() && Objects.equals(jdbcHelper.getJdbcConnectionUser(), resultSet.getString(1)));
|
|
| 256 | + return jdbcHelper.runSelect("SELECT pg_catalog.pg_get_userbyid(d.datdba) FROM pg_catalog.pg_database d WHERE d.datname = (SELECT current_database())", resultSet -> Objects.equals(jdbcHelper.getJdbcConnectionUser(), resultSet.getString(1)));
|
|
| 257 | 257 | } catch (SQLException e) {
|
| 258 | 258 | throw new TopiaException(e);
|
| 259 | 259 | }
|
| ... | ... | @@ -262,7 +262,7 @@ public class JdbcSecurityHelperPostgres implements JdbcSecurityHelper { |
| 262 | 262 | @Override
|
| 263 | 263 | public boolean isSuperUser() {
|
| 264 | 264 | try {
|
| 265 | - return jdbcHelper.runSelect("SELECT usesuper FROM pg_user WHERE usename = CURRENT_USER", resultSet -> resultSet.next() && resultSet.getBoolean(1));
|
|
| 265 | + return jdbcHelper.runSelect("SELECT usesuper FROM pg_user WHERE usename = CURRENT_USER", resultSet -> resultSet.getBoolean(1));
|
|
| 266 | 266 | } catch (SQLException e) {
|
| 267 | 267 | throw new TopiaException(e);
|
| 268 | 268 | }
|
| ... | ... | @@ -275,14 +275,8 @@ public class JdbcSecurityHelperPostgres implements JdbcSecurityHelper { |
| 275 | 275 | " FROM pg_proc p INNER JOIN pg_namespace ns ON (p.pronamespace = ns.oid)" +
|
| 276 | 276 | " WHERE ns.nspname = 'public' AND p.proname ILIKE '%s%%';", functionPattern);
|
| 277 | 277 | try {
|
| 278 | - return jdbcHelper.runSelect(sql, resultSet -> {
|
|
| 279 | - Set<String> result = new LinkedHashSet<>();
|
|
| 280 | - while (resultSet.next()) {
|
|
| 281 | - String functionPrototype = resultSet.getString(1);
|
|
| 282 | - result.add(functionPrototype);
|
|
| 283 | - }
|
|
| 284 | - return result;
|
|
| 285 | - });
|
|
| 278 | + List<String> result = jdbcHelper.runMultipleSelect(sql, resultSet -> resultSet.getString(1));
|
|
| 279 | + return Set.copyOf(result);
|
|
| 286 | 280 | } catch (SQLException e) {
|
| 287 | 281 | throw new TopiaException(e);
|
| 288 | 282 | }
|
| ... | ... | @@ -38,6 +38,9 @@ import org.nuiton.topia.service.migration.version.TMSVersion; |
| 38 | 38 | import java.io.IOException;
|
| 39 | 39 | import java.nio.file.Files;
|
| 40 | 40 | import java.nio.file.Path;
|
| 41 | +import java.util.Date;
|
|
| 42 | +import java.util.List;
|
|
| 43 | +import java.util.Objects;
|
|
| 41 | 44 | import java.util.Optional;
|
| 42 | 45 | |
| 43 | 46 | /**
|
| ... | ... | @@ -56,11 +59,10 @@ public class TopiaMigrationServiceContext { |
| 56 | 59 | * Service configuration.
|
| 57 | 60 | */
|
| 58 | 61 | protected final TopiaMigrationServiceConfiguration configuration;
|
| 59 | - protected final MigrationServiceSqlHelper sqlHelper;
|
|
| 60 | 62 | /**
|
| 61 | - * Jdbc helper used to perform some sql code.
|
|
| 63 | + * Migration sql helper (could be legacy or default).
|
|
| 62 | 64 | */
|
| 63 | - private final JdbcHelper jdbcHelper;
|
|
| 65 | + protected final MigrationServiceSqlHelper sqlHelper;
|
|
| 64 | 66 | /**
|
| 65 | 67 | * Available migration resources found in class-path.
|
| 66 | 68 | */
|
| ... | ... | @@ -74,17 +76,25 @@ public class TopiaMigrationServiceContext { |
| 74 | 76 | */
|
| 75 | 77 | protected final boolean dbNotVersioned;
|
| 76 | 78 | /**
|
| 77 | - * Is legacy TMSVersion table exists?
|
|
| 79 | + * Is legacy migration table exists? (only used with default mode)
|
|
| 78 | 80 | */
|
| 79 | 81 | protected final boolean legacyVersionTableExist;
|
| 80 | 82 | /**
|
| 81 | - * Is TMSVersion schema exists?
|
|
| 83 | + * Is default migration schema exists?
|
|
| 84 | + */
|
|
| 85 | + protected final boolean versionSchemaExist;
|
|
| 86 | + /**
|
|
| 87 | + * Is default migration table exists?
|
|
| 82 | 88 | */
|
| 83 | - protected boolean versionSchemaExist;
|
|
| 89 | + protected final boolean versionTableExist;
|
|
| 84 | 90 | /**
|
| 85 | - * Is TMSVersion table exists?
|
|
| 91 | + * Can we apply migration versions?
|
|
| 86 | 92 | */
|
| 87 | - protected boolean versionTableExist;
|
|
| 93 | + protected final boolean canApplyMigrationVersions;
|
|
| 94 | + /**
|
|
| 95 | + * Migration versions to apply.
|
|
| 96 | + */
|
|
| 97 | + protected final List<Version> versionsToApply;
|
|
| 88 | 98 | /**
|
| 89 | 99 | * Current database version.
|
| 90 | 100 | */
|
| ... | ... | @@ -114,10 +124,11 @@ public class TopiaMigrationServiceContext { |
| 114 | 124 | try {
|
| 115 | 125 | versionTableExist = sqlHelper.isTableExist(jdbcHelper);
|
| 116 | 126 | if (versionTableExist) {
|
| 127 | + log.info("Detected standard migration table {}.", sqlHelper.gav());
|
|
| 117 | 128 | versionSchemaExist = true;
|
| 118 | 129 | Optional<TMSVersion> tmsVersion = sqlHelper.getVersion(jdbcHelper);
|
| 119 | 130 | if (tmsVersion.isPresent()) {
|
| 120 | - v = tmsVersion.get().toVersion();
|
|
| 131 | + v = tmsVersion.get().getVersion();
|
|
| 121 | 132 | }
|
| 122 | 133 | if (v == null) {
|
| 123 | 134 | log.warn(String.format("Version not found on table %s", sqlHelper.gav()));
|
| ... | ... | @@ -126,9 +137,10 @@ public class TopiaMigrationServiceContext { |
| 126 | 137 | versionSchemaExist = sqlHelper.isSchemaExist(jdbcHelper);
|
| 127 | 138 | legacyVersionTableExist = legacySqlHelper.isTableExist(jdbcHelper);
|
| 128 | 139 | if (legacyVersionTableExist) {
|
| 140 | + log.info("Detected legacy migration table {}.", legacySqlHelper.gav());
|
|
| 129 | 141 | Optional<TMSVersion> tmsVersion = legacySqlHelper.getVersion(jdbcHelper);
|
| 130 | 142 | if (tmsVersion.isPresent()) {
|
| 131 | - v = tmsVersion.get().toVersion();
|
|
| 143 | + v = tmsVersion.get().getVersion();
|
|
| 132 | 144 | }
|
| 133 | 145 | if (v == null) {
|
| 134 | 146 | log.warn(String.format("Version not found on legacy table %s", legacySqlHelper.gav()));
|
| ... | ... | @@ -137,6 +149,7 @@ public class TopiaMigrationServiceContext { |
| 137 | 149 | }
|
| 138 | 150 | } finally {
|
| 139 | 151 | if (v == null) {
|
| 152 | + //FIXME Is this can really happen?
|
|
| 140 | 153 | // la base dans ce cas n'est pas versionee.
|
| 141 | 154 | // On dit que la version de la base est 0
|
| 142 | 155 | // et les schema de cette version 0 doivent
|
| ... | ... | @@ -151,7 +164,6 @@ public class TopiaMigrationServiceContext { |
| 151 | 164 | }
|
| 152 | 165 | return new TopiaMigrationServiceContext(configuration,
|
| 153 | 166 | sqlHelper,
|
| 154 | - jdbcHelper,
|
|
| 155 | 167 | legacyVersionTableExist,
|
| 156 | 168 | versionSchemaExist,
|
| 157 | 169 | versionTableExist,
|
| ... | ... | @@ -175,9 +187,10 @@ public class TopiaMigrationServiceContext { |
| 175 | 187 | try {
|
| 176 | 188 | versionTableExist = sqlHelper.isTableExist(jdbcHelper);
|
| 177 | 189 | if (versionTableExist) {
|
| 190 | + log.info("Detected legacy migration table {}.", sqlHelper.gav());
|
|
| 178 | 191 | Optional<TMSVersion> tmsVersion = sqlHelper.getVersion(jdbcHelper);
|
| 179 | 192 | if (tmsVersion.isPresent()) {
|
| 180 | - v = tmsVersion.get().toVersion();
|
|
| 193 | + v = tmsVersion.get().getVersion();
|
|
| 181 | 194 | }
|
| 182 | 195 | if (v == null) {
|
| 183 | 196 | log.warn(String.format("Version not found on table %s", sqlHelper.gav()));
|
| ... | ... | @@ -199,7 +212,6 @@ public class TopiaMigrationServiceContext { |
| 199 | 212 | }
|
| 200 | 213 | return new TopiaMigrationServiceContext(configuration,
|
| 201 | 214 | sqlHelper,
|
| 202 | - jdbcHelper,
|
|
| 203 | 215 | false,
|
| 204 | 216 | true,
|
| 205 | 217 | versionTableExist,
|
| ... | ... | @@ -211,15 +223,14 @@ public class TopiaMigrationServiceContext { |
| 211 | 223 | |
| 212 | 224 | protected TopiaMigrationServiceContext(TopiaMigrationServiceConfiguration configuration,
|
| 213 | 225 | MigrationServiceSqlHelper sqlHelper,
|
| 214 | - JdbcHelper jdbcHelper,
|
|
| 215 | - boolean legacyVersionTableExist, boolean versionSchemaExist,
|
|
| 226 | + boolean legacyVersionTableExist,
|
|
| 227 | + boolean versionSchemaExist,
|
|
| 216 | 228 | boolean versionTableExist,
|
| 217 | 229 | boolean dbNotVersioned,
|
| 218 | 230 | Version dbVersion,
|
| 219 | 231 | MigrationVersionResourceProvider resources) {
|
| 220 | 232 | this.configuration = configuration;
|
| 221 | 233 | this.sqlHelper = sqlHelper;
|
| 222 | - this.jdbcHelper = jdbcHelper;
|
|
| 223 | 234 | this.versionSchemaExist = versionSchemaExist;
|
| 224 | 235 | this.versionTableExist = versionTableExist;
|
| 225 | 236 | this.dbNotVersioned = dbNotVersioned;
|
| ... | ... | @@ -231,26 +242,24 @@ public class TopiaMigrationServiceContext { |
| 231 | 242 | } catch (IOException e) {
|
| 232 | 243 | throw new IllegalStateException("Can't create scripts path", e);
|
| 233 | 244 | }
|
| 234 | - }
|
|
| 235 | 245 | |
| 236 | - public Path getScriptPath() {
|
|
| 237 | - return scriptPath;
|
|
| 238 | - }
|
|
| 246 | + List<Version> allVersions = getResources().getAvailableVersions();
|
|
| 247 | + log.info(String.format("Detected available versions: %1$s", allVersions));
|
|
| 239 | 248 | |
| 240 | - public boolean isVersionSchemaExist() {
|
|
| 241 | - return versionSchemaExist;
|
|
| 242 | - }
|
|
| 249 | + versionsToApply = getResources().getVersionsAfter(dbVersion);
|
|
| 250 | + log.info(String.format("Detected versions to apply: %1$s", versionsToApply));
|
|
| 243 | 251 | |
| 244 | - public boolean isVersionTableExist() {
|
|
| 245 | - return versionTableExist;
|
|
| 246 | - }
|
|
| 252 | + Optional<TopiaMigrationServiceAskUserToMigrate> askUserToMigrate = Optional.ofNullable(configuration.getCallback());
|
|
| 247 | 253 | |
| 248 | - public boolean isLegacyVersionTableExist() {
|
|
| 249 | - return legacyVersionTableExist;
|
|
| 254 | + // ask to perform the migration
|
|
| 255 | + canApplyMigrationVersions = askUserToMigrate.map(c -> c.canIMigrate(getDbVersion(), versionsToApply)).orElse(true);
|
|
| 256 | + if (!canApplyMigrationVersions) {
|
|
| 257 | + log.warn("Detected that we can not migrate versions (maybe version of database is too old?).");
|
|
| 258 | + }
|
|
| 250 | 259 | }
|
| 251 | 260 | |
| 252 | - public boolean isDbNotVersioned() {
|
|
| 253 | - return dbNotVersioned;
|
|
| 261 | + public Path getScriptPath() {
|
|
| 262 | + return scriptPath;
|
|
| 254 | 263 | }
|
| 255 | 264 | |
| 256 | 265 | public Version getModelVersion() {
|
| ... | ... | @@ -265,34 +274,65 @@ public class TopiaMigrationServiceContext { |
| 265 | 274 | return resources;
|
| 266 | 275 | }
|
| 267 | 276 | |
| 268 | - public void createSchemaIfNotExist() {
|
|
| 277 | + public boolean adaptVersionTable(TopiaSqlSupport sqlSupport) {
|
|
| 278 | + |
|
| 279 | + Version modelVersion = getModelVersion();
|
|
| 280 | + boolean dbVersionEqualsModelVersion = Objects.equals(dbVersion, modelVersion);
|
|
| 281 | + |
|
| 269 | 282 | if (!versionSchemaExist) {
|
| 270 | - sqlHelper.createSchema(jdbcHelper);
|
|
| 271 | - versionSchemaExist = true;
|
|
| 283 | + log.info("Create default migration schema.");
|
|
| 284 | + sqlHelper.createSchema(sqlSupport);
|
|
| 272 | 285 | }
|
| 273 | - }
|
|
| 274 | - |
|
| 275 | - public void createTableIfNotExist() {
|
|
| 276 | 286 | if (!versionTableExist) {
|
| 277 | - sqlHelper.createTable(jdbcHelper);
|
|
| 278 | - versionTableExist = true;
|
|
| 287 | + log.info("Create default migration table.");
|
|
| 288 | + sqlHelper.createTable(sqlSupport);
|
|
| 289 | + } else {
|
|
| 290 | + if (dbVersionEqualsModelVersion) {
|
|
| 291 | + log.info("Database is up to date, no migration needed.");
|
|
| 292 | + return false;
|
|
| 293 | + }
|
|
| 294 | + }
|
|
| 295 | + if (legacyVersionTableExist) {
|
|
| 296 | + log.info("Drop legacy migration table.");
|
|
| 297 | + MigrationServiceSqlHelper.LEGACY.dropTable(sqlSupport);
|
|
| 298 | + if (dbVersionEqualsModelVersion) {
|
|
| 299 | + log.info("Database is up to date (version: {}), but was coming from legacy migration table, fill new migration table.", modelVersion);
|
|
| 300 | + saveVersion(sqlSupport, modelVersion, null);
|
|
| 301 | + return false;
|
|
| 302 | + }
|
|
| 303 | + log.info("Fill version table {} from legacy table.", dbVersion);
|
|
| 304 | + saveVersion(sqlSupport, dbVersion, null);
|
|
| 305 | + } else if (dbNotVersioned) {
|
|
| 306 | + log.info("Database is empty, no migration needed, set ");
|
|
| 307 | + saveVersion(sqlSupport, modelVersion, new Date());
|
|
| 308 | + return false;
|
|
| 279 | 309 | }
|
| 310 | + // In all other cases, we can try to perform migration
|
|
| 311 | + return true;
|
|
| 280 | 312 | }
|
| 281 | 313 | |
| 282 | - protected void saveModelVersionAndCommit() {
|
|
| 283 | - Version version = getModelVersion();
|
|
| 284 | - log.info(String.format("[ Version %s ] Saving persistence model database version and commit.", version));
|
|
| 285 | - sqlHelper.saveAndCommit(jdbcHelper, version.getVersion());
|
|
| 286 | - dbVersion = version;
|
|
| 287 | - }
|
|
| 314 | + public List<Version> getVersionsToApply() {
|
|
| 315 | + |
|
| 316 | + if (versionsToApply.isEmpty()) {
|
|
| 317 | + log.info("No version to apply, no migration needed.");
|
|
| 318 | + // No migration, so nothing to save
|
|
| 319 | +// context.saveModelVersion();
|
|
| 320 | + return null;
|
|
| 321 | + }
|
|
| 322 | + |
|
| 323 | + // ask to perform the migration
|
|
| 288 | 324 | |
| 289 | - public Optional<TopiaMigrationServiceAskUserToMigrate> getAskUserToMigrate() {
|
|
| 290 | - return Optional.ofNullable(configuration.getCallback());
|
|
| 325 | + if (!canApplyMigrationVersions) {
|
|
| 326 | + // user cancel migration
|
|
| 327 | + log.warn("Migration was cancelled by migration callback (maybe version of database is too old?).");
|
|
| 328 | + return null;
|
|
| 329 | + }
|
|
| 330 | + return versionsToApply;
|
|
| 291 | 331 | }
|
| 292 | 332 | |
| 293 | - protected void saveVersion(TopiaSqlSupport sqlSupport, Version version) {
|
|
| 294 | - log.info(String.format("[ Version %s ] Saving new database version.", version));
|
|
| 295 | - sqlHelper.save(sqlSupport, version.getVersion());
|
|
| 333 | + protected void saveVersion(TopiaSqlSupport sqlSupport, Version version, Date date) {
|
|
| 334 | + log.info(String.format("[ Version %s ] Saving new database version (date: %s).", version, date));
|
|
| 335 | + sqlHelper.save(sqlSupport, version.getVersion(), date);
|
|
| 296 | 336 | dbVersion = version;
|
| 297 | 337 | }
|
| 298 | 338 | |
| ... | ... | @@ -308,8 +348,4 @@ public class TopiaMigrationServiceContext { |
| 308 | 348 | return new MigrationVersionResourceExecutor(migrationVersionResource, sqlSupport, configuration.getClassifier(), getScriptPath(), migrationVersionResource.getScriptVariables());
|
| 309 | 349 | }
|
| 310 | 350 | |
| 311 | - public void dropLegacyTable() {
|
|
| 312 | - MigrationServiceSqlHelper.LEGACY.dropTable(jdbcHelper);
|
|
| 313 | - }
|
|
| 314 | - |
|
| 315 | 351 | } |
| ... | ... | @@ -34,10 +34,10 @@ import org.nuiton.topia.service.migration.resources.MigrationVersionResource; |
| 34 | 34 | import org.nuiton.topia.service.migration.resources.MigrationVersionResourceExecutor;
|
| 35 | 35 | |
| 36 | 36 | import java.io.IOException;
|
| 37 | +import java.util.Date;
|
|
| 37 | 38 | import java.util.List;
|
| 38 | 39 | import java.util.Map;
|
| 39 | 40 | import java.util.Objects;
|
| 40 | -import java.util.Optional;
|
|
| 41 | 41 | |
| 42 | 42 | /**
|
| 43 | 43 | * Topia migration service default implementation.
|
| ... | ... | @@ -68,82 +68,38 @@ public class TopiaMigrationServiceImpl implements TopiaMigrationService { |
| 68 | 68 | @Override
|
| 69 | 69 | public void runSchemaMigration() throws TopiaMigrationServiceException {
|
| 70 | 70 | |
| 71 | + log.info("Migration - Prepare internal states.");
|
|
| 71 | 72 | TopiaMigrationServiceContext context = TopiaMigrationServiceContext.create(configuration);
|
| 72 | - Version modelVersion = context.getModelVersion();
|
|
| 73 | - Version dbVersion = context.getDbVersion();
|
|
| 74 | - boolean dbNotVersioned = context.isDbNotVersioned();
|
|
| 75 | - boolean legacyVersionTableExist = context.isLegacyVersionTableExist();
|
|
| 76 | - boolean versionSchemaExist = context.isVersionSchemaExist();
|
|
| 77 | - boolean versionTableExist = context.isVersionTableExist();
|
|
| 78 | - |
|
| 79 | - log.info(String.format("Starting Topia Migration Service - Model version : %s, Database version : %s", modelVersion, dbVersion));
|
|
| 80 | - |
|
| 81 | - if (!versionSchemaExist) {
|
|
| 82 | - log.info("Create migration schema.");
|
|
| 83 | - context.createSchemaIfNotExist();
|
|
| 84 | - }
|
|
| 85 | - if (!versionTableExist) {
|
|
| 86 | - log.info("Create migration table.");
|
|
| 87 | - context.createTableIfNotExist();
|
|
| 88 | - }
|
|
| 89 | - if (legacyVersionTableExist) {
|
|
| 90 | - log.info("Drop legacy migration table.");
|
|
| 91 | - context.dropLegacyTable();
|
|
| 92 | - if (dbVersion.equals(modelVersion)) {
|
|
| 93 | - log.info("Database is up to date, but was coming from legacy migration table, fill new migration table.");
|
|
| 94 | - context.saveModelVersionAndCommit();
|
|
| 73 | + log.info("Migration - Prepare internal states done.");
|
|
| 74 | + |
|
| 75 | + try (TopiaPersistenceContext persistenceContext = context.newPersistenceContext()) {
|
|
| 76 | + log.info("Migration - Adapting migration table.");
|
|
| 77 | + boolean canContinue = adaptVersionTable(persistenceContext, context);
|
|
| 78 | + log.info("Migration - Adapting migration table done.");
|
|
| 79 | + if (!canContinue) {
|
|
| 80 | + log.info(String.format("Migration - End - db version: %s (nothing more to do).", context.getDbVersion()));
|
|
| 81 | + return;
|
|
| 82 | + }
|
|
| 83 | + log.info("Migration - Getting migration versions to apply.");
|
|
| 84 | + List<Version> versionsToApply = context.getVersionsToApply();
|
|
| 85 | + log.info("Migration - Getting migration versions to apply done.");
|
|
| 86 | + if (versionsToApply == null) {
|
|
| 87 | + log.info(String.format("Migration - End - db version: %s (nothing more to do).", context.getDbVersion()));
|
|
| 95 | 88 | return;
|
| 96 | 89 | }
|
| 97 | - }
|
|
| 98 | - |
|
| 99 | - if (versionTableExist && dbVersion.equals(modelVersion)) {
|
|
| 100 | - log.info("Database is up to date, no migration needed.");
|
|
| 101 | - return;
|
|
| 102 | - }
|
|
| 103 | - |
|
| 104 | - if (versionTableExist && dbNotVersioned) {
|
|
| 105 | - log.info("Database is empty, no migration needed.");
|
|
| 106 | - context.saveModelVersionAndCommit();
|
|
| 107 | - return;
|
|
| 108 | - }
|
|
| 109 | - |
|
| 110 | - List<Version> allVersions = context.getResources().getAvailableVersions();
|
|
| 111 | - log.info(String.format("Available versions: %1$s", allVersions));
|
|
| 112 | - |
|
| 113 | - List<Version> versionsToApply = context.getResources().getVersionsAfter(dbVersion);
|
|
| 114 | - |
|
| 115 | - if (versionsToApply.isEmpty()) {
|
|
| 116 | - //TODO This case should never happen?
|
|
| 117 | - log.info("No version to apply, no migration needed.");
|
|
| 118 | - context.saveModelVersionAndCommit();
|
|
| 119 | - return;
|
|
| 120 | - }
|
|
| 121 | - |
|
| 122 | - log.info(String.format("Versions to apply: %1$s", versionsToApply));
|
|
| 123 | - |
|
| 124 | - Optional<TopiaMigrationServiceAskUserToMigrate> askUserToMigrate = context.getAskUserToMigrate();
|
|
| 125 | - |
|
| 126 | - // ask to perform the migration
|
|
| 127 | - boolean performMigration = askUserToMigrate.map(c -> c.canIMigrate(dbVersion, versionsToApply)).orElse(true);
|
|
| 128 | - |
|
| 129 | - log.debug("Handler choose : " + performMigration);
|
|
| 130 | 90 | |
| 131 | - if (!performMigration) {
|
|
| 132 | - // user cancel migration
|
|
| 133 | - return;
|
|
| 134 | - }
|
|
| 91 | + log.info(String.format("Migration - Will apply versions: %1$s.", versionsToApply));
|
|
| 135 | 92 | |
| 136 | - long statementCount = 0;
|
|
| 137 | - try (TopiaPersistenceContext persistenceContext = context.newPersistenceContext()) {
|
|
| 93 | + long statementCount = 0;
|
|
| 138 | 94 | for (Version version : versionsToApply) {
|
| 139 | 95 | long t0 = TimeLog.getTime();
|
| 140 | 96 | long versionCount = migrateVersion(persistenceContext, version, context);
|
| 141 | 97 | statementCount += versionCount;
|
| 142 | 98 | TIME_LOG.log(t0, "migrationVersion", version.toString() + " - " + versionCount + " sql statements");
|
| 143 | 99 | }
|
| 100 | + log.info(String.format("Migration - End - db version: %s - consume %d sql statement(s)", context.getDbVersion(), statementCount));
|
|
| 144 | 101 | }
|
| 145 | 102 | |
| 146 | - log.info(String.format("Ends migration - db version: %s - consume %d sql statement(s)", context.getDbVersion(), statementCount));
|
|
| 147 | 103 | }
|
| 148 | 104 | |
| 149 | 105 | @Override
|
| ... | ... | @@ -151,6 +107,17 @@ public class TopiaMigrationServiceImpl implements TopiaMigrationService { |
| 151 | 107 | configuration = null;
|
| 152 | 108 | }
|
| 153 | 109 | |
| 110 | + protected boolean adaptVersionTable(TopiaPersistenceContext persistenceContext, TopiaMigrationServiceContext context) {
|
|
| 111 | + try {
|
|
| 112 | + boolean canContinue = context.adaptVersionTable(persistenceContext.getSqlSupport());
|
|
| 113 | + persistenceContext.commit();
|
|
| 114 | + return canContinue;
|
|
| 115 | + } catch (Exception e) {
|
|
| 116 | + persistenceContext.rollback();
|
|
| 117 | + throw new TopiaMigrationServiceException("Exception during adapting migration table", e);
|
|
| 118 | + }
|
|
| 119 | + }
|
|
| 120 | + |
|
| 154 | 121 | protected long migrateVersion(TopiaPersistenceContext persistenceContext, Version version, TopiaMigrationServiceContext context) {
|
| 155 | 122 | |
| 156 | 123 | MigrationVersionResource resource = context.getResource(version);
|
| ... | ... | @@ -175,14 +142,15 @@ public class TopiaMigrationServiceImpl implements TopiaMigrationService { |
| 175 | 142 | }
|
| 176 | 143 | }
|
| 177 | 144 | // here we use the sqlSupport to save version, to stay on the very same transaction
|
| 178 | - context.saveVersion(sqlSupport, version);
|
|
| 145 | + context.saveVersion(sqlSupport, version, new Date());
|
|
| 179 | 146 | |
| 147 | + // do commit, this migration version is now completed
|
|
| 148 | + persistenceContext.commit();
|
|
| 180 | 149 | } catch (Exception e) {
|
| 181 | 150 | // Exception, rollback transaction
|
| 182 | 151 | persistenceContext.rollback();
|
| 183 | 152 | throw new TopiaMigrationServiceException("Exception during schema migration on version: " + version, e);
|
| 184 | 153 | }
|
| 185 | - persistenceContext.commit();
|
|
| 186 | 154 | return outStatementCount;
|
| 187 | 155 | }
|
| 188 | 156 |
| ... | ... | @@ -27,6 +27,10 @@ import org.nuiton.topia.persistence.jdbc.JdbcHelper; |
| 27 | 27 | import org.nuiton.topia.persistence.support.TopiaSqlSupport;
|
| 28 | 28 | |
| 29 | 29 | import java.sql.SQLException;
|
| 30 | +import java.sql.Timestamp;
|
|
| 31 | +import java.util.Comparator;
|
|
| 32 | +import java.util.Date;
|
|
| 33 | +import java.util.List;
|
|
| 30 | 34 | import java.util.Optional;
|
| 31 | 35 | |
| 32 | 36 | /**
|
| ... | ... | @@ -38,12 +42,8 @@ import java.util.Optional; |
| 38 | 42 | * @since 9.0.7
|
| 39 | 43 | */
|
| 40 | 44 | public interface MigrationServiceSqlHelper {
|
| 41 | - String GET_VERSION_TABLE_STATEMENT = "SELECT version FROM %s;";
|
|
| 42 | - String CREATE_VERSION_SCHEMA_STATEMENT = "CREATE SCHEMA %s;";
|
|
| 43 | - String CREATE_VERSION_TABLE_STATEMENT = "CREATE TABLE %s(version VARCHAR(255) NOT NULL, PRIMARY KEY (version));";
|
|
| 44 | - String DELETE_VERSION_TABLE_STATEMENT = "DELETE FROM %s;";
|
|
| 45 | + String CREATE_VERSION_SCHEMA_STATEMENT = "CREATE SCHEMA IF NOT EXISTS %s;";
|
|
| 45 | 46 | String DROP_VERSION_TABLE_STATEMENT = "DROP TABLE %s;";
|
| 46 | - String FILL_VERSION_TABLE_STATEMENT = "INSERT INTO %s(version) VALUES('%s');";
|
|
| 47 | 47 | |
| 48 | 48 | /**
|
| 49 | 49 | * Legacy layout.
|
| ... | ... | @@ -51,6 +51,10 @@ public interface MigrationServiceSqlHelper { |
| 51 | 51 | MigrationServiceSqlHelper LEGACY = new MigrationServiceSqlHelper() {
|
| 52 | 52 | public static final String SCHEMA_NAME = "public";
|
| 53 | 53 | public static final String TABLE_NAME = "tms_version";
|
| 54 | + public static final String CREATE_VERSION_TABLE_STATEMENT = "CREATE TABLE %s(version VARCHAR(255) NOT NULL, PRIMARY KEY(version));";
|
|
| 55 | + public static final String FILL_VERSION_TABLE_STATEMENT = "INSERT INTO %s(version) VALUES('%s');";
|
|
| 56 | + public static final String GET_VERSION_TABLE_STATEMENT = "SELECT version FROM %s;";
|
|
| 57 | + public static final String DELETE_VERSION_TABLE_STATEMENT = "DELETE FROM %s;";
|
|
| 54 | 58 | |
| 55 | 59 | @Override
|
| 56 | 60 | public String schemaName() {
|
| ... | ... | @@ -61,13 +65,46 @@ public interface MigrationServiceSqlHelper { |
| 61 | 65 | public String tableName() {
|
| 62 | 66 | return TABLE_NAME;
|
| 63 | 67 | }
|
| 68 | + |
|
| 69 | + @Override
|
|
| 70 | + public String createTableSql() {
|
|
| 71 | + return String.format(CREATE_VERSION_TABLE_STATEMENT, gav());
|
|
| 72 | + }
|
|
| 73 | + |
|
| 74 | + @Override
|
|
| 75 | + public String getVersionSql() {
|
|
| 76 | + return String.format(GET_VERSION_TABLE_STATEMENT, gav());
|
|
| 77 | + }
|
|
| 78 | + |
|
| 79 | + @Override
|
|
| 80 | + public Optional<TMSVersion> getVersion(JdbcHelper jdbcHelper) {
|
|
| 81 | + try {
|
|
| 82 | + try {
|
|
| 83 | + TMSVersion result = jdbcHelper.runSelect(getVersionSql(), r -> new TMSVersion(r.getString(1), null));
|
|
| 84 | + return Optional.ofNullable(result);
|
|
| 85 | + } catch (Exception e) {
|
|
| 86 | + throw new TopiaException("Could not obtain version", e);
|
|
| 87 | + }
|
|
| 88 | + } catch (Exception e) {
|
|
| 89 | + throw new TopiaException("Could not obtain version", e);
|
|
| 90 | + }
|
|
| 91 | + }
|
|
| 92 | + |
|
| 93 | + @Override
|
|
| 94 | + public String saveTableSql(String version, Date date) {
|
|
| 95 | + return String.format(DELETE_VERSION_TABLE_STATEMENT, gav()) + String.format(FILL_VERSION_TABLE_STATEMENT, gav(), version);
|
|
| 96 | + }
|
|
| 64 | 97 | };
|
| 65 | 98 | /**
|
| 66 | 99 | * Default layout.
|
| 67 | 100 | */
|
| 68 | 101 | MigrationServiceSqlHelper DEFAULT = new MigrationServiceSqlHelper() {
|
| 69 | 102 | public static final String SCHEMA_NAME = "common";
|
| 103 | + //FIXME Find a better name
|
|
| 70 | 104 | public static final String TABLE_NAME = "database_version";
|
| 105 | + public static final String CREATE_VERSION_TABLE_STATEMENT = "CREATE TABLE %s(version VARCHAR(255) NOT NULL, date TIMESTAMP, PRIMARY KEY(version));";
|
|
| 106 | + public static final String FILL_VERSION_TABLE_STATEMENT = "INSERT INTO %s(version, date) VALUES('%s', %s);";
|
|
| 107 | + public static final String GET_VERSION_TABLE_STATEMENT = "SELECT version, date FROM %s;";
|
|
| 71 | 108 | |
| 72 | 109 | @Override
|
| 73 | 110 | public String schemaName() {
|
| ... | ... | @@ -78,6 +115,35 @@ public interface MigrationServiceSqlHelper { |
| 78 | 115 | public String tableName() {
|
| 79 | 116 | return TABLE_NAME;
|
| 80 | 117 | }
|
| 118 | + |
|
| 119 | + @Override
|
|
| 120 | + public String createTableSql() {
|
|
| 121 | + return String.format(CREATE_VERSION_TABLE_STATEMENT, gav());
|
|
| 122 | + }
|
|
| 123 | + |
|
| 124 | + @Override
|
|
| 125 | + public String getVersionSql() {
|
|
| 126 | + return String.format(GET_VERSION_TABLE_STATEMENT, gav());
|
|
| 127 | + }
|
|
| 128 | + |
|
| 129 | + @Override
|
|
| 130 | + public Optional<TMSVersion> getVersion(JdbcHelper jdbcHelper) {
|
|
| 131 | + try {
|
|
| 132 | + List<TMSVersion> result = jdbcHelper.runMultipleSelect(getVersionSql(), r -> new TMSVersion(r.getString(1), r.getTimestamp(2)));
|
|
| 133 | + if (result.isEmpty()) {
|
|
| 134 | + return Optional.empty();
|
|
| 135 | + }
|
|
| 136 | + result.sort(Comparator.comparing(TMSVersion::getVersion).reversed());
|
|
| 137 | + return Optional.of(result.get(0));
|
|
| 138 | + } catch (Exception e) {
|
|
| 139 | + throw new TopiaException("Could not obtain version", e);
|
|
| 140 | + }
|
|
| 141 | + }
|
|
| 142 | + |
|
| 143 | + @Override
|
|
| 144 | + public String saveTableSql(String version, Date date) {
|
|
| 145 | + return String.format(FILL_VERSION_TABLE_STATEMENT, gav(), version, date == null ? "NULL" : ("'" + new Timestamp(date.getTime()) + "'::timestamp"));
|
|
| 146 | + }
|
|
| 81 | 147 | };
|
| 82 | 148 | |
| 83 | 149 | /**
|
| ... | ... | @@ -110,59 +176,22 @@ public interface MigrationServiceSqlHelper { |
| 110 | 176 | return schemaName() + "." + tableName();
|
| 111 | 177 | }
|
| 112 | 178 | |
| 113 | - default Optional<TMSVersion> getVersion(JdbcHelper jdbcHelper) {
|
|
| 114 | - try {
|
|
| 115 | - String version = jdbcHelper.runSelectOnString(getVersionSql());
|
|
| 116 | - return Optional.ofNullable(version == null ? null : new TMSVersion(version));
|
|
| 117 | - } catch (Exception e) {
|
|
| 118 | - throw new TopiaException("Could not obtain version", e);
|
|
| 119 | - }
|
|
| 120 | - }
|
|
| 121 | - |
|
| 122 | - default String getVersionSql() {
|
|
| 123 | - return String.format(GET_VERSION_TABLE_STATEMENT, gav());
|
|
| 124 | - }
|
|
| 179 | + Optional<TMSVersion> getVersion(JdbcHelper jdbcHelper);
|
|
| 125 | 180 | |
| 126 | - default String createSchemaSql() {
|
|
| 127 | - return String.format(CREATE_VERSION_SCHEMA_STATEMENT, gav());
|
|
| 128 | - }
|
|
| 181 | + String getVersionSql();
|
|
| 129 | 182 | |
| 130 | - default String createTableSql() {
|
|
| 131 | - return String.format(CREATE_VERSION_TABLE_STATEMENT, gav());
|
|
| 132 | - }
|
|
| 183 | + String createTableSql();
|
|
| 133 | 184 | |
| 134 | - default String deleteTableSql() {
|
|
| 135 | - return String.format(DELETE_VERSION_TABLE_STATEMENT, gav());
|
|
| 136 | - }
|
|
| 185 | + String saveTableSql(String version, Date date);
|
|
| 137 | 186 | |
| 138 | - default String fillVersionSql(String version) {
|
|
| 139 | - return String.format(FILL_VERSION_TABLE_STATEMENT, gav(), version);
|
|
| 187 | + default String createSchemaSql() {
|
|
| 188 | + return String.format(CREATE_VERSION_SCHEMA_STATEMENT, schemaName());
|
|
| 140 | 189 | }
|
| 141 | 190 | |
| 142 | 191 | default String dropTableSql() {
|
| 143 | 192 | return String.format(DROP_VERSION_TABLE_STATEMENT, gav());
|
| 144 | 193 | }
|
| 145 | 194 | |
| 146 | - default String saveTableSql(String version) {
|
|
| 147 | - return deleteTableSql() + fillVersionSql(version);
|
|
| 148 | - }
|
|
| 149 | - |
|
| 150 | - default void saveAndCommit(JdbcHelper jdbcHelper, String version) {
|
|
| 151 | - try {
|
|
| 152 | - jdbcHelper.runUpdate(saveTableSql(version) + "commit;");
|
|
| 153 | - } catch (Exception e) {
|
|
| 154 | - throw new TopiaException(String.format("Could not save version %s", version), e);
|
|
| 155 | - }
|
|
| 156 | - }
|
|
| 157 | - |
|
| 158 | - default void save(TopiaSqlSupport sqlSupport, String version) {
|
|
| 159 | - try {
|
|
| 160 | - sqlSupport.executeSql(saveTableSql(version));
|
|
| 161 | - } catch (Exception e) {
|
|
| 162 | - throw new TopiaException(String.format("Could not save version %s", version), e);
|
|
| 163 | - }
|
|
| 164 | - }
|
|
| 165 | - |
|
| 166 | 195 | default boolean isTableExist(JdbcHelper jdbcHelper) {
|
| 167 | 196 | try {
|
| 168 | 197 | return jdbcHelper.isTableExist(schemaName(), tableName());
|
| ... | ... | @@ -179,27 +208,35 @@ public interface MigrationServiceSqlHelper { |
| 179 | 208 | }
|
| 180 | 209 | }
|
| 181 | 210 | |
| 182 | - default void createSchema(JdbcHelper jdbcHelper) {
|
|
| 211 | + default void createSchema(TopiaSqlSupport sqlSupport) {
|
|
| 183 | 212 | try {
|
| 184 | - jdbcHelper.runUpdate(createSchemaSql());
|
|
| 213 | + sqlSupport.executeSql(createSchemaSql());
|
|
| 185 | 214 | } catch (Exception e) {
|
| 186 | - throw new TopiaException(String.format("Could not create version table %s", gav()), e);
|
|
| 215 | + throw new TopiaException(String.format("Could not create version schema %s", gav()), e);
|
|
| 187 | 216 | }
|
| 188 | 217 | }
|
| 189 | 218 | |
| 190 | - default void createTable(JdbcHelper jdbcHelper) {
|
|
| 219 | + default void createTable(TopiaSqlSupport sqlSupport) {
|
|
| 191 | 220 | try {
|
| 192 | - jdbcHelper.runUpdate(createTableSql());
|
|
| 221 | + sqlSupport.executeSql(createTableSql());
|
|
| 193 | 222 | } catch (Exception e) {
|
| 194 | 223 | throw new TopiaException(String.format("Could not create version table %s", gav()), e);
|
| 195 | 224 | }
|
| 196 | 225 | }
|
| 197 | 226 | |
| 198 | - default void dropTable(JdbcHelper jdbcHelper) {
|
|
| 227 | + default void dropTable(TopiaSqlSupport sqlSupport) {
|
|
| 199 | 228 | try {
|
| 200 | - jdbcHelper.runUpdate(dropTableSql());
|
|
| 229 | + sqlSupport.executeSql(dropTableSql());
|
|
| 201 | 230 | } catch (Exception e) {
|
| 202 | 231 | throw new TopiaException(String.format("Could not drop version table %s", gav()), e);
|
| 203 | 232 | }
|
| 204 | 233 | }
|
| 234 | + |
|
| 235 | + default void save(TopiaSqlSupport sqlSupport, String version, Date date) {
|
|
| 236 | + try {
|
|
| 237 | + sqlSupport.executeSql(saveTableSql(version, date));
|
|
| 238 | + } catch (Exception e) {
|
|
| 239 | + throw new TopiaException(String.format("Could not save version %s", version), e);
|
|
| 240 | + }
|
|
| 241 | + }
|
|
| 205 | 242 | } |
| ... | ... | @@ -25,6 +25,7 @@ package org.nuiton.topia.service.migration.version; |
| 25 | 25 | import io.ultreia.java4all.util.Version;
|
| 26 | 26 | |
| 27 | 27 | import java.io.Serializable;
|
| 28 | +import java.util.Date;
|
|
| 28 | 29 | |
| 29 | 30 | /**
|
| 30 | 31 | * @author Tony Chemit - dev@tchemit.fr
|
| ... | ... | @@ -33,27 +34,23 @@ public class TMSVersion implements Serializable { |
| 33 | 34 | |
| 34 | 35 | private static final long serialVersionUID = 1L;
|
| 35 | 36 | |
| 36 | - private String version;
|
|
| 37 | + private final Version version;
|
|
| 38 | + private final Date date;
|
|
| 37 | 39 | |
| 38 | - public TMSVersion() {
|
|
| 39 | - }
|
|
| 40 | - |
|
| 41 | - public TMSVersion(String version) {
|
|
| 40 | + public TMSVersion(String version, Date date) {
|
|
| 42 | 41 | if (version == null || version.isEmpty()) {
|
| 43 | 42 | throw new IllegalArgumentException("version parameter can not be null nor empty.");
|
| 44 | 43 | }
|
| 45 | - this.version = version;
|
|
| 44 | + this.version = Version.valueOf(version);
|
|
| 45 | + this.date = date;
|
|
| 46 | 46 | }
|
| 47 | 47 | |
| 48 | - public String getVersion() {
|
|
| 48 | + public Version getVersion() {
|
|
| 49 | 49 | return version;
|
| 50 | 50 | }
|
| 51 | 51 | |
| 52 | - public void setVersion(String version) {
|
|
| 53 | - this.version = version;
|
|
| 52 | + public Date getDate() {
|
|
| 53 | + return date;
|
|
| 54 | 54 | }
|
| 55 | 55 | |
| 56 | - public Version toVersion() {
|
|
| 57 | - return version == null || version.isEmpty() ? null : Version.valueOf(version);
|
|
| 58 | - }
|
|
| 59 | 56 | } |
| ... | ... | @@ -29,6 +29,8 @@ import org.nuiton.topia.service.sql.internal.SqlRequestConsumer; |
| 29 | 29 | import org.nuiton.topia.service.sql.internal.SqlRequestSetConsumerContext;
|
| 30 | 30 | import org.nuiton.topia.service.sql.internal.request.AddVersionTableRequest;
|
| 31 | 31 | |
| 32 | +import java.util.Date;
|
|
| 33 | + |
|
| 32 | 34 | /**
|
| 33 | 35 | * Created on 21/02/2021.
|
| 34 | 36 | *
|
| ... | ... | @@ -42,7 +44,7 @@ public class AddVersionTableConsumer implements SqlRequestConsumer<AddVersionTab |
| 42 | 44 | try {
|
| 43 | 45 | SqlScriptWriter writer = context.getWriter();
|
| 44 | 46 | writer.writeSql(MigrationServiceSqlHelper.CURRENT.createTableSql());
|
| 45 | - writer.writeSql(MigrationServiceSqlHelper.CURRENT.fillVersionSql(request.getDbVersion().getVersion()));
|
|
| 47 | + writer.writeSql(MigrationServiceSqlHelper.CURRENT.saveTableSql(request.getDbVersion().getVersion(), new Date()));
|
|
| 46 | 48 | } catch (Exception e) {
|
| 47 | 49 | throw new TopiaException(String.format("Could not add version table for reason: %s", e.getMessage()), e);
|
| 48 | 50 | }
|