Skip to content

Commit 012de05

Browse files
committed
fix: remove conflicting constraints on NOT NULL columns in applied_slas and csat_responses tables, update constraints for csat_responses and applied_slas tables to cascade deletes instead
1 parent 6357faf commit 012de05

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

internal/migrations/v0.6.0.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,6 @@ func V0_6_0(db *sqlx.DB, fs stuffbin.FileSystem, ko *koanf.Koanf) error {
8686
}
8787
}
8888

89-
// Add `contacts:read` permission to Agent role
90-
_, err = db.Exec(`
91-
UPDATE roles
92-
SET permissions = array_append(permissions, 'contacts:read')
93-
WHERE name = 'Agent' AND NOT ('contacts:read' = ANY(permissions));
94-
`)
95-
if err != nil {
96-
return err
97-
}
98-
9989
// Add `custom_attributes:manage` permission to Admin role
10090
_, err = db.Exec(`
10191
UPDATE roles
@@ -311,5 +301,28 @@ func V0_6_0(db *sqlx.DB, fs stuffbin.FileSystem, ko *koanf.Koanf) error {
311301
if err != nil {
312302
return err
313303
}
304+
305+
// Replace constraints for applied_slas and csat_responses tables.
306+
_, err = db.Exec(`
307+
ALTER TABLE applied_slas
308+
DROP CONSTRAINT IF EXISTS applied_slas_conversation_id_fkey,
309+
DROP CONSTRAINT IF EXISTS applied_slas_sla_policy_id_fkey,
310+
ALTER COLUMN conversation_id SET NOT NULL,
311+
ALTER COLUMN sla_policy_id SET NOT NULL,
312+
ADD CONSTRAINT applied_slas_conversation_id_fkey
313+
FOREIGN KEY (conversation_id) REFERENCES conversations(id)
314+
ON DELETE CASCADE ON UPDATE CASCADE,
315+
ADD CONSTRAINT applied_slas_sla_policy_id_fkey
316+
FOREIGN KEY (sla_policy_id) REFERENCES sla_policies(id)
317+
ON DELETE CASCADE ON UPDATE CASCADE;
318+
319+
320+
ALTER TABLE csat_responses
321+
DROP CONSTRAINT IF EXISTS csat_responses_conversation_id_fkey,
322+
ALTER COLUMN conversation_id SET NOT NULL,
323+
ADD CONSTRAINT csat_responses_conversation_id_fkey
324+
FOREIGN KEY (conversation_id) REFERENCES conversations(id)
325+
ON DELETE CASCADE ON UPDATE CASCADE;
326+
`)
314327
return nil
315328
}

schema.sql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,8 @@ CREATE TABLE csat_responses (
415415
updated_at TIMESTAMPTZ DEFAULT NOW(),
416416
uuid UUID DEFAULT gen_random_uuid() NOT NULL UNIQUE,
417417

418-
-- Keep CSAT responses even if the conversation or agent is deleted.
419-
conversation_id BIGINT REFERENCES conversations(id) ON DELETE SET NULL ON UPDATE CASCADE NOT NULL,
418+
-- Cascade deletes when conversation is deleted.
419+
conversation_id BIGINT REFERENCES conversations(id) ON DELETE CASCADE ON UPDATE CASCADE NOT NULL,
420420

421421
rating INT DEFAULT 0 NOT NULL,
422422
feedback TEXT NULL,
@@ -447,9 +447,9 @@ CREATE TABLE applied_slas (
447447

448448
status applied_sla_status DEFAULT 'pending' NOT NULL,
449449

450-
-- Conversation / SLA policy maybe deleted but for reports the applied SLA should remain.
451-
conversation_id BIGINT REFERENCES conversations(id) ON DELETE SET NULL ON UPDATE CASCADE NOT NULL,
452-
sla_policy_id INT REFERENCES sla_policies(id) ON DELETE SET NULL ON UPDATE CASCADE NOT NULL,
450+
-- Cascade deletes when conversation or SLA policy is deleted.
451+
conversation_id BIGINT REFERENCES conversations(id) ON DELETE CASCADE ON UPDATE CASCADE NOT NULL,
452+
sla_policy_id INT REFERENCES sla_policies(id) ON DELETE CASCADE ON UPDATE CASCADE NOT NULL,
453453

454454
first_response_deadline_at TIMESTAMPTZ NULL,
455455
resolution_deadline_at TIMESTAMPTZ NULL,

0 commit comments

Comments
 (0)