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

SchemaDumper mixes table comment and primary key comment #29966

Closed
yskkin opened this issue Jul 27, 2017 · 3 comments · Fixed by #36384
Closed

SchemaDumper mixes table comment and primary key comment #29966

yskkin opened this issue Jul 27, 2017 · 3 comments · Fixed by #36384

Comments

@yskkin
Copy link
Contributor

yskkin commented Jul 27, 2017

Steps to reproduce

begin
  require "bundler/inline"
rescue LoadError => e
  $stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
  raise e
end

gemfile(true) do
  source "https://rubygems.org"
  gem "rails", "~>5.1"
  gem "arel", github: "rails/arel"
  gem "pg"
end

require "active_record"
require "minitest/autorun"
require "logger"

# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)

# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: "postgresql", database: "test")
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :payments, force: true, id: false do |t|
    t.bigint :pk, comment: "primary key comment", primary_key: true
    t.decimal :amount, precision: 10, scale: 0, default: 0, null: false
  end
  change_table_comment :payments, "table comment"
end

class Payment < ActiveRecord::Base
end

class BugTest < Minitest::Test
  def test_comment_should_appear_only_once_in_create_table_line
    stream = StringIO.new
    ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)

    create_table_line = stream.string[/^\s*create_table.+$/]
    puts stream.string
    assert_equal 1, create_table_line.scan("comment:").length
  end
end

Expected behavior

dumped schema should be:

  create_table "payments", id: false, force: :cascade, comment: "table comment" do |t|
    t.bigint "pk", comment: "primary key comment"
    t.decimal "amount", precision: 10, default: "0", null: false
  end

Actual behavior

dumped schema is:

  create_table "payments", primary_key: "pk", id: :bigint, comment: "primary key comment", default: nil, force: :cascade, comment: "table comment" do |t|
    t.decimal "amount", precision: 10, default: "0", null: false
  end

System configuration

Rails version: master

Ruby version: 2.4.0-p0

@yskkin
Copy link
Contributor Author

yskkin commented Jul 27, 2017

I was checking against 5.1.
This is fixed on master.
sorry for noise.

@yskkin yskkin closed this as completed Jul 27, 2017
@guigs
Copy link
Contributor

guigs commented May 27, 2019

Hey, I'm still having this issue with Rails 5.2.3 and also on master.

I just had to modify the test to create pk with type integer instead of bigint.

Looks like if pk is type bigint it ignores all other options for column, including the comment.

@guigs
Copy link
Contributor

guigs commented May 27, 2019

The problem is that a :comment option is added for the pk column (except if it is bigint) in:

pkcolspec = column_spec_for_primary_key(pkcol)
if pkcolspec.present?
tbl.print ", #{format_colspec(pkcolspec)}"
end

Then another :comment option is added for the table in
table_options = @connection.table_options(table)
if table_options.present?
tbl.print ", #{format_options(table_options)}"
end

guigs added a commit to guigs/rails that referenced this issue Jun 3, 2019
Before this fix it would either generate an invalid schema, passing `comment` option twice to `create_table`, or it move the comment from primary key column to the table if table had no comment when the dump was generated.

The situation now is that a comment on primary key will be ignored (not present on schema).

Fixes rails#29966
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants