Skip to content

Commit

Permalink
Merge pull request #62 from attilahorvath/fix-separate-schemas
Browse files Browse the repository at this point in the history
Fix working with separate schemas
  • Loading branch information
bibendi committed Jun 13, 2024
2 parents f758e57 + e4b9c36 commit b9c92a8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
12 changes: 9 additions & 3 deletions lib/active_record/postgres_enum/postgresql_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
43 changes: 43 additions & 0 deletions spec/active_record/postgres_enum_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b9c92a8

Please sign in to comment.