Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix conflict with existing add_foreign_key method (#1) #6

Open
wants to merge 1 commit into
base: rails-4-2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/monkey_patch_postgres.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def drop_schema(name, options = {})
# @param [String] referenced_table_name the name of the table referenced by the foreign key
# @param [String] referenced_field_name (:id) the name of the column referenced by the foreign key
# @return [optional] undefined
def add_foreign_key(referencing_table_name, referencing_field_name, referenced_table_name, referenced_field_name = :id)
def add_foreign_key_constraint(referencing_table_name, referencing_field_name, referenced_table_name, referenced_field_name = :id)
execute("ALTER TABLE #{referencing_table_name} add foreign key (#{referencing_field_name}) references #{referenced_table_name}(#{referenced_field_name})")
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/monkey_patch_redshift.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def drop_schema(name, options = {})
# @param [String] referenced_table_name the name of the table referenced by the foreign key
# @param [String] referenced_field_name (:id) the name of the column referenced by the foreign key
# @return [optional] undefined
def add_foreign_key(referencing_table_name, referencing_field_name, referenced_table_name, referenced_field_name = :id)
def add_foreign_key_constraint(referencing_table_name, referencing_field_name, referenced_table_name, referenced_field_name = :id)
execute("ALTER TABLE #{referencing_table_name} add foreign key (#{referencing_field_name}) references #{referenced_table_name}(#{referenced_field_name})")
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/partitioned/partitioned_base/redshift_sql_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def unique_index_name(name, *partition_key_values)
#
def add_references_to_partition_table(*partition_key_values)
configurator.foreign_keys(*partition_key_values).each do |foreign_key|
add_foreign_key(partition_table_name(*partition_key_values),
add_foreign_key_constraint(partition_table_name(*partition_key_values),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style comment: Would you mind fixing the indents of rest of the arguments being passed in in the next few lines?

foreign_key.referencing_field,
foreign_key.referenced_table,
foreign_key.referenced_field)
Expand Down Expand Up @@ -267,7 +267,7 @@ def add_references_to_partition_table(*partition_key_values)

extend Forwardable
def_delegators :parent_table_class, :connection, :find_by_sql, :transaction, :find, :configurator
def_delegators :connection, :execute, :add_index, :remove_index, :create_schema, :drop_schema, :add_foreign_key,
def_delegators :connection, :execute, :add_index, :remove_index, :create_schema, :drop_schema, :add_foreign_key_constraint,
:create_table, :drop_table
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/partitioned/partitioned_base/sql_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def unique_index_name(name, *partition_key_values)
#
def add_references_to_partition_table(*partition_key_values)
configurator.foreign_keys(*partition_key_values).each do |foreign_key|
add_foreign_key(partition_table_name(*partition_key_values),
add_foreign_key_constraint(partition_table_name(*partition_key_values),
foreign_key.referencing_field,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above, can you fix the indents here?

foreign_key.referenced_table,
foreign_key.referenced_field)
Expand Down Expand Up @@ -326,7 +326,7 @@ def add_references_to_partition_table(*partition_key_values)

extend Forwardable
def_delegators :parent_table_class, :connection, :find_by_sql, :transaction, :find, :configurator
def_delegators :connection, :execute, :add_index, :remove_index, :create_schema, :drop_schema, :add_foreign_key,
def_delegators :connection, :execute, :add_index, :remove_index, :create_schema, :drop_schema, :add_foreign_key_constraint,
:create_table, :drop_table
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/monkey_patch_postgres_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class Employee < ActiveRecord::Base

end # drop_schema

describe "add_foreign_key" do
describe "add_foreign_key_constraint" do

it "added foreign key constraint" do
create_new_schema
Expand All @@ -148,15 +148,15 @@ class Employee < ActiveRecord::Base
id serial not null primary key
);
SQL
ActiveRecord::Base.connection.add_foreign_key("employees_partitions.temp", :company_id, "companies", :id)
ActiveRecord::Base.connection.add_foreign_key_constraint("employees_partitions.temp", :company_id, "companies", :id)
result = ActiveRecord::Base.connection.execute <<-SQL
SELECT constraint_type FROM information_schema.table_constraints
WHERE table_name = 'temp' AND constraint_name = 'temp_company_id_fkey';
SQL
expect(result.values.first).to eq ["FOREIGN KEY"]
end

end # add_foreign_key
end # add_foreign_key_constraint

end # PostgreSQLAdapter

Expand Down