@@ -368,6 +368,202 @@ async fn ownership_remediation_leaves_unrelated_objects_unchanged() {
368368 . expect ( "PostgreSQL connection completes" ) ;
369369}
370370
371+ /// Ownership checks resolve the product schema from the ledger rather than
372+ /// from the first schema on the connection's search path. That preserves
373+ /// unrelated, same-prefix objects in an earlier schema.
374+ #[ tokio:: test]
375+ async fn ownership_remediation_follows_the_ledger_namespace_across_search_path_schemas ( ) {
376+ let ( mut client, connection) = tokio_postgres:: connect ( & database_url ( ) , NoTls )
377+ . await
378+ . expect ( "connect to disposable PostgreSQL" ) ;
379+ let connection_task = tokio:: spawn ( connection) ;
380+ let suffix = Uuid :: new_v4 ( ) . simple ( ) . to_string ( ) ;
381+ let schema = format ! ( "Activation_{suffix}" ) ;
382+ let quoted_schema = format ! ( "\" {schema}\" " ) ;
383+ let decoy_schema = format ! ( "decoy_{suffix}" ) ;
384+ let runtime_role = format ! ( "activation_runtime_{suffix}" ) ;
385+ let transaction = client. transaction ( ) . await . expect ( "start transaction" ) ;
386+ transaction
387+ . batch_execute ( & format ! (
388+ "CREATE ROLE {runtime_role} NOLOGIN;
389+ CREATE SCHEMA {quoted_schema};
390+ CREATE SCHEMA {decoy_schema};
391+ CREATE TABLE {quoted_schema}.fixture_schema_migrations(version bigint PRIMARY KEY);
392+ CREATE TABLE {quoted_schema}.fixture_activations(activation_id uuid PRIMARY KEY);
393+ CREATE SEQUENCE {quoted_schema}.fixture_sequence;
394+ ALTER SEQUENCE {quoted_schema}.fixture_sequence OWNER TO {runtime_role};
395+ CREATE FUNCTION {quoted_schema}.fixture_transform(value integer) RETURNS integer
396+ LANGUAGE sql IMMUTABLE AS $body$ SELECT value $body$;
397+ ALTER FUNCTION {quoted_schema}.fixture_transform(integer) OWNER TO {runtime_role};
398+ CREATE TABLE {decoy_schema}.fixture_records(record_id bigint PRIMARY KEY);
399+ ALTER TABLE {decoy_schema}.fixture_records OWNER TO {runtime_role};
400+ CREATE SEQUENCE {decoy_schema}.fixture_sequence;
401+ ALTER SEQUENCE {decoy_schema}.fixture_sequence OWNER TO {runtime_role};
402+ CREATE FUNCTION {decoy_schema}.fixture_transform(value integer) RETURNS integer
403+ LANGUAGE sql IMMUTABLE AS $body$ SELECT value $body$;
404+ ALTER FUNCTION {decoy_schema}.fixture_transform(integer) OWNER TO {runtime_role};
405+ CREATE FUNCTION {decoy_schema}.fixture_trigger() RETURNS trigger
406+ LANGUAGE plpgsql AS $body$ BEGIN RETURN NEW; END $body$;
407+ CREATE TRIGGER decoy_trigger BEFORE INSERT ON {decoy_schema}.fixture_records
408+ FOR EACH ROW EXECUTE FUNCTION {decoy_schema}.fixture_trigger();
409+ ALTER SCHEMA {decoy_schema} OWNER TO {runtime_role};
410+ GRANT CREATE ON SCHEMA {decoy_schema} TO {runtime_role};
411+ GRANT TRIGGER ON {decoy_schema}.fixture_records TO {runtime_role};
412+ SET search_path TO {decoy_schema},{quoted_schema};"
413+ ) )
414+ . await
415+ . expect ( "create product and earlier decoy schemas" ) ;
416+ let migration_role: String = transaction
417+ . query_one ( "SELECT quote_ident(current_user)" , & [ ] )
418+ . await
419+ . expect ( "read quoted migration role" )
420+ . get ( 0 ) ;
421+ let expected = vec ! [
422+ format!( "ALTER SEQUENCE {quoted_schema}.fixture_sequence OWNER TO {migration_role}" ) ,
423+ format!(
424+ "ALTER ROUTINE {quoted_schema}.fixture_transform(value integer) OWNER TO {migration_role}"
425+ ) ,
426+ ] ;
427+
428+ let weakness = first_authority_weakness ( & transaction, & layout ( ) , & runtime_role)
429+ . await
430+ . expect ( "inspect indirect ownership authority" )
431+ . expect ( "actual product objects weaken the split role" ) ;
432+ match weakness. cause {
433+ AuthorityCause :: OwnedObjects { statements, .. } => assert_eq ! ( statements, expected) ,
434+ cause => panic ! ( "expected ownership corrections, got {cause:?}" ) ,
435+ }
436+ let fixes = stray_authority ( & transaction, & layout ( ) , Some ( & runtime_role) )
437+ . await
438+ . expect ( "identify exact ownership remediation" ) ;
439+ assert_eq ! ( fixes, expected) ;
440+ for fix in & fixes {
441+ transaction
442+ . batch_execute ( fix)
443+ . await
444+ . expect ( "execute emitted ownership remediation" ) ;
445+ }
446+
447+ let owners = transaction
448+ . query_one (
449+ "SELECT
450+ pg_get_userbyid((SELECT relowner FROM pg_class c JOIN pg_namespace n
451+ ON n.oid=c.relnamespace WHERE n.nspname=$1 AND c.relname='fixture_sequence')),
452+ pg_get_userbyid((SELECT proowner FROM pg_proc p JOIN pg_namespace n
453+ ON n.oid=p.pronamespace WHERE n.nspname=$1 AND p.proname='fixture_transform')),
454+ pg_get_userbyid((SELECT nspowner FROM pg_namespace WHERE nspname=$2)),
455+ pg_get_userbyid((SELECT relowner FROM pg_class c JOIN pg_namespace n
456+ ON n.oid=c.relnamespace WHERE n.nspname=$2 AND c.relname='fixture_records')),
457+ pg_get_userbyid((SELECT relowner FROM pg_class c JOIN pg_namespace n
458+ ON n.oid=c.relnamespace WHERE n.nspname=$2 AND c.relname='fixture_sequence')),
459+ pg_get_userbyid((SELECT proowner FROM pg_proc p JOIN pg_namespace n
460+ ON n.oid=p.pronamespace WHERE n.nspname=$2 AND p.proname='fixture_transform'))" ,
461+ & [ & schema, & decoy_schema] ,
462+ )
463+ . await
464+ . expect ( "read product and decoy owners" ) ;
465+ let current_user: String = transaction
466+ . query_one ( "SELECT current_user::text" , & [ ] )
467+ . await
468+ . expect ( "read migration role" )
469+ . get ( 0 ) ;
470+ assert_eq ! ( owners. get:: <_, String >( 0 ) , current_user) ;
471+ assert_eq ! ( owners. get:: <_, String >( 1 ) , current_user) ;
472+ for column in 2 ..6 {
473+ assert_eq ! (
474+ owners. get:: <_, String >( column) ,
475+ runtime_role,
476+ "decoy owner in column {column} must remain unchanged"
477+ ) ;
478+ }
479+ assert ! (
480+ first_authority_weakness( & transaction, & layout( ) , & runtime_role)
481+ . await
482+ . expect( "reinspect ownership boundary" )
483+ . is_none( )
484+ ) ;
485+ assert ! (
486+ stray_authority( & transaction, & layout( ) , Some ( & runtime_role) )
487+ . await
488+ . expect( "reinspect ownership remediation" )
489+ . is_empty( )
490+ ) ;
491+
492+ transaction
493+ . rollback ( )
494+ . await
495+ . expect ( "roll back isolated multi-schema fixture" ) ;
496+ drop ( client) ;
497+ connection_task
498+ . await
499+ . expect ( "join PostgreSQL connection" )
500+ . expect ( "PostgreSQL connection completes" ) ;
501+ }
502+
503+ /// Before the ledger exists, an apply connection still has an explicit
504+ /// migration role and ownership remediation uses its current schema.
505+ #[ tokio:: test]
506+ async fn ownership_remediation_falls_back_to_current_schema_before_the_ledger_exists ( ) {
507+ let ( mut client, connection) = tokio_postgres:: connect ( & database_url ( ) , NoTls )
508+ . await
509+ . expect ( "connect to disposable PostgreSQL" ) ;
510+ let connection_task = tokio:: spawn ( connection) ;
511+ let suffix = Uuid :: new_v4 ( ) . simple ( ) . to_string ( ) ;
512+ let schema = format ! ( "activation_{suffix}" ) ;
513+ let runtime_role = format ! ( "activation_runtime_{suffix}" ) ;
514+ let transaction = client. transaction ( ) . await . expect ( "start transaction" ) ;
515+ transaction
516+ . batch_execute ( & format ! (
517+ "CREATE ROLE {runtime_role} NOLOGIN;
518+ CREATE SCHEMA {schema};
519+ SET search_path TO {schema};
520+ CREATE TABLE fixture_records(record_id bigint PRIMARY KEY);
521+ ALTER TABLE fixture_records OWNER TO {runtime_role};"
522+ ) )
523+ . await
524+ . expect ( "create pre-ledger product object" ) ;
525+ let migration_role: String = transaction
526+ . query_one ( "SELECT quote_ident(current_user)" , & [ ] )
527+ . await
528+ . expect ( "read quoted migration role" )
529+ . get ( 0 ) ;
530+ let expected = format ! ( "ALTER TABLE {schema}.fixture_records OWNER TO {migration_role}" ) ;
531+
532+ let fixes = stray_authority ( & transaction, & layout ( ) , Some ( & runtime_role) )
533+ . await
534+ . expect ( "identify pre-ledger ownership remediation" ) ;
535+ assert_eq ! ( fixes, vec![ expected] ) ;
536+ transaction
537+ . batch_execute ( & fixes[ 0 ] )
538+ . await
539+ . expect ( "execute pre-ledger ownership remediation" ) ;
540+ let owner: String = transaction
541+ . query_one (
542+ "SELECT pg_get_userbyid(c.relowner) FROM pg_class c JOIN pg_namespace n
543+ ON n.oid=c.relnamespace WHERE n.nspname=$1 AND c.relname='fixture_records'" ,
544+ & [ & schema] ,
545+ )
546+ . await
547+ . expect ( "read corrected owner" )
548+ . get ( 0 ) ;
549+ let current_user: String = transaction
550+ . query_one ( "SELECT current_user::text" , & [ ] )
551+ . await
552+ . expect ( "read migration role" )
553+ . get ( 0 ) ;
554+ assert_eq ! ( owner, current_user) ;
555+
556+ transaction
557+ . rollback ( )
558+ . await
559+ . expect ( "roll back isolated pre-ledger fixture" ) ;
560+ drop ( client) ;
561+ connection_task
562+ . await
563+ . expect ( "join PostgreSQL connection" )
564+ . expect ( "PostgreSQL connection completes" ) ;
565+ }
566+
371567#[ tokio:: test]
372568async fn postgres_version_floor_precedes_missing_ledger_observation ( ) {
373569 let ( client, connection) = tokio_postgres:: connect ( & database_url ( ) , NoTls )
0 commit comments