Skip to content

Commit

Permalink
Respect configured primary key type in migration (#836)
Browse files Browse the repository at this point in the history
* Respect configured primary key type in pay migration

* Actually use primary_key_type in migration

* Standard format

* Remove note about migration IDs from docs
  • Loading branch information
MSchmidt committed Aug 9, 2023
1 parent 16786d4 commit ea73cf8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
36 changes: 24 additions & 12 deletions db/migrate/1_create_pay_tables.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
class CreatePayTables < ActiveRecord::Migration[6.0]
def change
create_table :pay_customers do |t|
t.belongs_to :owner, polymorphic: true, index: false
primary_key_type, foreign_key_type = primary_and_foreign_key_types

create_table :pay_customers, id: primary_key_type do |t|
t.belongs_to :owner, polymorphic: true, index: false, type: foreign_key_type
t.string :processor, null: false
t.string :processor_id
t.boolean :default
Expand All @@ -12,8 +14,8 @@ def change
add_index :pay_customers, [:owner_type, :owner_id, :deleted_at, :default], name: :pay_customer_owner_index
add_index :pay_customers, [:processor, :processor_id], unique: true

create_table :pay_merchants do |t|
t.belongs_to :owner, polymorphic: true, index: false
create_table :pay_merchants, id: primary_key_type do |t|
t.belongs_to :owner, polymorphic: true, index: false, type: foreign_key_type
t.string :processor, null: false
t.string :processor_id
t.boolean :default
Expand All @@ -22,8 +24,8 @@ def change
end
add_index :pay_merchants, [:owner_type, :owner_id, :processor]

create_table :pay_payment_methods do |t|
t.belongs_to :customer, foreign_key: {to_table: :pay_customers}, null: false, index: false
create_table :pay_payment_methods, id: primary_key_type do |t|
t.belongs_to :customer, foreign_key: {to_table: :pay_customers}, null: false, index: false, type: foreign_key_type
t.string :processor_id, null: false
t.boolean :default
t.string :type
Expand All @@ -32,8 +34,8 @@ def change
end
add_index :pay_payment_methods, [:customer_id, :processor_id], unique: true

create_table :pay_subscriptions do |t|
t.belongs_to :customer, foreign_key: {to_table: :pay_customers}, null: false, index: false
create_table :pay_subscriptions, id: primary_key_type do |t|
t.belongs_to :customer, foreign_key: {to_table: :pay_customers}, null: false, index: false, type: foreign_key_type
t.string :name, null: false
t.string :processor_id, null: false
t.string :processor_plan, null: false
Expand All @@ -56,9 +58,9 @@ def change
add_index :pay_subscriptions, [:metered]
add_index :pay_subscriptions, [:pause_starts_at]

create_table :pay_charges do |t|
t.belongs_to :customer, foreign_key: {to_table: :pay_customers}, null: false, index: false
t.belongs_to :subscription, foreign_key: {to_table: :pay_subscriptions}, null: true
create_table :pay_charges, id: primary_key_type do |t|
t.belongs_to :customer, foreign_key: {to_table: :pay_customers}, null: false, index: false, type: foreign_key_type
t.belongs_to :subscription, foreign_key: {to_table: :pay_subscriptions}, null: true, type: foreign_key_type
t.string :processor_id, null: false
t.integer :amount, null: false
t.string :currency
Expand All @@ -70,11 +72,21 @@ def change
end
add_index :pay_charges, [:customer_id, :processor_id], unique: true

create_table :pay_webhooks do |t|
create_table :pay_webhooks, id: primary_key_type do |t|
t.string :processor
t.string :event_type
t.public_send Pay::Adapter.json_column_type, :event
t.timestamps
end
end

private

def primary_and_foreign_key_types
config = Rails.configuration.generators
setting = config.options[config.orm][:primary_key_type]
primary_key_type = setting || :primary_key
foreign_key_type = setting || :bigint
[primary_key_type, foreign_key_type]
end
end
6 changes: 2 additions & 4 deletions docs/1_installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ Copy the Pay migrations to your app:
bin/rails pay:install:migrations
````

>If your models rely on non integer ids (uuids for example) you will need to alter the migrations.
Then run the migrations:

```bash
Expand Down Expand Up @@ -115,12 +113,12 @@ If you want to automatically sync whenever any other attribute changes, override

```rb
class User < ApplicationRecord

def pay_should_sync_customer?
# super will invoke Pay's default (e-mail changed)
super || self.saved_change_to_address? || self.saved_change_to_name?
end

end
```

Expand Down

0 comments on commit ea73cf8

Please sign in to comment.