diff --git a/app/models/channel/facebook_page.rb b/app/models/channel/facebook_page.rb index caf1f4082..52b6be04f 100644 --- a/app/models/channel/facebook_page.rb +++ b/app/models/channel/facebook_page.rb @@ -14,7 +14,8 @@ # # Indexes # -# index_channel_facebook_pages_on_page_id (page_id) +# index_channel_facebook_pages_on_page_id (page_id) +# index_channel_facebook_pages_on_page_id_and_account_id (page_id,account_id) UNIQUE # module Channel @@ -22,7 +23,7 @@ module Channel self.table_name = 'channel_facebook_pages' validates :account_id, presence: true - validates_uniqueness_of :page_id, scope: :account_id + validates :page_id, uniqueness: { scope: :account_id } mount_uploader :avatar, AvatarUploader belongs_to :account diff --git a/db/migrate/20191204192301_add_index_unique_channel_facebook_page_id_account_id.rb b/db/migrate/20191204192301_add_index_unique_channel_facebook_page_id_account_id.rb new file mode 100644 index 000000000..88cf7be58 --- /dev/null +++ b/db/migrate/20191204192301_add_index_unique_channel_facebook_page_id_account_id.rb @@ -0,0 +1,5 @@ +class AddIndexUniqueChannelFacebookPageIdAccountId < ActiveRecord::Migration[6.0] + def change + add_index :channel_facebook_pages, [:page_id, :account_id], unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index b4bf583e3..6a08c27f5 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.define(version: 2019_12_02_173004) do +ActiveRecord::Schema.define(version: 2019_12_04_192301) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -52,6 +52,7 @@ ActiveRecord::Schema.define(version: 2019_12_02_173004) do t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "avatar" + t.index ["page_id", "account_id"], name: "index_channel_facebook_pages_on_page_id_and_account_id", unique: true t.index ["page_id"], name: "index_channel_facebook_pages_on_page_id" end diff --git a/spec/factories/channel/facebook_pages.rb b/spec/factories/channel/facebook_pages.rb new file mode 100644 index 000000000..158b88b50 --- /dev/null +++ b/spec/factories/channel/facebook_pages.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :channel_facebook_page, class: Channel::FacebookPage do + name { Faker::Name.name } + page_access_token { SecureRandom.uuid } + user_access_token { SecureRandom.uuid } + page_id { SecureRandom.uuid } + account + end +end diff --git a/spec/models/channel/facebook_page_spec.rb b/spec/models/channel/facebook_page_spec.rb new file mode 100644 index 000000000..0e3f13196 --- /dev/null +++ b/spec/models/channel/facebook_page_spec.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Channel::FacebookPage do + before { create(:channel_facebook_page) } + + it { is_expected.to validate_presence_of(:account_id) } + it { is_expected.to validate_uniqueness_of(:page_id).scoped_to(:account_id) } + it { is_expected.to belong_to(:account) } + it { is_expected.to have_one(:inbox).dependent(:destroy) } +end