Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/artifact-store-tables.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@truefoundry/trueforge": patch
---

Add `artifact` and `artifact_version` tables.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { sql, type Kysely } from 'kysely';

/** Artifact metadata; file bytes live in object storage. No FK to session: artifacts outlive it. */
export async function up(db: Kysely<unknown>): Promise<void> {
await sql`SET LOCAL lock_timeout = '5s'`.execute(db);

await db.schema
.createTable('artifact')
.addColumn('id', 'text', col => col.notNull())
.addColumn('tenant_id', 'text', col => col.notNull())
.addColumn('slug', 'text', col => col.notNull())
.addColumn('title', 'text', col => col.notNull())
.addColumn('latest_version', 'integer', col => col.notNull())
Comment thread
rutvij-menavlikar marked this conversation as resolved.
Resolved
.addColumn('created_by_subject', 'jsonb', col => col.notNull())
.addColumn('created_by_session_id', 'text', col => col.notNull())
.addColumn('created_at', 'timestamptz', col => col.notNull())
.addColumn('updated_at', 'timestamptz', col => col.notNull())
.addPrimaryKeyConstraint('artifact_pkey', ['id'])
.addUniqueConstraint('artifact_tenant_slug_uq', ['tenant_id', 'slug'])
.execute();

await sql`
CREATE INDEX artifact_list_idx
ON artifact (tenant_id, (created_by_subject->>'subject_id'), updated_at DESC, id)
`.execute(db);
await sql`CREATE INDEX artifact_session_idx ON artifact (tenant_id, created_by_session_id)`.execute(db);
Comment thread
rutvij-menavlikar marked this conversation as resolved.
Resolved
await db.schema
.createTable('artifact_version')
.addColumn('artifact_id', 'text', col => col.notNull())
.addColumn('version', 'integer', col => col.notNull())
.addColumn('storage_prefix', 'text', col => col.notNull())
.addColumn('total_bytes', 'bigint', col => col.notNull())
.addColumn('created_at', 'timestamptz', col => col.notNull())
.addPrimaryKeyConstraint('artifact_version_pkey', ['artifact_id', 'version'])
.addForeignKeyConstraint('artifact_version_artifact_id_fkey', ['artifact_id'], 'artifact', ['id'], fk =>
fk.onDelete('cascade'),
)
.execute();
}

export async function down(db: Kysely<unknown>): Promise<void> {
await sql`SET LOCAL lock_timeout = '5s'`.execute(db);
await db.schema.dropTable('artifact_version').ifExists().execute();
await db.schema.dropTable('artifact').ifExists().execute();
}
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { sql, type Kysely } from 'kysely';

/** Artifact metadata (mirrors Postgres). STRICT tables; jsonb as BLOB; timestamps as ISO TEXT. */
export async function up(db: Kysely<unknown>): Promise<void> {
await db.transaction().execute(async trx => {
await sql`
CREATE TABLE artifact (
id TEXT NOT NULL,
tenant_id TEXT NOT NULL,
slug TEXT NOT NULL,
title TEXT NOT NULL,
latest_version INTEGER NOT NULL,
created_by_subject BLOB NOT NULL,
created_by_session_id TEXT NOT NULL,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL,
PRIMARY KEY (id),
CONSTRAINT artifact_tenant_slug_uq UNIQUE (tenant_id, slug)
) STRICT
`.execute(trx);

await sql`
CREATE INDEX artifact_list_idx
ON artifact (tenant_id, json_extract(created_by_subject, '$.subject_id'), updated_at DESC, id)
`.execute(trx);
await sql`CREATE INDEX artifact_session_idx ON artifact (tenant_id, created_by_session_id)`.execute(trx);
await sql`
CREATE TABLE artifact_version (
artifact_id TEXT NOT NULL,
version INTEGER NOT NULL,
storage_prefix TEXT NOT NULL,
total_bytes INTEGER NOT NULL,
created_at TEXT NOT NULL,
PRIMARY KEY (artifact_id, version),
FOREIGN KEY (artifact_id) REFERENCES artifact (id) ON DELETE CASCADE
) STRICT
`.execute(trx);
});
}

export async function down(db: Kysely<unknown>): Promise<void> {
await sql`PRAGMA foreign_keys = OFF`.execute(db);
try {
await db.transaction().execute(async trx => {
await sql`DROP TABLE IF EXISTS artifact_version`.execute(trx);
await sql`DROP TABLE IF EXISTS artifact`.execute(trx);
});
} finally {
await sql`PRAGMA foreign_keys = ON`.execute(db);
}
}