Skip to content

Commit

Permalink
Fix #78, add better tests for multiple schemas, and allow change type…
Browse files Browse the repository at this point in the history
… to receive schema
  • Loading branch information
Carlos Silva committed Jan 19, 2023
1 parent d2bef22 commit 936c770
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
16 changes: 14 additions & 2 deletions lib/torque/postgresql/adapter/schema_statements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/torque/postgresql/table_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/torque/postgresql/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Torque
module PostgreSQL
VERSION = '2.4.1'
VERSION = '2.4.2'
end
end
33 changes: 33 additions & 0 deletions spec/tests/schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -86,16 +106,29 @@

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

it 'can load the schema from the module' do
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

0 comments on commit 936c770

Please sign in to comment.