diff --git a/packages/bsky/src/data-plane/server/db/migrations/20241215T172532040Z-poll-answers.ts b/packages/bsky/src/data-plane/server/db/migrations/20241215T172532040Z-poll-answers.ts new file mode 100644 index 00000000000..e75b56c7c57 --- /dev/null +++ b/packages/bsky/src/data-plane/server/db/migrations/20241215T172532040Z-poll-answers.ts @@ -0,0 +1,27 @@ +import { Kysely, sql } from 'kysely' + +export async function up(db: Kysely): Promise { + await db.schema + .createTable('poll_answer') + .addColumn('uri', 'varchar', (col) => col.primaryKey()) + .addColumn('cid', 'varchar', (col) => col.notNull()) + .addColumn('creator', 'varchar', (col) => col.notNull()) + .addColumn('subject', 'varchar', (col) => col.notNull()) + .addColumn('subjectCid', 'varchar', (col) => col.notNull()) + .addColumn('createdAt', 'varchar', (col) => col.notNull()) + .addColumn('indexedAt', 'varchar', (col) => col.notNull()) + .addColumn('answer', 'int8', (col) => col.notNull()) + .addColumn('sortAt', 'varchar', (col) => + col + .generatedAlwaysAs(sql`least("createdAt", "indexedAt")`) + .stored() + .notNull(), + ) + // Aids in index uniqueness plus answer counting + .addUniqueConstraint('poll_answer_unique_subject_answer', ['subject', 'answer', 'creator']) + .execute() +} + +export async function down(db: Kysely): Promise { + await db.schema.dropTable('poll_answer').execute() +} diff --git a/packages/bsky/src/data-plane/server/db/migrations/index.ts b/packages/bsky/src/data-plane/server/db/migrations/index.ts index 5b8684a2a78..721fe002dc3 100644 --- a/packages/bsky/src/data-plane/server/db/migrations/index.ts +++ b/packages/bsky/src/data-plane/server/db/migrations/index.ts @@ -45,3 +45,4 @@ export * as _20240808T224251220Z from './20240808T224251220Z-post-gate-flags' export * as _20240829T211238293Z from './20240829T211238293Z-simplify-actor-sync' export * as _20240831T134810923Z from './20240831T134810923Z-pinned-posts' export * as _20241114T153108102Z from './20241114T153108102Z-add-starter-packs-name' +export * as _20241215T172532040Z from './20241215T172532040Z-poll-answers'