Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(db): support PostgreSQL 14 in Slack archive migration (#7634)

teo980d469732ff
diff --git a/crates/macro_db_client/migrations/20260930171728_slack_archive_import.sql b/crates/macro_db_client/migrations/20260930171728_slack_archive_import.sql
index c10589e4d8..2dfdabcd93 100644
--- a/crates/macro_db_client/migrations/20260930171728_slack_archive_import.sql
+++ b/crates/macro_db_client/migrations/20260930171728_slack_archive_import.sql
@@ -191,7 +191,7 @@ CREATE TABLE slack_import_upload (
verified_at timestamptz,
verified_version_id text CHECK (length(verified_version_id) > 0),
verified_etag text CHECK (length(verified_etag) > 0),
- UNIQUE NULLS NOT DISTINCT (job_id, slack_channel_id, part_index),
+ UNIQUE (job_id, slack_channel_id, part_index),
FOREIGN KEY (job_id, slack_channel_id)
REFERENCES slack_import_conversation (job_id, slack_channel_id) ON DELETE CASCADE,
CHECK (num_nonnulls(slack_channel_id, part_index, record_count) IN (0, 3)),
@@ -199,6 +199,12 @@ CREATE TABLE slack_import_upload (
(verified_version_id IS NOT NULL OR verified_etag IS NOT NULL))
);
+-- PostgreSQL 14 does not support UNIQUE NULLS NOT DISTINCT. The check above
+-- requires channel/index to be both present or both absent, so the ordinary
+-- unique constraint covers conversation parts and this index covers users.
+CREATE UNIQUE INDEX slack_import_upload_one_users_per_job
+ ON slack_import_upload (job_id) WHERE slack_channel_id IS NULL;
+
-- Lock job before conversation in every registration/seal transaction. This
-- makes a concurrent seal/finalize and registration serialize on the same row.
CREATE FUNCTION protect_slack_import_upload() RETURNS trigger
@@ -340,4 +346,3 @@ CREATE INDEX slack_import_message_map_message_idx ON slack_import_message_map (m
ALTER TABLE import_entity DROP CONSTRAINT import_entity_initiator_check;
ALTER TABLE import_entity ADD CONSTRAINT import_entity_initiator_check
CHECK (initiator IN ('onboarding', 'chat', 'archive'));
-
diff --git a/crates/macro_db_client/tests/slack_archive_unique_keys.sql b/crates/macro_db_client/tests/slack_archive_unique_keys.sql
new file mode 100644
index 0000000000..1ffcc366d6
--- /dev/null
+++ b/crates/macro_db_client/tests/slack_archive_unique_keys.sql
@@ -0,0 +1,57 @@
+-- Run after migration 20260930171728 on PostgreSQL 14 and 16:
+-- psql "$DATABASE_URL" -X -v ON_ERROR_STOP=1 -f tests/slack_archive_unique_keys.sql
+BEGIN;
+
+-- Copy the real indexes and checks, without the unrelated job/FK/trigger setup.
+-- Allow a NULL record_count here to exercise NULL-key uniqueness independently
+-- of the original migration's upload validation.
+CREATE TEMP TABLE upload_keys (LIKE slack_import_upload INCLUDING ALL);
+ALTER TABLE upload_keys ALTER COLUMN record_count DROP NOT NULL;
+
+INSERT INTO upload_keys (job_id, object_key, sha256, byte_length)
+VALUES ('00000000-0000-0000-0000-000000000001', 'users', repeat('a', 64), 1);
+
+DO $$
+BEGIN
+ BEGIN
+ INSERT INTO upload_keys (job_id, object_key, sha256, byte_length)
+ VALUES ('00000000-0000-0000-0000-000000000001', 'other-users', repeat('a', 64), 1);
+ RAISE EXCEPTION 'duplicate NULL upload key was accepted';
+ EXCEPTION WHEN unique_violation THEN NULL;
+ END;
+END;
+$$;
+
+-- A different job can still register its own users object.
+INSERT INTO upload_keys (job_id, object_key, sha256, byte_length)
+VALUES ('00000000-0000-0000-0000-000000000002', 'second-job-users', repeat('a', 64), 1);
+
+INSERT INTO upload_keys (job_id, slack_channel_id, part_index, object_key, sha256, byte_length, record_count)
+VALUES ('00000000-0000-0000-0000-000000000001', 'C123', 0, 'part-0', repeat('a', 64), 1, 1);
+
+DO $$
+BEGIN
+ BEGIN
+ INSERT INTO upload_keys (job_id, slack_channel_id, part_index, object_key, sha256, byte_length, record_count)
+ VALUES ('00000000-0000-0000-0000-000000000001', 'C123', 0, 'other-part-0', repeat('a', 64), 1, 1);
+ RAISE EXCEPTION 'duplicate conversation part was accepted';
+ EXCEPTION WHEN unique_violation THEN NULL;
+ END;
+END;
+$$;
+
+-- Preserve the conflict target used by the existing upload adapter.
+INSERT INTO upload_keys (job_id, slack_channel_id, part_index, object_key, sha256, byte_length, record_count)
+VALUES ('00000000-0000-0000-0000-000000000001', 'C123', 0, 'retry-part-0', repeat('a', 64), 1, 1)
+ON CONFLICT (job_id, slack_channel_id, part_index) DO NOTHING;
+
+INSERT INTO upload_keys (job_id, slack_channel_id, part_index, object_key, sha256, byte_length, record_count)
+VALUES ('00000000-0000-0000-0000-000000000001', 'C123', 1, 'part-1', repeat('a', 64), 1, 1);
+
+DO $$
+BEGIN
+ ASSERT (SELECT count(*) = 4 FROM upload_keys), 'valid upload keys did not remain distinct';
+END;
+$$;
+
+ROLLBACK;