From ce7c86263a692446546a35d0a99c116aa1a60831 Mon Sep 17 00:00:00 2001 From: Carlos Silva Date: Sun, 3 Apr 2022 21:23:37 -0300 Subject: [PATCH] Fix quouting and loading enum types --- .circleci/config.yml | 1 + .../postgresql/adapter/database_statements.rb | 11 ++++++++++- lib/torque/postgresql/adapter/quoting.rb | 5 ++--- spec/spec_helper.rb | 4 ---- spec/tests/arel_spec.rb | 13 ++++++++++++- 5 files changed, 25 insertions(+), 9 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 9bc35c2..84b708c 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -4,6 +4,7 @@ orbs: jobs: test: + parallelism: 3 parameters: ruby-version: type: string diff --git a/lib/torque/postgresql/adapter/database_statements.rb b/lib/torque/postgresql/adapter/database_statements.rb index 2bd4a66..536df32 100644 --- a/lib/torque/postgresql/adapter/database_statements.rb +++ b/lib/torque/postgresql/adapter/database_statements.rb @@ -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 @@ -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, diff --git a/lib/torque/postgresql/adapter/quoting.rb b/lib/torque/postgresql/adapter/quoting.rb index 5812b94..ef4de0e 100644 --- a/lib/torque/postgresql/adapter/quoting.rb +++ b/lib/torque/postgresql/adapter/quoting.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index bab9017..2b6488c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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 diff --git a/spec/tests/arel_spec.rb b/spec/tests/arel_spec.rb index 4d11e0c..4821721 100644 --- a/spec/tests/arel_spec.rb +++ b/spec/tests/arel_spec.rb @@ -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)