Skip to content

Commit

Permalink
chore(satp-hermes): improve DB management
Browse files Browse the repository at this point in the history
Signed-off-by: Rafael Belchior <[email protected]>
  • Loading branch information
RafaelAPB authored and Yogesh01000100 committed Aug 23, 2024
1 parent e027214 commit babc569
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 33 deletions.
12 changes: 8 additions & 4 deletions packages/cactus-plugin-satp-hermes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -179,4 +183,4 @@
"runOnChangeOnly": true
}
}
}
}
5 changes: 4 additions & 1 deletion packages/cactus-plugin-satp-hermes/src/knex/knexfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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");
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Knex } from "knex";

export async function up(knex: Knex): Promise<void> {
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<void> {
return knex.schema.dropTable("remote-logs");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 20240821000000_seed_dev_logs.ts

import { Knex } from "knex";

export async function seed(knex: Knex): Promise<void> {
// 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" }),
});
}

0 comments on commit babc569

Please sign in to comment.