11//! Upgrade contracts for the event follow-up trigger retirements: push
22//! (0063), then channel TTL (0065).
33use super :: postgres_tests:: { connect_test_pool, reset_public_schema} ;
4- use super :: { run_migrations , run_migrations_through} ;
4+ use super :: run_migrations_through;
55use sqlx:: PgPool ;
66
77async fn ttl_definitions ( pool : & PgPool ) -> Vec < ( String , String ) > {
@@ -21,6 +21,51 @@ async fn push_trigger_count(pool: &PgPool) -> i64 {
2121 . expect ( "count push triggers" )
2222}
2323
24+ /// Insert a channel with an expired TTL deadline, then a raw `INSERT INTO
25+ /// events` on it that bypasses the application follow-up. Returns whether the
26+ /// database itself pushed the deadline forward.
27+ async fn raw_insert_refreshes_expired_ttl ( pool : & PgPool , content : & str ) -> bool {
28+ let community = uuid:: Uuid :: new_v4 ( ) ;
29+ let channel = uuid:: Uuid :: new_v4 ( ) ;
30+ sqlx:: query ( "INSERT INTO communities (id, host) VALUES ($1, $2)" )
31+ . bind ( community)
32+ . bind ( format ! ( "retirement-{community}.example" ) )
33+ . execute ( pool)
34+ . await
35+ . unwrap ( ) ;
36+ sqlx:: query (
37+ "INSERT INTO channels (id, community_id, name, created_by, ttl_seconds, ttl_deadline) \
38+ VALUES ($1, $2, 'ttl', $3, 60, clock_timestamp() - interval '1 day')",
39+ )
40+ . bind ( channel)
41+ . bind ( community)
42+ . bind ( [ 11_u8 ; 32 ] )
43+ . execute ( pool)
44+ . await
45+ . unwrap ( ) ;
46+ sqlx:: query (
47+ "INSERT INTO events (community_id, id, pubkey, created_at, kind, tags, content, sig, channel_id) \
48+ VALUES ($1, $2, $3, now(), 9, '[]', $4, $5, $6)",
49+ )
50+ . bind ( community)
51+ . bind ( [ 12_u8 ; 32 ] )
52+ . bind ( [ 11_u8 ; 32 ] )
53+ . bind ( content)
54+ . bind ( [ 13_u8 ; 64 ] )
55+ . bind ( channel)
56+ . execute ( pool)
57+ . await
58+ . unwrap ( ) ;
59+ sqlx:: query_scalar (
60+ "SELECT ttl_deadline > clock_timestamp() FROM channels WHERE id=$1 AND community_id=$2" ,
61+ )
62+ . bind ( channel)
63+ . bind ( community)
64+ . fetch_one ( pool)
65+ . await
66+ . unwrap ( )
67+ }
68+
2469#[ tokio:: test]
2570#[ ignore = "requires Postgres" ]
2671async fn migration_schema_push_retirement_preserves_ttl_and_published_checksums ( ) {
@@ -80,38 +125,8 @@ async fn migration_schema_push_retirement_preserves_ttl_and_published_checksums(
80125
81126 // A raw writer has no application TTL follow-up. The preserved deferred
82127 // trigger must still refresh its channel when the event commits.
83- let community = uuid:: Uuid :: new_v4 ( ) ;
84- let channel = uuid:: Uuid :: new_v4 ( ) ;
85- sqlx:: query ( "INSERT INTO communities (id, host) VALUES ($1, $2)" )
86- . bind ( community)
87- . bind ( format ! ( "retirement-{community}.example" ) )
88- . execute ( & pool)
89- . await
90- . unwrap ( ) ;
91- sqlx:: query (
92- "INSERT INTO channels (id, community_id, name, created_by, ttl_seconds, ttl_deadline) \
93- VALUES ($1, $2, 'ttl', $3, 60, clock_timestamp() - interval '1 day')",
94- )
95- . bind ( channel)
96- . bind ( community)
97- . bind ( [ 11_u8 ; 32 ] )
98- . execute ( & pool)
99- . await
100- . unwrap ( ) ;
101- sqlx:: query ( "INSERT INTO events (community_id, id, pubkey, created_at, kind, tags, content, sig, channel_id) \
102- VALUES ($1, $2, $3, now(), 9, '[]', 'TTL survives push retirement', $4, $5)")
103- . bind ( community) . bind ( [ 12_u8 ; 32 ] ) . bind ( [ 11_u8 ; 32 ] ) . bind ( [ 13_u8 ; 64 ] ) . bind ( channel)
104- . execute ( & pool) . await . unwrap ( ) ;
105- let refreshed: bool = sqlx:: query_scalar (
106- "SELECT ttl_deadline > clock_timestamp() FROM channels WHERE id=$1 AND community_id=$2" ,
107- )
108- . bind ( channel)
109- . bind ( community)
110- . fetch_one ( & pool)
111- . await
112- . unwrap ( ) ;
113128 assert ! (
114- refreshed ,
129+ raw_insert_refreshes_expired_ttl ( & pool , "TTL survives push retirement" ) . await ,
115130 "raw inserts still refresh TTL after push retirement"
116131 ) ;
117132 pool. close ( ) . await ;
@@ -146,7 +161,9 @@ async fn migration_schema_ttl_retirement_drops_every_partition_trigger_and_keeps
146161 . await
147162 . unwrap ( ) ;
148163
149- run_migrations ( & pool) . await . expect ( "retire TTL trigger" ) ;
164+ run_migrations_through ( & pool, 65 )
165+ . await
166+ . expect ( "retire TTL trigger" ) ;
150167 assert ! (
151168 ttl_definitions( & pool) . await . is_empty( ) ,
152169 "retired on parent and every partition"
@@ -160,5 +177,12 @@ async fn migration_schema_ttl_retirement_drops_every_partition_trigger_and_keeps
160177 . await
161178 . unwrap ( ) ;
162179 assert_eq ! ( checksums_after, checksums_before) ;
180+
181+ // With both triggers gone, a raw writer that skips the application
182+ // follow-up no longer extends an ephemeral channel's deadline.
183+ assert ! (
184+ !raw_insert_refreshes_expired_ttl( & pool, "TTL no longer refreshed by the database" ) . await ,
185+ "raw inserts must not refresh TTL after 0065"
186+ ) ;
163187 pool. close ( ) . await ;
164188}
0 commit comments