Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: metadata keys query #724

Merged
merged 1 commit into from
Jan 26, 2025
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
7 changes: 3 additions & 4 deletions packages/backend/src/api/v1/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,11 @@ filters.get("/metadata", async (ctx: Context) => {

const rows = await sql`
select distinct
jsonb_object_keys(metadata) as key
key
from
run
metadata_cache
where
project_id = ${projectId}
and metadata is not null
project_id = ${projectId}
order by
key;
`;
Expand Down
30 changes: 22 additions & 8 deletions packages/backend/src/create-indexes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sql from "./utils/db";

// TODO: rename to "async migration" or something like this
export async function createIndexes() {
await sql`select pg_advisory_lock(123456789)`;

Expand All @@ -11,7 +12,7 @@ export async function createIndexes() {
operation,
statement
from
_db_migration_index
_db_migration_async
where
status in ('pending', 'failed')
order
Expand All @@ -24,7 +25,7 @@ export async function createIndexes() {
try {
await sql`
update
_db_migration_index
_db_migration_async
set
status = 'in-progress'
where
Expand All @@ -33,6 +34,7 @@ export async function createIndexes() {

await sql.unsafe(statement);

// TODO: rename to "create_index" and "drop_index"
if (operation === "create") {
const [validCheck] = await sql`
select
Expand All @@ -49,7 +51,7 @@ export async function createIndexes() {
if (validCheck) {
await sql`
update
_db_migration_index
_db_migration_async
set
status = 'done'
where
Expand All @@ -60,7 +62,7 @@ export async function createIndexes() {
await sql.unsafe(`drop index if exists ${name}`);
await sql`
update
_db_migration_index
_db_migration_async
set
status = 'failed'
where
Expand All @@ -83,7 +85,7 @@ export async function createIndexes() {
if (!stillExists) {
await sql`
update
_db_migration_index
_db_migration_async
set
status = 'done'
where
Expand All @@ -93,21 +95,33 @@ export async function createIndexes() {
} else {
await sql`
update
_db_migration_index
_db_migration_async
set
status = 'failed'
where
id = ${id}
`;
console.warn(`Index drop "${name}" failed; index still exists.`);
}
} else if (operation === "create-materialized-view") {
await sql`
update
_db_migration_async
set
status = 'done'
where
id = ${id}
`;
console.log(
`Materialized view migration "${name}" completed successfully.`,
);
}
} catch (err) {
console.error(`Index migration "${name}" errored:`, err);
await sql.unsafe(`drop index if exists ${name}`);
await sql.unsafe(`drop index if exists ${name}`); // TODO: only drop if it was an index, not a materialized views
await sql`
update
_db_migration_index
_db_migration_async
set
status = 'failed'
where
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import ratelimit from "./utils/ratelimit";
import { initSentry, requestHandler, tracingMiddleWare } from "./utils/sentry";
import licenseMiddleware from "./utils/license";
import config from "./utils/config";
import { startMaterializedViewRefreshJob } from "./jobs/materializedViews";
import { startMaterializedViewRefreshJob } from "./jobs/materialized-views";
import { createIndexes } from "./create-indexes";

checkDbConnection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import sql from "../utils/db";
import { sleep } from "../utils/misc";

export async function startMaterializedViewRefreshJob() {
// TODO: locks
if (process.env.DISABLE_MATERIALIZED_VIEW_REFRESH) {
return;
}
try {
const views = ["run_parent_feedback_cache"];
const views = ["run_parent_feedback_cache", "metadata_cache"];

while (true) {
for (const view of views) {
Expand All @@ -18,7 +19,7 @@ export async function startMaterializedViewRefreshJob() {
);
}

await sleep(1000);
await sleep(60000);
}
} catch (error) {
console.error(error);
Expand Down
22 changes: 22 additions & 0 deletions packages/db/0060.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
alter table _db_migration_index rename to _db_migration_async;

insert into _db_migration_async (name, operation, statement) values
('metadata_cache', 'create-materialized-view',
'create materialized view metadata_cache as
select
run.project_id,
run.type,
jsonb_object_keys(run.metadata) as key,
now() as refreshed_at
from
run
group by
run.project_id,
run.type,
jsonb_object_keys(run.metadata);'
),
('metadata_cache_project_id_idx', 'create', 'create index concurrently if not exists metadata_cache_project_id_idx on metadata_cache(project_id)'),
('metadata_cache_project_id_type_key_idx', 'create', 'create unique index concurrently if not exists metadata_cache_project_id_type_key_idx on metadata_cache (project_id, type, key)');



Loading