# disabled to run within tenant because multiregion primitives are not supported within tenant
subtest mrbackup

new-cluster name=s1 allow-implicit-access disable-tenant localities=us-east-1,us-west-1,eu-central-1
----

exec-sql
CREATE DATABASE d PRIMARY REGION "us-east-1" REGIONS "us-west-1", "eu-central-1";
CREATE TABLE d.t (x INT);
INSERT INTO d.t VALUES (1), (2), (3);
----

query-sql
SELECT region FROM [SHOW REGIONS FROM DATABASE d] ORDER BY 1;
----
eu-central-1
us-east-1
us-west-1

query-sql
SHOW DATABASES;
----
d root us-east-1  {eu-central-1,us-east-1,us-west-1} zone
data root <nil> <nil> {} <nil>
defaultdb root <nil> <nil> {} <nil>
postgres root <nil> <nil> {} <nil>
system node <nil> <nil> {} <nil>

# backup a MR database, table, and cluster
exec-sql
BACKUP DATABASE d INTO 'nodelocal://1/database_backup/';
----

exec-sql
BACKUP TABLE d.t INTO 'nodelocal://1/table_backup/';
----

exec-sql
BACKUP INTO 'nodelocal://1/cluster_backup/';
----

subtest end

# create cluster with a single region
new-cluster name=s2 share-io-dir=s1 allow-implicit-access localities=us-east-1
----

# test cluster restore with remove_regions
subtest restore_regionless_on_single_region_cluster

query-sql
SHOW DATABASES;
----
defaultdb root <nil> <nil> {} <nil>
postgres root <nil> <nil> {} <nil>
system node <nil> <nil> {} <nil>

exec-sql
RESTORE FROM LATEST IN 'nodelocal://1/cluster_backup/' WITH remove_regions;
----

# check cluster's regions
query-sql
SHOW REGIONS FROM CLUSTER;
----
us-east-1 {us-east1}

query-sql
SHOW DATABASES;
----
d root <nil> <nil> {} <nil>
data root <nil> <nil> {} <nil>
defaultdb root <nil> <nil> {} <nil>
postgres root <nil> <nil> {} <nil>
system node <nil> <nil> {} <nil>

# ensure that database d is regionless
query-sql
SELECT region FROM [SHOW REGIONS FROM DATABASE d] ORDER BY 1;
----

# show tables - make sure these are regionless as well
query-sql
SELECT table_name, locality FROM [SHOW TABLES FROM d] ORDER BY 1;
----
t <nil>

# ensure that tables belonging to d can be modified & have the correct values
exec-sql
INSERT INTO d.t VALUES (4), (5);
----

query-sql
SELECT * FROM d.t ORDER BY 1;
----
1
2
3
4
5

subtest restore_regionless_on_single_region_cluster/alter-region
# ensure we can't add a regular region
# this is due to a validation that ensures a primary region exists
# before a "regular" region exists
exec-sql
ALTER DATABASE d ADD REGION "us-east-1";
----
pq: cannot add region "us-east-1" to database d
HINT: you must add a PRIMARY REGION first using ALTER DATABASE d PRIMARY REGION "us-east-1"

# we also can't add a primary region to our regionless database out-of-the-box now...
exec-sql
ALTER DATABASE d PRIMARY REGION "us-east-1";
----
pq: zone configuration for database d has field "num_replicas" set which will be overwritten when setting the the initial PRIMARY REGION (expected=nil actual=5)
HINT: discard the zone config using CONFIGURE ZONE DISCARD before continuing

# ...let's listen to the error message
exec-sql
ALTER DATABASE d CONFIGURE ZONE DISCARD;
----

exec-sql
ALTER DATABASE d PRIMARY REGION "us-east-1";
----
pq: type "d.public.crdb_internal_region" already exists
DETAIL: multi-region databases employ an internal enum called crdb_internal_region to manage regions which conflicts with the existing object
HINT: object "crdb_internal_regions" must be renamed or dropped before adding the primary region

# ...let's listen to the error message (again)
exec-sql
DROP TYPE d.public.crdb_internal_region;
----

exec-sql
ALTER DATABASE d PRIMARY REGION "us-east-1";
----

query-sql
SELECT region FROM [SHOW REGIONS FROM DATABASE d] ORDER BY 1;
----
us-east-1

query-sql
SELECT table_name, locality FROM [SHOW TABLES FROM d] ORDER BY 1;
----
t REGIONAL BY TABLE IN PRIMARY REGION

# ensure that tables belonging to d can be modified & have the correct values
exec-sql
INSERT INTO d.t VALUES (6), (7);
----

query-sql
SELECT * FROM d.t;
----
1
2
3
4
5
6
7

subtest end

subtest end

# test db restore with remove_regions
subtest restore_regionless_on_single_region_db

exec-sql
DROP DATABASE d;
----

exec-sql
RESTORE DATABASE d FROM LATEST IN 'nodelocal://1/database_backup/' WITH remove_regions;
----

# check to see if restored database, d, shows up
query-sql
SHOW DATABASES;
----
d root <nil> <nil> {} <nil>
data root <nil> <nil> {} <nil>
defaultdb root <nil> <nil> {} <nil>
postgres root <nil> <nil> {} <nil>
system node <nil> <nil> {} <nil>

# ensure that database d is regionless
query-sql
SELECT region FROM [SHOW REGIONS FROM DATABASE d] ORDER BY 1;
----

# show tables - make sure these are regionless as well
query-sql
SELECT table_name, locality FROM [SHOW TABLES FROM d] ORDER BY 1;
----
t <nil>

# ensure that tables belonging to d can be modified & have the correct values
exec-sql
INSERT INTO d.t VALUES (4), (5);
----

query-sql
SELECT * FROM d.t ORDER BY 1;
----
1
2
3
4
5

subtest restore_regionless_on_single_region_db/alter-region
# ensure we can't add a regular region
# this is due to a validation that ensures a primary region exists
# before a "regular" region exists
exec-sql
ALTER DATABASE d ADD REGION "us-east-1";
----
pq: cannot add region "us-east-1" to database d
HINT: you must add a PRIMARY REGION first using ALTER DATABASE d PRIMARY REGION "us-east-1"

# we also can't add a primary region to our regionless database out-of-the-box now...
exec-sql
ALTER DATABASE d PRIMARY REGION "us-east-1";
----
pq: type "d.public.crdb_internal_region" already exists
DETAIL: multi-region databases employ an internal enum called crdb_internal_region to manage regions which conflicts with the existing object
HINT: object "crdb_internal_regions" must be renamed or dropped before adding the primary region

# ...let's listen to the error message
exec-sql
DROP TYPE d.public.crdb_internal_region;
----

exec-sql
ALTER DATABASE d PRIMARY REGION "us-east-1";
----

query-sql
SELECT region FROM [SHOW REGIONS FROM DATABASE d] ORDER BY 1;
----
us-east-1

query-sql
SELECT table_name, locality FROM [SHOW TABLES FROM d] ORDER BY 1;
----
t REGIONAL BY TABLE IN PRIMARY REGION

# ensure that tables belonging to d can be modified & have the correct values
exec-sql
INSERT INTO d.t VALUES (6), (7);
----

query-sql
SELECT * FROM d.t;
----
1
2
3
4
5
6
7

subtest end

subtest end

# test table restore with remove_regions
subtest restore_regionless_on_single_region_table

exec-sql
DROP DATABASE d;
----

exec-sql
CREATE DATABASE d;
----

exec-sql
RESTORE TABLE d.t FROM LATEST IN 'nodelocal://1/table_backup/' WITH remove_regions;
----

query-sql
SELECT table_name, locality FROM [SHOW TABLES FROM d] ORDER BY 1;
----
t <nil>

# ensure that tables belonging to d can be modified & have the correct values
exec-sql
INSERT INTO d.t VALUES (4), (5);
----

query-sql
SELECT * FROM d.t ORDER BY 1;
----
1
2
3
4
5

subtest restore_regionless_on_single_region_table/alter-region
# ensure we can't add a regular region
# this is due to a validation that ensures a primary region exists
# before a "regular" region exists
exec-sql
ALTER DATABASE d ADD REGION "us-east-1";
----
pq: cannot add region "us-east-1" to database d
HINT: you must add a PRIMARY REGION first using ALTER DATABASE d PRIMARY REGION "us-east-1"

exec-sql
ALTER DATABASE d PRIMARY REGION "us-east-1";
----

query-sql
SELECT region FROM [SHOW REGIONS FROM DATABASE d] ORDER BY 1;
----
us-east-1

query-sql
SELECT table_name, locality FROM [SHOW TABLES FROM d] ORDER BY 1;
----
t REGIONAL BY TABLE IN PRIMARY REGION

# ensure that tables belonging to d can be modified & have the correct values
exec-sql
INSERT INTO d.t VALUES (6), (7);
----

query-sql
SELECT * FROM d.t;
----
1
2
3
4
5
6
7

subtest end

subtest end
