diff --git a/lib/torque/postgresql/adapter/schema_statements.rb b/lib/torque/postgresql/adapter/schema_statements.rb index b0610df..96ebe5f 100644 --- a/lib/torque/postgresql/adapter/schema_statements.rb +++ b/lib/torque/postgresql/adapter/schema_statements.rb @@ -33,9 +33,9 @@ def drop_type(name, options = {}) end # Renames a type. - def rename_type(type_name, new_name) + def rename_type(type_name, new_name, options = {}) execute <<-SQL.squish - ALTER TYPE #{quote_type_name(type_name)} + ALTER TYPE #{quote_type_name(type_name, options[:schema])} RENAME TO #{Quoting::Name.new(nil, new_name.to_s).quoted} SQL end @@ -102,6 +102,18 @@ def create_table(table_name, **options, &block) super table_name, **options, &block end + # Simply add the schema to the table name when changing a table + def change_table(table_name, **options) + table_name = "#{options[:schema]}.#{table_name}" if options[:schema].present? + super table_name, **options + end + + # Simply add the schema to the table name when dropping a table + def drop_table(table_name, **options) + table_name = "#{options[:schema]}.#{table_name}" if options[:schema].present? + super table_name, **options + end + # Add the schema option when extracting table options def table_options(table_name) parts = table_name.split('.').reverse diff --git a/lib/torque/postgresql/table_name.rb b/lib/torque/postgresql/table_name.rb index 2bb27f3..0a4e4e8 100644 --- a/lib/torque/postgresql/table_name.rb +++ b/lib/torque/postgresql/table_name.rb @@ -12,8 +12,8 @@ def schema return @schema if defined?(@schema) @schema = ([@klass] + @klass.module_parents[0..-2]).find do |klass| - next unless klass.respond_to?(:schema) - break klass.schema + next unless klass.respond_to?(:schema) && !(value = klass.schema).nil? + break value end end diff --git a/lib/torque/postgresql/version.rb b/lib/torque/postgresql/version.rb index 80e7d92..77c9b97 100644 --- a/lib/torque/postgresql/version.rb +++ b/lib/torque/postgresql/version.rb @@ -2,6 +2,6 @@ module Torque module PostgreSQL - VERSION = '2.4.1' + VERSION = '2.4.2' end end diff --git a/spec/tests/schema_spec.rb b/spec/tests/schema_spec.rb index 3bf9b0a..bab6454 100644 --- a/spec/tests/schema_spec.rb +++ b/spec/tests/schema_spec.rb @@ -40,6 +40,26 @@ connection.schemas_whitelist.push('legacy') expect(connection.schema_exists?(:legacy)).to be_truthy end + + context 'reverting' do + let(:migration) { ActiveRecord::Migration::Current.new('Testing') } + + before { connection.create_schema(:legacy) } + + it 'reverts the creation of a schema' do + expect(connection.schema_exists?(:legacy, filtered: false)).to be_truthy + migration.revert { migration.connection.create_schema(:legacy) } + expect(connection.schema_exists?(:legacy, filtered: false)).to be_falsey + end + + it 'reverts the creation of a table' do + connection.create_table(:users, schema: :legacy) { |t| t.string(:name) } + + expect(connection.table_exists?('legacy.users')).to be_truthy + migration.revert { migration.connection.create_table(:users, schema: :legacy) } + expect(connection.table_exists?('legacy.users')).to be_falsey + end + end end context 'on schema' do @@ -86,8 +106,11 @@ context 'on relation' do let(:model) { Internal::User } + let(:table_name) { Torque::PostgreSQL::TableName.new(model, 'users') } it 'adds the schema to the query' do + model.reset_table_name + expect(table_name.to_s).to eq('internal.users') expect(model.all.to_sql).to match(/FROM "internal"."users"/) end @@ -95,7 +118,17 @@ allow(Internal).to receive(:schema).and_return('internal') allow(model).to receive(:schema).and_return(nil) + model.reset_table_name + expect(table_name.to_s).to eq('internal.users') expect(model.all.to_sql).to match(/FROM "internal"."users"/) end + + it 'does not change anything if the model has not configured a schema' do + allow(model).to receive(:schema).and_return(nil) + + model.reset_table_name + expect(table_name.to_s).to eq('users') + expect(model.all.to_sql).to match(/FROM "users"/) + end end end