Fork of github.com/did-method-plc/did-method-plc

migration

dholms bdccd99e ba750237

Changed files
+39 -34
packages
server
src
tests
migrations
+29 -29
packages/server/src/db/index.ts
···
-
import { Kysely, Migrator, PostgresDialect, sql } from 'kysely'
import { Pool as PgPool, types as pgTypes } from 'pg'
import { CID } from 'multiformats/cid'
import { cidForCbor, check } from '@atproto/common'
···
.insertInto('operations')
.values({
did,
-
operation: JSON.stringify(proposed),
cid: cid.toString(),
-
nullified: 0,
-
createdAt: new Date().toISOString(),
})
.execute()
···
const nullfiedStrs = nullified.map((cid) => cid.toString())
await tx
.updateTable('operations')
-
.set({ nullified: 1 })
.where('did', '=', did)
.where('cid', 'in', nullfiedStrs)
.execute()
···
.selectFrom('operations')
.select('cid')
.where('did', '=', did)
-
.where('nullified', '=', 0)
.orderBy('createdAt', 'desc')
.limit(2)
.execute()
···
.selectFrom('operations')
.select('cid')
.where('did', '=', did)
-
.where('nullified', '=', 0)
.where('cid', 'not in', notIncludedStr)
.orderBy('createdAt', 'desc')
.executeTakeFirst()
···
.selectFrom('operations')
.selectAll()
.where('did', '=', did)
-
.where('nullified', '=', 0)
.orderBy('createdAt', 'asc')
.execute()
return res.map((row) => ({
did: row.did,
-
operation: JSON.parse(row.operation),
cid: CID.parse(row.cid),
-
nullified: row.nullified === 1,
-
createdAt: new Date(row.createdAt),
}))
}
async fullExport(): Promise<Record<string, OpLogExport>> {
-
const res = await this.db
-
.selectFrom('operations')
-
.selectAll()
-
.orderBy('did')
-
.orderBy('createdAt')
-
.execute()
-
return res.reduce((acc, cur) => {
-
acc[cur.did] ??= []
-
acc[cur.did].push({
-
op: JSON.parse(cur.operation),
-
nullified: cur.nullified === 1,
-
createdAt: cur.createdAt,
-
})
-
return acc
-
}, {} as Record<string, OpLogExport>)
}
}
···
interface OperationsTable {
did: string
-
operation: string
cid: string
-
nullified: 0 | 1
-
createdAt: string
}
interface DatabaseSchema {
···
+
import { Generated, Kysely, Migrator, PostgresDialect, sql } from 'kysely'
import { Pool as PgPool, types as pgTypes } from 'pg'
import { CID } from 'multiformats/cid'
import { cidForCbor, check } from '@atproto/common'
···
.insertInto('operations')
.values({
did,
+
operation: proposed,
cid: cid.toString(),
+
nullified: false,
})
.execute()
···
const nullfiedStrs = nullified.map((cid) => cid.toString())
await tx
.updateTable('operations')
+
.set({ nullified: true })
.where('did', '=', did)
.where('cid', 'in', nullfiedStrs)
.execute()
···
.selectFrom('operations')
.select('cid')
.where('did', '=', did)
+
.where('nullified', '=', false)
.orderBy('createdAt', 'desc')
.limit(2)
.execute()
···
.selectFrom('operations')
.select('cid')
.where('did', '=', did)
+
.where('nullified', '=', false)
.where('cid', 'not in', notIncludedStr)
.orderBy('createdAt', 'desc')
.executeTakeFirst()
···
.selectFrom('operations')
.selectAll()
.where('did', '=', did)
+
.where('nullified', '=', false)
.orderBy('createdAt', 'asc')
.execute()
return res.map((row) => ({
did: row.did,
+
operation: row.operation,
cid: CID.parse(row.cid),
+
nullified: row.nullified,
+
createdAt: row.createdAt,
}))
}
async fullExport(): Promise<Record<string, OpLogExport>> {
+
return {}
+
// const res = await this.db
+
// .selectFrom('operations')
+
// .selectAll()
+
// .orderBy('did')
+
// .orderBy('createdAt')
+
// .execute()
+
// return res.reduce((acc, cur) => {
+
// acc[cur.did] ??= []
+
// acc[cur.did].push({
+
// op: cur.operation),
+
// nullified: cur.nullified === 1,
+
// createdAt: cur.createdAt,
+
// })
+
// return acc
+
// }, {} as Record<string, OpLogExport>)
}
}
···
interface OperationsTable {
did: string
+
operation: plc.CompatibleOpOrTombstone
cid: string
+
nullified: boolean
+
createdAt: Generated<Date>
}
interface DatabaseSchema {
+3 -1
packages/server/src/migrations/20230223T215019669Z-refactor.ts
···
createdAt: row.createdAt,
}))
-
await db.insertInto('operations_new').values(vals).execute()
await db.schema.dropTable('operations').execute()
···
createdAt: row.createdAt,
}))
+
if (vals.length > 0) {
+
await db.insertInto('operations_new').values(vals).execute()
+
}
await db.schema.dropTable('operations').execute()
+7 -4
packages/server/tests/migrations/refactor.test.ts
···
import { cidForCbor, DAY } from '@atproto/common'
import { Secp256k1Keypair } from '@atproto/crypto'
import * as plc from '@did-plc/lib'
import { Database } from '../../src'
describe('refactor migration', () => {
let db: Database
beforeAll(async () => {
const dbPostgresUrl = process.env.DB_POSTGRES_URL
···
})
await db.migrateToOrThrow('_20221020T204908820Z')
})
afterAll(async () => {
···
createdAt: time,
})
}
-
await db.db.insertInto('operations').values(ops).execute()
-
before = await db.db
.selectFrom('operations')
.selectAll()
.orderBy('did', 'asc')
···
})
it('correctly migrated all data', async () => {
-
const migrated = await db.db
.selectFrom('operations')
.selectAll()
.orderBy('did', 'asc')
···
it('migrates down', async () => {
await db.migrateToOrThrow('_20221020T204908820Z')
-
const migratedBack = await db.db
.selectFrom('operations')
.selectAll()
.orderBy('did', 'asc')
···
import { cidForCbor, DAY } from '@atproto/common'
import { Secp256k1Keypair } from '@atproto/crypto'
import * as plc from '@did-plc/lib'
+
import { Kysely } from 'kysely'
import { Database } from '../../src'
describe('refactor migration', () => {
let db: Database
+
let rawDb: Kysely<any>
beforeAll(async () => {
const dbPostgresUrl = process.env.DB_POSTGRES_URL
···
})
await db.migrateToOrThrow('_20221020T204908820Z')
+
rawDb = db.db
})
afterAll(async () => {
···
createdAt: time,
})
}
+
await rawDb.insertInto('operations').values(ops).execute()
+
before = await rawDb
.selectFrom('operations')
.selectAll()
.orderBy('did', 'asc')
···
})
it('correctly migrated all data', async () => {
+
const migrated = await rawDb
.selectFrom('operations')
.selectAll()
.orderBy('did', 'asc')
···
it('migrates down', async () => {
await db.migrateToOrThrow('_20221020T204908820Z')
+
const migratedBack = await rawDb
.selectFrom('operations')
.selectAll()
.orderBy('did', 'asc')