From 778baba118e90b2bc1ab0985ff62979873526763 Mon Sep 17 00:00:00 2001 From: David Cook Date: Fri, 28 Oct 2022 17:00:52 +1100 Subject: [PATCH] User may have many WebhookEndpoints [migration] Although we won't be allowing multiple in the this PR, we certainly plan to in the future. The migration helper add_reference couldn't handle the custom column name, so I had to put it together manually. --- app/models/spree/user.rb | 2 ++ ...53214_add_spree_user_reference_to_webhook_endpoint.rb | 9 +++++++++ db/schema.rb | 3 +++ spec/models/spree/user_spec.rb | 1 + 4 files changed, 15 insertions(+) create mode 100644 db/migrate/20221028053214_add_spree_user_reference_to_webhook_endpoint.rb diff --git a/app/models/spree/user.rb b/app/models/spree/user.rb index e95df0fcfd..599815c055 100644 --- a/app/models/spree/user.rb +++ b/app/models/spree/user.rb @@ -38,6 +38,8 @@ module Spree has_many :customers has_many :credit_cards has_many :report_rendering_options, class_name: "::ReportRenderingOptions", dependent: :destroy + has_many :webhook_endpoints, dependent: :destroy + accepts_nested_attributes_for :enterprise_roles, allow_destroy: true accepts_nested_attributes_for :bill_address diff --git a/db/migrate/20221028053214_add_spree_user_reference_to_webhook_endpoint.rb b/db/migrate/20221028053214_add_spree_user_reference_to_webhook_endpoint.rb new file mode 100644 index 0000000000..462e978b9c --- /dev/null +++ b/db/migrate/20221028053214_add_spree_user_reference_to_webhook_endpoint.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class AddSpreeUserReferenceToWebhookEndpoint < ActiveRecord::Migration[6.1] + def change + add_column :webhook_endpoints, :user_id, :bigint, default: 0, null: false + add_index :webhook_endpoints, :user_id + add_foreign_key :webhook_endpoints, :spree_users, column: :user_id + end +end diff --git a/db/schema.rb b/db/schema.rb index d28ee2ba80..551c5c2e3a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1193,6 +1193,8 @@ ActiveRecord::Schema.define(version: 2023_02_13_160135) do t.string "url", null: false t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false + t.bigint "user_id", default: 0, null: false + t.index ["user_id"], name: "index_webhook_endpoints_on_user_id" end add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" @@ -1299,4 +1301,5 @@ ActiveRecord::Schema.define(version: 2023_02_13_160135) do add_foreign_key "subscriptions", "spree_shipping_methods", column: "shipping_method_id", name: "subscriptions_shipping_method_id_fk" add_foreign_key "variant_overrides", "enterprises", column: "hub_id", name: "variant_overrides_hub_id_fk" add_foreign_key "variant_overrides", "spree_variants", column: "variant_id", name: "variant_overrides_variant_id_fk" + add_foreign_key "webhook_endpoints", "spree_users", column: "user_id" end diff --git a/spec/models/spree/user_spec.rb b/spec/models/spree/user_spec.rb index a99a18941d..9dac88ad76 100644 --- a/spec/models/spree/user_spec.rb +++ b/spec/models/spree/user_spec.rb @@ -7,6 +7,7 @@ describe Spree::User do describe "associations" do it { is_expected.to have_many(:owned_enterprises) } + it { is_expected.to have_many(:webhook_endpoints).dependent(:destroy) } describe "addresses" do let(:user) { create(:user, bill_address: create(:address)) }