From 8c4b97897e15683d2f0a68fcc0bc24296b242e5a Mon Sep 17 00:00:00 2001 From: Anna Velentsevich Date: Thu, 10 Oct 2024 14:28:17 +0200 Subject: [PATCH 1/5] migration to add credit_note id to wallet_transaction --- app/models/wallet_transaction.rb | 6 ++++-- ...add_reference_to_credit_note_from_wallet_transaction.rb | 7 +++++++ db/schema.rb | 4 +++- 3 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20241010055733_add_reference_to_credit_note_from_wallet_transaction.rb diff --git a/app/models/wallet_transaction.rb b/app/models/wallet_transaction.rb index 6cacaa4cf68..5a8a34e6ea7 100644 --- a/app/models/wallet_transaction.rb +++ b/app/models/wallet_transaction.rb @@ -53,13 +53,15 @@ class WalletTransaction < ApplicationRecord # transaction_type :integer not null # created_at :datetime not null # updated_at :datetime not null +# credit_note_id :uuid # invoice_id :uuid # wallet_id :uuid not null # # Indexes # -# index_wallet_transactions_on_invoice_id (invoice_id) -# index_wallet_transactions_on_wallet_id (wallet_id) +# index_wallet_transactions_on_credit_note_id (credit_note_id) +# index_wallet_transactions_on_invoice_id (invoice_id) +# index_wallet_transactions_on_wallet_id (wallet_id) # # Foreign Keys # diff --git a/db/migrate/20241010055733_add_reference_to_credit_note_from_wallet_transaction.rb b/db/migrate/20241010055733_add_reference_to_credit_note_from_wallet_transaction.rb new file mode 100644 index 00000000000..0adff7ad100 --- /dev/null +++ b/db/migrate/20241010055733_add_reference_to_credit_note_from_wallet_transaction.rb @@ -0,0 +1,7 @@ +class AddReferenceToCreditNoteFromWalletTransaction < ActiveRecord::Migration[7.1] + disable_ddl_transaction! + + def change + add_reference :wallet_transactions, :credit_note, type: :uuid, null: true, index: { algorithm: :concurrently } + end +end diff --git a/db/schema.rb b/db/schema.rb index 75244a8931a..70e4422fdfa 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_10_08_080209) do +ActiveRecord::Schema[7.1].define(version: 2024_10_10_055733) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" enable_extension "plpgsql" @@ -1133,6 +1133,8 @@ t.integer "transaction_status", default: 0, null: false t.boolean "invoice_requires_successful_payment", default: false, null: false t.jsonb "metadata", default: [] + t.uuid "credit_note_id" + t.index ["credit_note_id"], name: "index_wallet_transactions_on_credit_note_id" t.index ["invoice_id"], name: "index_wallet_transactions_on_invoice_id" t.index ["wallet_id"], name: "index_wallet_transactions_on_wallet_id" end From 119751254d5ab33ebe365ddab1a0baaf0b626156 Mon Sep 17 00:00:00 2001 From: Anna Velentsevich Date: Thu, 10 Oct 2024 14:29:19 +0200 Subject: [PATCH 2/5] add relation to credit_note from wallet_transaction --- app/models/wallet_transaction.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/wallet_transaction.rb b/app/models/wallet_transaction.rb index 5a8a34e6ea7..839b86f6d46 100644 --- a/app/models/wallet_transaction.rb +++ b/app/models/wallet_transaction.rb @@ -5,6 +5,7 @@ class WalletTransaction < ApplicationRecord belongs_to :wallet belongs_to :invoice, optional: true + belongs_to :credit_note, optional: true STATUSES = [ :pending, From 4f3fd42bf9c68d4fd529d594f4ded843ecdbbafe Mon Sep 17 00:00:00 2001 From: Anna Velentsevich Date: Fri, 11 Oct 2024 14:24:28 +0200 Subject: [PATCH 3/5] add explainatory comment --- app/models/wallet_transaction.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/models/wallet_transaction.rb b/app/models/wallet_transaction.rb index 839b86f6d46..be4cdeca42b 100644 --- a/app/models/wallet_transaction.rb +++ b/app/models/wallet_transaction.rb @@ -4,6 +4,8 @@ class WalletTransaction < ApplicationRecord include PaperTrailTraceable belongs_to :wallet + + # these two relationships are populated only for outbound transactions belongs_to :invoice, optional: true belongs_to :credit_note, optional: true From 70adba1a3aa14bdc3e16bdeecc27dae37a1fa959 Mon Sep 17 00:00:00 2001 From: Anna Velentsevich Date: Fri, 11 Oct 2024 15:27:13 +0200 Subject: [PATCH 4/5] use more tiny migrations to set up indexes --- app/models/wallet_transaction.rb | 1 + ...add_reference_to_credit_note_from_wallet_transaction.rb | 2 ++ ...y_constraint_to_credit_note_id_at_wallet_transaction.rb | 7 +++++++ ...n_foreign_key_from_wallet_transaction_to_credit_note.rb | 7 +++++++ db/schema.rb | 3 ++- 5 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20241011123148_add_foreign_key_constraint_to_credit_note_id_at_wallet_transaction.rb create mode 100644 db/migrate/20241011123621_change_validate_on_foreign_key_from_wallet_transaction_to_credit_note.rb diff --git a/app/models/wallet_transaction.rb b/app/models/wallet_transaction.rb index be4cdeca42b..30873cd4532 100644 --- a/app/models/wallet_transaction.rb +++ b/app/models/wallet_transaction.rb @@ -68,6 +68,7 @@ class WalletTransaction < ApplicationRecord # # Foreign Keys # +# fk_rails_... (credit_note_id => credit_notes.id) # fk_rails_... (invoice_id => invoices.id) # fk_rails_... (wallet_id => wallets.id) # diff --git a/db/migrate/20241010055733_add_reference_to_credit_note_from_wallet_transaction.rb b/db/migrate/20241010055733_add_reference_to_credit_note_from_wallet_transaction.rb index 0adff7ad100..0cb3ad70c30 100644 --- a/db/migrate/20241010055733_add_reference_to_credit_note_from_wallet_transaction.rb +++ b/db/migrate/20241010055733_add_reference_to_credit_note_from_wallet_transaction.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class AddReferenceToCreditNoteFromWalletTransaction < ActiveRecord::Migration[7.1] disable_ddl_transaction! diff --git a/db/migrate/20241011123148_add_foreign_key_constraint_to_credit_note_id_at_wallet_transaction.rb b/db/migrate/20241011123148_add_foreign_key_constraint_to_credit_note_id_at_wallet_transaction.rb new file mode 100644 index 00000000000..4f4a539b72b --- /dev/null +++ b/db/migrate/20241011123148_add_foreign_key_constraint_to_credit_note_id_at_wallet_transaction.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class AddForeignKeyConstraintToCreditNoteIdAtWalletTransaction < ActiveRecord::Migration[7.1] + def change + add_foreign_key :wallet_transactions, :credit_notes, validate: false + end +end diff --git a/db/migrate/20241011123621_change_validate_on_foreign_key_from_wallet_transaction_to_credit_note.rb b/db/migrate/20241011123621_change_validate_on_foreign_key_from_wallet_transaction_to_credit_note.rb new file mode 100644 index 00000000000..096fe03457d --- /dev/null +++ b/db/migrate/20241011123621_change_validate_on_foreign_key_from_wallet_transaction_to_credit_note.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class ChangeValidateOnForeignKeyFromWalletTransactionToCreditNote < ActiveRecord::Migration[7.1] + def change + validate_foreign_key :wallet_transactions, :credit_notes + end +end diff --git a/db/schema.rb b/db/schema.rb index 70e4422fdfa..d35ae1919df 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_10_10_055733) do +ActiveRecord::Schema[7.1].define(version: 2024_10_11_123621) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" enable_extension "plpgsql" @@ -1298,6 +1298,7 @@ add_foreign_key "subscriptions", "plans" add_foreign_key "taxes", "organizations" add_foreign_key "usage_thresholds", "plans" + add_foreign_key "wallet_transactions", "credit_notes" add_foreign_key "wallet_transactions", "invoices" add_foreign_key "wallet_transactions", "wallets" add_foreign_key "wallets", "customers" From 89f5f10a0641a7f75052d04c643f3aec32fa5c0e Mon Sep 17 00:00:00 2001 From: Anna Velentsevich Date: Fri, 11 Oct 2024 15:32:35 +0200 Subject: [PATCH 5/5] fix linter --- app/models/wallet_transaction.rb | 2 +- ...5733_add_reference_to_credit_note_from_wallet_transaction.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/wallet_transaction.rb b/app/models/wallet_transaction.rb index 30873cd4532..689a9796f91 100644 --- a/app/models/wallet_transaction.rb +++ b/app/models/wallet_transaction.rb @@ -4,7 +4,7 @@ class WalletTransaction < ApplicationRecord include PaperTrailTraceable belongs_to :wallet - + # these two relationships are populated only for outbound transactions belongs_to :invoice, optional: true belongs_to :credit_note, optional: true diff --git a/db/migrate/20241010055733_add_reference_to_credit_note_from_wallet_transaction.rb b/db/migrate/20241010055733_add_reference_to_credit_note_from_wallet_transaction.rb index 0cb3ad70c30..55b37b30a70 100644 --- a/db/migrate/20241010055733_add_reference_to_credit_note_from_wallet_transaction.rb +++ b/db/migrate/20241010055733_add_reference_to_credit_note_from_wallet_transaction.rb @@ -4,6 +4,6 @@ class AddReferenceToCreditNoteFromWalletTransaction < ActiveRecord::Migration[7. disable_ddl_transaction! def change - add_reference :wallet_transactions, :credit_note, type: :uuid, null: true, index: { algorithm: :concurrently } + add_reference :wallet_transactions, :credit_note, type: :uuid, null: true, index: {algorithm: :concurrently} end end