-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)}" | ||
|
@@ -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)}" | ||
|
@@ -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? | ||
|
||
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)} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why this change? is this fixing a bug, changing any current behaviour? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For consistency. data_source_exists?: sql = "SELECT table_name FROM information_schema.tables "
sql << "WHERE table_schema = #{quote(schema)} AND table_name = #{quote(name)}" sql = "SELECT table_name FROM information_schema.tables WHERE table_type = 'VIEW'"
sql << " AND table_schema = #{quote(schema)} AND table_name = #{quote(name)}" 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) | ||
|
@@ -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 | ||
|
@@ -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' | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).