From a07200bedff8cd67308f35f68f2a2a2be2d7a0e8 Mon Sep 17 00:00:00 2001 From: Sojan Jose Date: Fri, 30 Apr 2021 18:45:24 +0530 Subject: [PATCH] chore: Campaign ID migration for existing accounts (#2189) * chore: Campaign ID migration for existing accounts * chore: update factory * chore: minor fixes * chore: fixes --- .../api/v1/accounts/campaigns_controller.rb | 2 +- app/models/campaign.rb | 4 +++- .../api/v1/models/_campaign.json.jbuilder | 8 ++++---- ...dd_camp_dp_id_seq_for_existing_accounts.rb | 20 +++++++++++++++++++ ...0138_rename_campaign_content_to_message.rb | 5 +++++ db/schema.rb | 4 ++-- .../v1/accounts/campaigns_controller_spec.rb | 12 +++++------ spec/factories/campaigns.rb | 2 +- 8 files changed, 42 insertions(+), 15 deletions(-) create mode 100644 db/migrate/20210430095748_add_camp_dp_id_seq_for_existing_accounts.rb create mode 100644 db/migrate/20210430100138_rename_campaign_content_to_message.rb diff --git a/app/controllers/api/v1/accounts/campaigns_controller.rb b/app/controllers/api/v1/accounts/campaigns_controller.rb index f6162d2d0..c4555017d 100644 --- a/app/controllers/api/v1/accounts/campaigns_controller.rb +++ b/app/controllers/api/v1/accounts/campaigns_controller.rb @@ -23,6 +23,6 @@ class Api::V1::Accounts::CampaignsController < Api::V1::Accounts::BaseController end def campaign_params - params.require(:campaign).permit(:title, :description, :content, :enabled, :inbox_id, :sender_id, trigger_rules: {}) + params.require(:campaign).permit(:title, :description, :message, :enabled, :inbox_id, :sender_id, trigger_rules: {}) end end diff --git a/app/models/campaign.rb b/app/models/campaign.rb index 71cf58ce7..bde63ce10 100644 --- a/app/models/campaign.rb +++ b/app/models/campaign.rb @@ -3,9 +3,9 @@ # Table name: campaigns # # id :bigint not null, primary key -# content :text not null # description :text # enabled :boolean default(TRUE) +# message :text not null # title :string not null # trigger_rules :jsonb # created_at :datetime not null @@ -28,6 +28,8 @@ class Campaign < ApplicationRecord validates :account_id, presence: true validates :inbox_id, presence: true + validates :title, presence: true + validates :message, presence: true belongs_to :account belongs_to :inbox belongs_to :sender, class_name: 'User', optional: true diff --git a/app/views/api/v1/models/_campaign.json.jbuilder b/app/views/api/v1/models/_campaign.json.jbuilder index 836751728..ecadb7b9e 100644 --- a/app/views/api/v1/models/_campaign.json.jbuilder +++ b/app/views/api/v1/models/_campaign.json.jbuilder @@ -1,9 +1,6 @@ json.id resource.display_id -json.content resource.content -json.description resource.description -json.enabled resource.enabled json.title resource.title -json.trigger_rules resource.trigger_rules +json.description resource.description json.account_id resource.account_id json.inbox do json.partial! 'api/v1/models/inbox.json.jbuilder', resource: resource.inbox @@ -11,5 +8,8 @@ end json.sender do json.partial! 'api/v1/models/agent.json.jbuilder', resource: resource.sender if resource.sender.present? end +json.message resource.message +json.enabled resource.enabled +json.trigger_rules resource.trigger_rules json.created_at resource.created_at json.updated_at resource.updated_at diff --git a/db/migrate/20210430095748_add_camp_dp_id_seq_for_existing_accounts.rb b/db/migrate/20210430095748_add_camp_dp_id_seq_for_existing_accounts.rb new file mode 100644 index 000000000..1b37e8345 --- /dev/null +++ b/db/migrate/20210430095748_add_camp_dp_id_seq_for_existing_accounts.rb @@ -0,0 +1,20 @@ +class AddCampDpIdSeqForExistingAccounts < ActiveRecord::Migration[6.0] + def up + ::Account.find_in_batches do |accounts_batch| + Rails.logger.info "migrated till #{accounts_batch.first.id}\n" + accounts_batch.each do |account| + display_id = account.campaigns.count + ActiveRecord::Base.connection.exec_query("create sequence IF NOT EXISTS camp_dpid_seq_#{account.id} START #{display_id + 1}") + end + end + end + + def down + ::Account.find_in_batches do |accounts_batch| + Rails.logger.info "migrated till #{accounts_batch.first.id}\n" + accounts_batch.each do |account| + ActiveRecord::Base.connection.exec_query("drop sequence IF EXISTS camp_dpid_seq_#{account.id}") + end + end + end +end diff --git a/db/migrate/20210430100138_rename_campaign_content_to_message.rb b/db/migrate/20210430100138_rename_campaign_content_to_message.rb new file mode 100644 index 000000000..161ef299e --- /dev/null +++ b/db/migrate/20210430100138_rename_campaign_content_to_message.rb @@ -0,0 +1,5 @@ +class RenameCampaignContentToMessage < ActiveRecord::Migration[6.0] + def change + rename_column :campaigns, :content, :message + end +end diff --git a/db/schema.rb b/db/schema.rb index 331a7609c..6dd7b6380 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: 2021_04_28_151147) do +ActiveRecord::Schema.define(version: 2021_04_30_100138) do # These are extensions that must be enabled in order to support this database enable_extension "pg_stat_statements" @@ -117,7 +117,7 @@ ActiveRecord::Schema.define(version: 2021_04_28_151147) do t.integer "display_id", null: false t.string "title", null: false t.text "description" - t.text "content", null: false + t.text "message", null: false t.integer "sender_id" t.boolean "enabled", default: true t.bigint "account_id", null: false diff --git a/spec/controllers/api/v1/accounts/campaigns_controller_spec.rb b/spec/controllers/api/v1/accounts/campaigns_controller_spec.rb index 2e2936884..5a90929d5 100644 --- a/spec/controllers/api/v1/accounts/campaigns_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/campaigns_controller_spec.rb @@ -75,7 +75,7 @@ RSpec.describe 'Campaigns API', type: :request do context 'when it is an unauthenticated user' do it 'returns unauthorized' do post "/api/v1/accounts/#{account.id}/campaigns", - params: { inbox_id: inbox.id, title: 'test', content: 'test message' }, + params: { inbox_id: inbox.id, title: 'test', message: 'test message' }, as: :json expect(response).to have_http_status(:unauthorized) @@ -88,7 +88,7 @@ RSpec.describe 'Campaigns API', type: :request do it 'returns unauthorized for agents' do post "/api/v1/accounts/#{account.id}/campaigns", - params: { inbox_id: inbox.id, title: 'test', content: 'test message' }, + params: { inbox_id: inbox.id, title: 'test', message: 'test message' }, headers: agent.create_new_auth_token, as: :json @@ -97,7 +97,7 @@ RSpec.describe 'Campaigns API', type: :request do it 'creates a new campaign' do post "/api/v1/accounts/#{account.id}/campaigns", - params: { inbox_id: inbox.id, title: 'test', content: 'test message' }, + params: { inbox_id: inbox.id, title: 'test', message: 'test message' }, headers: administrator.create_new_auth_token, as: :json @@ -114,7 +114,7 @@ RSpec.describe 'Campaigns API', type: :request do context 'when it is an unauthenticated user' do it 'returns unauthorized' do patch "/api/v1/accounts/#{account.id}/campaigns/#{campaign.display_id}", - params: { inbox_id: inbox.id, title: 'test', content: 'test message' }, + params: { inbox_id: inbox.id, title: 'test', message: 'test message' }, as: :json expect(response).to have_http_status(:unauthorized) @@ -127,7 +127,7 @@ RSpec.describe 'Campaigns API', type: :request do it 'returns unauthorized for agents' do patch "/api/v1/accounts/#{account.id}/campaigns/#{campaign.display_id}", - params: { inbox_id: inbox.id, title: 'test', content: 'test message' }, + params: { inbox_id: inbox.id, title: 'test', message: 'test message' }, headers: agent.create_new_auth_token, as: :json @@ -136,7 +136,7 @@ RSpec.describe 'Campaigns API', type: :request do it 'updates the campaign' do patch "/api/v1/accounts/#{account.id}/campaigns/#{campaign.display_id}", - params: { inbox_id: inbox.id, title: 'test', content: 'test message' }, + params: { inbox_id: inbox.id, title: 'test', message: 'test message' }, headers: administrator.create_new_auth_token, as: :json diff --git a/spec/factories/campaigns.rb b/spec/factories/campaigns.rb index bdd84efc9..4d4c6de18 100644 --- a/spec/factories/campaigns.rb +++ b/spec/factories/campaigns.rb @@ -3,7 +3,7 @@ FactoryBot.define do factory :campaign do sequence(:title) { |n| "Campaign #{n}" } - sequence(:content) { |n| "Campaign content #{n}" } + sequence(:message) { |n| "Campaign message #{n}" } after(:build) do |campaign| campaign.account ||= create(:account) campaign.inbox ||= create(