Skip to content

Commit

Permalink
Added migration script for search on favorites aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanislav Kiselev committed Jan 17, 2024
1 parent c033701 commit c1fdea9
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 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;
`);
}

0 comments on commit c1fdea9

Please sign in to comment.