From 2ecfe879dca8347218808966fbc5b6d652b91c0b Mon Sep 17 00:00:00 2001 From: dholms Date: Wed, 13 Sep 2023 20:53:36 -0500 Subject: [PATCH] tidy migrations --- ...18T170914772Z-sequencer-leader-sequence.ts | 25 --- ...0727T172043676Z-user-account-cursor-idx.ts | 13 -- .../20230801T141349990Z-invite-note.ts | 12 -- ...1Z-feed-item-delete-invite-for-user-idx.ts | 14 -- .../20230808T172813122Z-repo-rev.ts | 15 -- .../20230810T203412859Z-action-duration.ts | 23 --- .../20230818T134357818Z-runtime-flags.ts | 13 -- .../20230825T142507884Z-blob-tempkey-idx.ts | 13 -- ...0230828T153013575Z-repo-history-rewrite.ts | 62 ------- .../migrations/20230914T014727199Z-repo-v3.ts | 165 ++++++++++++++++++ packages/pds/src/db/migrations/index.ts | 10 +- 11 files changed, 166 insertions(+), 199 deletions(-) delete mode 100644 packages/pds/src/db/migrations/20230718T170914772Z-sequencer-leader-sequence.ts delete mode 100644 packages/pds/src/db/migrations/20230727T172043676Z-user-account-cursor-idx.ts delete mode 100644 packages/pds/src/db/migrations/20230801T141349990Z-invite-note.ts delete mode 100644 packages/pds/src/db/migrations/20230807T035309811Z-feed-item-delete-invite-for-user-idx.ts delete mode 100644 packages/pds/src/db/migrations/20230808T172813122Z-repo-rev.ts delete mode 100644 packages/pds/src/db/migrations/20230810T203412859Z-action-duration.ts delete mode 100644 packages/pds/src/db/migrations/20230818T134357818Z-runtime-flags.ts delete mode 100644 packages/pds/src/db/migrations/20230825T142507884Z-blob-tempkey-idx.ts delete mode 100644 packages/pds/src/db/migrations/20230828T153013575Z-repo-history-rewrite.ts create mode 100644 packages/pds/src/db/migrations/20230914T014727199Z-repo-v3.ts diff --git a/packages/pds/src/db/migrations/20230718T170914772Z-sequencer-leader-sequence.ts b/packages/pds/src/db/migrations/20230718T170914772Z-sequencer-leader-sequence.ts deleted file mode 100644 index aae6db339b9..00000000000 --- a/packages/pds/src/db/migrations/20230718T170914772Z-sequencer-leader-sequence.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { Kysely, sql } from 'kysely' -import { Dialect } from '..' - -export async function up(db: Kysely, dialect: Dialect): Promise { - if (dialect === 'sqlite') return - const res = await db - .selectFrom('repo_seq') - .select('seq') - .where('seq', 'is not', null) - .orderBy('seq', 'desc') - .limit(1) - .executeTakeFirst() - const startAt = res?.seq ? res.seq + 50000 : 1 - await sql`CREATE SEQUENCE repo_seq_sequence START ${sql.literal( - startAt, - )};`.execute(db) -} - -export async function down( - db: Kysely, - dialect: Dialect, -): Promise { - if (dialect === 'sqlite') return - await sql`DROP SEQUENCE repo_seq_sequence;`.execute(db) -} diff --git a/packages/pds/src/db/migrations/20230727T172043676Z-user-account-cursor-idx.ts b/packages/pds/src/db/migrations/20230727T172043676Z-user-account-cursor-idx.ts deleted file mode 100644 index 15f38eafd65..00000000000 --- a/packages/pds/src/db/migrations/20230727T172043676Z-user-account-cursor-idx.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { Kysely } from 'kysely' - -export async function up(db: Kysely): Promise { - await db.schema - .createIndex('user_account_cursor_idx') - .on('user_account') - .columns(['createdAt', 'did']) - .execute() -} - -export async function down(db: Kysely): Promise { - await db.schema.dropIndex('user_account_cursor_idx').execute() -} diff --git a/packages/pds/src/db/migrations/20230801T141349990Z-invite-note.ts b/packages/pds/src/db/migrations/20230801T141349990Z-invite-note.ts deleted file mode 100644 index c83a3030350..00000000000 --- a/packages/pds/src/db/migrations/20230801T141349990Z-invite-note.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Kysely } from 'kysely' - -export async function up(db: Kysely): Promise { - await db.schema - .alterTable('user_account') - .addColumn('inviteNote', 'varchar') - .execute() -} - -export async function down(db: Kysely): Promise { - await db.schema.alterTable('user_account').dropColumn('inviteNote').execute() -} diff --git a/packages/pds/src/db/migrations/20230807T035309811Z-feed-item-delete-invite-for-user-idx.ts b/packages/pds/src/db/migrations/20230807T035309811Z-feed-item-delete-invite-for-user-idx.ts deleted file mode 100644 index f93e277ffe4..00000000000 --- a/packages/pds/src/db/migrations/20230807T035309811Z-feed-item-delete-invite-for-user-idx.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { Kysely } from 'kysely' - -export async function up(db: Kysely): Promise { - // supports listing user invites - await db.schema - .createIndex('invite_code_for_user_idx') - .on('invite_code') - .column('forUser') - .execute() -} - -export async function down(db: Kysely): Promise { - await db.schema.dropIndex('invite_code_for_user_idx').execute() -} diff --git a/packages/pds/src/db/migrations/20230808T172813122Z-repo-rev.ts b/packages/pds/src/db/migrations/20230808T172813122Z-repo-rev.ts deleted file mode 100644 index e4c17d73291..00000000000 --- a/packages/pds/src/db/migrations/20230808T172813122Z-repo-rev.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { Kysely } from 'kysely' - -export async function up(db: Kysely): Promise { - await db.schema.alterTable('record').addColumn('repoRev', 'varchar').execute() - await db.schema - .createIndex('record_repo_rev_idx') - .on('record') - .columns(['did', 'repoRev']) - .execute() -} - -export async function down(db: Kysely): Promise { - await db.schema.dropIndex('record_repo_rev_idx').execute() - await db.schema.alterTable('record').dropColumn('repoRev').execute() -} diff --git a/packages/pds/src/db/migrations/20230810T203412859Z-action-duration.ts b/packages/pds/src/db/migrations/20230810T203412859Z-action-duration.ts deleted file mode 100644 index 0530d4d74fd..00000000000 --- a/packages/pds/src/db/migrations/20230810T203412859Z-action-duration.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { Kysely } from 'kysely' - -export async function up(db: Kysely): Promise { - await db.schema - .alterTable('moderation_action') - .addColumn('durationInHours', 'integer') - .execute() - await db.schema - .alterTable('moderation_action') - .addColumn('expiresAt', 'varchar') - .execute() -} - -export async function down(db: Kysely): Promise { - await db.schema - .alterTable('moderation_action') - .dropColumn('durationInHours') - .execute() - await db.schema - .alterTable('moderation_action') - .dropColumn('expiresAt') - .execute() -} diff --git a/packages/pds/src/db/migrations/20230818T134357818Z-runtime-flags.ts b/packages/pds/src/db/migrations/20230818T134357818Z-runtime-flags.ts deleted file mode 100644 index c93ccd74158..00000000000 --- a/packages/pds/src/db/migrations/20230818T134357818Z-runtime-flags.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { Kysely } from 'kysely' - -export async function up(db: Kysely): Promise { - await db.schema - .createTable('runtime_flag') - .addColumn('name', 'varchar', (col) => col.primaryKey()) - .addColumn('value', 'varchar', (col) => col.notNull()) - .execute() -} - -export async function down(db: Kysely): Promise { - await db.schema.dropTable('runtime_flag').execute() -} diff --git a/packages/pds/src/db/migrations/20230825T142507884Z-blob-tempkey-idx.ts b/packages/pds/src/db/migrations/20230825T142507884Z-blob-tempkey-idx.ts deleted file mode 100644 index 42cb843661e..00000000000 --- a/packages/pds/src/db/migrations/20230825T142507884Z-blob-tempkey-idx.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { Kysely } from 'kysely' - -export async function up(db: Kysely): Promise { - await db.schema - .createIndex('blob_tempkey_idx') - .on('blob') - .column('tempKey') - .execute() -} - -export async function down(db: Kysely): Promise { - await db.schema.dropIndex('blob_tempkey_idx').execute() -} diff --git a/packages/pds/src/db/migrations/20230828T153013575Z-repo-history-rewrite.ts b/packages/pds/src/db/migrations/20230828T153013575Z-repo-history-rewrite.ts deleted file mode 100644 index 368d7cbdbe5..00000000000 --- a/packages/pds/src/db/migrations/20230828T153013575Z-repo-history-rewrite.ts +++ /dev/null @@ -1,62 +0,0 @@ -import { Kysely } from 'kysely' - -export async function up(db: Kysely): Promise { - await db.schema.alterTable('repo_root').addColumn('rev', 'varchar').execute() - await db.schema - .alterTable('ipld_block') - .addColumn('repoRev', 'varchar') - .execute() - await db.schema - .alterTable('repo_blob') - .addColumn('repoRev', 'varchar') - .execute() - await db.schema.alterTable('repo_blob').dropColumn('commit').execute() - - await db.schema - .createIndex('ipld_block_repo_rev_idx') - .on('ipld_block') - .columns(['creator', 'repoRev', 'cid']) - .execute() - - await db.schema - .createIndex('repo_blob_repo_rev_idx') - .on('repo_blob') - .columns(['did', 'repoRev']) - .execute() - - await db.schema.dropTable('repo_commit_history').execute() - await db.schema.dropTable('repo_commit_block').execute() -} - -export async function down(db: Kysely): Promise { - await db.schema - .createTable('repo_commit_block') - .addColumn('commit', 'varchar', (col) => col.notNull()) - .addColumn('block', 'varchar', (col) => col.notNull()) - .addColumn('creator', 'varchar', (col) => col.notNull()) - .addPrimaryKeyConstraint('repo_commit_block_pkey', [ - 'creator', - 'commit', - 'block', - ]) - .execute() - await db.schema - .createTable('repo_commit_history') - .addColumn('commit', 'varchar', (col) => col.notNull()) - .addColumn('prev', 'varchar') - .addColumn('creator', 'varchar', (col) => col.notNull()) - .addPrimaryKeyConstraint('repo_commit_history_pkey', ['creator', 'commit']) - .execute() - - await db.schema.dropIndex('ipld_block_repo_rev_idx').execute() - - await db.schema.dropIndex('repo_blob_repo_rev_idx').execute() - - await db.schema.alterTable('repo_root').dropColumn('rev').execute() - await db.schema.alterTable('ipld_block').dropColumn('repoRev').execute() - await db.schema.alterTable('repo_blob').dropColumn('repoRev').execute() - await db.schema - .alterTable('repo_blob') - .addColumn('commit', 'varchar') - .execute() -} diff --git a/packages/pds/src/db/migrations/20230914T014727199Z-repo-v3.ts b/packages/pds/src/db/migrations/20230914T014727199Z-repo-v3.ts new file mode 100644 index 00000000000..cd9569bc33c --- /dev/null +++ b/packages/pds/src/db/migrations/20230914T014727199Z-repo-v3.ts @@ -0,0 +1,165 @@ +import { Kysely, sql } from 'kysely' +import { Dialect } from '..' + +export async function up(db: Kysely, dialect: Dialect): Promise { + // sequencer leader sequence + if (dialect !== 'sqlite') { + const res = await db + .selectFrom('repo_seq') + .select('seq') + .where('seq', 'is not', null) + .orderBy('seq', 'desc') + .limit(1) + .executeTakeFirst() + const startAt = res?.seq ? res.seq + 50000 : 1 + await sql`CREATE SEQUENCE repo_seq_sequence START ${sql.literal( + startAt, + )};`.execute(db) + } + + // user account cursor idx + await db.schema + .createIndex('user_account_cursor_idx') + .on('user_account') + .columns(['createdAt', 'did']) + .execute() + + // invite note + await db.schema + .alterTable('user_account') + .addColumn('inviteNote', 'varchar') + .execute() + + // listing user invites + await db.schema + .createIndex('invite_code_for_user_idx') + .on('invite_code') + .column('forUser') + .execute() + + // mod action duration + await db.schema + .alterTable('moderation_action') + .addColumn('durationInHours', 'integer') + .execute() + await db.schema + .alterTable('moderation_action') + .addColumn('expiresAt', 'varchar') + .execute() + + // runtime flag + await db.schema + .createTable('runtime_flag') + .addColumn('name', 'varchar', (col) => col.primaryKey()) + .addColumn('value', 'varchar', (col) => col.notNull()) + .execute() + + // blob tempkey idx + await db.schema + .createIndex('blob_tempkey_idx') + .on('blob') + .column('tempKey') + .execute() + + // repo v3 + await db.schema.alterTable('repo_root').addColumn('rev', 'varchar').execute() + await db.schema.alterTable('record').addColumn('repoRev', 'varchar').execute() + await db.schema + .alterTable('ipld_block') + .addColumn('repoRev', 'varchar') + .execute() + await db.schema + .alterTable('repo_blob') + .addColumn('repoRev', 'varchar') + .execute() + await db.schema.alterTable('repo_blob').dropColumn('commit').execute() + + await db.schema + .createIndex('record_repo_rev_idx') + .on('record') + .columns(['did', 'repoRev']) + .execute() + + await db.schema + .createIndex('ipld_block_repo_rev_idx') + .on('ipld_block') + .columns(['creator', 'repoRev', 'cid']) + .execute() + + await db.schema + .createIndex('repo_blob_repo_rev_idx') + .on('repo_blob') + .columns(['did', 'repoRev']) + .execute() + + await db.schema.dropTable('repo_commit_history').execute() + await db.schema.dropTable('repo_commit_block').execute() +} + +export async function down( + db: Kysely, + dialect: Dialect, +): Promise { + // repo v3 + await db.schema + .createTable('repo_commit_block') + .addColumn('commit', 'varchar', (col) => col.notNull()) + .addColumn('block', 'varchar', (col) => col.notNull()) + .addColumn('creator', 'varchar', (col) => col.notNull()) + .addPrimaryKeyConstraint('repo_commit_block_pkey', [ + 'creator', + 'commit', + 'block', + ]) + .execute() + await db.schema + .createTable('repo_commit_history') + .addColumn('commit', 'varchar', (col) => col.notNull()) + .addColumn('prev', 'varchar') + .addColumn('creator', 'varchar', (col) => col.notNull()) + .addPrimaryKeyConstraint('repo_commit_history_pkey', ['creator', 'commit']) + .execute() + + await db.schema.dropIndex('record_repo_rev_idx').execute() + await db.schema.dropIndex('ipld_block_repo_rev_idx').execute() + await db.schema.dropIndex('repo_blob_repo_rev_idx').execute() + + await db.schema.alterTable('repo_root').dropColumn('rev').execute() + await db.schema.alterTable('record').dropColumn('repoRev').execute() + await db.schema.alterTable('ipld_block').dropColumn('repoRev').execute() + await db.schema.alterTable('repo_blob').dropColumn('repoRev').execute() + await db.schema + .alterTable('repo_blob') + .addColumn('commit', 'varchar') + .execute() + + // blob tempkey idx + await db.schema.dropIndex('blob_tempkey_idx').execute() + + // runtime flag + await db.schema.dropTable('runtime_flag').execute() + + // mod action duration + await db.schema + .alterTable('moderation_action') + .dropColumn('durationInHours') + .execute() + await db.schema + .alterTable('moderation_action') + .dropColumn('expiresAt') + .execute() + + // listing user invites + await db.schema.dropIndex('invite_code_for_user_idx').execute() + + // invite note + await db.schema.alterTable('user_account').dropColumn('inviteNote').execute() + + // user account cursor idx + await db.schema.dropIndex('user_account_cursor_idx').execute() + + // sequencer leader sequence + if (dialect !== 'sqlite') { + await sql`DROP SEQUENCE repo_seq_sequence;`.execute(db) + } +} diff --git a/packages/pds/src/db/migrations/index.ts b/packages/pds/src/db/migrations/index.ts index c8fd3d13b23..8ebd59d6ef3 100644 --- a/packages/pds/src/db/migrations/index.ts +++ b/packages/pds/src/db/migrations/index.ts @@ -3,12 +3,4 @@ // this with kysely's FileMigrationProvider, but it doesn't play nicely with the build process. export * as _20230613T164932261Z from './20230613T164932261Z-init' -export * as _20230718T170914772Z from './20230718T170914772Z-sequencer-leader-sequence' -export * as _20230727T172043676Z from './20230727T172043676Z-user-account-cursor-idx' -export * as _20230801T141349990Z from './20230801T141349990Z-invite-note' -export * as _20230807T035309811Z from './20230807T035309811Z-feed-item-delete-invite-for-user-idx' -export * as _20230808T172813122Z from './20230808T172813122Z-repo-rev' -export * as _20230810T203412859Z from './20230810T203412859Z-action-duration' -export * as _20230818T134357818Z from './20230818T134357818Z-runtime-flags' -export * as _20230825T142507884Z from './20230825T142507884Z-blob-tempkey-idx' -export * as _20230828T153013575Z from './20230828T153013575Z-repo-history-rewrite' +export * as _20230914T014727199Z from './20230914T014727199Z-repo-v3'