Search code examples
sqlruby-on-railsdatabasepostgresqlrails-migrations

Remove SQL code from Rails schema file


So a while ago I was working with triggers and added a few to one of my tables for insert and update events and thus they went to my schema.rb but then I found a way not to use triggers and removed them from the dabatase console but their sql code obviously hadn't been deleted from the schema file, so now I have something like that in it:

  execute(<<-TRIGGERSQL)
CREATE OR REPLACE FUNCTION public.arguments_vector_update()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN
    IF TG_OP = 'INSERT' THEN
        new.tsv_body = to_tsvector('pg_catalog.simple', COALESCE(NEW.text, ''));
    END IF;
    IF TG_OP = 'UPDATE' THEN
        IF NEW.text <> OLD.text THEN
            new.tsv_body = to_tsvector('pg_catalog.simple', COALESCE(NEW.text, ''));
        END IF;
    END IF;
    RETURN NEW;
END
$function$
  TRIGGERSQL

and I think I've deleted the migration file too -_-

I could probably add a new migration with the code to remove the trigers but it'd make the schema file even messier, so I wonder if there is another way to remove this code from the schema file ?

I am using PSQL, if it's important.


Solution

  • rake -T in a Rails app will have this to say:

    rake db:schema:dump # Create a db/schema.rb file that is portable against any DB supported by AR
    

    So you can rake db:schema:dump to generate a fresh db/schema.rb file any time you need one.