# Create a database with some tables and write protected timestamps on the
# tables and database. Check that span configurations are as we expect.

exec-sql
CREATE DATABASE db;
CREATE TABLE db.t1(id INT);
CREATE TABLE db.t2();
----

# Schema object IDs
# db: 104
# t1: 106
# t2: 107

# Alter zone config fields on the database and one of the tables to ensure
# things are cascading.
exec-sql
ALTER DATABASE db CONFIGURE ZONE USING num_replicas=7;
ALTER TABLE db.t1 CONFIGURE ZONE USING num_voters=5;
----

# Mark the database as offline.
mark-database-offline database=db
----

# Write a protected timestamp on the offline db.
protect record-id=9 ts=9
descs 104
----

# We should still see the protectedts translate onto the tables in the offline db.
translate database=db
----
/Table/10{6-7}                             num_replicas=7 num_voters=5 protection_policies=[{ts: 9}]
/Table/10{7-8}                             num_replicas=7 protection_policies=[{ts: 9}]

release record-id=9
----

mark-database-public database=db
----

translate database=db
----
/Table/10{6-7}                             num_replicas=7 num_voters=5
/Table/10{7-8}                             num_replicas=7


# Write a protected timestamp on t1.
protect record-id=1 ts=1
descs 106
----

translate database=db
----
/Table/10{6-7}                             num_replicas=7 num_voters=5 protection_policies=[{ts: 1}]
/Table/10{7-8}                             num_replicas=7

# Write a protected timestamp on db, so we should see it on both t1 and t2.
protect record-id=2 ts=2
descs 104
----

translate database=db
----
/Table/10{6-7}                             num_replicas=7 num_voters=5 protection_policies=[{ts: 1} {ts: 2}]
/Table/10{7-8}                             num_replicas=7 protection_policies=[{ts: 2}]

# Write a protected timestamp on the cluster.
protect record-id=3 ts=3
cluster
----

# Write a protected timestamp on some tenants.
protect record-id=4 ts=4
tenants 111,112,113
----

# Write another protected timestamp on a subset of the tenants.
protect record-id=5 ts=3
tenants 111,112
----

translate system-span-configurations
----
{entire-keyspace}                          protection_policies=[{ts: 3}]
{source=1,target=111}                      protection_policies=[{ts: 3} {ts: 4}]
{source=1,target=112}                      protection_policies=[{ts: 3} {ts: 4}]
{source=1,target=113}                      protection_policies=[{ts: 4}]

translate database=db
----
/Table/10{6-7}                             num_replicas=7 num_voters=5 protection_policies=[{ts: 1} {ts: 2}]
/Table/10{7-8}                             num_replicas=7 protection_policies=[{ts: 2}]

# Release the protected timestamp on table t1
release record-id=1
----

translate system-span-configurations
----
{entire-keyspace}                          protection_policies=[{ts: 3}]
{source=1,target=111}                      protection_policies=[{ts: 3} {ts: 4}]
{source=1,target=112}                      protection_policies=[{ts: 3} {ts: 4}]
{source=1,target=113}                      protection_policies=[{ts: 4}]

translate database=db
----
/Table/10{6-7}                             num_replicas=7 num_voters=5 protection_policies=[{ts: 2}]
/Table/10{7-8}                             num_replicas=7 protection_policies=[{ts: 2}]

# Release the protected timestamp on database db
release record-id=2
----

translate system-span-configurations
----
{entire-keyspace}                          protection_policies=[{ts: 3}]
{source=1,target=111}                      protection_policies=[{ts: 3} {ts: 4}]
{source=1,target=112}                      protection_policies=[{ts: 3} {ts: 4}]
{source=1,target=113}                      protection_policies=[{ts: 4}]

translate database=db
----
/Table/10{6-7}                             num_replicas=7 num_voters=5
/Table/10{7-8}                             num_replicas=7


# Release the protected timestamp on the cluster and tenants.
release record-id=3
----

release record-id=4
----

block-gc-jobs
----

# Create an index on t1 to ensure that subzones also see protected timestamps.
exec-sql
CREATE INDEX idx ON db.t1(id);
ALTER INDEX db.t1@idx CONFIGURE ZONE USING gc.ttlseconds = 1;
----

protect record-id=6 ts=6
descs 106
----

translate system-span-configurations
----
{source=1,target=111}                      protection_policies=[{ts: 3}]
{source=1,target=112}                      protection_policies=[{ts: 3}]

translate database=db
----
/Table/106{-/2}                            num_replicas=7 num_voters=5 protection_policies=[{ts: 6}]
/Table/106/{2-3}                           ttl_seconds=1 num_replicas=7 num_voters=5 protection_policies=[{ts: 6}]
/Table/106/{3-4}                           ttl_seconds=1 num_replicas=7 num_voters=5 protection_policies=[{ts: 6}]
/Table/10{6/4-7}                           num_replicas=7 num_voters=5 protection_policies=[{ts: 6}]
/Table/10{7-8}                             num_replicas=7


unblock-gc-jobs
----
