Fork of github.com/did-method-plc/did-method-plc
1import { Kysely, sql } from 'kysely' 2 3export async function up(db: Kysely<any>): Promise<void> { 4 await db.schema 5 .createTable('operations_new') 6 .addColumn('did', 'text', (col) => col.notNull()) 7 .addColumn('operation', 'jsonb', (col) => col.notNull()) 8 .addColumn('cid', 'text', (col) => col.notNull()) 9 .addColumn('nullified', 'boolean', (col) => col.notNull()) 10 .addColumn('createdAt', 'timestamptz', (col) => 11 col.notNull().defaultTo(sql`current_timestamp`), 12 ) 13 .addPrimaryKeyConstraint('operations_primary_key', ['did', 'cid']) 14 .execute() 15 16 const dump = await db.selectFrom('operations').selectAll().execute() 17 const vals = dump.map((row) => ({ 18 did: row.did, 19 operation: row.operation, 20 cid: row.cid, 21 nullified: row.nullified === 1 ? true : false, 22 createdAt: row.createdAt, 23 })) 24 25 if (vals.length > 0) { 26 await db.insertInto('operations_new').values(vals).execute() 27 } 28 29 await db.schema.dropTable('operations').execute() 30 31 await db.schema.alterTable('operations_new').renameTo('operations').execute() 32 33 await db.schema 34 .createIndex('operations_createdAt_index') 35 .on('operations') 36 .column('createdAt') 37 .execute() 38} 39 40export async function down(db: Kysely<any>): Promise<void> { 41 await db.schema 42 .createTable('operations_new') 43 .addColumn('did', 'varchar', (col) => col.notNull()) 44 .addColumn('operation', 'text', (col) => col.notNull()) 45 .addColumn('cid', 'varchar', (col) => col.notNull()) 46 .addColumn('nullified', 'int2', (col) => col.defaultTo(0)) 47 .addColumn('createdAt', 'varchar', (col) => col.notNull()) 48 .addPrimaryKeyConstraint('primary_key', ['did', 'cid']) 49 .execute() 50 51 const dump = await db.selectFrom('operations').selectAll().execute() 52 const vals = dump.map((row) => ({ 53 did: row.did, 54 operation: JSON.stringify(row.operation), 55 cid: row.cid, 56 nullified: row.nullified ? 1 : 0, 57 createdAt: row.createdAt.toISOString(), 58 })) 59 60 await db.insertInto('operations_new').values(vals).execute() 61 62 await db.schema.dropIndex('operations_createdAt_index').execute() 63 await db.schema.dropTable('operations').execute() 64 65 await db.schema.alterTable('operations_new').renameTo('operations').execute() 66}