diff --git a/docs/downgrade.sql b/docs/downgrade.sql index e7de398a22..895046087a 100644 --- a/docs/downgrade.sql +++ b/docs/downgrade.sql @@ -1,6 +1,7 @@ BEGIN; DELETE FROM "migrations" WHERE name IN ( + 'RefactorScheduledPosts1716804636187', 'RemoveEnumTypenameSuffix1716462794927', 'CreateScheduledNote1714728200194', 'AddBackTimezone1715351290096', @@ -33,6 +34,38 @@ DELETE FROM "migrations" WHERE name IN ( 'RemoveNativeUtilsMigration1705877093218' ); +-- refactor-scheduled-post +CREATE TABLE "scheduled_note" ( + "id" character varying(32) NOT NULL PRIMARY KEY, + "noteId" character varying(32) NOT NULL, + "userId" character varying(32) NOT NULL, + "scheduledAt" TIMESTAMP WITH TIME ZONE NOT NULL +); +COMMENT ON COLUMN "scheduled_note"."noteId" IS 'The ID of the temporarily created note that corresponds to the schedule.'; +CREATE EXTENSION pgcrypto; +CREATE FUNCTION generate_scheduled_note_id(size int) RETURNS text AS $$ DECLARE + characters text := 'abcdefghijklmnopqrstuvwxyz0123456789'; + bytes bytea := gen_random_bytes(size); + l int := length(characters); + i int := 0; + output text := ''; + BEGIN + WHILE i < size LOOP + output := output || substr(characters, get_byte(bytes, i) % l + 1, 1); + i := i + 1; + END LOOP; + RETURN output; + END; +$$ LANGUAGE plpgsql VOLATILE; +INSERT INTO "scheduled_note" ("id", "noteId", "userId", "scheduledAt") (SELECT generate_scheduled_note_id(16), "id", "userId", "scheduledAt" FROM "note" WHERE "note"."scheduledAt" IS NOT NULL); +DROP EXTENSION pgcrypto; +DROP FUNCTION "generate_scheduled_note_id"; +CREATE INDEX "IDX_noteId_ScheduledNote" ON "scheduled_note" ("noteId"); +CREATE INDEX "IDX_userId_ScheduledNote" ON "scheduled_note" ("userId"); +ALTER TABLE "scheduled_note" ADD FOREIGN KEY ("noteId") REFERENCES "note"("id") ON DELETE CASCADE ON UPDATE NO ACTION; +ALTER TABLE "scheduled_note" ADD FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE NO ACTION; +ALTER TABLE "note" DROP COLUMN "scheduledAt"; + -- remove-enum-typename-suffix ALTER TYPE "antenna_src" RENAME TO "antenna_src_enum"; ALTER TYPE "drive_file_usage_hint" RENAME TO "drive_file_usage_hint_enum";