-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added migration script for search on favorites aliases
- Loading branch information
Stanislav Kiselev
committed
Jan 17, 2024
1 parent
c033701
commit c1fdea9
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/db/migrations/20240117092824_add_sort_columns_to_favorites.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import type {Knex} from 'knex'; | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
return knex.raw(` | ||
ALTER TABLE favorites ADD COLUMN display_alias TEXT; | ||
ALTER TABLE favorites ADD COLUMN sort_alias bytea; | ||
UPDATE favorites SET display_alias = alias; | ||
CREATE INDEX favorites_alias_idx ON favorites(alias); | ||
UPDATE favorites SET sort_alias = naturalsort(alias); | ||
CREATE INDEX sort_favorites_alias_idx ON favorites(sort_alias); | ||
CREATE FUNCTION update_favorites() RETURNS trigger AS $$ | ||
BEGIN | ||
NEW.sort_alias := naturalsort(NEW.alias); | ||
RETURN NEW; | ||
END | ||
$$ LANGUAGE plpgsql; | ||
CREATE TRIGGER before_favorites_insert_or_update | ||
BEFORE INSERT OR UPDATE ON favorites | ||
FOR EACH ROW EXECUTE PROCEDURE update_favorites(); | ||
`); | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> { | ||
return knex.raw(` | ||
DROP TRIGGER before_favorites_insert_or_update on favorites; | ||
DROP FUNCTION update_favorites(); | ||
DROP INDEX sort_favorites_alias_idx; | ||
DROP INDEX favorites_alias_idx; | ||
ALTER TABLE favorites DROP COLUMN sort_alias; | ||
ALTER TABLE favorites DROP COLUMN display_alias; | ||
`); | ||
} |