diff --git a/source/demyo-dao/src/main/java/org/demyo/dao/IRawSQLDao.java b/source/demyo-dao/src/main/java/org/demyo/dao/IRawSQLDao.java index 9e258cb6..c9fbc8f2 100644 --- a/source/demyo-dao/src/main/java/org/demyo/dao/IRawSQLDao.java +++ b/source/demyo-dao/src/main/java/org/demyo/dao/IRawSQLDao.java @@ -4,8 +4,7 @@ import java.util.Map; /** - * Provides access to raw SQL operations, for cases when flexibility and reflection primes over clean Hibernate - * beans. + * Provides access to raw SQL operations, for cases when flexibility and reflection primes over clean Hibernate beans. */ public interface IRawSQLDao { /** @@ -13,9 +12,19 @@ public interface IRawSQLDao { */ void pruneAllTables(); + /** + * Fix the values of the identity columns, for example after an import or migration. + *

+ * Due to a change in H2 2.x, the behaviour of identity columns has changed (see + * https://github.com/h2database/h2database/issues/3454). As such, we need to manually fix the values of the + * identity columns when we assign values of our own. + *

+ */ + void fixAutoIncrements(); + /** * Inserts arbitrary data into a table. - * + * * @param tableName The table into which to insert the data. * @param values A map of column-to-value data to insert. */ @@ -23,7 +32,7 @@ public interface IRawSQLDao { /** * Counts the number of entries in a table. - * + * * @param tableName The table to retrieve the number of entries from. * @return The number of entries */ @@ -31,7 +40,7 @@ public interface IRawSQLDao { /** * Gets the raw data from a table. - * + * * @param tableName The table to query. * @return The data contained inside the table. */ @@ -39,7 +48,7 @@ public interface IRawSQLDao { /** * Gets the version of the Demyo schema. - * + * * @return The schema version. */ int getSchemaVersion(); diff --git a/source/demyo-dao/src/main/java/org/demyo/dao/impl/RawSQLDao.java b/source/demyo-dao/src/main/java/org/demyo/dao/impl/RawSQLDao.java index 8a773318..87ff3797 100644 --- a/source/demyo-dao/src/main/java/org/demyo/dao/impl/RawSQLDao.java +++ b/source/demyo-dao/src/main/java/org/demyo/dao/impl/RawSQLDao.java @@ -10,6 +10,8 @@ import jakarta.persistence.PersistenceContext; import jakarta.persistence.Query; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; @@ -21,6 +23,7 @@ */ @Repository public class RawSQLDao implements IRawSQLDao { + private static final Logger LOGGER = LoggerFactory.getLogger(RawSQLDao.class); @PersistenceContext private EntityManager entityManager; @@ -46,8 +49,7 @@ private void executeUpdate(String sql) { @Override public void pruneAllTables() { - // To get the list of tables: - // grep "CREATE TABLE" create-tables.sql | sed 's/CREATE TABLE /"/' | sed 's/ (/",/' | tac + LOGGER.debug("Pruning all tables"); for (String table : new String[] { "searches", "albums_borrowers", "borrowers", "derivatives_images", "derivatives_prices", "derivatives", "derivative_types", "sources", "albums_tags", "tags", "readers_favourite_series", "readers_favourite_albums", "readers_reading_list", "albums_colorists", @@ -58,6 +60,15 @@ public void pruneAllTables() { } } + @Override + public void fixAutoIncrements() { + LOGGER.debug("Fixing auto-increments"); + for (String table : new String[] { "albums", "authors", "bindings", "borrowers", "collections", "configuration", + "derivative_types", "derivatives", "images", "publishers", "readers", "searches", "series", "sources", "tags" }) { + executeUpdate("ALTER TABLE "+table+" ALTER COLUMN ID RESTART WITH (SELECT MAX(id) + 1 FROM "+table+")"); + } + } + @Override public void insert(String tableName, Map values) { StringBuilder insertSb = new StringBuilder("INSERT INTO ").append(tableName).append('('); diff --git a/source/demyo-service/src/main/java/org/demyo/service/importing/Demyo2Importer.java b/source/demyo-service/src/main/java/org/demyo/service/importing/Demyo2Importer.java index 1625fc8d..98ebb98d 100644 --- a/source/demyo-service/src/main/java/org/demyo/service/importing/Demyo2Importer.java +++ b/source/demyo-service/src/main/java/org/demyo/service/importing/Demyo2Importer.java @@ -130,6 +130,8 @@ public void importFile(String originalFilename, Path file) throws DemyoException stopWatch.split(); splitTime = stopWatch.getSplitTime() - splitTime; + rawSqlDao.fixAutoIncrements(); + // Move extracted images to the right directory if (isArchive) { restoreImages(archiveDirectory, "images");