From f09bb33bb534fe2e729e292c8ac6b0e2ffdcea2a Mon Sep 17 00:00:00 2001 From: Doug Cole Date: Sat, 31 Mar 2012 13:38:11 -0700 Subject: add use_returning as a postgresql connection config --- .../adapters/postgresql/postgresql_adapter_test.rb | 9 +++++++ .../test/schema/postgresql_specific_schema.rb | 29 ++++++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb b/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb index a71d0bb848..19da6163b5 100644 --- a/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb +++ b/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb @@ -49,6 +49,15 @@ module ActiveRecord assert_equal expect, id end + def test_insert_sql_with_returning_disabled + @connection.use_returning = false + id = @connection.insert_sql("insert into postgresql_partitioned_table_parent (number) VALUES (1)") + expect = @connection.query('select max(id) from postgresql_partitioned_table_parent').first.first + assert_equal expect, id + ensure + @connection.use_returning = true + end + def test_serial_sequence assert_equal 'public.accounts_id_seq', @connection.serial_sequence('accounts', 'id') diff --git a/activerecord/test/schema/postgresql_specific_schema.rb b/activerecord/test/schema/postgresql_specific_schema.rb index 25b416a906..84228cdd0a 100644 --- a/activerecord/test/schema/postgresql_specific_schema.rb +++ b/activerecord/test/schema/postgresql_specific_schema.rb @@ -1,8 +1,8 @@ ActiveRecord::Schema.define do %w(postgresql_tsvectors postgresql_hstores postgresql_arrays postgresql_moneys postgresql_numbers postgresql_times postgresql_network_addresses postgresql_bit_strings - postgresql_oids postgresql_xml_data_type defaults geometrics postgresql_timestamp_with_zones).each do |table_name| - execute "DROP TABLE IF EXISTS #{quote_table_name table_name}" + postgresql_oids postgresql_xml_data_type defaults geometrics postgresql_timestamp_with_zones postgresql_partitioned_table postgresql_partitioned_table_parent).each do |table_name| + execute "DROP TABLE IF EXISTS #{quote_table_name table_name}" end execute 'DROP SEQUENCE IF EXISTS companies_nonstd_seq CASCADE' @@ -10,6 +10,8 @@ ActiveRecord::Schema.define do execute "ALTER TABLE companies ALTER COLUMN id SET DEFAULT nextval('companies_nonstd_seq')" execute 'DROP SEQUENCE IF EXISTS companies_id_seq' + execute 'DROP FUNCTION IF EXISTS partitioned_insert_trigger()' + %w(accounts_id_seq developers_id_seq projects_id_seq topics_id_seq customers_id_seq orders_id_seq).each do |seq_name| execute "SELECT setval('#{seq_name}', 100)" end @@ -125,6 +127,29 @@ _SQL ); _SQL + execute <<_SQL + CREATE TABLE postgresql_partitioned_table_parent ( + id SERIAL PRIMARY KEY, + number integer + ); + CREATE TABLE postgresql_partitioned_table ( ) + INHERITS (postgresql_partitioned_table_parent); + + CREATE OR REPLACE FUNCTION partitioned_insert_trigger() + RETURNS TRIGGER AS $$ + BEGIN + INSERT INTO postgresql_partitioned_table VALUES (NEW.*); + RETURN NULL; + END; + $$ + LANGUAGE plpgsql; + + CREATE TRIGGER insert_partitioning_trigger + BEFORE INSERT ON postgresql_partitioned_table_parent + FOR EACH ROW EXECUTE PROCEDURE partitioned_insert_trigger(); +_SQL + + begin execute <<_SQL CREATE TABLE postgresql_xml_data_type ( -- cgit v1.2.3 From 945c53e7b54eff25e1cc1cf81ba6c96cce6664de Mon Sep 17 00:00:00 2001 From: Doug Cole Date: Sat, 31 Mar 2012 18:41:24 -0700 Subject: improve test coverage --- .../adapters/postgresql/postgresql_adapter_test.rb | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb b/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb index 19da6163b5..1452911974 100644 --- a/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb +++ b/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb @@ -58,6 +58,32 @@ module ActiveRecord @connection.use_returning = true end + def test_exec_insert_with_returning_disabled + @connection.use_returning = false + result = @connection.exec_insert("insert into postgresql_partitioned_table_parent (number) VALUES (1)", nil, [], 'id', 'postgresql_partitioned_table_parent_id_seq') + expect = @connection.query('select max(id) from postgresql_partitioned_table_parent').first.first + assert_equal expect, result.rows.first.first + ensure + @connection.use_returning = true + end + + def test_exec_insert_with_returning_disabled_and_no_sequence_name_given + @connection.use_returning = false + result = @connection.exec_insert("insert into postgresql_partitioned_table_parent (number) VALUES (1)", nil, [], 'id') + expect = @connection.query('select max(id) from postgresql_partitioned_table_parent').first.first + assert_equal expect, result.rows.first.first + ensure + @connection.use_returning = true + end + + def test_sql_for_insert_with_returning_disabled + @connection.use_returning = false + result = @connection.sql_for_insert('sql', nil, nil, nil, 'binds') + assert_equal ['sql', 'binds'], result + ensure + @connection.use_returning = true + end + def test_serial_sequence assert_equal 'public.accounts_id_seq', @connection.serial_sequence('accounts', 'id') -- cgit v1.2.3 From 3a0d08163a76427611df0c109a5159daaf5b51bf Mon Sep 17 00:00:00 2001 From: Doug Cole Date: Sun, 1 Apr 2012 15:23:06 -0700 Subject: pick better names and add a little documentation --- .../cases/adapters/postgresql/postgresql_adapter_test.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb b/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb index 1452911974..3073b9f011 100644 --- a/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb +++ b/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb @@ -50,38 +50,38 @@ module ActiveRecord end def test_insert_sql_with_returning_disabled - @connection.use_returning = false + @connection.disable_insert_returning! id = @connection.insert_sql("insert into postgresql_partitioned_table_parent (number) VALUES (1)") expect = @connection.query('select max(id) from postgresql_partitioned_table_parent').first.first assert_equal expect, id ensure - @connection.use_returning = true + @connection.enable_insert_returning! end def test_exec_insert_with_returning_disabled - @connection.use_returning = false + @connection.disable_insert_returning! result = @connection.exec_insert("insert into postgresql_partitioned_table_parent (number) VALUES (1)", nil, [], 'id', 'postgresql_partitioned_table_parent_id_seq') expect = @connection.query('select max(id) from postgresql_partitioned_table_parent').first.first assert_equal expect, result.rows.first.first ensure - @connection.use_returning = true + @connection.enable_insert_returning! end def test_exec_insert_with_returning_disabled_and_no_sequence_name_given - @connection.use_returning = false + @connection.disable_insert_returning! result = @connection.exec_insert("insert into postgresql_partitioned_table_parent (number) VALUES (1)", nil, [], 'id') expect = @connection.query('select max(id) from postgresql_partitioned_table_parent').first.first assert_equal expect, result.rows.first.first ensure - @connection.use_returning = true + @connection.enable_insert_returning! end def test_sql_for_insert_with_returning_disabled - @connection.use_returning = false + @connection.disable_insert_returning! result = @connection.sql_for_insert('sql', nil, nil, nil, 'binds') assert_equal ['sql', 'binds'], result ensure - @connection.use_returning = true + @connection.enable_insert_returning! end def test_serial_sequence -- cgit v1.2.3 From cd6ddc865a68d126a845d03337e1c7f775588db2 Mon Sep 17 00:00:00 2001 From: Doug Cole Date: Sat, 7 Apr 2012 18:23:26 -0700 Subject: refactor configuration of insert_returning --- .../adapters/postgresql/postgresql_adapter_test.rb | 34 ++++++++++------------ 1 file changed, 15 insertions(+), 19 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb b/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb index 3073b9f011..92e31a3e44 100644 --- a/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb +++ b/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb @@ -50,38 +50,30 @@ module ActiveRecord end def test_insert_sql_with_returning_disabled - @connection.disable_insert_returning! - id = @connection.insert_sql("insert into postgresql_partitioned_table_parent (number) VALUES (1)") - expect = @connection.query('select max(id) from postgresql_partitioned_table_parent').first.first + connection = connection_without_insert_returning + id = connection.insert_sql("insert into postgresql_partitioned_table_parent (number) VALUES (1)") + expect = connection.query('select max(id) from postgresql_partitioned_table_parent').first.first assert_equal expect, id - ensure - @connection.enable_insert_returning! end def test_exec_insert_with_returning_disabled - @connection.disable_insert_returning! - result = @connection.exec_insert("insert into postgresql_partitioned_table_parent (number) VALUES (1)", nil, [], 'id', 'postgresql_partitioned_table_parent_id_seq') - expect = @connection.query('select max(id) from postgresql_partitioned_table_parent').first.first + connection = connection_without_insert_returning + result = connection.exec_insert("insert into postgresql_partitioned_table_parent (number) VALUES (1)", nil, [], 'id', 'postgresql_partitioned_table_parent_id_seq') + expect = connection.query('select max(id) from postgresql_partitioned_table_parent').first.first assert_equal expect, result.rows.first.first - ensure - @connection.enable_insert_returning! end def test_exec_insert_with_returning_disabled_and_no_sequence_name_given - @connection.disable_insert_returning! - result = @connection.exec_insert("insert into postgresql_partitioned_table_parent (number) VALUES (1)", nil, [], 'id') - expect = @connection.query('select max(id) from postgresql_partitioned_table_parent').first.first + connection = connection_without_insert_returning + result = connection.exec_insert("insert into postgresql_partitioned_table_parent (number) VALUES (1)", nil, [], 'id') + expect = connection.query('select max(id) from postgresql_partitioned_table_parent').first.first assert_equal expect, result.rows.first.first - ensure - @connection.enable_insert_returning! end def test_sql_for_insert_with_returning_disabled - @connection.disable_insert_returning! - result = @connection.sql_for_insert('sql', nil, nil, nil, 'binds') + connection = connection_without_insert_returning + result = connection.sql_for_insert('sql', nil, nil, nil, 'binds') assert_equal ['sql', 'binds'], result - ensure - @connection.enable_insert_returning! end def test_serial_sequence @@ -239,6 +231,10 @@ module ActiveRecord ctx.exec_insert(sql, 'SQL', binds) end + + def connection_without_insert_returning + ActiveRecord::Base.postgresql_connection(ActiveRecord::Base.configurations['arunit'].merge(:insert_returning => false)) + end end end end -- cgit v1.2.3