hippofish/packages/backend/migration/1706413792769-separate-hard-mute-words-and-patterns.js
naskya d70d0f4232
refactor (backend): separate muted words and muted patterns
Co-authored-by: sup39 <dev@sup39.dev>
2024-03-01 22:52:21 +09:00

32 lines
927 B
JavaScript

export class SeparateHardMuteWordsAndPatterns1706413792769 {
name = "SeparateHardMuteWordsAndPatterns1706413792769";
async up(queryRunner) {
await queryRunner.query(
`ALTER TABLE "user_profile" ADD "mutedPatterns" text[] DEFAULT '{}'`,
);
await queryRunner.query(`
UPDATE "user_profile" SET
"mutedPatterns" = ARRAY(
SELECT jsonb_array_elements_text(jsonb_path_query_array(
"mutedWords",
'$ ? (@.type() == "string")'
))
),
"mutedWords" = jsonb_path_query_array(
"mutedWords",
'$ ? (@.type() == "array")'
)
`);
await queryRunner.query(
`ALTER TABLE "user_profile" ALTER "mutedPatterns" SET NOT NULL`,
);
}
async down(queryRunner) {
await queryRunner.query(
`UPDATE "user_profile" SET "mutedWords" = "mutedWords" || array_to_json("mutedPatterns")::jsonb`,
);
await queryRunner.query(`ALTER TABLE "user_profile" DROP "mutedPatterns"`);
}
}