Skip to content

Commit

Permalink
Fix quouting and loading enum types
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Silva committed Apr 4, 2022
1 parent 2e9f817 commit ce7c862
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ orbs:

jobs:
test:
parallelism: 3
parameters:
ruby-version:
type: string
Expand Down
11 changes: 10 additions & 1 deletion lib/torque/postgresql/adapter/database_statements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ def configure_connection
execute("SET SESSION IntervalStyle TO 'iso_8601'", 'SCHEMA')
end

# Since enums create new types, type map needs to be rebooted to include
# the new ones, both normal and array one
def create_enum(name, *)
super

oid = query_value("SELECT #{quote(name)}::regtype::oid", "SCHEMA").to_i
load_additional_types([oid])
end

# Change some of the types being mapped
def initialize_type_map(m = type_map)
super
Expand All @@ -54,7 +63,7 @@ def load_additional_types(oids = nil)

# Add the composite types to be loaded too.
def torque_load_additional_types(oids = nil)
filter = "AND a.typelem::integer IN (%s)" % oids.join(", ") if oids
filter = ("AND a.typelem::integer IN (%s)" % oids.join(', ')) if oids

query = <<-SQL
SELECT a.typelem AS oid, t.typname, t.typelem,
Expand Down
5 changes: 2 additions & 3 deletions lib/torque/postgresql/adapter/quoting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@ def quote_default_expression(value, column)

type =
if column.is_a?(ColumnDefinition) && column.options.try(:[], :array)
# This is the general way
lookup_cast_type(column.sql_type)
elsif column.is_a?(Column) && column.array?
# When using +change_column_default+
lookup_cast_type_from_column(column)
end

puts column.inspect
puts value.inspect
puts type.inspect
type.nil? ? super : quote(type.serialize(value.to_a))
end
end
Expand Down
4 changes: 0 additions & 4 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@
DatabaseCleaner.strategy = :transaction
end

config.before(:each, js: true) do
DatabaseCleaner.strategy = :truncation
end

config.before(:each) do
DatabaseCleaner.start
end
Expand Down
13 changes: 12 additions & 1 deletion spec/tests/arel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,18 @@
expect(Author.new.tag_ids).to eq([])
end

it 'works with an array with enum values' do
it 'works with an array with enum values for a new enum' do
value = ['a', 'b']

expect do
connection.create_enum(:samples, %i[a b c d])
connection.add_column(:authors, :samples, :enum, enum_type: :samples, array: true, default: value)
end.not_to raise_error

expect(Author.new.samples).to eq(value)
end

it 'works with an array with enum values for an existing enum' do
value = ['visitor', 'assistant']
expect { connection.add_column(:authors, :roles, :enum, enum_type: :roles, array: true, default: value) }.not_to raise_error
expect(Author.new.roles).to eq(value)
Expand Down

0 comments on commit ce7c862

Please sign in to comment.