From e4b9c365c099f9d38ea27f4a418d56209e30d9e7 Mon Sep 17 00:00:00 2001 From: Attila Horvath Date: Wed, 12 Jun 2024 11:55:10 +0100 Subject: [PATCH] Fix working with separate schemas --- README.md | 2 +- .../postgres_enum/postgresql_adapter.rb | 12 ++++-- spec/active_record/postgres_enum_spec.rb | 43 +++++++++++++++++++ 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index dc00682..fa8a99c 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,7 @@ rename_enum_value :mood, "pensive", "wistful" ## Development -After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. +After checking out the repo, run `bin/setup` to install dependencies. Then, run `rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). diff --git a/lib/active_record/postgres_enum/postgresql_adapter.rb b/lib/active_record/postgres_enum/postgresql_adapter.rb index db94514..4dc24e1 100644 --- a/lib/active_record/postgres_enum/postgresql_adapter.rb +++ b/lib/active_record/postgres_enum/postgresql_adapter.rb @@ -10,8 +10,10 @@ module PostgreSQLAdapter t.typtype, array_to_string(array_agg(e.enumlabel ORDER BY e.enumsortorder), '\t\t', '') as enumlabels FROM pg_type t + INNER JOIN pg_namespace n ON n.oid = t.typnamespace LEFT JOIN pg_enum e ON e.enumtypid = t.oid - WHERE typtype = 'e' + WHERE t.typtype = 'e' + AND n.nspname = ANY(current_schemas(true)) GROUP BY t.OID, t.typname, t.typtype ORDER BY t.typname SQL @@ -58,8 +60,12 @@ def add_enum_value(name, value, after: nil, before: nil, if_not_exists: nil) def remove_enum_value(name, value) sql = %{ DELETE FROM pg_enum - WHERE enumlabel=#{quote value} - AND enumtypid=(SELECT oid FROM pg_type WHERE typname='#{name}') + WHERE enumlabel = #{quote value} + AND enumtypid IN (SELECT t.oid + FROM pg_type t + INNER JOIN pg_namespace n ON n.oid = t.typnamespace + WHERE t.typname = '#{name}' + AND n.nspname = ANY(current_schemas(true))) } execute sql end diff --git a/spec/active_record/postgres_enum_spec.rb b/spec/active_record/postgres_enum_spec.rb index d041bb2..f85f177 100644 --- a/spec/active_record/postgres_enum_spec.rb +++ b/spec/active_record/postgres_enum_spec.rb @@ -197,5 +197,48 @@ expect(col.type).to eq :enum expect(col.sql_type).to eq "foo" end + + context "only affects the selected schema" do + before do + @default_schema = connection.schema_search_path + + connection.execute "CREATE SCHEMA IF NOT EXISTS myschema" + connection.schema_search_path = "myschema" + + expect { connection.create_enum(:foo, %w[a1 a2]) }.to_not raise_error + end + + after do + connection.execute "DROP SCHEMA myschema CASCADE" + connection.schema_search_path = @default_schema + + expect(connection.enum_types[:foo]).to eq %w[a1 a2] + end + + it "drops only the separate enum" do + expect { connection.drop_enum(:foo) }.to_not raise_error + expect(connection.enum_types[:foo]).to be_nil + end + + it "renames only the separate enum" do + expect { connection.rename_enum(:foo, :bar) }.to_not raise_error + expect(connection.enum_types[:bar]).to eq %w[a1 a2] + end + + it "adds an enum value to the separate enum only" do + expect { connection.add_enum_value(:foo, "a3") }.to_not raise_error + expect(connection.enum_types[:foo]).to eq %w[a1 a2 a3] + end + + it "removes an enum value from the separate enum only" do + expect { connection.remove_enum_value(:foo, "a2") }.to_not raise_error + expect(connection.enum_types[:foo]).to eq %w[a1] + end + + it "renames an enum value in the separate enum only" do + expect { connection.rename_enum_value(:foo, "a2", "b2") }.to_not raise_error + expect(connection.enum_types[:foo]).to eq %w[a1 b2] + end + end end end