From c768aa3441d3e36e2746b7831d127d46cfa988f2 Mon Sep 17 00:00:00 2001 From: Rafael Belchior Date: Wed, 21 Aug 2024 11:35:42 +0100 Subject: [PATCH] chore(satp-hermes): improve DB management Signed-off-by: Rafael Belchior --- .../cactus-plugin-satp-hermes/package.json | 12 ++++--- .../src/knex/knexfile.ts | 5 ++- .../20220331132128_create_logs_table.js | 15 -------- .../20220331132128_create_logs_table.ts | 16 +++++++++ ...20240130234303_create_remote_logs_table.js | 13 ------- ...20240130234303_create_remote_logs_table.ts | 14 ++++++++ .../seeds/1724235145_create_dummy_entries.ts | 35 +++++++++++++++++++ 7 files changed, 77 insertions(+), 33 deletions(-) delete mode 100644 packages/cactus-plugin-satp-hermes/src/knex/migrations/20220331132128_create_logs_table.js create mode 100644 packages/cactus-plugin-satp-hermes/src/knex/migrations/20220331132128_create_logs_table.ts delete mode 100644 packages/cactus-plugin-satp-hermes/src/knex/migrations/20240130234303_create_remote_logs_table.js create mode 100644 packages/cactus-plugin-satp-hermes/src/knex/migrations/20240130234303_create_remote_logs_table.ts create mode 100644 packages/cactus-plugin-satp-hermes/src/knex/seeds/1724235145_create_dummy_entries.ts diff --git a/packages/cactus-plugin-satp-hermes/package.json b/packages/cactus-plugin-satp-hermes/package.json index f1a6130b2d1..7857a76b1b3 100644 --- a/packages/cactus-plugin-satp-hermes/package.json +++ b/packages/cactus-plugin-satp-hermes/package.json @@ -78,11 +78,15 @@ "watch": "tsc --build --watch", "forge": "forge build ./src/solidity/*.sol --out ./src/solidity/generated", "forge:test": "forge build ./src/test/solidity/contracts/*.sol --out ./src/test/solidity/generated", + "db:setup": "bash -c 'npm run db:destroy || true && run-s db:start db:migrate db:seed'", + "db:destroy": "docker-compose down -v && npm run db:cleanup", "db:start": "docker-compose up -d", "db:stop": "docker-compose down", - "db:reset": "docker-compose down -v", - "migrate": "knex migrate:latest --env production --knexfile src/knex/knexfile.js", - "seed": "knex seed:run --env development --knexfile src/knex/knexfile.js" + "db:reset": "run-s db:destroy db:start db:migrate db:seed", + "db:migrate": "knex migrate:latest --knexfile src/knex/knexfile.js", + "db:migrate:production": "knex migrate:latest --env production --knexfile src/knex/knexfile.ts", + "db:seed": "knex seed:run --knexfile src/knex/knexfile.ts", + "db:cleanup": "find src/knex/data -name '.dev-*.sqlite3' -delete" }, "jest": { "moduleNameMapper": { @@ -179,4 +183,4 @@ "runOnChangeOnly": true } } -} +} \ No newline at end of file diff --git a/packages/cactus-plugin-satp-hermes/src/knex/knexfile.ts b/packages/cactus-plugin-satp-hermes/src/knex/knexfile.ts index 0598fa7f29d..3c0722fd4ca 100644 --- a/packages/cactus-plugin-satp-hermes/src/knex/knexfile.ts +++ b/packages/cactus-plugin-satp-hermes/src/knex/knexfile.ts @@ -8,11 +8,14 @@ module.exports = { development: { client: "sqlite3", connection: { - filename: path.resolve(__dirname, ".dev-" + uuidv4() + ".sqlite3"), + filename: path.join(__dirname, "data", "/.dev-" + uuidv4() + ".sqlite3"), }, migrations: { directory: path.resolve(__dirname, "migrations"), }, + seeds: { + directory: path.resolve(__dirname, "seeds"), + }, useNullAsDefault: true, }, production: { diff --git a/packages/cactus-plugin-satp-hermes/src/knex/migrations/20220331132128_create_logs_table.js b/packages/cactus-plugin-satp-hermes/src/knex/migrations/20220331132128_create_logs_table.js deleted file mode 100644 index 94c6d8712fe..00000000000 --- a/packages/cactus-plugin-satp-hermes/src/knex/migrations/20220331132128_create_logs_table.js +++ /dev/null @@ -1,15 +0,0 @@ -exports.up = async (knex) => { - return await knex.schema.createTable("logs", function (table) { - table.string("sessionID").notNullable(); - table.string("type").notNullable(); - table.string("key").notNullable(); - table.string("operation").notNullable(); - table.string("timestamp").notNullable(); - table.string("data").notNullable(); - table.primary("key"); - }); -}; - -exports.down = async (knex) => { - return await knex.schema.dropTable("logs"); -}; diff --git a/packages/cactus-plugin-satp-hermes/src/knex/migrations/20220331132128_create_logs_table.ts b/packages/cactus-plugin-satp-hermes/src/knex/migrations/20220331132128_create_logs_table.ts new file mode 100644 index 00000000000..cbccd817200 --- /dev/null +++ b/packages/cactus-plugin-satp-hermes/src/knex/migrations/20220331132128_create_logs_table.ts @@ -0,0 +1,16 @@ +import { Knex } from "knex"; + +export function up(knex: Knex): Knex.SchemaBuilder { + return knex.schema.createTable("logs", (table) => { + table.string("sessionID").notNullable(); + table.string("type").notNullable(); + table.string("key").notNullable().primary(); + table.string("operation").notNullable(); + table.string("timestamp").notNullable(); + table.string("data").notNullable(); + }); +} + +export function down(knex: Knex): Knex.SchemaBuilder { + return knex.schema.dropTable("logs"); +} diff --git a/packages/cactus-plugin-satp-hermes/src/knex/migrations/20240130234303_create_remote_logs_table.js b/packages/cactus-plugin-satp-hermes/src/knex/migrations/20240130234303_create_remote_logs_table.js deleted file mode 100644 index 50625d7833b..00000000000 --- a/packages/cactus-plugin-satp-hermes/src/knex/migrations/20240130234303_create_remote_logs_table.js +++ /dev/null @@ -1,13 +0,0 @@ -exports.up = async (knex) => { - return await knex.schema.createTable("remote-logs", function (table) { - table.string("key").notNullable(); - table.string("hash").notNullable(); - table.string("signature").notNullable(); - table.string("signerPubKey").notNullable(); - table.primary("key"); - }); -}; - -exports.down = async (knex) => { - return await knex.schema.dropTable("remote-logs"); -}; diff --git a/packages/cactus-plugin-satp-hermes/src/knex/migrations/20240130234303_create_remote_logs_table.ts b/packages/cactus-plugin-satp-hermes/src/knex/migrations/20240130234303_create_remote_logs_table.ts new file mode 100644 index 00000000000..7b51ef32c14 --- /dev/null +++ b/packages/cactus-plugin-satp-hermes/src/knex/migrations/20240130234303_create_remote_logs_table.ts @@ -0,0 +1,14 @@ +import { Knex } from "knex"; + +export async function up(knex: Knex): Promise { + return knex.schema.createTable("remote-logs", (table) => { + table.string("hash").notNullable(); + table.string("signature").notNullable(); + table.string("signerPubKey").notNullable(); + table.string("key").notNullable().primary(); + }); +} + +export async function down(knex: Knex): Promise { + return knex.schema.dropTable("remote-logs"); +} diff --git a/packages/cactus-plugin-satp-hermes/src/knex/seeds/1724235145_create_dummy_entries.ts b/packages/cactus-plugin-satp-hermes/src/knex/seeds/1724235145_create_dummy_entries.ts new file mode 100644 index 00000000000..6958544a308 --- /dev/null +++ b/packages/cactus-plugin-satp-hermes/src/knex/seeds/1724235145_create_dummy_entries.ts @@ -0,0 +1,35 @@ +// 20240821000000_seed_dev_logs.ts + +import { Knex } from "knex"; + +export async function seed(knex: Knex): Promise { + // Check if we're in the development environment + if (process.env.NODE_ENV !== "development") { + console.log("Skipping seed: Not in development environment"); + return; + } + + // Function to clear table if it exists + async function clearTableIfExists(tableName: string) { + if (await knex.schema.hasTable(tableName)) { + await knex(tableName).del(); + console.log(`Cleared existing entries from ${tableName}`); + } else { + console.log(`Table ${tableName} does not exist, skipping clear`); + } + } + + // Clear existing entries if tables exist + await clearTableIfExists("logs"); + await clearTableIfExists("remote-logs"); + + // Insert a single deterministic log entry + await knex("logs").insert({ + sessionID: "test-session-001", + type: "info", + key: "test-log-001", + operation: "create", + timestamp: "2024-08-21T12:00:00Z", + data: JSON.stringify({ message: "This is a test log entry" }), + }); +}