Skip to content

Commit

Permalink
ActiveRecord::SchemaMigration has no primary key.
Browse files Browse the repository at this point in the history
Before this patch, using `ActiveRecord::Base.primary_key_prefix_type`
with `:table_name_with_underscore` would change the `SchemaMigration` model
to have a primary key. This resulted in broken queries for PG because it tried
to return the inserted PK (which does not exist).

Closes #15051.
Closes #15419.
  • Loading branch information
senny committed Jun 6, 2014
1 parent d23cbbb commit 8fa8b71
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
7 changes: 7 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,3 +1,10 @@
* `ActiveRecord::SchemaMigration` has no primary key regardless of the
`primary_key_prefix_type` configuration.

Fixes #15051.

*Yves Senn*

* `rake db:migrate:status` works with legacy migration numbers like `00018_xyz.rb`.

Fixes #15538.
Expand Down
3 changes: 3 additions & 0 deletions activerecord/lib/active_record/schema_migration.rb
Expand Up @@ -5,6 +5,9 @@
module ActiveRecord
class SchemaMigration < ActiveRecord::Base
class << self
def primary_key
nil
end

def table_name
"#{table_name_prefix}#{ActiveRecord::Base.schema_migrations_table_name}#{table_name_suffix}"
Expand Down
14 changes: 14 additions & 0 deletions activerecord/test/cases/ar_schema_test.rb
Expand Up @@ -17,6 +17,20 @@ def setup
ActiveRecord::SchemaMigration.delete_all rescue nil
end

def test_has_no_primary_key
old_primary_key_prefix_type = ActiveRecord::Base.primary_key_prefix_type
ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore
assert_nil ActiveRecord::SchemaMigration.primary_key

ActiveRecord::SchemaMigration.create_table
assert_difference "ActiveRecord::SchemaMigration.count", 1 do
ActiveRecord::SchemaMigration.create version: 12
end
ensure
ActiveRecord::SchemaMigration.drop_table
ActiveRecord::Base.primary_key_prefix_type = old_primary_key_prefix_type
end

def test_schema_define
ActiveRecord::Schema.define(:version => 7) do
create_table :fruits do |t|
Expand Down

2 comments on commit 8fa8b71

@schneems
Copy link
Member

Choose a reason for hiding this comment

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

Hey Senny, can you take a look at #22976 any problems you see with specifying a primary key of "version" for SchemaMigration?

@senny
Copy link
Member Author

@senny senny commented on 8fa8b71 Jan 11, 2016

Choose a reason for hiding this comment

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

@schneems using version as primary key seems fine 👍

Please sign in to comment.