# This test asserts that the span configs associated with the replicating app tenant also get
# replicated

create-replication-clusters
----

exec-sql as=source-system
ALTER TENANT ALL SET CLUSTER SETTING spanconfig.reconciliation_job.checkpoint_interval = '10ms'
----


exec-sql as=source-tenant
CREATE TABLE d.x (id INT PRIMARY KEY, n INT);
----

exec-sql as=source-tenant
CREATE TABLE d.x2 (id INT PRIMARY KEY, n INT);
----


# Get the mininum and maximum user table id
let $min_table_id  as=source-tenant
SELECT 'd.x'::regclass::oid::int::string
----

let $max_table_id  as=source-tenant
SELECT 'd.x2'::regclass::oid::int::string
----


start-replication-stream
----

# TODO(adityamaru): This is a hack until we have the time we started replicating
# from persisted in the replication job details.
let $start as=source-system
SELECT clock_timestamp()::timestamp::string
----

wait-until-replicated-time ts=$start
----

list-ttls as=source-system min_table_id=$min_table_id max_table_id=$max_table_id
----
14400
14400

exec-sql as=source-tenant
ALTER TABLE d.x CONFIGURE ZONE USING gc.ttlseconds=1234;
----

list-ttls as=source-system min_table_id=$min_table_id max_table_id=$max_table_id
----
1234
14400


# TODO (msbutler): figure out how to combine span configs, causing a span config delete.


let $cutover as=source-system
SELECT clock_timestamp()::timestamp::string
----

wait-until-replicated-time ts=$cutover
----

list-ttls as=destination-system min_table_id=$min_table_id max_table_id=$max_table_id
----
1234
14400


# Replicate another zone config change, but cutover to an aost before the change.
exec-sql as=source-tenant
ALTER TABLE d.x CONFIGURE ZONE USING gc.ttlseconds=4567;
----

list-ttls as=source-system min_table_id=$min_table_id max_table_id=$max_table_id
----
4567
14400

let $after_cutover as=source-system
SELECT clock_timestamp()::timestamp::string
----

wait-until-replicated-time ts=$after_cutover
----

list-ttls as=destination-system min_table_id=$min_table_id max_table_id=$max_table_id
----
4567
14400


cutover ts=$cutover
----

start-replicated-tenant
----

exec-sql as=destination-system
ALTER TENANT ALL SET CLUSTER SETTING spanconfig.reconciliation_job.checkpoint_interval = '10ms'
----

# TODO(msbutler): trigger a full reconciliation job on the new app tenant after cutover, so that we
# observe the ttl equal to 1234.

list-ttls as=destination-system min_table_id=$min_table_id max_table_id=$max_table_id
----
4567
14400
