Skip to content

Commit

Permalink
chore: db migration and cleanup (promptfoo#564)
Browse files Browse the repository at this point in the history
  • Loading branch information
typpo authored Mar 18, 2024
1 parent 2dedc49 commit 1afbb97
Show file tree
Hide file tree
Showing 9 changed files with 255 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ Here are some of the available scripts:
- `build:watch`: Continuously watch and transpile TypeScript files on changes
- `test`: Run test suite
- `test:watch`: Continuously run test suite on changes
- `db:generate`: Generate new db migrations (and create the db if it doesn't already exist)
- `db:generate`: Generate new db migrations (and create the db if it doesn't already exist). Note that after generating a new migration, you'll have to `npm i` to copy the migrations into `dist/`.
- `db:migrate`: Run existing db migrations (and create the db if it doesn't already exist)

# [» View full documentation «](https://promptfoo.dev/docs/intro)
Expand Down
3 changes: 1 addition & 2 deletions drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import 'dotenv/config';
import type { Config } from 'drizzle-kit';

export default {
schema: './src/database.ts',
out: './drizzle',
driver: 'better-sqlite',
} satisfies Config;
} satisfies Config;
3 changes: 3 additions & 0 deletions drizzle/0001_wide_calypso.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE datasets ADD `tests` text;--> statement-breakpoint
ALTER TABLE `datasets` DROP COLUMN `test_case_id`;--> statement-breakpoint
ALTER TABLE `prompts` DROP COLUMN `hash`;
237 changes: 237 additions & 0 deletions drizzle/meta/0001_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
{
"version": "5",
"dialect": "sqlite",
"id": "476c887f-edcb-4b29-b054-50a8383cc6c2",
"prevId": "8b53403f-5b6f-436a-862e-9fd17a52204e",
"tables": {
"datasets": {
"name": "datasets",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true,
"autoincrement": false
},
"tests": {
"name": "tests",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"created_at": {
"name": "created_at",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": "CURRENT_TIMESTAMP"
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"evals": {
"name": "evals",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true,
"autoincrement": false
},
"created_at": {
"name": "created_at",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": "CURRENT_TIMESTAMP"
},
"description": {
"name": "description",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"results": {
"name": "results",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"config": {
"name": "config",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"evals_to_datasets": {
"name": "evals_to_datasets",
"columns": {
"eval_id": {
"name": "eval_id",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"dataset_id": {
"name": "dataset_id",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {
"evals_to_datasets_eval_id_evals_id_fk": {
"name": "evals_to_datasets_eval_id_evals_id_fk",
"tableFrom": "evals_to_datasets",
"tableTo": "evals",
"columnsFrom": [
"eval_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
},
"evals_to_datasets_dataset_id_datasets_id_fk": {
"name": "evals_to_datasets_dataset_id_datasets_id_fk",
"tableFrom": "evals_to_datasets",
"tableTo": "datasets",
"columnsFrom": [
"dataset_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {
"evals_to_datasets_eval_id_dataset_id_pk": {
"columns": [
"dataset_id",
"eval_id"
],
"name": "evals_to_datasets_eval_id_dataset_id_pk"
}
},
"uniqueConstraints": {}
},
"evals_to_prompts": {
"name": "evals_to_prompts",
"columns": {
"eval_id": {
"name": "eval_id",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"prompt_id": {
"name": "prompt_id",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {
"evals_to_prompts_eval_id_evals_id_fk": {
"name": "evals_to_prompts_eval_id_evals_id_fk",
"tableFrom": "evals_to_prompts",
"tableTo": "evals",
"columnsFrom": [
"eval_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
},
"evals_to_prompts_prompt_id_prompts_id_fk": {
"name": "evals_to_prompts_prompt_id_prompts_id_fk",
"tableFrom": "evals_to_prompts",
"tableTo": "prompts",
"columnsFrom": [
"prompt_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {
"evals_to_prompts_eval_id_prompt_id_pk": {
"columns": [
"eval_id",
"prompt_id"
],
"name": "evals_to_prompts_eval_id_prompt_id_pk"
}
},
"uniqueConstraints": {}
},
"prompts": {
"name": "prompts",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true,
"autoincrement": false
},
"created_at": {
"name": "created_at",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": "CURRENT_TIMESTAMP"
},
"prompt": {
"name": "prompt",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
}
}
7 changes: 7 additions & 0 deletions drizzle/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
"when": 1710348564252,
"tag": "0000_lush_hellion",
"breakpoints": true
},
{
"idx": 1,
"version": "5",
"when": 1710706957023,
"tag": "0001_wide_calypso",
"breakpoints": true
}
]
}
3 changes: 1 addition & 2 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export const prompts = sqliteTable('prompts', {
id: text('id').primaryKey(),
createdAt: integer('created_at').notNull().default(sql`CURRENT_TIMESTAMP`),
prompt: text('prompt').notNull(),
hash: text('hash').notNull(),
});

export const promptsRelations = relations(prompts, ({ many }) => ({
Expand All @@ -26,7 +25,7 @@ export const promptsRelations = relations(prompts, ({ many }) => ({

export const datasets = sqliteTable('datasets', {
id: text('id').primaryKey(),
testCaseId: text('test_case_id').notNull(),
tests: text('tests', { mode: 'json' }).$type<UnifiedConfig['tests']>(),
createdAt: integer('created_at').notNull().default(sql`CURRENT_TIMESTAMP`),
});

Expand Down
6 changes: 3 additions & 3 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ export async function writeResultsToDatabase(
config,
results,
})
.onConflictDoNothing()
.run(),
);

Expand All @@ -450,7 +451,6 @@ export async function writeResultsToDatabase(
.values({
id: promptId,
prompt: prompt.display,
hash: promptId,
})
.onConflictDoNothing()
.run(),
Expand All @@ -471,13 +471,13 @@ export async function writeResultsToDatabase(
}

// Record dataset relation
const datasetId = sha256(JSON.stringify(config.tests));
const datasetId = sha256(JSON.stringify(config.tests || []));
promises.push(
db
.insert(datasets)
.values({
id: datasetId,
testCaseId: JSON.stringify(config.tests),
tests: config.tests,
})
.onConflictDoNothing()
.run(),
Expand Down
2 changes: 1 addition & 1 deletion src/web/nextui/src/app/datasets/Datasets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export default function Datasets() {
<TableCell style={{ width: '20%' }}>
{testCasesData.recentEvalId ? (
<Link href={`/eval?evalId=${testCasesData.recentEvalId}`}>
{testCasesData.recentEvalId.slice(0, 6)}
{testCasesData.recentEvalId}
</Link>
) : (
'Unknown'
Expand Down
2 changes: 1 addition & 1 deletion src/web/nextui/src/app/prompts/Prompts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export default function Prompts() {
<TableCell style={{ width: '20%' }}>
{promptRow.recentEvalDate ? (
<Link href={`/eval?evalId=${promptRow.recentEvalId}`}>
{promptRow.recentEvalDate}
{promptRow.recentEvalDate.slice(0, 10)}
</Link>
) : (
'Unknown'
Expand Down

0 comments on commit 1afbb97

Please sign in to comment.