working on transformation of legacy notes tables

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2025-08-12 23:41:56 +02:00
parent 09dd21eee8
commit 1df7a2bd3c
11 changed files with 163 additions and 48 deletions

View File

@@ -35,7 +35,7 @@ public class NoteModule extends BaseHandler implements NoteService {
}
@Override
public void deleteEntity(String module, long entityId) {
public void deleteEntity(String module, String entityId) {
notesDb.deleteEntity(module,entityId);
}
@@ -79,8 +79,7 @@ public class NoteModule extends BaseHandler implements NoteService {
notes = notesDb.list(user.get().id());
} else {
var head = path.pop();
long entityId = Long.parseLong(head);
notes = getNotes(module, entityId);
notes = getNotes(module, head);
}
var authors = notes.values().stream().map(Note::authorId).distinct().map(registry.userService()::loadUser).collect(Collectors.toMap(UmbrellaUser::id,UmbrellaUser::toMap));
return sendContent(ex, Map.of("notes",mapValues(notes),"authors",authors));
@@ -137,7 +136,7 @@ public class NoteModule extends BaseHandler implements NoteService {
}
}
public Map<Long,Note> getNotes(String module, long entityId) throws UmbrellaException{
public Map<Long,Note> getNotes(String module, String entityId) throws UmbrellaException{
return notesDb.list(module,entityId);
}

View File

@@ -7,7 +7,7 @@ import java.util.Map;
public interface NotesDb {
long delete(long noteId);
void deleteEntity(String module, long entityId);
void deleteEntity(String module, String entityId);
/**
* get all lists of a person
@@ -21,7 +21,7 @@ public interface NotesDb {
* @param entityId
* @return
*/
Map<Long, Note> list(String module, long entityId);
Map<Long, Note> list(String module, String entityId);
Note load(long noteId);

View File

@@ -11,6 +11,7 @@ import static java.text.MessageFormat.format;
import static java.time.ZoneOffset.UTC;
import de.srsoftware.tools.jdbc.Query;
import de.srsoftware.umbrella.core.BaseDb;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import de.srsoftware.umbrella.core.model.Note;
import java.sql.Connection;
@@ -18,58 +19,100 @@ import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
public class SqliteDb implements NotesDb {
public class SqliteDb extends BaseDb implements NotesDb {
private static final int INITIAL_DB_VERSION = 1;
private static final System.Logger LOG = System.getLogger("NotesDB");
private final Connection db;
public SqliteDb(Connection conn) {
this.db = conn;
init();
super(conn);
}
private int createTables() {
createNotesTables();
return createSettingsTable();
protected int createTables() {
int currentVersion = createSettingsTable();
switch (currentVersion){
case 0:
createNotesTable();
case 1:
addModuleColumn();
addEntityIdColumn();
calcReferences();
dropUriColumn();
}
return setCurrentVersion(2);
}
private int createSettingsTable() {
private void addEntityIdColumn() {
var createTable = """
CREATE TABLE IF NOT EXISTS {0} ( {1} VARCHAR(255) PRIMARY KEY, {2} VARCHAR(255) NOT NULL);
ALTER TABLE notes
ADD COLUMN entity_id VARCHAR(255);
""";
try {
var stmt = db.prepareStatement(format(createTable,TABLE_SETTINGS, KEY, VALUE));
var stmt = db.prepareStatement(createTable);
stmt.execute();
stmt.close();
} catch (SQLException e) {
LOG.log(ERROR,ERROR_FAILED_CREATE_TABLE,TABLE_SETTINGS,e);
throw new RuntimeException(e);
}
Integer version = null;
try {
var rs = select(VALUE).from(TABLE_SETTINGS).where(KEY, equal(DB_VERSION)).exec(db);
if (rs.next()) version = rs.getInt(VALUE);
rs.close();
if (version == null) {
version = INITIAL_DB_VERSION;
insertInto(TABLE_SETTINGS, KEY, VALUE).values(DB_VERSION,version).execute(db).close();
}
return version;
} catch (SQLException e) {
LOG.log(ERROR,ERROR_READ_TABLE,DB_VERSION,TABLE_SETTINGS,e);
LOG.log(ERROR,"Failed to add \"entity_id\" column to table {0}",TABLE_NOTES,e);
throw new RuntimeException(e);
}
}
private void createNotesTables() {
private void addModuleColumn() {
var createTable = """
ALTER TABLE notes
ADD COLUMN module VARCHAR(20);
""";
try {
var stmt = db.prepareStatement(createTable);
stmt.execute();
stmt.close();
} catch (SQLException e) {
LOG.log(ERROR,"Failed to add \"module\" column to table {0}",TABLE_NOTES,e);
throw new RuntimeException(e);
}
}
private void calcReferences(){
var createTable = """
UPDATE notes
SET module = SUBSTR(uri, 1, INSTR(uri, ':') - 1),
entity_id = SUBSTR(uri, INSTR(uri, ':') + 1);
""";
try {
var stmt = db.prepareStatement(createTable);
stmt.execute();
stmt.close();
} catch (SQLException e) {
LOG.log(ERROR,"Failed fill \"module\" and \"entity_id\" columns",e);
throw new RuntimeException(e);
}
}
private void createNotesTable() {
var createTable = """
CREATE TABLE IF NOT EXISTS notes (
id INTEGER PRIMARY KEY,
user_id INT NOT NULL,
uri VARCHAR(255) NOT NULL,
note TEXT NOT NULL,
timestamp INT)""";
try {
var stmt = db.prepareStatement(createTable);
stmt.execute();
stmt.close();
} catch (SQLException e) {
LOG.log(ERROR,ERROR_FAILED_CREATE_TABLE,TABLE_NOTES,e);
throw new RuntimeException(e);
}
}
private void createNewNotesTable() {
var createTable = """
CREATE TABLE IF NOT EXISTS "{0}" (
{1} INTEGER NOT NULL PRIMARY KEY,
{2} TEXT NOT NULL,
{3} VARCHAR(20) NOT NULL,
{4} INTEGER NOT NULL,
{4} VARCHAR(255) NOT NULL,
{5} INTEGER NOT NULL,
{6} DATETIME NOT NULL
)""";
@@ -96,7 +139,7 @@ CREATE TABLE IF NOT EXISTS "{0}" (
}
@Override
public void deleteEntity(String module, long entityId) {
public void deleteEntity(String module, String entityId) {
try {
Query.delete().from(TABLE_NOTES)
.where(MODULE,equal(module)).where(ENTITY_ID,equal(entityId))
@@ -106,6 +149,18 @@ CREATE TABLE IF NOT EXISTS "{0}" (
}
}
private void dropUriColumn(){
var createTable = "ALTER TABLE notes DROP COLUMN uri";
try {
var stmt = db.prepareStatement(createTable);
stmt.execute();
stmt.close();
} catch (SQLException e) {
LOG.log(ERROR,"Failed drop \"uri\" column",e);
throw new RuntimeException(e);
}
}
private void init(){
var version = createTables();
LOG.log(INFO,"Updated task db to version {0}",version);
@@ -128,7 +183,7 @@ CREATE TABLE IF NOT EXISTS "{0}" (
}
@Override
public Map<Long, Note> list(String module, long entityId) {
public Map<Long, Note> list(String module, String entityId) {
try {
var notes = new HashMap<Long, Note>();
var rs = select(ALL).from(TABLE_NOTES).where(MODULE,equal(module)).where(ENTITY_ID,equal(entityId)).exec(db);