Skip to content

Commit 7e73545

Browse files
authored
Merge pull request rails#48126 from nshki/feat/action-mailbox-configured-primary-keys
feat: use configured pk type in Action Mailbox migration
2 parents 018b2ae + 1df5ece commit 7e73545

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed

actionmailbox/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Support configured primary key types in generated migrations.
2+
3+
*Nishiki Liu*
4+
15
* Fixed ingress controllers' ability to accept emails that contain no UTF-8 encoded parts.
26

37
Fixes #46297.

actionmailbox/db/migrate/20180917164000_create_action_mailbox_tables.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class CreateActionMailboxTables < ActiveRecord::Migration[6.0]
22
def change
3-
create_table :action_mailbox_inbound_emails do |t|
3+
create_table :action_mailbox_inbound_emails, id: primary_key_type do |t|
44
t.integer :status, default: 0, null: false
55
t.string :message_id, null: false
66
t.string :message_checksum, null: false
@@ -10,4 +10,10 @@ def change
1010
t.index [ :message_id, :message_checksum ], name: "index_action_mailbox_inbound_emails_uniqueness", unique: true
1111
end
1212
end
13+
14+
private
15+
def primary_key_type
16+
config = Rails.configuration.generators
17+
config.options[config.orm][:primary_key_type] || :primary_key
18+
end
1319
end

actionmailbox/test/dummy/db/migrate/20180208205311_create_action_mailbox_tables.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class CreateActionMailboxTables < ActiveRecord::Migration[6.0]
22
def change
3-
create_table :action_mailbox_inbound_emails do |t|
3+
create_table :action_mailbox_inbound_emails, id: primary_key_type do |t|
44
t.integer :status, default: 0, null: false
55
t.string :message_id, null: false
66
t.string :message_checksum, null: false
@@ -10,4 +10,10 @@ def change
1010
t.index [ :message_id, :message_checksum ], name: "index_action_mailbox_inbound_emails_uniqueness", unique: true
1111
end
1212
end
13+
14+
private
15+
def primary_key_type
16+
config = Rails.configuration.generators
17+
config.options[config.orm][:primary_key_type] || :primary_key
18+
end
1319
end

actionmailbox/test/migrations_test.rb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
require ActionMailbox::Engine.root.join("db/migrate/20180917164000_create_action_mailbox_tables.rb").to_s
5+
6+
class ActionMailbox::MigrationsTest < ActiveSupport::TestCase
7+
setup do
8+
@original_verbose = ActiveRecord::Migration.verbose
9+
ActiveRecord::Migration.verbose = false
10+
11+
@connection = ActiveRecord::Base.connection
12+
@original_options = Rails.configuration.generators.options.deep_dup
13+
end
14+
15+
teardown do
16+
Rails.configuration.generators.options = @original_options
17+
rerun_migration
18+
ActiveRecord::Migration.verbose = @original_verbose
19+
end
20+
21+
test "migration creates tables with default primary key type" do
22+
action_mailbox_tables.each do |table|
23+
assert_equal :integer, primary_key(table).type
24+
end
25+
end
26+
27+
test "migration creates tables with configured primary key type" do
28+
Rails.configuration.generators do |g|
29+
g.orm :active_record, primary_key_type: :string
30+
end
31+
32+
rerun_migration
33+
34+
action_mailbox_tables.each do |table|
35+
assert_equal :string, primary_key(table).type
36+
end
37+
end
38+
39+
private
40+
def rerun_migration
41+
CreateActionMailboxTables.migrate(:down)
42+
CreateActionMailboxTables.migrate(:up)
43+
end
44+
45+
def action_mailbox_tables
46+
[:action_mailbox_inbound_emails]
47+
end
48+
49+
def primary_key(table)
50+
@connection.columns(table).find { |c| c.name == "id" }
51+
end
52+
end

0 commit comments

Comments
 (0)