Skip to content

Commit

Permalink
create unique index note_edit + note_id
Browse files Browse the repository at this point in the history
  • Loading branch information
gdbroman committed Aug 3, 2023
1 parent 0030b0c commit 4024714
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
26 changes: 19 additions & 7 deletions app/src/os/services/ship/notes/notes.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ create table if not exists notes (
create table if not exists notes_edits (
id TEXT,
note_edit TEXT PRIMARY KEY,
note_edit TEXT NOT NULL,
note_id TEXT NOT NULL
);
create index if not exists notes_edits_note_edit_note_id on notes_edits (note_edit, note_id);
`;

export const notesWipeSql = `
Expand Down Expand Up @@ -126,10 +128,10 @@ export class NotesDB extends AbstractDataAccess<any> {

// If the note edit already exists, update the id.
const existingNoteEdit = this.db
.prepare(`SELECT * FROM notes_edits WHERE note_edit = ?`)
.get(note_edit);
.prepare(`SELECT * FROM notes_edits WHERE note_edit = ? AND note_id = ?`)
.get(note_edit, note_id);
if (existingNoteEdit) {
return this.updateNoteEditId({ note_edit, id });
return this.updateNoteEditId({ note_edit, note_id, id });
}

const info = this.db
Expand All @@ -148,6 +150,14 @@ export class NotesDB extends AbstractDataAccess<any> {
}) => {
if (!this.db) return -1;

// If the note edit already exists, do nothing.
const existingNoteEdit = this.db
.prepare(`SELECT * FROM notes_edits WHERE note_edit = ? AND note_id = ?`)
.get(note_edit, note_id);
if (existingNoteEdit) {
return existingNoteEdit.id;
}

const info = this.db
.prepare(`INSERT INTO notes_edits (note_edit, note_id) VALUES (?, ?)`)
.run(note_edit, note_id);
Expand All @@ -166,11 +176,13 @@ export class NotesDB extends AbstractDataAccess<any> {
return noteId;
};

updateNoteEditId: NotesDB_UpdateNoteEditId = ({ note_edit }) => {
updateNoteEditId: NotesDB_UpdateNoteEditId = ({ note_edit, note_id, id }) => {
if (!this.db) return -1;
const info = this.db
.prepare(`UPDATE notes_edits SET id = ? WHERE note_edit = ?`)
.run(note_edit, Date.now());
.prepare(
`UPDATE notes_edits SET id = ? WHERE note_edit = ? AND note_id = ?`
)
.run(id, note_edit, note_id);
const noteId = info.lastInsertRowid;

return noteId;
Expand Down
1 change: 1 addition & 0 deletions app/src/os/services/ship/notes/notes.db.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export type NotesDB_UpdateNoteTitle = (payload: {

export type NotesDB_UpdateNoteEditId = (payload: {
note_edit: string;
note_id: string;
id: string;
}) => string;

Expand Down

0 comments on commit 4024714

Please sign in to comment.