diff --git a/packages/access-api/migrations/0005_drop_delegations_audience_to_accounts_did_fk.sql b/packages/access-api/migrations/0005_drop_delegations_audience_to_accounts_did_fk.sql index 37f7c4dd0..8bbae4624 100644 --- a/packages/access-api/migrations/0005_drop_delegations_audience_to_accounts_did_fk.sql +++ b/packages/access-api/migrations/0005_drop_delegations_audience_to_accounts_did_fk.sql @@ -5,14 +5,11 @@ goal: remove the foreign key constraint on delegations.audience -> accounts.did. We want to be able to store delegations whose audience is not an account did. sqlite doesn't support `alter table drop constraint`. -So here we will: -* create delegations_new table without the constraint -* insert all from delegations -> delegations_new -* rename delegations_new -> delegations +AND cloudflare d1 doesn't support `DROP TABLE` */ CREATE TABLE - IF NOT EXISTS delegations_new ( + IF NOT EXISTS delegations_v2 ( cid TEXT NOT NULL PRIMARY KEY, bytes BLOB NOT NULL, audience TEXT NOT NULL, @@ -23,8 +20,5 @@ CREATE TABLE UNIQUE (cid) ); -INSERT INTO delegations_new (cid, bytes, audience, issuer, expiration, inserted_at, updated_at) +INSERT INTO delegations_v2 (cid, bytes, audience, issuer, expiration, inserted_at, updated_at) SELECT cid, bytes, audience, issuer, expiration, inserted_at, updated_at FROM delegations; - -ALTER TABLE delegations RENAME TO delegations_1677808856; -ALTER TABLE delegations_new RENAME TO delegations; diff --git a/packages/access-api/src/models/accounts.js b/packages/access-api/src/models/accounts.js index 8200417ef..2feb1dbc5 100644 --- a/packages/access-api/src/models/accounts.js +++ b/packages/access-api/src/models/accounts.js @@ -1,9 +1,5 @@ // eslint-disable-next-line no-unused-vars import * as Ucanto from '@ucanto/interface' -import { - delegationsToBytes, - expirationToDate, -} from '@web3-storage/access/encoding' import { Kysely } from 'kysely' import { D1Dialect } from 'kysely-d1' import { GenericPlugin } from '../utils/d1.js' @@ -51,26 +47,6 @@ export class Accounts { return { data: result } } - /** - * - * @param {Ucanto.Delegation} del - */ - async addDelegation(del) { - const result = await this.d1 - .insertInto('delegations') - .values({ - cid: del.cid.toV1().toString(), - audience: del.audience.did(), - issuer: del.issuer.did(), - bytes: delegationsToBytes([del]), - expires_at: expirationToDate(del.expiration), - }) - .onConflict((oc) => oc.column('cid').doNothing()) - .returningAll() - .executeTakeFirst() - return result - } - /** * @param {Ucanto.URI<"did:">} did */ @@ -81,32 +57,4 @@ export class Accounts { .where('accounts.did', '=', did) .executeTakeFirst() } - - /** - * @param {Ucanto.URI<"did:">} did - */ - async getDelegations(did) { - return await this.d1 - .selectFrom('delegations') - .selectAll() - .where('delegations.audience', '=', did) - .execute() - } - - /** - * @param {string} cid - */ - async getDelegationsByCid(cid) { - return await this.d1 - .selectFrom('delegations') - .selectAll() - .where('delegations.cid', '=', cid) - .where((qb) => - qb - .where('delegations.expires_at', '>=', new Date()) - // eslint-disable-next-line unicorn/no-null - .orWhere('delegations.expires_at', 'is', null) - ) - .executeTakeFirst() - } } diff --git a/packages/access-api/src/models/delegations.js b/packages/access-api/src/models/delegations.js index 8645c5cf4..394f41d4b 100644 --- a/packages/access-api/src/models/delegations.js +++ b/packages/access-api/src/models/delegations.js @@ -11,7 +11,7 @@ import { /** * @typedef Tables - * @property {DelegationRow} delegations + * @property {DelegationRow} delegations_v2 */ /** @@ -25,6 +25,9 @@ import { export class DbDelegationsStorage { /** @type {DelegationsDatabase} */ #db + #tables = { + delegations: /** @type {const} */ ('delegations_v2'), + } /** * @param {DelegationsDatabase} db @@ -39,7 +42,7 @@ export class DbDelegationsStorage { async count() { const { size } = await this.#db - .selectFrom('delegations') + .selectFrom(this.#tables.delegations) .select((e) => e.fn.count('cid').as('size')) .executeTakeFirstOrThrow() return BigInt(size) @@ -49,7 +52,14 @@ export class DbDelegationsStorage { * @param {import('../types/delegations').Query} query */ async *find(query) { - for await (const row of await selectByAudience(this.#db, query.audience)) { + const { audience } = query + const { delegations } = this.#tables + const selection = await this.#db + .selectFrom(delegations) + .selectAll() + .where(`${delegations}.audience`, '=', audience) + .execute() + for await (const row of selection) { yield rowToDelegation(row) } } @@ -66,7 +76,7 @@ export class DbDelegationsStorage { } const values = delegations.map((d) => createDelegationRowUpdate(d)) await this.#db - .insertInto('delegations') + .insertInto(this.#tables.delegations) .values(values) .onConflict((oc) => oc.column('cid').doNothing()) .executeTakeFirst() @@ -87,7 +97,7 @@ export class DbDelegationsStorage { ) } for await (const row of this.#db - .selectFrom('delegations') + .selectFrom(this.#tables.delegations) .select(['bytes']) .stream()) { yield rowToDelegation(row) @@ -121,15 +131,3 @@ function createDelegationRowUpdate(d) { bytes: delegationsToBytes([d]), } } - -/** - * @param {DelegationsDatabase} db - * @param {Ucanto.DID} audience - */ -async function selectByAudience(db, audience) { - return await db - .selectFrom('delegations') - .selectAll() - .where('delegations.audience', '=', audience) - .execute() -}