chore: Campaign ID migration for existing accounts (#2189)

* chore: Campaign ID migration for existing accounts

* chore: update factory

* chore: minor fixes

* chore: fixes
This commit is contained in:
Sojan Jose 2021-04-30 18:45:24 +05:30 committed by GitHub
parent bd7e9d2790
commit a07200bedf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 42 additions and 15 deletions

View file

@ -23,6 +23,6 @@ class Api::V1::Accounts::CampaignsController < Api::V1::Accounts::BaseController
end end
def campaign_params 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
end end

View file

@ -3,9 +3,9 @@
# Table name: campaigns # Table name: campaigns
# #
# id :bigint not null, primary key # id :bigint not null, primary key
# content :text not null
# description :text # description :text
# enabled :boolean default(TRUE) # enabled :boolean default(TRUE)
# message :text not null
# title :string not null # title :string not null
# trigger_rules :jsonb # trigger_rules :jsonb
# created_at :datetime not null # created_at :datetime not null
@ -28,6 +28,8 @@
class Campaign < ApplicationRecord class Campaign < ApplicationRecord
validates :account_id, presence: true validates :account_id, presence: true
validates :inbox_id, presence: true validates :inbox_id, presence: true
validates :title, presence: true
validates :message, presence: true
belongs_to :account belongs_to :account
belongs_to :inbox belongs_to :inbox
belongs_to :sender, class_name: 'User', optional: true belongs_to :sender, class_name: 'User', optional: true

View file

@ -1,9 +1,6 @@
json.id resource.display_id json.id resource.display_id
json.content resource.content
json.description resource.description
json.enabled resource.enabled
json.title resource.title json.title resource.title
json.trigger_rules resource.trigger_rules json.description resource.description
json.account_id resource.account_id json.account_id resource.account_id
json.inbox do json.inbox do
json.partial! 'api/v1/models/inbox.json.jbuilder', resource: resource.inbox json.partial! 'api/v1/models/inbox.json.jbuilder', resource: resource.inbox
@ -11,5 +8,8 @@ end
json.sender do json.sender do
json.partial! 'api/v1/models/agent.json.jbuilder', resource: resource.sender if resource.sender.present? json.partial! 'api/v1/models/agent.json.jbuilder', resource: resource.sender if resource.sender.present?
end end
json.message resource.message
json.enabled resource.enabled
json.trigger_rules resource.trigger_rules
json.created_at resource.created_at json.created_at resource.created_at
json.updated_at resource.updated_at json.updated_at resource.updated_at

View file

@ -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

View file

@ -0,0 +1,5 @@
class RenameCampaignContentToMessage < ActiveRecord::Migration[6.0]
def change
rename_column :campaigns, :content, :message
end
end

View file

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # 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 # These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements" 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.integer "display_id", null: false
t.string "title", null: false t.string "title", null: false
t.text "description" t.text "description"
t.text "content", null: false t.text "message", null: false
t.integer "sender_id" t.integer "sender_id"
t.boolean "enabled", default: true t.boolean "enabled", default: true
t.bigint "account_id", null: false t.bigint "account_id", null: false

View file

@ -75,7 +75,7 @@ RSpec.describe 'Campaigns API', type: :request do
context 'when it is an unauthenticated user' do context 'when it is an unauthenticated user' do
it 'returns unauthorized' do it 'returns unauthorized' do
post "/api/v1/accounts/#{account.id}/campaigns", 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 as: :json
expect(response).to have_http_status(:unauthorized) expect(response).to have_http_status(:unauthorized)
@ -88,7 +88,7 @@ RSpec.describe 'Campaigns API', type: :request do
it 'returns unauthorized for agents' do it 'returns unauthorized for agents' do
post "/api/v1/accounts/#{account.id}/campaigns", 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, headers: agent.create_new_auth_token,
as: :json as: :json
@ -97,7 +97,7 @@ RSpec.describe 'Campaigns API', type: :request do
it 'creates a new campaign' do it 'creates a new campaign' do
post "/api/v1/accounts/#{account.id}/campaigns", 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, headers: administrator.create_new_auth_token,
as: :json as: :json
@ -114,7 +114,7 @@ RSpec.describe 'Campaigns API', type: :request do
context 'when it is an unauthenticated user' do context 'when it is an unauthenticated user' do
it 'returns unauthorized' do it 'returns unauthorized' do
patch "/api/v1/accounts/#{account.id}/campaigns/#{campaign.display_id}", 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 as: :json
expect(response).to have_http_status(:unauthorized) expect(response).to have_http_status(:unauthorized)
@ -127,7 +127,7 @@ RSpec.describe 'Campaigns API', type: :request do
it 'returns unauthorized for agents' do it 'returns unauthorized for agents' do
patch "/api/v1/accounts/#{account.id}/campaigns/#{campaign.display_id}", 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, headers: agent.create_new_auth_token,
as: :json as: :json
@ -136,7 +136,7 @@ RSpec.describe 'Campaigns API', type: :request do
it 'updates the campaign' do it 'updates the campaign' do
patch "/api/v1/accounts/#{account.id}/campaigns/#{campaign.display_id}", 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, headers: administrator.create_new_auth_token,
as: :json as: :json

View file

@ -3,7 +3,7 @@
FactoryBot.define do FactoryBot.define do
factory :campaign do factory :campaign do
sequence(:title) { |n| "Campaign #{n}" } sequence(:title) { |n| "Campaign #{n}" }
sequence(:content) { |n| "Campaign content #{n}" } sequence(:message) { |n| "Campaign message #{n}" }
after(:build) do |campaign| after(:build) do |campaign|
campaign.account ||= create(:account) campaign.account ||= create(:account)
campaign.inbox ||= create( campaign.inbox ||= create(