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

Commits:

1 changed file:

Changes:

  • toolkit/persistence/src/main/java/org/nuiton/topia/service/migration/TopiaMigrationServiceContext.java
    ... ... @@ -38,6 +38,7 @@ 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.sql.SQLException;
    
    41 42
     import java.util.Date;
    
    42 43
     import java.util.List;
    
    43 44
     import java.util.Objects;
    
    ... ... @@ -149,14 +150,15 @@ public class TopiaMigrationServiceContext {
    149 150
                 }
    
    150 151
             } finally {
    
    151 152
                 if (v == null) {
    
    152
    -                //FIXME Is this can really happen?
    
    153
    -                // la base dans ce cas n'est pas versionee.
    
    154
    -                // On dit que la version de la base est 0
    
    155
    -                // et les schema de cette version 0 doivent
    
    156
    -                // etre detenu en local
    
    157
    -                v = Version.VZERO;
    
    153
    +                // If no version found, db not versioned
    
    158 154
                     dbNotVersioned = true;
    
    159
    -                log.info("Database version not found, so database schema is considered as V0");
    
    155
    +                try {
    
    156
    +                    // We can still try to detect database version from the structure of the database
    
    157
    +                    // See https://gitlab.com/ultreiaio/ird-observe/-/issues/2712
    
    158
    +                    v = detectVersionFromStructure(jdbcHelper);
    
    159
    +                    log.warn("Database version table not found, but database version was guessed : {}", v);
    
    160
    +                } catch (SQLException ignored) {
    
    161
    +                }
    
    160 162
                 } else {
    
    161 163
                     log.info(String.format("Detected database version: %s", v));
    
    162 164
                 }
    
    ... ... @@ -173,6 +175,25 @@ public class TopiaMigrationServiceContext {
    173 175
             );
    
    174 176
         }
    
    175 177
     
    
    178
    +    private static Version detectVersionFromStructure(JdbcHelper jdbcHelper) throws SQLException {
    
    179
    +        if (!jdbcHelper.isSchemaExist("ps_logbook")) {
    
    180
    +            // We are before v9.0
    
    181
    +            return Version.valueOf("7.6");
    
    182
    +        }
    
    183
    +        // try to detect v9.0
    
    184
    +        if (jdbcHelper.isTableExist("ps_logbook", "wellPlan")) {
    
    185
    +            // We are on a version 9.0.x
    
    186
    +            return Version.valueOf("9.0");
    
    187
    +        }
    
    188
    +        // try to detect v9.1
    
    189
    +        if (jdbcHelper.isTableExist("ps_logbook", "well")) {
    
    190
    +            // We are on a version 9.1.x
    
    191
    +            return Version.valueOf("9.1");
    
    192
    +        }
    
    193
    +        // We could not find the database version
    
    194
    +        throw new TopiaMigrationServiceException("Could not detected database version, no migration table found.");
    
    195
    +    }
    
    196
    +
    
    176 197
         public static TopiaMigrationServiceContext createLegacy(TopiaMigrationServiceConfiguration configuration) {
    
    177 198
             TopiaApplicationContext<?> topiaApplicationContext = configuration.getApplicationContext();
    
    178 199
             boolean versionTableExist;
    
    ... ... @@ -303,9 +324,14 @@ public class TopiaMigrationServiceContext {
    303 324
                 log.info("Fill version table {} from legacy table.", dbVersion);
    
    304 325
                 saveVersion(sqlSupport, dbVersion, null);
    
    305 326
             } else if (dbNotVersioned) {
    
    306
    -            log.info("Database is empty, no migration needed, set ");
    
    307
    -            saveVersion(sqlSupport, modelVersion, new Date());
    
    308
    -            return false;
    
    327
    +            if (Version.VZERO.equals(dbVersion)) {
    
    328
    +                log.info("Database is empty, no migration needed, set current version {}", modelVersion);
    
    329
    +                saveVersion(sqlSupport, modelVersion, new Date());
    
    330
    +                return false;
    
    331
    +            } else {
    
    332
    +                log.info("Fill version table {} from guessed version (even if no legacy nor default version table detected).", dbVersion);
    
    333
    +                saveVersion(sqlSupport, dbVersion, null);
    
    334
    +            }
    
    309 335
             }
    
    310 336
             // In all other cases, we can try to perform migration
    
    311 337
             return true;