foreign key fix
This commit is contained in:
parent
e6fe8ab7e3
commit
3aca2c438d
|
|
@ -132,6 +132,45 @@ func (h *Handler) ResetAndReseedDatabase(c *fiber.Ctx) error {
|
||||||
|
|
||||||
var sqlBuilder strings.Builder
|
var sqlBuilder strings.Builder
|
||||||
sqlBuilder.WriteString("BEGIN;\n")
|
sqlBuilder.WriteString("BEGIN;\n")
|
||||||
|
// Ensure we never attempt to insert NULL set_id into question_set_items.
|
||||||
|
// Some deployments may already have the buggy function from migration 000024,
|
||||||
|
// so we patch it at runtime to make reseed safe.
|
||||||
|
sqlBuilder.WriteString(`
|
||||||
|
CREATE OR REPLACE FUNCTION clone_default_initial_assessment_items(target_set_id BIGINT)
|
||||||
|
RETURNS VOID AS $$
|
||||||
|
DECLARE
|
||||||
|
template_set_id BIGINT;
|
||||||
|
BEGIN
|
||||||
|
IF target_set_id IS NULL THEN
|
||||||
|
RETURN;
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
SELECT id
|
||||||
|
INTO template_set_id
|
||||||
|
FROM question_sets
|
||||||
|
WHERE set_type = 'INITIAL_ASSESSMENT'
|
||||||
|
AND owner_type = 'STANDALONE'
|
||||||
|
AND status = 'PUBLISHED'
|
||||||
|
ORDER BY created_at DESC
|
||||||
|
LIMIT 1;
|
||||||
|
|
||||||
|
IF template_set_id IS NULL THEN
|
||||||
|
RETURN;
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
INSERT INTO question_set_items (set_id, question_id, display_order)
|
||||||
|
SELECT target_set_id, qsi.question_id, qsi.display_order
|
||||||
|
FROM question_set_items qsi
|
||||||
|
WHERE qsi.set_id = template_set_id
|
||||||
|
AND NOT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM question_set_items existing
|
||||||
|
WHERE existing.set_id = target_set_id
|
||||||
|
AND existing.question_id = qsi.question_id
|
||||||
|
);
|
||||||
|
END;
|
||||||
|
$$ LANGUAGE plpgsql;
|
||||||
|
`)
|
||||||
sqlBuilder.WriteString(`
|
sqlBuilder.WriteString(`
|
||||||
DO $$
|
DO $$
|
||||||
DECLARE
|
DECLARE
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user