# We start off by creating a simple database -> table -> index hierarchy for a
# secondary tenant. We set a zone configuration on the index but not on the
# table. This means the table has a "placeholder zone config".

exec-sql
CREATE DATABASE db;
CREATE TABLE db.t(i INT PRIMARY KEY, j INT);
CREATE INDEX idx ON db.t (j);
ALTER DATABASE db CONFIGURE ZONE USING num_replicas=7;
ALTER INDEX db.t@idx CONFIGURE ZONE USING num_voters = 5;
----

query-sql
SHOW ZONE CONFIGURATION FOR DATABASE db
----
DATABASE db ALTER DATABASE db CONFIGURE ZONE USING
	range_min_bytes = 134217728,
	range_max_bytes = 536870912,
	gc.ttlseconds = 14400,
	num_replicas = 7,
	constraints = '[]',
	lease_preferences = '[]'

query-sql
SHOW ZONE CONFIGURATION FOR TABLE db.t
----
DATABASE db ALTER DATABASE db CONFIGURE ZONE USING
	range_min_bytes = 134217728,
	range_max_bytes = 536870912,
	gc.ttlseconds = 14400,
	num_replicas = 7,
	constraints = '[]',
	lease_preferences = '[]'

query-sql
SHOW ZONE CONFIGURATION FOR INDEX db.t@idx
----
INDEX db.public.t@idx ALTER INDEX db.public.t@idx CONFIGURE ZONE USING
	range_min_bytes = 134217728,
	range_max_bytes = 536870912,
	gc.ttlseconds = 14400,
	num_replicas = 7,
	num_voters = 5,
	constraints = '[]',
	voter_constraints = '[]',
	lease_preferences = '[]'

# First entry = primary index (table's config above)
# Second entry = index idx, so numvoters should be overridden.
# Third entry = Any future indexes that may be added to this table. Should be
#               the same as the table's config.
translate database=db table=t
----
/Tenant/10/Table/106{-/2}                  num_replicas=7 rangefeed_enabled=true
/Tenant/10/Table/106/{2-3}                 num_replicas=7 num_voters=5 rangefeed_enabled=true
/Tenant/10/Table/10{6/3-7}                 num_replicas=7 rangefeed_enabled=true

# Configure GC ttl on the database and override it for the index. The table
# continues to hold a placeholder zone config.
exec-sql
ALTER DATABASE db CONFIGURE ZONE USING gc.ttlseconds = 3600;
ALTER INDEX db.t@idx CONFIGURE ZONE USING gc.ttlseconds = 25
----

translate database=db table=t
----
/Tenant/10/Table/106{-/2}                  ttl_seconds=3600 num_replicas=7 rangefeed_enabled=true
/Tenant/10/Table/106/{2-3}                 ttl_seconds=25 num_replicas=7 num_voters=5 rangefeed_enabled=true
/Tenant/10/Table/10{6/3-7}                 ttl_seconds=3600 num_replicas=7 rangefeed_enabled=true

# Configure a zone config field on the table, so that it is no longer a
# placeholder zone config.
exec-sql
ALTER TABLE db.t CONFIGURE ZONE USING range_min_bytes = 1000, range_max_bytes=100000000;
----

query-sql
SHOW ZONE CONFIGURATION FOR INDEX db.t@idx
----
INDEX db.public.t@idx ALTER INDEX db.public.t@idx CONFIGURE ZONE USING
	range_min_bytes = 1000,
	range_max_bytes = 100000000,
	gc.ttlseconds = 25,
	num_replicas = 7,
	num_voters = 5,
	constraints = '[]',
	voter_constraints = '[]',
	lease_preferences = '[]'

translate database=db table=t
----
/Tenant/10/Table/106{-/2}                  range_max_bytes=100000000 range_min_bytes=1000 ttl_seconds=3600 num_replicas=7 rangefeed_enabled=true
/Tenant/10/Table/106/{2-3}                 range_max_bytes=100000000 range_min_bytes=1000 ttl_seconds=25 num_replicas=7 num_voters=5 rangefeed_enabled=true
/Tenant/10/Table/10{6/3-7}                 range_max_bytes=100000000 range_min_bytes=1000 ttl_seconds=3600 num_replicas=7 rangefeed_enabled=true

block-gc-jobs
----

# Create an index and create a zone configuration in the same transaction.
exec-sql
CREATE INDEX idx2 ON db.t (j);
ALTER INDEX db.t@idx2 CONFIGURE ZONE USING gc.ttlseconds = 1;
----

# Both the newly added index and the temporary index have the configured zone configuration.
translate database=db table=t
----
/Tenant/10/Table/106{-/2}                  range_max_bytes=100000000 range_min_bytes=1000 ttl_seconds=3600 num_replicas=7 rangefeed_enabled=true
/Tenant/10/Table/106/{2-3}                 range_max_bytes=100000000 range_min_bytes=1000 ttl_seconds=25 num_replicas=7 num_voters=5 rangefeed_enabled=true
/Tenant/10/Table/106/{3-4}                 range_max_bytes=100000000 range_min_bytes=1000 ttl_seconds=3600 num_replicas=7 rangefeed_enabled=true
/Tenant/10/Table/106/{4-5}                 range_max_bytes=100000000 range_min_bytes=1000 ttl_seconds=1 num_replicas=7 rangefeed_enabled=true
/Tenant/10/Table/106/{5-6}                 range_max_bytes=100000000 range_min_bytes=1000 ttl_seconds=1 num_replicas=7 rangefeed_enabled=true
/Tenant/10/Table/10{6/6-7}                 range_max_bytes=100000000 range_min_bytes=1000 ttl_seconds=3600 num_replicas=7 rangefeed_enabled=true

unblock-gc-jobs
----

exec-sql
SHOW JOBS WHEN COMPLETE (SELECT job_id FROM [SHOW JOBS])
----

# The zone configuration for the temporary index is cleaned up
translate database=db table=t
----
/Tenant/10/Table/106{-/2}                  range_max_bytes=100000000 range_min_bytes=1000 ttl_seconds=3600 num_replicas=7 rangefeed_enabled=true
/Tenant/10/Table/106/{2-3}                 range_max_bytes=100000000 range_min_bytes=1000 ttl_seconds=25 num_replicas=7 num_voters=5 rangefeed_enabled=true
/Tenant/10/Table/106/{3-4}                 range_max_bytes=100000000 range_min_bytes=1000 ttl_seconds=3600 num_replicas=7 rangefeed_enabled=true
/Tenant/10/Table/106/{4-5}                 range_max_bytes=100000000 range_min_bytes=1000 ttl_seconds=1 num_replicas=7 rangefeed_enabled=true
/Tenant/10/Table/10{6/5-7}                 range_max_bytes=100000000 range_min_bytes=1000 ttl_seconds=3600 num_replicas=7 rangefeed_enabled=true

# Create and drop an index inside the same transaction. The related
# zone configuration should also be cleaned up.
exec-sql
CREATE TABLE db.t2(i INT PRIMARY KEY, j INT);
CREATE INDEX idx ON db.t2 (j);
ALTER INDEX db.t2@idx CONFIGURE ZONE USING gc.ttlseconds = 1;
DROP INDEX db.t2@idx
----

translate database=db table=t2
----
/Tenant/10/Table/10{7-8}                   ttl_seconds=3600 num_replicas=7 rangefeed_enabled=true
