[Ovirt-devel] [PATCH 2/3] enhance & fix foreigner plugin to manage correctly named foreign key
Mohammed Morsi
mmorsi at redhat.com
Wed Oct 14 19:14:40 UTC 2009
A couple things. First I notice alot of whitespace changes, ideally
these wouldn't be here, or even be in their own formatting patch, as
they kinda clutter up the actual content changes.
Second I was just wondering if you could provide a little more detail as
to what this patch does as it's not immediately apparent looking at it.
If this is a known foreigner plugin fix could you link the thread of
discussion detailing it, or if we need it for our project perhaps a
blurb below the patch summary as to the changes needed to foreigner to
adapt it to ovirt.
Thanks alot,
-Mo
Michel Loiseleur wrote:
> Signed-off-by: Michel Loiseleur <mloiseleur at linagora.com>
> ---
> .../abstract/schema_definitions.rb | 26 ++++++++++----------
> .../lib/foreigner/connection_adapters/sql_2003.rb | 21 +++++++--------
> 2 files changed, 23 insertions(+), 24 deletions(-)
>
> diff --git a/src/vendor/plugins/foreigner/lib/foreigner/connection_adapters/abstract/schema_definitions.rb b/src/vendor/plugins/foreigner/lib/foreigner/connection_adapters/abstract/schema_definitions.rb
> index e417c5b..f31fc2b 100644
> --- a/src/vendor/plugins/foreigner/lib/foreigner/connection_adapters/abstract/schema_definitions.rb
> +++ b/src/vendor/plugins/foreigner/lib/foreigner/connection_adapters/abstract/schema_definitions.rb
> @@ -8,17 +8,17 @@ module Foreigner
> base::TableDefinition.class_eval do
> include Foreigner::ConnectionAdapters::TableDefinition
> end
> -
> +
> base::Table.class_eval do
> include Foreigner::ConnectionAdapters::Table
> end
> end
> end
> -
> +
> module TableDefinition
> class ForeignKey < Struct.new(:base, :to_table, :options)
> def to_sql
> - base.foreign_key_definition(to_table, options)
> + base.foreign_key_definition(@table_name, to_table, options)
> end
> alias to_s :to_sql
> end
> @@ -30,18 +30,18 @@ module Foreigner
> alias_method_chain :to_sql, :foreign_keys
> end
> end
> -
> +
> module InstanceMethods
> # Adds a :foreign_key option to TableDefinition.references.
> # If :foreign_key is true, a foreign key constraint is added to the table.
> # You can also specify a hash, which is passed as foreign key options.
> - #
> + #
> # ===== Examples
> # ====== Add goat_id column and a foreign key to the goats table.
> # t.references(:goat, :foreign_key => true)
> # ====== Add goat_id column and a cascading foreign key to the goats table.
> # t.references(:goat, :foreign_key => {:dependent => :delete})
> - #
> + #
> # Note: No foreign key is created if :polymorphic => true is used.
> # Note: If no name is specified, the database driver creates one for you!
> def references_with_foreign_keys(*args)
> @@ -55,7 +55,7 @@ module Foreigner
>
> references_without_foreign_keys(*(args << options))
> end
> -
> +
> # Defines a foreign key for the table. +to_table+ can be a single Symbol, or
> # an Array of Symbols. See SchemaStatements#add_foreign_key
> #
> @@ -74,13 +74,13 @@ module Foreigner
> foreign_keys << ForeignKey.new(@base, to_table, options)
> end
> end
> -
> +
> def to_sql_with_foreign_keys
> sql = to_sql_without_foreign_keys
> sql << ', ' << (foreign_keys * ', ') if foreign_keys.present?
> sql
> end
> -
> +
> private
> def foreign_keys
> @foreign_keys ||= []
> @@ -112,7 +112,7 @@ module Foreigner
> def foreign_key(to_table, options = {})
> @base.add_foreign_key(@table_name, to_table, options)
> end
> -
> +
> # Remove the given foreign key from the table.
> #
> # ===== Examples
> @@ -125,17 +125,17 @@ module Foreigner
> def remove_foreign_key(options = {})
> @base.remove_foreign_key(@table_name, options)
> end
> -
> +
> # Adds a :foreign_key option to TableDefinition.references.
> # If :foreign_key is true, a foreign key constraint is added to the table.
> # You can also specify a hash, which is passed as foreign key options.
> - #
> + #
> # ===== Examples
> # ====== Add goat_id column and a foreign key to the goats table.
> # t.references(:goat, :foreign_key => true)
> # ====== Add goat_id column and a cascading foreign key to the goats table.
> # t.references(:goat, :foreign_key => {:dependent => :delete})
> - #
> + #
> # Note: No foreign key is created if :polymorphic => true is used.
> def references_with_foreign_keys(*args)
> options = args.extract_options!
> diff --git a/src/vendor/plugins/foreigner/lib/foreigner/connection_adapters/sql_2003.rb b/src/vendor/plugins/foreigner/lib/foreigner/connection_adapters/sql_2003.rb
> index 1a1019b..957111b 100644
> --- a/src/vendor/plugins/foreigner/lib/foreigner/connection_adapters/sql_2003.rb
> +++ b/src/vendor/plugins/foreigner/lib/foreigner/connection_adapters/sql_2003.rb
> @@ -4,25 +4,24 @@ module Foreigner
> def supports_foreign_keys?
> true
> end
> -
> +
> def add_foreign_key(from_table, to_table, options = {})
> column = options[:column] || "#{to_table.to_s.singularize}_id"
> - foreign_key_name = foreign_key_name(from_table, column, options)
>
> - sql =
> - "ALTER TABLE #{quote_table_name(from_table)} " +
> - "ADD CONSTRAINT #{quote_column_name(foreign_key_name)} " +
> - foreign_key_definition(to_table, options)
> -
> + sql = "ALTER TABLE #{quote_table_name(from_table)} ADD " <<
> + foreign_key_definition(from_table, to_table, options)
> +
> execute(sql)
> end
> -
> - def foreign_key_definition(to_table, options = {})
> +
> + def foreign_key_definition(from_table, to_table, options = {})
> column = options[:column] || "#{to_table.to_s.singularize}_id"
> + foreign_key_name = foreign_key_name(from_table, column, options)
> primary_key = options[:primary_key] || "id"
> dependency = dependency_sql(options[:dependent])
>
> - sql = "FOREIGN KEY (#{quote_column_name(column)}) REFERENCES #{quote_table_name(to_table)}(#{primary_key})"
> + sql = "CONSTRAINT #{quote_column_name(foreign_key_name)} "
> + sql << "FOREIGN KEY (#{quote_column_name(column)}) REFERENCES #{quote_table_name(to_table)}(#{primary_key})"
> sql << " #{dependency}" unless dependency.blank?
> sql
> end
> @@ -36,7 +35,7 @@ module Foreigner
>
> execute "ALTER TABLE #{quote_table_name(table)} DROP FOREIGN KEY #{quote_column_name(foreign_key_name)}"
> end
> -
> +
> private
> def foreign_key_name(table, column, options = {})
> if options[:name]
>
More information about the ovirt-devel
mailing list