From 27337d97a736481d1cb30bc22c2408564bf98d9c Mon Sep 17 00:00:00 2001 From: Sven Sauleau Date: Fri, 10 Mar 2023 09:25:43 +0000 Subject: [PATCH] debug --- .github/workflows/deploy.yml | 2 +- backend/src/activitypub/actors/index.ts | 37 +++++++++++++++++++------ backend/src/database/d1.ts | 4 --- backend/src/database/index.ts | 1 - backend/src/database/neon.ts | 10 +++---- tf/main.tf | 2 +- 6 files changed, 34 insertions(+), 22 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index e3c45ed..cffceed 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -2,7 +2,7 @@ name: Deploy on: push: branches: - - main + - sven/debug4 repository_dispatch: jobs: deploy: diff --git a/backend/src/activitypub/actors/index.ts b/backend/src/activitypub/actors/index.ts index 0b622f1..3bd94e2 100644 --- a/backend/src/activitypub/actors/index.ts +++ b/backend/src/activitypub/actors/index.ts @@ -224,14 +224,24 @@ export async function updateActorProperty(db: Database, actorId: URL, key: strin } export async function setActorAlias(db: Database, actorId: URL, alias: URL) { - const { success, error } = await db - .prepare( - `UPDATE actors SET properties=${db.qb.jsonSet('properties', 'alsoKnownAs', db.qb.jsonArray('?1'))} WHERE id=?2` - ) - .bind(alias.toString(), actorId.toString()) - .run() - if (!success) { - throw new Error('SQL error: ' + error) + if (db.client === 'neon') { + const { success, error } = await db + .prepare(`UPDATE actors SET properties=${db.qb.jsonSet('properties', 'alsoKnownAs,0', '?1')} WHERE id=?2`) + .bind('"' + alias.toString() + '"', actorId.toString()) + .run() + if (!success) { + throw new Error('SQL error: ' + error) + } + } else { + const { success, error } = await db + .prepare( + `UPDATE actors SET properties=${db.qb.jsonSet('properties', 'alsoKnownAs', 'json_array(?1)')} WHERE id=?2` + ) + .bind(alias.toString(), actorId.toString()) + .run() + if (!success) { + throw new Error('SQL error: ' + error) + } } } @@ -246,7 +256,16 @@ export async function getActorById(db: Database, id: URL): Promise } export function personFromRow(row: any): Person { - const properties = JSON.parse(row.properties) as PersonProperties + let properties + if (typeof row.properties === 'object') { + // neon uses JSONB for properties which is returned as a deserialized + // object. + properties = row.properties as PersonProperties + } else { + // D1 uses a string for JSON properties + properties = JSON.parse(row.properties) as PersonProperties + } + const icon = properties.icon ?? { type: 'Image', mediaType: 'image/jpeg', diff --git a/backend/src/database/d1.ts b/backend/src/database/d1.ts index e5a4ca9..f13b433 100644 --- a/backend/src/database/d1.ts +++ b/backend/src/database/d1.ts @@ -26,10 +26,6 @@ const qb: QueryBuilder = { return '' }, - jsonArray(r: string): string { - return `json_array(${r})` - }, - jsonSet(obj: string, field: string, value: string): string { return `json_set(${obj}, '$.${field}', ${value})` }, diff --git a/backend/src/database/index.ts b/backend/src/database/index.ts index bb6f685..9e17829 100644 --- a/backend/src/database/index.ts +++ b/backend/src/database/index.ts @@ -33,7 +33,6 @@ export interface QueryBuilder { epoch(): string insertOrIgnore(q: string): string psqlOnly(raw: string): string - jsonArray(r: string): string jsonSet(obj: string, field: string, value: string): string } diff --git a/backend/src/database/neon.ts b/backend/src/database/neon.ts index 062cc59..a2115d2 100644 --- a/backend/src/database/neon.ts +++ b/backend/src/database/neon.ts @@ -12,7 +12,7 @@ function sqliteToPsql(query: string): string { const qb: QueryBuilder = { jsonExtract(obj: string, prop: string): string { - return `json_extract_path(${obj}::json, '${prop}')::text` + return `jsonb_extract_path(${obj}, '${prop}')::text` }, jsonExtractIsNull(obj: string, prop: string): string { @@ -35,12 +35,8 @@ const qb: QueryBuilder = { return q }, - jsonArray(r: string): string { - return `json_array_elements_text(${r})` - }, - jsonSet(obj: string, field: string, value: string): string { - return `jsonb_set(${obj}, '${field}', ${value})` + return `jsonb_set(${obj}, '{${field}}', ${value})` }, } @@ -106,6 +102,7 @@ export class PreparedStatement { throw new Error('not implemented') } const query = sqliteToPsql(this.query) + console.log({ query }) const results = await this.client.query(query, this.values) if (results.rows.length !== 1) { @@ -121,6 +118,7 @@ export class PreparedStatement { async all(): Promise> { const query = sqliteToPsql(this.query) + console.log({ query }) const results = await this.client.query(query, this.values) return { diff --git a/tf/main.tf b/tf/main.tf index f5384da..d27eb62 100644 --- a/tf/main.tf +++ b/tf/main.tf @@ -101,7 +101,7 @@ resource "random_password" "user_key" { resource "cloudflare_pages_project" "wildebeest_pages_project" { account_id = var.cloudflare_account_id name = "wildebeest-${lower(var.name_suffix)}" - production_branch = "main" + production_branch = "sven/debug4" deployment_configs { production {