# backup-dropped-desctiprors tests backup and restore interaction with database, schema
# and type descriptors in the DROP state.
subtest dropped-database-descriptors

new-cluster name=s1
----

exec-sql
SET use_declarative_schema_changer = 'on';
----

exec-sql
SET CLUSTER SETTING jobs.debug.pausepoints = 'newschemachanger.before.exec';
----

exec-sql
CREATE DATABASE dd;
CREATE TABLE dd.foo (id INT);
CREATE SCHEMA dd.s;
----

let $dd_id
SELECT id FROM system.namespace WHERE name = 'dd' AND "parentID" = 0;
----

new-schema-change expect-pausepoint
DROP DATABASE dd CASCADE;
----
job paused at pausepoint

# At this point, we have a descriptor entry for `dd` in a dropped state.
query-sql
WITH tbls AS (
	SELECT id, crdb_internal.pb_to_json('cockroach.sql.sqlbase.Descriptor', descriptor) AS orig FROM system.descriptor
)
SELECT orig->'database'->'name', orig->'database'->'state' FROM tbls WHERE id = $dd_id;
----
"dd" "DROP"

# A database backup should fail since we are explicitly targeting a dropped
# object.
exec-sql
BACKUP DATABASE dd INTO 'nodelocal://1/dropped-database';
----
pq: failed to resolve targets specified in the BACKUP stmt: database "dd" does not exist, or invalid RESTORE timestamp: supplied backups do not cover requested time

# A cluster backup should succeed.
exec-sql
BACKUP INTO 'nodelocal://1/cluster/dropped-database';
----

# The dropped descriptors should not end up in the cluster backup.
query-sql
SELECT count(*)
  FROM [SHOW BACKUP LATEST IN 'nodelocal://1/cluster/dropped-database']
  WHERE object_name IN ('dd', 'foo', 's');
----
0

subtest end

# Test backup/restore interaction with dropped schema and type in a database.
subtest dropped-schema-descriptors

new-cluster name=s2
----

exec-sql
CREATE DATABASE d2;
----

exec-sql
CREATE TABLE d2.t2 (id INT);
----

exec-sql
CREATE TYPE d2.typ AS ENUM ('hello');
CREATE SCHEMA d2.s;
CREATE TABLE d2.s.t (id INT);
----

let $d2_id
SELECT id FROM system.namespace WHERE name = 'd2' AND "parentID" = 0;
----

let $s_id
SELECT id FROM system.namespace WHERE name = 's' AND "parentID" = $d2_id;
----

let $typ_id
SELECT id FROM system.namespace WHERE name = 'typ' AND "parentID" = $d2_id;
----

let $typArray_id
SELECT id FROM system.namespace WHERE name = '_typ' AND "parentID" = $d2_id;
----

exec-sql
SET use_declarative_schema_changer = 'on';
----

exec-sql
SET CLUSTER SETTING jobs.debug.pausepoints = 'newschemachanger.before.exec';
----

new-schema-change expect-pausepoint
DROP SCHEMA d2.s CASCADE;
----
job paused at pausepoint

new-schema-change expect-pausepoint
DROP TYPE d2.typ;
----
job paused at pausepoint

query-sql
WITH tbls AS (
	SELECT id, crdb_internal.pb_to_json('cockroach.sql.sqlbase.Descriptor', descriptor) AS orig FROM system.descriptor
)
SELECT orig->'schema'->'name', orig->'schema'->'state' FROM tbls WHERE id = $s_id;
----
"s" "DROP"

query-sql
WITH tbls AS (
	SELECT id, crdb_internal.pb_to_json('cockroach.sql.sqlbase.Descriptor', descriptor) AS orig FROM system.descriptor
)
SELECT orig->'type'->'name', orig->'type'->'state' FROM tbls WHERE id = $typ_id OR id = $typArray_id;
----
"typ" "DROP"
"_typ" "DROP"

# A database backup should succeed and should not include the dropped schema,
# type, and table.
exec-sql
BACKUP DATABASE d2 INTO 'nodelocal://1/dropped-schema-in-database';
----

query-sql
SELECT count(*)
  FROM [SHOW BACKUP LATEST IN 'nodelocal://1/dropped-schema-in-database']
  WHERE object_name IN ('s', 't', 'typ', '_typ');
----
0

# A cluster backup should succeed but should not include the dropped schema,
# type, and table.
exec-sql
BACKUP INTO 'nodelocal://1/cluster/dropped-schema-in-database';
----

query-sql
SELECT count(*)
  FROM [SHOW BACKUP LATEST IN 'nodelocal://1/cluster/dropped-schema-in-database']
  WHERE object_name IN ('s', 't', 'typ', '_typ');
----
0

# Restore the backups to check they are valid.
exec-sql
RESTORE DATABASE d2 FROM LATEST IN 'nodelocal://1/dropped-schema-in-database' WITH new_db_name = 'd3';
----

exec-sql
USE d3;
----

# We expect to not see the dropped schema 's'.
query-sql
SELECT schema_name FROM [SHOW SCHEMAS] ORDER BY 1;
----
crdb_internal
information_schema
pg_catalog
pg_extension
public


query-sql
SELECT schema_name, table_name FROM [SHOW TABLES];
----
public t2

exec-sql
RESTORE DATABASE d2 FROM LATEST IN 'nodelocal://1/cluster/dropped-schema-in-database' WITH new_db_name ='d4';
----

exec-sql
USE d4;
----

query-sql
SELECT schema_name FROM [SHOW SCHEMAS] ORDER BY 1;
----
crdb_internal
information_schema
pg_catalog
pg_extension
public

query-sql
SELECT schema_name, table_name FROM [SHOW TABLES];
----
public t2

subtest end
