new-cluster name=s1
----

subtest double-db

exec-sql
CREATE DATABASE db1;
DROP DATABASE db1;
CREATE DATABASE db1;
----

exec-sql
BACKUP INTO 'nodelocal://1/doubleDB' WITH revision_history;
----

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

exec-sql
RESTORE FROM LATEST IN 'nodelocal://1/doubleDB'
----

exec-sql
DROP DATABASE db1;
----

exec-sql
RESTORE DATABASE db1 FROM LATEST IN 'nodelocal://1/doubleDB';
----

query-sql
SELECT count(*) FROM [SHOW DATABASES] WHERE database_name = 'db1'
----
1

subtest end

subtest privileges-zone-config

exec-sql
CREATE DATABASE test;
SET database = test;
CREATE USER testuser;
CREATE TABLE test_table();
CREATE TABLE test_table2();
GRANT ZONECONFIG ON DATABASE test TO testuser;
GRANT ZONECONFIG ON test_table TO testuser;
GRANT ALL ON test_table2 TO testuser;
----

exec-sql
BACKUP INTO 'nodelocal://1/priv-zone-cfg'
----

new-cluster name=s3 share-io-dir=s2
----

exec-sql
RESTORE FROM LATEST IN 'nodelocal://1/priv-zone-cfg';
----

query-sql
show grants on database test;
----
test admin ALL true
test public CONNECT false
test root ALL true
test testuser ZONECONFIG false

query-sql
show grants on test.test_table
----
test public test_table admin ALL true
test public test_table root ALL true
test public test_table testuser ZONECONFIG false

query-sql
show grants on test.test_table2
----
test public test_table2 admin ALL true
test public test_table2 root ALL true
test public test_table2 testuser ALL false

subtest end

subtest create-without-interleave

exec-sql
CREATE DATABASE newtest;
----

exec-sql
SET database = newtest;
----

exec-sql
CREATE TABLE t1 (k INT8 PRIMARY KEY, v1 INT8);
----

exec-sql
ALTER TABLE t1 ADD COLUMN v2 INT8;
----

exec-sql
CREATE INDEX t1_v2 ON t1 (v2);
----

exec-sql
CREATE TABLE t2 (k INT8 PRIMARY KEY, v1 INT8, v2 INT8);
----

exec-sql
CREATE TABLE t3 (k INT8 PRIMARY KEY);
----

exec-sql
CREATE TABLE t4 (k INT8 PRIMARY KEY, v1 INT8, v2 INT8, CONSTRAINT fk_t3 FOREIGN KEY (k) REFERENCES t3);
----

exec-sql
INSERT INTO t1 VALUES (1, 1, 1), (2, 2, 2), (3, 3, 3);
----

exec-sql
INSERT INTO t2 VALUES (1, 1, 1), (2, 2, 2), (3, 3, 3);
----

exec-sql
INSERT INTO t3 VALUES (1), (2), (3);
----

exec-sql
INSERT INTO t4 VALUES (1, 1, 1), (2, 2, 2), (3, 3, 3);
----

exec-sql
BACKUP INTO 'nodelocal://1/foo/create-without-interleave'
----

exec-sql
CREATE DATABASE "restore-test"
----

exec-sql
RESTORE newtest.* FROM LATEST IN 'nodelocal://1/foo/create-without-interleave' WITH into_db='restore-test'
----

exec-sql
USE "restore-test"
----

query-sql
SELECT * FROM "restore-test".t1 ORDER BY k
----
1 1 1
2 2 2
3 3 3

query-sql
SELECT * FROM "restore-test".t2 ORDER BY k
----
1 1 1
2 2 2
3 3 3

query-sql
SELECT * FROM "restore-test".t4 ORDER BY k
----
1 1 1
2 2 2
3 3 3

subtest end

# incorrect-schema-id is a regression test to ensure that we use the correct schema ID
# when checking for object collision during restore.
subtest incorrect-schema-id

new-cluster name=s4
----

exec-sql
CREATE DATABASE b;
----

# The ID of "me" is normally 109. However, we can't add an assertion for that since ID
# generation is not deterministic.
exec-sql
CREATE SCHEMA b.me;
----

exec-sql
CREATE TABLE b.me.foo (id INT);
----

exec-sql
INSERT INTO b.me.foo VALUES (1), (2), (3);
----

exec-sql
BACKUP INTO 'nodelocal://1/cluster'
----

new-cluster name=s5 share-io-dir=s4
----

exec-sql
CREATE DATABASE d
----

exec-sql
CREATE SCHEMA d.foo;
CREATE SCHEMA d.bar;
CREATE SCHEMA d.baz;
----

# The ID of "collide" is normally 109. However, we can't add an assertion for that since ID
# generation is not deterministic.
exec-sql
CREATE DATABASE collide
----

exec-sql
RESTORE TABLE b.me.foo FROM LATEST IN 'nodelocal://1/cluster' WITH into_db=collide
----

query-sql
SELECT * FROM collide.me.foo ORDER BY id
----
1
2
3

exec-sql
CREATE DATABASE d2;
CREATE SCHEMA d2.me;
----

exec-sql
RESTORE TABLE b.me.foo FROM LATEST IN 'nodelocal://1/cluster' WITH into_db=d2
----

query-sql
SELECT * FROM d2.me.foo ORDER BY id
----
1
2
3

exec-sql expect-error-regex=relation \"foo\" already exists
RESTORE TABLE b.me.foo FROM LATEST IN 'nodelocal://1/cluster' WITH into_db=d2
----
regex matches error

subtest end
