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

Extract extract_schema_qualified_name method #23497

Merged
merged 1 commit into from
Apr 19, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,7 @@ def table_exists?(table_name)
def data_source_exists?(table_name)
return false unless table_name.present?

schema, name = table_name.to_s.split('.', 2)
schema, name = @config[:database], schema unless name # A table was provided without a schema
schema, name = extract_schema_qualified_name(table_name)

sql = "SELECT table_name FROM information_schema.tables "
sql << "WHERE table_schema = #{quote(schema)} AND table_name = #{quote(name)}"
Expand All @@ -386,8 +385,7 @@ def views # :nodoc:
def view_exists?(view_name) # :nodoc:
return false unless view_name.present?

schema, name = view_name.to_s.split('.', 2)
schema, name = @config[:database], schema unless name # A view was provided without a schema
schema, name = extract_schema_qualified_name(view_name)

sql = "SELECT table_name FROM information_schema.tables WHERE table_type = 'VIEW'"
sql << " AND table_schema = #{quote(schema)} AND table_name = #{quote(name)}"
Expand Down Expand Up @@ -523,15 +521,19 @@ def add_index(table_name, column_name, options = {}) #:nodoc:
end

def foreign_keys(table_name)
raise ArgumentError unless table_name.present?
Copy link
Member

Choose a reason for hiding this comment

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

Is this condition possible?
I guess we need a regression test for this now.

Copy link
Member Author

Choose a reason for hiding this comment

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

@jeremy said he prefer to raise an ArgumentError for an exceptional case like this in #21614 (comment).


schema, name = extract_schema_qualified_name(table_name)

fk_info = select_all <<-SQL.strip_heredoc
SELECT fk.referenced_table_name as 'to_table'
,fk.referenced_column_name as 'primary_key'
,fk.column_name as 'column'
,fk.constraint_name as 'name'
FROM information_schema.key_column_usage fk
WHERE fk.referenced_column_name is not null
AND fk.table_schema = '#{@config[:database]}'
AND fk.table_name = '#{table_name}'
AND fk.table_schema = #{quote(schema)}
AND fk.table_name = #{quote(name)}
Copy link
Member

Choose a reason for hiding this comment

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

why this change? is this fixing a bug, changing any current behaviour?

Copy link
Member Author

Choose a reason for hiding this comment

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

For consistency.

data_source_exists?:
https://github.com/kamipo/rails/blob/extract_schema_qualified_name/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb#L375-L376

        sql = "SELECT table_name FROM information_schema.tables "
        sql << "WHERE table_schema = #{quote(schema)} AND table_name = #{quote(name)}"

view_exists?:
https://github.com/kamipo/rails/blob/extract_schema_qualified_name/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb#L390-L391

        sql = "SELECT table_name FROM information_schema.tables WHERE table_type = 'VIEW'"
        sql << " AND table_schema = #{quote(schema)} AND table_name = #{quote(name)}"

primary_keys:
https://github.com/kamipo/rails/blob/extract_schema_qualified_name/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb#L605-L606

        select_values(<<-SQL.strip_heredoc, 'SCHEMA')
          SELECT column_name
          FROM information_schema.key_column_usage
          WHERE constraint_name = 'PRIMARY'
            AND table_schema = #{quote(schema)}
            AND table_name = #{quote(name)}
          ORDER BY ordinal_position
        SQL

SQL

create_table_info = create_table_info(table_name)
Expand Down Expand Up @@ -594,8 +596,7 @@ def show_variable(name)
def primary_keys(table_name) # :nodoc:
raise ArgumentError unless table_name.present?

schema, name = table_name.to_s.split('.', 2)
schema, name = @config[:database], schema unless name # A table was provided without a schema
schema, name = extract_schema_qualified_name(table_name)

select_values(<<-SQL.strip_heredoc, 'SCHEMA')
SELECT column_name
Expand Down Expand Up @@ -898,6 +899,12 @@ def create_table_definition(name, temporary = false, options = nil, as = nil) #
MySQL::TableDefinition.new(name, temporary, options, as)
end

def extract_schema_qualified_name(string) # :nodoc:
schema, name = string.to_s.scan(/[^`.\s]+|`[^`]*`/)
schema, name = @config[:database], schema unless name
[schema, name]
end

def integer_to_sql(limit) # :nodoc:
case limit
when 1; 'tinyint'
Expand Down