Skip to content

Commit

Permalink
Refactor create_table's options separation
Browse files Browse the repository at this point in the history
`create_table` and `t.column` have the same named options (e.g.
`:comment`, `:primary_key`), so it should be separated table options
from column options.

Related #36373.
  • Loading branch information
kamipo committed Jun 2, 2019
1 parent f7396f9 commit c3ca9b0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
Expand Up @@ -264,8 +264,7 @@ def initialize(
if_not_exists: false,
options: nil,
as: nil,
comment: nil,
**
comment: nil
)
@conn = conn
@columns_hash = {}
Expand Down
Expand Up @@ -291,25 +291,25 @@ def primary_key(table_name)
# SELECT * FROM orders INNER JOIN line_items ON order_id=orders.id
#
# See also TableDefinition#column for details on how to create columns.
def create_table(table_name, **options)
td = create_table_definition(table_name, options)
def create_table(table_name, id: :primary_key, primary_key: nil, force: nil, **options)
td = create_table_definition(
table_name, options.extract!(:temporary, :if_not_exists, :options, :as, :comment)
)

if options[:id] != false && !options[:as]
pk = options.fetch(:primary_key) do
Base.get_primary_key table_name.to_s.singularize
end
if id && !td.as
pk = primary_key || Base.get_primary_key(table_name.to_s.singularize)

if pk.is_a?(Array)
td.primary_keys pk
else
td.primary_key pk, options.fetch(:id, :primary_key), options.except(:comment)
td.primary_key pk, id, options
end
end

yield td if block_given?

if options[:force]
drop_table(table_name, options.merge(if_exists: true))
if force
drop_table(table_name, force: force, if_exists: true)
end

result = execute schema_creation.accept td
Expand All @@ -321,7 +321,7 @@ def create_table(table_name, **options)
end

if supports_comments? && !supports_comments_in_create?
if table_comment = options[:comment].presence
if table_comment = td.comment.presence
change_table_comment(table_name, table_comment)
end

Expand Down

0 comments on commit c3ca9b0

Please sign in to comment.