Skip to content

Commit

Permalink
Added migration script for search on favorites aliases (#56)
Browse files Browse the repository at this point in the history
* Added migration script for search on favorites aliases

* Update src/db/migrations/20240117092824_add_sort_columns_to_favorites.ts

Co-authored-by: Evgenij Shangin <[email protected]>

* Update src/db/migrations/20240117092824_add_sort_columns_to_favorites.ts

Co-authored-by: Evgenij Shangin <[email protected]>

---------

Co-authored-by: Stanislav Kiselev <“[email protected]”>
Co-authored-by: Evgenij Shangin <[email protected]>
  • Loading branch information
3 people authored Jan 18, 2024
1 parent c033701 commit 3acffec
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/db/migrations/20240117092824_add_sort_columns_to_favorites.ts
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 favorites_sort_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 favorites_sort_alias_idx;
DROP INDEX favorites_alias_idx;
ALTER TABLE favorites DROP COLUMN sort_alias;
ALTER TABLE favorites DROP COLUMN display_alias;
`);
}

0 comments on commit 3acffec

Please sign in to comment.