diff --git a/app/models/wallet_transaction.rb b/app/models/wallet_transaction.rb index 6cacaa4cf68..689a9796f91 100644 --- a/app/models/wallet_transaction.rb +++ b/app/models/wallet_transaction.rb @@ -4,7 +4,10 @@ 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 STATUSES = [ :pending, @@ -53,16 +56,19 @@ 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 # +# 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 new file mode 100644 index 00000000000..55b37b30a70 --- /dev/null +++ b/db/migrate/20241010055733_add_reference_to_credit_note_from_wallet_transaction.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +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/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 75244a8931a..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_08_080209) 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" @@ -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 @@ -1296,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"