Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion key_share_node/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ services:
PGTZ: "UTC"
volumes:
- ${PG_DATA_DIR}:/var/lib/postgresql/data
- ../../key_share_node/pg_interface/src/bin/migrate/migrate.sql:/docker-entrypoint-initdb.d/migrate.sql

key_share_node:
build:
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,85 +2,20 @@ import type { Knex } from "knex";

export async function up(knex: Knex): Promise<void> {
//
// public.key_shares
// public.2_users
//
const keySharesExists = await knex.schema
const usersExists = await knex.schema
.withSchema("public")
.hasTable("key_shares");
if (!keySharesExists) {
await knex.schema
.withSchema("public")
.createTable("key_shares", (table) => {
table
.uuid("share_id")
.notNullable()
.defaultTo(knex.raw("gen_random_uuid()"))
.primary({ constraintName: "key_shares_pkey" });
table.uuid("wallet_id").notNullable();
table.binary("enc_share").notNullable();
table.string("status").notNullable();
table
.timestamp("reshared_at", { useTz: true })
.notNullable()
.defaultTo(knex.fn.now());
table
.timestamp("created_at", { useTz: true })
.notNullable()
.defaultTo(knex.fn.now());
table
.timestamp("updated_at", { useTz: true })
.notNullable()
.defaultTo(knex.fn.now());
table.jsonb("aux");

table.unique(["wallet_id"], {
indexName: "key_shares_unique",
});
});
}

//
// public.pg_dumps
//
const pgDumpsExists = await knex.schema
.withSchema("public")
.hasTable("pg_dumps");
if (!pgDumpsExists) {
await knex.schema.withSchema("public").createTable("pg_dumps", (table) => {
table
.uuid("dump_id")
.notNullable()
.defaultTo(knex.raw("gen_random_uuid()"))
.primary({ constraintName: "pg_dumps_pkey" });
table.string("status", 16).notNullable();
table.string("dump_path", 255);
table.jsonb("meta");
table
.timestamp("created_at", { useTz: true })
.notNullable()
.defaultTo(knex.fn.now());
table
.timestamp("updated_at", { useTz: true })
.notNullable()
.defaultTo(knex.fn.now());
});
}

//
// public.users
//
const usersExists = await knex.schema.withSchema("public").hasTable("users");
.hasTable("2_users");
if (!usersExists) {
await knex.schema.withSchema("public").createTable("users", (table) => {
await knex.schema.withSchema("public").createTable("2_users", (table) => {
table
.uuid("user_id")
.notNullable()
.defaultTo(knex.raw("gen_random_uuid()"))
.primary({ constraintName: "users_pkey" });
table
.string("email", 255)
.notNullable()
.unique({ indexName: "users_email_key" });
.primary({ constraintName: "2_users_pkey" });
table.string("auth_type", 64).notNullable();
table.string("email", 255).notNullable();
table.string("status", 16).notNullable().defaultTo("active");
table
.timestamp("created_at", { useTz: true })
Expand All @@ -91,28 +26,32 @@ export async function up(knex: Knex): Promise<void> {
.notNullable()
.defaultTo(knex.fn.now());
table.jsonb("aux");

table.unique(["auth_type", "email"], {
indexName: "2_users_auth_type_email_key",
});
});
}

//
// public.wallets
// public.2_wallets
//
const walletsExists = await knex.schema
.withSchema("public")
.hasTable("wallets");
.hasTable("2_wallets");
if (!walletsExists) {
await knex.schema.withSchema("public").createTable("wallets", (table) => {
await knex.schema.withSchema("public").createTable("2_wallets", (table) => {
table
.uuid("wallet_id")
.notNullable()
.defaultTo(knex.raw("gen_random_uuid()"))
.primary({ constraintName: "wallets_pkey" });
.primary({ constraintName: "2_wallets_pkey" });
table.uuid("user_id").notNullable();
table.string("curve_type", 16).notNullable();
table
.binary("public_key")
.notNullable()
.unique({ indexName: "wallets_public_key_key" });
.unique({ indexName: "2_wallets_public_key_key" });
table
.timestamp("created_at", { useTz: true })
.notNullable()
Expand All @@ -126,24 +65,62 @@ export async function up(knex: Knex): Promise<void> {
}

//
// public.server_keypairs
// public.2_key_shares
//
const keySharesExists = await knex.schema
.withSchema("public")
.hasTable("2_key_shares");
if (!keySharesExists) {
await knex.schema
.withSchema("public")
.createTable("2_key_shares", (table) => {
table
.uuid("share_id")
.notNullable()
.defaultTo(knex.raw("gen_random_uuid()"))
.primary({ constraintName: "2_key_shares_pkey" });
table.uuid("wallet_id").notNullable();
table.binary("enc_share").notNullable();
table.string("status").notNullable();
table
.timestamp("reshared_at", { useTz: true })
.notNullable()
.defaultTo(knex.fn.now());
table
.timestamp("created_at", { useTz: true })
.notNullable()
.defaultTo(knex.fn.now());
table
.timestamp("updated_at", { useTz: true })
.notNullable()
.defaultTo(knex.fn.now());
table.jsonb("aux");

table.unique(["wallet_id"], {
indexName: "2_key_shares_unique",
});
});
}

//
// public.2_server_keypairs
//
const serverKeypairsExists = await knex.schema
.withSchema("public")
.hasTable("server_keypairs");
.hasTable("2_server_keypairs");
if (!serverKeypairsExists) {
await knex.schema
.withSchema("public")
.createTable("server_keypairs", (table) => {
.createTable("2_server_keypairs", (table) => {
table
.uuid("keypair_id")
.notNullable()
.defaultTo(knex.raw("gen_random_uuid()"))
.primary({ constraintName: "server_keypairs_pkey" });
.primary({ constraintName: "2_server_keypairs_pkey" });
table
.specificType("version", "integer generated always as identity")
.notNullable()
.unique({ indexName: "server_keypairs_version_key" });
.unique({ indexName: "2_server_keypairs_version_key" });
table.binary("public_key").notNullable();
table.text("enc_private_key").notNullable();
table.boolean("is_active").notNullable().defaultTo(true);
Expand All @@ -159,17 +136,46 @@ export async function up(knex: Knex): Promise<void> {
});
}

//
// public.2_pg_dumps
//
const pgDumpsExists = await knex.schema
.withSchema("public")
.hasTable("2_pg_dumps");
if (!pgDumpsExists) {
await knex.schema
.withSchema("public")
.createTable("2_pg_dumps", (table) => {
table
.uuid("dump_id")
.notNullable()
.defaultTo(knex.raw("gen_random_uuid()"))
.primary({ constraintName: "2_pg_dumps_pkey" });
table.string("status", 16).notNullable();
table.string("dump_path", 255);
table.jsonb("meta");
table
.timestamp("created_at", { useTz: true })
.notNullable()
.defaultTo(knex.fn.now());
table
.timestamp("updated_at", { useTz: true })
.notNullable()
.defaultTo(knex.fn.now());
});
}

await knex.raw(`
Copy link
Collaborator

@eldenpark eldenpark Dec 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be safer to define this using table.index? (curious your thoughts)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be safer to define this using table.index? (curious)

table.index() doesn’t support WHERE clauses, so it can’t be used to define partial indexes

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://knexjs.org/guide/schema-builder.html#index

Looks like we could, see "predicate". But raw SQL is also okay. No action required as of now!

CREATE INDEX IF NOT EXISTS idx_server_keypairs_is_active
ON public.server_keypairs (is_active)
CREATE INDEX IF NOT EXISTS idx_2_server_keypairs_is_active
ON public."2_server_keypairs" (is_active)
WHERE is_active = true
`);
}

export async function down(knex: Knex): Promise<void> {
await knex.schema.withSchema("public").dropTableIfExists("server_keypairs");
await knex.schema.withSchema("public").dropTableIfExists("wallets");
await knex.schema.withSchema("public").dropTableIfExists("users");
await knex.schema.withSchema("public").dropTableIfExists("pg_dumps");
await knex.schema.withSchema("public").dropTableIfExists("key_shares");
await knex.schema.withSchema("public").dropTableIfExists("2_users");
await knex.schema.withSchema("public").dropTableIfExists("2_wallets");
await knex.schema.withSchema("public").dropTableIfExists("2_key_shares");
await knex.schema.withSchema("public").dropTableIfExists("2_server_keypairs");
await knex.schema.withSchema("public").dropTableIfExists("2_pg_dumps");
}
Loading