plsql command works in tableplus but fails when done using goose migrations #915
Answered
by
mfridman
hackertron
asked this question in
Q&A
-
below is the sql code -- +goose Up
-- Add full-text search capabilities
ALTER TABLE jobs ADD COLUMN search_vector tsvector;
-- Create GIN index for fast full-text search
CREATE INDEX jobs_search_idx ON jobs USING GIN(search_vector);
-- Create trigger function to update search_vector
CREATE OR REPLACE FUNCTION jobs_search_trigger() RETURNS TRIGGER LANGUAGE plpgsql AS $$
DECLARE
title_text text := COALESCE(NEW.title, '');
description_text text := COALESCE(NEW.description, '');
company_text text := COALESCE(NEW.company, '');
location_text text := COALESCE(NEW.location, '');
BEGIN
NEW.search_vector := (
setweight(to_tsvector('english', title_text), 'A') ||
setweight(to_tsvector('english', description_text), 'B') ||
setweight(to_tsvector('english', company_text), 'C') ||
setweight(to_tsvector('english', location_text), 'D')
);
RETURN NEW;
END;
$$;
-- Attach the trigger to the jobs table
CREATE TRIGGER jobs_search_update
BEFORE INSERT OR UPDATE ON jobs
FOR EACH ROW
EXECUTE FUNCTION jobs_search_trigger();
-- +goose Down
-- Rollback: Remove full-text search functionality
DROP FUNCTION IF EXISTS jobs_search_trigger();
DROP TRIGGER IF EXISTS jobs_search_update ON jobs;
ALTER TABLE jobs DROP COLUMN IF EXISTS search_vector;
DROP INDEX IF EXISTS jobs_search_idx; error
|
Beta Was this translation helpful? Give feedback.
Answered by
mfridman
Feb 18, 2025
Replies: 1 comment 3 replies
-
You need to wrap those complex statements, this might help: https://pressly.github.io/goose/documentation/annotations/#complex-statements e.g., [...]
-- +goose statementbegin
CREATE OR REPLACE FUNCTION jobs_search_trigger() RETURNS TRIGGER LANGUAGE plpgsql AS $$
DECLARE
title_text text := COALESCE(NEW.title, '');
description_text text := COALESCE(NEW.description, '');
company_text text := COALESCE(NEW.company, '');
location_text text := COALESCE(NEW.location, '');
BEGIN
NEW.search_vector := (
setweight(to_tsvector('english', title_text), 'A') ||
setweight(to_tsvector('english', description_text), 'B') ||
setweight(to_tsvector('english', company_text), 'C') ||
setweight(to_tsvector('english', location_text), 'D')
);
RETURN NEW;
END;
$$;
-- +goose statementend
[...] |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
hackertron
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You need to wrap those complex statements, this might help:
https://pressly.github.io/goose/documentation/annotations/#complex-statements
e.g.,