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,19 +150,40 @@ 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
    +                // This case should never happen :( but v9.0.x is sometime without versioning table
    
    156
    +                // We can still try to find db version from database structure
    
    157
    +                // See https://gitlab.com/ultreiaio/ird-observe/-/issues/2712
    
    158
    +                try {
    
    159
    +                    // try to detect v9.0
    
    160
    +                    if (jdbcHelper.isTableExist("ps_logbook", "wellPlan")) {
    
    161
    +                        // We are on a version 9.0.x
    
    162
    +                        v = Version.valueOf("9.0");
    
    163
    +                    }
    
    164
    +                } catch (SQLException ignored) {
    
    165
    +                }
    
    166
    +                if (v == null) {
    
    167
    +                    // try to detect v9.1
    
    168
    +                    try {
    
    169
    +                        if (jdbcHelper.isTableExist("ps_logbook", "well")) {
    
    170
    +                            // We are on a version 9.0.x
    
    171
    +                            v = Version.valueOf("9.1");
    
    172
    +                        }
    
    173
    +                    } catch (SQLException ignored) {
    
    174
    +                    }
    
    175
    +                }
    
    176
    +                if (v != null) {
    
    177
    +                    log.warn("Database version table not found, but database version was guessed : {}", v);
    
    178
    +                }
    
    160 179
                 } else {
    
    161 180
                     log.info(String.format("Detected database version: %s", v));
    
    162 181
                 }
    
    163 182
                 dbVersion = v;
    
    164 183
             }
    
    184
    +        if (dbVersion == null) {
    
    185
    +            throw new TopiaMigrationServiceException("Could not detected database version, no migration table found.");
    
    186
    +        }
    
    165 187
             return new TopiaMigrationServiceContext(configuration,
    
    166 188
                                                     sqlHelper,
    
    167 189
                                                     legacyVersionTableExist,
    
    ... ... @@ -303,9 +325,14 @@ public class TopiaMigrationServiceContext {
    303 325
                 log.info("Fill version table {} from legacy table.", dbVersion);
    
    304 326
                 saveVersion(sqlSupport, dbVersion, null);
    
    305 327
             } else if (dbNotVersioned) {
    
    306
    -            log.info("Database is empty, no migration needed, set ");
    
    307
    -            saveVersion(sqlSupport, modelVersion, new Date());
    
    308
    -            return false;
    
    328
    +            if (Version.VZERO.equals(dbVersion)) {
    
    329
    +                log.info("Database is empty, no migration needed, set current version {}", modelVersion);
    
    330
    +                saveVersion(sqlSupport, modelVersion, new Date());
    
    331
    +                return false;
    
    332
    +            } else {
    
    333
    +                log.info("Fill version table {} from guessed version (even if no legacy nor default version table detected).", dbVersion);
    
    334
    +                saveVersion(sqlSupport, dbVersion, null);
    
    335
    +            }
    
    309 336
             }
    
    310 337
             // In all other cases, we can try to perform migration
    
    311 338
             return true;