# LogicTest: !3node-tenant-default-configs
# Zone config logic tests that are only meant to work for the system tenant.

# Verify that the timeseries range has a zone config after bootstrap. It should
# only specify gc.ttlseconds, and everything should be implicitly inherited
# from the default range zone config.
query TT
SELECT target, raw_config_sql FROM [SHOW ZONE CONFIGURATIONS] WHERE target = 'RANGE timeseries'
----
RANGE timeseries  ALTER RANGE timeseries CONFIGURE ZONE USING
                    gc.ttlseconds = 14400

statement ok
CREATE TABLE t();

statement ok
ALTER TABLE t CONFIGURE ZONE USING num_replicas = 5;

# Should have no effect on the system tenant.
statement ok
SET CLUSTER SETTING sql.virtual_cluster.feature_access.zone_configs.enabled = false

statement ok
ALTER TABLE t CONFIGURE ZONE USING num_replicas = 3;

statement ok
CREATE TABLE a(id INT PRIMARY KEY)

# Check that global_reads cannot be set without a CCL binary and enterprise license.
skipif config enterprise-configs
statement error OSS binaries do not include enterprise features
ALTER TABLE a CONFIGURE ZONE USING global_reads = true

query IT
SELECT zone_id, target FROM crdb_internal.zones ORDER BY 1
----
0    RANGE default
1    DATABASE system
11   TABLE system.public.lease
16   RANGE meta
17   RANGE system
18   RANGE timeseries
22   RANGE liveness
25   TABLE system.public.replication_constraint_stats
27   TABLE system.public.replication_stats
42   TABLE system.public.statement_statistics
43   TABLE system.public.transaction_statistics
45   TABLE system.public.tenant_usage
58   TABLE system.public.span_stats_tenant_boundaries
61   TABLE system.public.statement_activity
62   TABLE system.public.transaction_activity
106  TABLE test.public.t

# The tests below test semantics around named zone for the system tenant. The
# system tenant is allowed to alter all named zones. All named zones bar
# RANGE DEFAULT can be deleted.
subtest named_zones_system_tenant

statement ok
ALTER RANGE liveness CONFIGURE ZONE USING num_replicas=3;

statement ok
ALTER RANGE liveness CONFIGURE ZONE DISCARD

statement ok
ALTER RANGE meta CONFIGURE ZONE USING num_replicas=3

statement ok
ALTER RANGE meta CONFIGURE ZONE DISCARD

statement ok
ALTER RANGE timeseries CONFIGURE ZONE USING num_replicas=3

statement ok
ALTER RANGE timeseries CONFIGURE ZONE DISCARD

statement ok
ALTER RANGE system CONFIGURE ZONE USING num_replicas=3

statement ok
ALTER RANGE system CONFIGURE ZONE DISCARD

statement ok
ALTER RANGE tenants CONFIGURE ZONE USING num_replicas=3

statement ok
ALTER RANGE tenants CONFIGURE ZONE DISCARD

statement ok
ALTER RANGE default CONFIGURE ZONE USING num_replicas=3

# Removing RANGE DEFAULT is not allowed (for both host and secondary tenants)
statement error pq: cannot remove default zone
ALTER RANGE default CONFIGURE ZONE DISCARD


# Regression test for github issue #93614, in which zone configurations
# for dropped tables were not translated to span configurations.
subtest regression_93614

statement ok
CREATE DATABASE db2;
CREATE TABLE db2.t (i INT PRIMARY KEY);

let $t_id
SELECT 'db2.t'::REGCLASS::INT

# Alter the zone configuration and drop the table in the same transaction
# because the reconciler listens to both system.zones and system.descriptor
# changes. After this transaction commits, there shall be no further updates
# in either of these tables for id = $t_id.
statement ok
BEGIN;
ALTER TABLE db2.t CONFIGURE ZONE USING range_max_bytes = 64<<20, range_min_bytes = 1<<20;
DROP TABLE db2.t;
COMMIT;

# Wait for the span configs corresponding to table db2.t to be reconciled.
query T retry
SELECT crdb_internal.pb_to_json('cockroach.roachpb.SpanConfig', config)
FROM system.span_configurations
WHERE end_key > (SELECT crdb_internal.table_span($t_id)[1])
----

statement ok
CREATE TABLE db2.t2 (i INT PRIMARY KEY);

statement ok
ALTER TABLE db2.t2 CONFIGURE ZONE USING range_max_bytes = 1<<30, range_min_bytes = 1<<26;

statement ok
ALTER DATABASE db2 CONFIGURE ZONE USING gc.ttlseconds = 90001;

# Both the dropped and the new table should eventually have span configurations
# and they should both inherit from the database's GC TTL setting.
query TT retry
SELECT
  crdb_internal.pretty_key(start_key, -1),
  crdb_internal.pb_to_json('cockroach.roachpb.SpanConfig', config)
FROM system.span_configurations
WHERE end_key > (SELECT crdb_internal.table_span($t_id)[1])
ORDER BY start_key
----

subtest transactional_schemachanges

statement ok
ALTER RANGE meta CONFIGURE ZONE DISCARD; ALTER RANGE meta CONFIGURE ZONE DISCARD;

subtest end
