diff --git a/src/db/migrations/20210311185912_move_duration_to_recordings_table.ts b/src/db/migrations/20210311185912_move_duration_to_recordings_table.ts new file mode 100644 index 0000000..e0ee501 --- /dev/null +++ b/src/db/migrations/20210311185912_move_duration_to_recordings_table.ts @@ -0,0 +1,47 @@ +import { Knex } from "knex"; + +export async function up(knex: Knex) { + return knex.transaction(async (trx) => { + await trx.schema.alterTable("recordings", (table) => { + table.integer("duration"); + }); + + // transfer recording durations from voicemails table + await trx("recordings").update({ + duration: knex("voicemails") + .select("duration") + .where("messageId", knex.raw("??", "recordings.messageId")), + }); + + // now we can make duration column not-nullable + await trx.schema.alterTable("recordings", (table) => { + table.integer("duration").notNullable().alter(); + }); + + await trx.schema.alterTable("voicemails", (table) => { + table.dropColumn("duration"); + }); + }); +} + +export async function down(knex: Knex) { + return knex.transaction(async (trx) => { + await trx.schema.alterTable("voicemails", (table) => { + table.integer("duration"); + }); + + await trx("voicemails").update({ + duration: knex("recordings") + .select("duration") + .where("messageId", knex.raw("??", "voicemails.messageId")), + }); + + await trx.schema.alterTable("voicemails", (table) => { + table.integer("duration").notNullable().alter(); + }); + + await trx.schema.alterTable("recordings", (table) => { + table.dropColumn("duration"); + }); + }); +} diff --git a/src/template.tsx b/src/template.tsx index 0d22936..3fd6e46 100644 --- a/src/template.tsx +++ b/src/template.tsx @@ -3,7 +3,7 @@ import ReactDOMServer from "react-dom/server"; import { getNationalNumber, formatSeconds } from "./util"; import { DateTime } from "luxon"; import type { Contact } from "./types"; -import type { StoredVoicemail } from "knex/types/tables"; +import type { StoredVoicemail, StoredRecording } from "knex/types/tables"; export function getTicketSubject( voicemail: StoredVoicemail, @@ -14,7 +14,10 @@ export function getTicketSubject( })`; } -export function getTicketBody(vm: StoredVoicemail, contact?: Contact) { +export function getTicketBody( + vm: StoredVoicemail & StoredRecording, + contact?: Contact +) { return ReactDOMServer.renderToStaticMarkup(