Skip to content

Commit ce7c862

Browse files
author
Carlos Silva
committed
Fix quouting and loading enum types
1 parent 2e9f817 commit ce7c862

File tree

5 files changed

+25
-9
lines changed

5 files changed

+25
-9
lines changed

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ orbs:
44

55
jobs:
66
test:
7+
parallelism: 3
78
parameters:
89
ruby-version:
910
type: string

lib/torque/postgresql/adapter/database_statements.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ def configure_connection
3434
execute("SET SESSION IntervalStyle TO 'iso_8601'", 'SCHEMA')
3535
end
3636

37+
# Since enums create new types, type map needs to be rebooted to include
38+
# the new ones, both normal and array one
39+
def create_enum(name, *)
40+
super
41+
42+
oid = query_value("SELECT #{quote(name)}::regtype::oid", "SCHEMA").to_i
43+
load_additional_types([oid])
44+
end
45+
3746
# Change some of the types being mapped
3847
def initialize_type_map(m = type_map)
3948
super
@@ -54,7 +63,7 @@ def load_additional_types(oids = nil)
5463

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

5968
query = <<-SQL
6069
SELECT a.typelem AS oid, t.typname, t.typelem,

lib/torque/postgresql/adapter/quoting.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,13 @@ def quote_default_expression(value, column)
2626

2727
type =
2828
if column.is_a?(ColumnDefinition) && column.options.try(:[], :array)
29+
# This is the general way
2930
lookup_cast_type(column.sql_type)
3031
elsif column.is_a?(Column) && column.array?
32+
# When using +change_column_default+
3133
lookup_cast_type_from_column(column)
3234
end
3335

34-
puts column.inspect
35-
puts value.inspect
36-
puts type.inspect
3736
type.nil? ? super : quote(type.serialize(value.to_a))
3837
end
3938
end

spec/spec_helper.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@
4646
DatabaseCleaner.strategy = :transaction
4747
end
4848

49-
config.before(:each, js: true) do
50-
DatabaseCleaner.strategy = :truncation
51-
end
52-
5349
config.before(:each) do
5450
DatabaseCleaner.start
5551
end

spec/tests/arel_spec.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,18 @@
6969
expect(Author.new.tag_ids).to eq([])
7070
end
7171

72-
it 'works with an array with enum values' do
72+
it 'works with an array with enum values for a new enum' do
73+
value = ['a', 'b']
74+
75+
expect do
76+
connection.create_enum(:samples, %i[a b c d])
77+
connection.add_column(:authors, :samples, :enum, enum_type: :samples, array: true, default: value)
78+
end.not_to raise_error
79+
80+
expect(Author.new.samples).to eq(value)
81+
end
82+
83+
it 'works with an array with enum values for an existing enum' do
7384
value = ['visitor', 'assistant']
7485
expect { connection.add_column(:authors, :roles, :enum, enum_type: :roles, array: true, default: value) }.not_to raise_error
7586
expect(Author.new.roles).to eq(value)

0 commit comments

Comments
 (0)