Skip to content

Commit

Permalink
revamp migrate script
Browse files Browse the repository at this point in the history
  • Loading branch information
dholms committed Nov 2, 2023
1 parent ec8ef4e commit c491782
Show file tree
Hide file tree
Showing 2 changed files with 504 additions and 0 deletions.
96 changes: 96 additions & 0 deletions packages/pds/src/migrate-script/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { Kysely, MigrationProvider, Migrator, SqliteDialect } from 'kysely'
import SqliteDB from 'better-sqlite3'

export const getDb = async (loc: string): Promise<MigrateDb> => {
const db = new Kysely<Schema>({
dialect: new SqliteDialect({
database: new SqliteDB(loc),
}),
})
const migrator = new Migrator({ db, provider: dbMigrationProvider })
const { error } = await migrator.migrateToLatest()
if (error) throw error
return db
}

const dbMigrationProvider: MigrationProvider = {
async getMigrations() {
return {
'1': {
async up(db: Kysely<unknown>) {
await db.schema
.createTable('status')
.addColumn('did', 'varchar', (col) => col.primaryKey())
.addColumn('pdsId', 'integer')
.addColumn('signingKey', 'varchar')
.addColumn('phase', 'integer')
.addColumn('importedRev', 'varchar')
.addColumn('failed', 'integer')
.execute()
await db.schema
.createTable('failed_pref')
.addColumn('did', 'varchar', (col) => col.primaryKey())
.execute()
await db.schema
.createTable('failed_blob')
.addColumn('did', 'varchar', (col) => col.notNull())
.addColumn('cid', 'varchar', (col) => col.notNull())
.addPrimaryKeyConstraint('failed_blob_pkey', ['did', 'cid'])
.execute()
await db.schema
.createTable('failed_takedown')
.addColumn('did', 'varchar', (col) => col.notNull())
.addColumn('recordUri', 'varchar')
.addColumn('recordCid', 'varchar')
.addColumn('blobCid', 'varchar')
.execute()
},
async down() {},
},
}
},
}

export type MigrateDb = Kysely<Schema>

type Schema = {
status: Status
failed_pref: FailedPreference
failed_blob: FailedBlob
failed_takedown: FailedTakedown
}

export enum TransferPhase {
notStarted = 0,
reservedKey = 1,
initImport = 2,
transferred = 4,
preferences = 5,
takedowns = 6,
completed = 7,
}

export type Status = {
did: string
pdsId: number | null
signingKey: string | null
phase: TransferPhase
importedRev: string | null
failed: 0 | 1
}

export type FailedPreference = {
did: string
}

export type FailedBlob = {
did: string
cid: string
}

export type FailedTakedown = {
did: string
blobCid?: string
recordUri?: string
recordCid?: string
}
Loading

0 comments on commit c491782

Please sign in to comment.