Skip to content

Commit

Permalink
Fixed auto_increments not being properly maintained
Browse files Browse the repository at this point in the history
H2 v2 deviated from MySQL so there's not auto-update of the internal counters.

Refs #90
  • Loading branch information
The4thLaw committed Feb 22, 2024
1 parent 8c92e70 commit 3c8e5fe
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
21 changes: 15 additions & 6 deletions source/demyo-dao/src/main/java/org/demyo/dao/IRawSQLDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,51 @@
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 {
/**
* Deletes content from all tables in Demyo.
*/
void pruneAllTables();

/**
* Fix the values of the identity columns, for example after an import or migration.
* <p>
* 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.
* </p>
*/
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.
*/
void insert(String tableName, Map<String, ? extends Object> values);

/**
* Counts the number of entries in a table.
*
*
* @param tableName The table to retrieve the number of entries from.
* @return The number of entries
*/
long count(String tableName);

/**
* Gets the raw data from a table.
*
*
* @param tableName The table to query.
* @return The data contained inside the table.
*/
List<Map<String, Object>> getRawRecords(String tableName);

/**
* Gets the version of the Demyo schema.
*
*
* @return The schema version.
*/
int getSchemaVersion();
Expand Down
15 changes: 13 additions & 2 deletions source/demyo-dao/src/main/java/org/demyo/dao/impl/RawSQLDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,6 +23,7 @@
*/
@Repository
public class RawSQLDao implements IRawSQLDao {
private static final Logger LOGGER = LoggerFactory.getLogger(RawSQLDao.class);

@PersistenceContext
private EntityManager entityManager;
Expand All @@ -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",
Expand All @@ -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<String, ? extends Object> values) {
StringBuilder insertSb = new StringBuilder("INSERT INTO ").append(tableName).append('(');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 3c8e5fe

Please sign in to comment.