Skip to content

Commit 936c770

Browse files
author
Carlos Silva
committed
Fix #78, add better tests for multiple schemas, and allow change type to receive schema
1 parent d2bef22 commit 936c770

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

lib/torque/postgresql/adapter/schema_statements.rb

+14-2
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ def drop_type(name, options = {})
3333
end
3434

3535
# Renames a type.
36-
def rename_type(type_name, new_name)
36+
def rename_type(type_name, new_name, options = {})
3737
execute <<-SQL.squish
38-
ALTER TYPE #{quote_type_name(type_name)}
38+
ALTER TYPE #{quote_type_name(type_name, options[:schema])}
3939
RENAME TO #{Quoting::Name.new(nil, new_name.to_s).quoted}
4040
SQL
4141
end
@@ -102,6 +102,18 @@ def create_table(table_name, **options, &block)
102102
super table_name, **options, &block
103103
end
104104

105+
# Simply add the schema to the table name when changing a table
106+
def change_table(table_name, **options)
107+
table_name = "#{options[:schema]}.#{table_name}" if options[:schema].present?
108+
super table_name, **options
109+
end
110+
111+
# Simply add the schema to the table name when dropping a table
112+
def drop_table(table_name, **options)
113+
table_name = "#{options[:schema]}.#{table_name}" if options[:schema].present?
114+
super table_name, **options
115+
end
116+
105117
# Add the schema option when extracting table options
106118
def table_options(table_name)
107119
parts = table_name.split('.').reverse

lib/torque/postgresql/table_name.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ def schema
1212
return @schema if defined?(@schema)
1313

1414
@schema = ([@klass] + @klass.module_parents[0..-2]).find do |klass|
15-
next unless klass.respond_to?(:schema)
16-
break klass.schema
15+
next unless klass.respond_to?(:schema) && !(value = klass.schema).nil?
16+
break value
1717
end
1818
end
1919

lib/torque/postgresql/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
module Torque
44
module PostgreSQL
5-
VERSION = '2.4.1'
5+
VERSION = '2.4.2'
66
end
77
end

spec/tests/schema_spec.rb

+33
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,26 @@
4040
connection.schemas_whitelist.push('legacy')
4141
expect(connection.schema_exists?(:legacy)).to be_truthy
4242
end
43+
44+
context 'reverting' do
45+
let(:migration) { ActiveRecord::Migration::Current.new('Testing') }
46+
47+
before { connection.create_schema(:legacy) }
48+
49+
it 'reverts the creation of a schema' do
50+
expect(connection.schema_exists?(:legacy, filtered: false)).to be_truthy
51+
migration.revert { migration.connection.create_schema(:legacy) }
52+
expect(connection.schema_exists?(:legacy, filtered: false)).to be_falsey
53+
end
54+
55+
it 'reverts the creation of a table' do
56+
connection.create_table(:users, schema: :legacy) { |t| t.string(:name) }
57+
58+
expect(connection.table_exists?('legacy.users')).to be_truthy
59+
migration.revert { migration.connection.create_table(:users, schema: :legacy) }
60+
expect(connection.table_exists?('legacy.users')).to be_falsey
61+
end
62+
end
4363
end
4464

4565
context 'on schema' do
@@ -86,16 +106,29 @@
86106

87107
context 'on relation' do
88108
let(:model) { Internal::User }
109+
let(:table_name) { Torque::PostgreSQL::TableName.new(model, 'users') }
89110

90111
it 'adds the schema to the query' do
112+
model.reset_table_name
113+
expect(table_name.to_s).to eq('internal.users')
91114
expect(model.all.to_sql).to match(/FROM "internal"."users"/)
92115
end
93116

94117
it 'can load the schema from the module' do
95118
allow(Internal).to receive(:schema).and_return('internal')
96119
allow(model).to receive(:schema).and_return(nil)
97120

121+
model.reset_table_name
122+
expect(table_name.to_s).to eq('internal.users')
98123
expect(model.all.to_sql).to match(/FROM "internal"."users"/)
99124
end
125+
126+
it 'does not change anything if the model has not configured a schema' do
127+
allow(model).to receive(:schema).and_return(nil)
128+
129+
model.reset_table_name
130+
expect(table_name.to_s).to eq('users')
131+
expect(model.all.to_sql).to match(/FROM "users"/)
132+
end
100133
end
101134
end

0 commit comments

Comments
 (0)