Skip to content

Commit

Permalink
notes triggers bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
alicilin committed Jul 4, 2022
1 parent 7c198aa commit feaeac5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 25 deletions.
4 changes: 2 additions & 2 deletions public/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ protocol.registerSchemesAsPrivileged(
);

async function setup() {
let D_OK = await fs.pathExists(path.join(os.homedir(), 'Not'));
let D_OK = await fs.pathExists(path.join(os.homedir(), 'Note'));
if (D_OK === false) {
await fs.ensureDir(path.join(os.homedir(), 'Not'));
await fs.ensureDir(path.join(os.homedir(), 'Note'));
}

let cparams = {
Expand Down
6 changes: 3 additions & 3 deletions public/migrations/20220702133706_notes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
*/
exports.up = function (knex) {
return knex.schema.createTable('notes', table => {
table.increments('id').primary();
table.string('title').notNullable();
table.string('notebook').notNullable().index();
table.increments('id').primary().index();
table.string('title', 100).notNullable();
table.string('notebook', 100).notNullable().index();
table.text('contents').notNullable();
table.timestamps(true, true, true);

Expand Down
60 changes: 40 additions & 20 deletions public/migrations/20220702140834_notes_triggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,48 @@
* @returns { Promise<void> }
*/
exports.up = function (knex) {
return knex.raw(`
CREATE TRIGGER notes_insert AFTER INSERT ON notes
BEGIN
INSERT INTO notes_fts (rowid, title, notebook, contents)
VALUES (new.id, new.title, new.notebook, new.contents);
END;
let raws = [];
//-------------------------------------
raws.push(
knex.raw(
`
CREATE TRIGGER notes_insert AFTER INSERT ON notes
BEGIN
INSERT INTO notes_fts (rowid, title, notebook, contents)
VALUES (new.id, new.title, new.notebook, new.contents);
END;
`
)
);

CREATE TRIGGER notes_delete AFTER DELETE ON notes
BEGIN
INSERT INTO notes_fts (notes_fts, rowid, title, notebook, contents)
VALUES ('delete', old.id, old.title, old.notebook, old.contents);
END;
raws.push(
knex.raw(
`
CREATE TRIGGER notes_delete AFTER DELETE ON notes
BEGIN
INSERT INTO notes_fts (notes_fts, rowid, title, notebook, contents)
VALUES ('delete', old.id, old.title, old.notebook, old.contents);
END;
`
)
);

CREATE TRIGGER notes_update AFTER UPDATE ON notes
BEGIN
INSERT INTO notes_fts (notes_fts, rowid, title, notebook, contents)
VALUES ('delete', old.id, old.title, old.notebook, old.contents);
INSERT INTO notes_fts (rowid, title, notebook, contents)
VALUES (new.id, new.title, new.notebook, new.contents);
END;
`);
raws.push(
knex.raw(
`
CREATE TRIGGER notes_update AFTER UPDATE ON notes
BEGIN
INSERT INTO notes_fts (notes_fts, rowid, title, notebook, contents)
VALUES ('delete', old.id, old.title, old.notebook, old.contents);
INSERT INTO notes_fts (rowid, title, notebook, contents)
VALUES (new.id, new.title, new.notebook, new.contents);
END;
`
)
);

return Promise.all(raws);
};

/**
Expand Down

0 comments on commit feaeac5

Please sign in to comment.