# descriptor-conflicts tests that a backup with non-system descriptors at IDs
# that conflict with system tables in the restoring cluster are handled
# appropriately during a cluster restore. The conflicting system tables in the
# restoring cluster should be copied to a descriptor ID higher than any
# descriptor in the backup.
#
# disabled to run within a tenant because they cannot set zone configs
# https://github.com/cockroachdb/cockroach/issues/49854?version=v22.2

new-cluster name=s1 disable-tenant
----

exec-sql
CREATE DATABASE foo;
CREATE SCHEMA foo.bar;
CREATE TYPE foo.bar.baz AS ENUM('a', 'b', 'c');
CREATE TABLE foo.bar.bat (pk int primary key, b foo.bar.baz);
INSERT INTO foo.bar.bat VALUES (1, 'a'),(2, 'b'),(3, 'c');
ALTER TABLE foo.bar.bat CONFIGURE ZONE USING gc.ttlseconds=999;
COMMENT ON TABLE foo.bar.bat IS 'should survive';
CREATE ROLE hamburger;
ALTER ROLE hamburger IN DATABASE foo SET application_name='helper';
----

exec-sql
BACKUP INTO 'nodelocal://1/conflicting-descriptors';
----

new-cluster name=s2 share-io-dir=s1 disable-tenant
----

# Create 4 dummy system tables that will have conflicting IDs with the database,
# schema, type, and table created in s1.

create-dummy-system-table
----

create-dummy-system-table
----

create-dummy-system-table
----

create-dummy-system-table
----

exec-sql
INSERT INTO system.crdb_internal_copy_104 VALUES ('tab_104', true, now(), 'b')
----

exec-sql
INSERT INTO system.crdb_internal_copy_105 VALUES ('tab_105', true, now(), 'b')
----

exec-sql
INSERT INTO system.crdb_internal_copy_106 VALUES ('tab_106', true, now(), 'b')
----

exec-sql
INSERT INTO system.crdb_internal_copy_107 VALUES ('tab_107', true, now(), 'b')
----

exec-sql
RESTORE FROM LATEST IN 'nodelocal://1/conflicting-descriptors';
----

query-sql
SELECT count(1) FROM [SHOW TABLES FROM system] WHERE table_name LIKE 'crdb_internal_copy_%';
----
4

# We should have left the 4 system tables where they were.
query-sql
SELECT name, id FROM system.namespace WHERE id IN (104,105,106,107);
----
crdb_internal_copy_104 104
crdb_internal_copy_105 105
crdb_internal_copy_106 106
crdb_internal_copy_107 107

query-sql
SELECT name, id FROM system.namespace WHERE name LIKE 'crdb_internal_copy_%';
----
crdb_internal_copy_104 104
crdb_internal_copy_105 105
crdb_internal_copy_106 106
crdb_internal_copy_107 107

# Check that we have not written over the new system table rows.
query-sql
SELECT name, value FROM system.crdb_internal_copy_104
----
tab_104 true

query-sql
SELECT name, value FROM system.crdb_internal_copy_107
----
tab_107 true

# Sanity check that we can write to the new remapped table that had an ID that
# now belongs to one of the new system tabels.
exec-sql
INSERT INTO data.bank VALUES (100,100,'');
----

exec-sql
INSERT INTO system.crdb_internal_copy_107 VALUES ('tab_107_1', true, now(), 'b')
----

query-sql
SELECT * FROM foo.bar.bat;
----
1 a
2 b
3 c

query-sql
SHOW ZONE CONFIGURATION FROM TABLE foo.bar.bat;
----
TABLE foo.bar.bat ALTER TABLE foo.bar.bat CONFIGURE ZONE USING
	range_min_bytes = 134217728,
	range_max_bytes = 536870912,
	gc.ttlseconds = 999,
	num_replicas = 3,
	constraints = '[]',
	lease_preferences = '[]'

query-sql
SELECT comment FROM [SHOW TABLES FROM foo WITH COMMENT] WHERE table_name = 'bat';
----
should survive

query-sql
select role_name, settings FROM system.database_role_settings AS drs JOIN system.namespace AS ns ON ns.id = drs.database_id WHERE ns.name = 'foo';
----
hamburger {application_name=helper}
