diff --git a/crates/db_schema/src/impls/activity.rs b/crates/db_schema/src/impls/activity.rs index 5ec5d49db..d946d628f 100644 --- a/crates/db_schema/src/impls/activity.rs +++ b/crates/db_schema/src/impls/activity.rs @@ -127,7 +127,7 @@ mod tests { let inserted_activity = Activity::create(&conn, &activity_form).unwrap(); let expected_activity = Activity { - ap_id: Some(ap_id.clone()), + ap_id: ap_id.clone(), id: inserted_activity.id, data: test_json, local: true, diff --git a/crates/db_schema/src/schema.rs b/crates/db_schema/src/schema.rs index 6cd2e2af0..034edbe9d 100644 --- a/crates/db_schema/src/schema.rs +++ b/crates/db_schema/src/schema.rs @@ -5,7 +5,7 @@ table! { local -> Bool, published -> Timestamp, updated -> Nullable, - ap_id -> Nullable, + ap_id -> Text, sensitive -> Nullable, } } diff --git a/crates/db_schema/src/source/activity.rs b/crates/db_schema/src/source/activity.rs index c5ac833ed..5bdeb1db2 100644 --- a/crates/db_schema/src/source/activity.rs +++ b/crates/db_schema/src/source/activity.rs @@ -10,7 +10,7 @@ pub struct Activity { pub local: bool, pub published: chrono::NaiveDateTime, pub updated: Option, - pub ap_id: Option, + pub ap_id: DbUrl, pub sensitive: Option, } diff --git a/migrations/2021-11-22-135324_add_activity_ap_id_index/down.sql b/migrations/2021-11-22-135324_add_activity_ap_id_index/down.sql new file mode 100644 index 000000000..d2d3ad9dc --- /dev/null +++ b/migrations/2021-11-22-135324_add_activity_ap_id_index/down.sql @@ -0,0 +1,5 @@ +alter table activity alter column ap_id drop not null; + +create unique index idx_activity_unique_apid on activity ((data ->> 'id'::text)); + +drop index idx_activity_ap_id; diff --git a/migrations/2021-11-22-135324_add_activity_ap_id_index/up.sql b/migrations/2021-11-22-135324_add_activity_ap_id_index/up.sql new file mode 100644 index 000000000..fedd94f5e --- /dev/null +++ b/migrations/2021-11-22-135324_add_activity_ap_id_index/up.sql @@ -0,0 +1,22 @@ + +-- Delete the empty ap_ids +delete from activity where ap_id is null; + +-- Make it required +alter table activity alter column ap_id set not null; + +-- Delete dupes, keeping the first one +delete +from activity +where id not in ( + select min(id) + from activity + group by ap_id +); + +-- The index +create unique index idx_activity_ap_id on activity(ap_id); + +-- Drop the old index +drop index idx_activity_unique_apid; +