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

skip-under-duress
----

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

set-cluster-setting setting=sql.multiregion.system_database_multiregion.enabled value=true
----

exec-sql
ALTER DATABASE system SET PRIMARY REGION "us-east-1";
CREATE DATABASE d PRIMARY REGION "us-east-1" REGIONS "us-west-1", "eu-central-1" SURVIVE REGION FAILURE;
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

exec-sql
BACKUP DATABASE d INTO 'nodelocal://1/database_backup/';
----

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

# A new cluster with the same locality settings.
new-cluster name=s2 share-io-dir=s1 allow-implicit-access disable-tenant localities=us-east-1,us-west-1,eu-central-1
----

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

exec-sql
DROP DATABASE d;
----

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

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

# A new cluster with different localities settings.
new-cluster name=s3 share-io-dir=s1 allow-implicit-access disable-tenant localities=eu-central-1,eu-north-1
----

exec-sql
RESTORE DATABASE d FROM LATEST IN 'nodelocal://1/database_backup/';
----
pq: detected a mismatch in regions between the restore cluster and the backup cluster, missing regions detected: us-east-1, us-west-1.
HINT: there are two ways you can resolve this issue: 1) update the cluster to which you're restoring to ensure that the regions present on the nodes' --locality flags match those present in the backup image, or 2) restore with the "skip_localities_check" option

exec-sql
RESTORE FROM LATEST IN 'nodelocal://1/full_cluster_backup/';
----
pq: detected a mismatch in regions between the restore cluster and the backup cluster, missing regions detected: us-east-1, us-west-1.
HINT: there are two ways you can resolve this issue: 1) update the cluster to which you're restoring to ensure that the regions present on the nodes' --locality flags match those present in the backup image, or 2) restore with the "skip_localities_check" option

exec-sql
RESTORE FROM LATEST IN 'nodelocal://1/full_cluster_backup/' WITH skip_localities_check;
----

exec-sql
INSERT INTO d.t VALUES (4);
----

exec-sql
SET enable_multiregion_placement_policy='true';
ALTER DATABASE d SURVIVE ZONE FAILURE;
ALTER DATABASE d PLACEMENT RESTRICTED;
ALTER DATABASE d SET PRIMARY REGION 'eu-central-1';
ALTER DATABASE d DROP REGION 'us-east-1';
ALTER DATABASE d DROP REGION 'us-west-1';
ALTER DATABASE d ADD REGION 'eu-north-1';
ALTER DATABASE d SET SECONDARY REGION 'eu-north-1';
----

exec-sql
RESTORE DATABASE d FROM LATEST IN 'nodelocal://1/database_backup/' WITH skip_localities_check, new_db_name='d_new';
----

exec-sql
INSERT INTO d_new.t VALUES (4);
----

exec-sql
SET enable_multiregion_placement_policy='true';
ALTER DATABASE d_new SURVIVE ZONE FAILURE;
ALTER DATABASE d PLACEMENT RESTRICTED;
ALTER DATABASE d_new SET PRIMARY REGION 'eu-central-1';
ALTER DATABASE d_new DROP REGION 'us-east-1';
ALTER DATABASE d_new DROP REGION 'us-west-1';
ALTER DATABASE d_new ADD REGION 'eu-north-1';
ALTER DATABASE d_new SET SECONDARY REGION 'eu-north-1';
----

exec-sql
DROP DATABASE d_new;
----

exec-sql
DROP DATABASE d;
----

exec-sql
DROP DATABASE data;
----

# Create a database with no regions to check default primary regions.
exec-sql
CREATE DATABASE no_region_db;
----

exec-sql
CREATE TABLE no_region_db.t (x INT);
INSERT INTO no_region_db.t VALUES (1), (2), (3);
CREATE DATABASE no_region_db_2;
CREATE TABLE no_region_db_2.t (x INT);
INSERT INTO no_region_db_2.t VALUES (1), (2), (3);
----

exec-sql
BACKUP DATABASE no_region_db INTO 'nodelocal://1/no_region_database_backup/';
----

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

exec-sql
DROP DATABASE no_region_db;
----

exec-sql
DROP DATABASE no_region_db_2;
----

set-cluster-setting setting=sql.defaults.primary_region value=non-existent-region
----

exec-sql
RESTORE DATABASE no_region_db FROM LATEST IN 'nodelocal://1/no_region_database_backup/';
----
pq: region "non-existent-region" does not exist
HINT: valid regions: eu-central-1, eu-north-1
--
set the default PRIMARY REGION to a region that exists (see SHOW REGIONS FROM CLUSTER) then using SET CLUSTER SETTING sql.defaults.primary_region = 'region'

set-cluster-setting setting=sql.defaults.primary_region value=eu-central-1
----

exec-sql
RESTORE DATABASE no_region_db FROM LATEST IN 'nodelocal://1/no_region_database_backup/';
----
NOTICE: setting the PRIMARY REGION as eu-central-1 on database no_region_db
HINT: to change the default primary region, use SET CLUSTER SETTING sql.defaults.primary_region = 'region' or use RESET CLUSTER SETTING sql.defaults.primary_region to disable this behavior

query-sql
SHOW DATABASES;
----
defaultdb root <nil> <nil> {} <nil>
no_region_db root eu-central-1  {eu-central-1} zone
postgres root <nil> <nil> {} <nil>
system node <nil> <nil> {} <nil>

query-sql
USE no_region_db;
SELECT schema_name, table_name, type, owner, locality FROM [SHOW TABLES];
----
public t table root REGIONAL BY TABLE IN PRIMARY REGION

exec-sql
CREATE DATABASE eu_central_db;
CREATE TABLE eu_central_db.t (x INT);
INSERT INTO eu_central_db.t VALUES (1), (2), (3);
----
NOTICE: defaulting to 'WITH PRIMARY REGION "eu-central-1"' as no primary region was specified

exec-sql
BACKUP DATABASE eu_central_db INTO 'nodelocal://1/eu_central_database_backup/';
----

# New cluster for a cluster backup.
new-cluster name=s4 share-io-dir=s1 allow-implicit-access disable-tenant localities=eu-central-1,eu-north-1
----

set-cluster-setting setting=sql.defaults.primary_region value=eu-north-1
----

exec-sql
RESTORE FROM LATEST IN 'nodelocal://1/no_region_cluster_backup/';
----
NOTICE: setting the PRIMARY REGION as eu-north-1 on database defaultdb
HINT: to change the default primary region, use SET CLUSTER SETTING sql.defaults.primary_region = 'region' or use RESET CLUSTER SETTING sql.defaults.primary_region to disable this behavior
NOTICE: setting the PRIMARY REGION as eu-north-1 on database postgres
HINT: to change the default primary region, use SET CLUSTER SETTING sql.defaults.primary_region = 'region' or use RESET CLUSTER SETTING sql.defaults.primary_region to disable this behavior
NOTICE: setting the PRIMARY REGION as eu-north-1 on database no_region_db
HINT: to change the default primary region, use SET CLUSTER SETTING sql.defaults.primary_region = 'region' or use RESET CLUSTER SETTING sql.defaults.primary_region to disable this behavior
NOTICE: setting the PRIMARY REGION as eu-north-1 on database no_region_db_2
HINT: to change the default primary region, use SET CLUSTER SETTING sql.defaults.primary_region = 'region' or use RESET CLUSTER SETTING sql.defaults.primary_region to disable this behavior

query-sql
SHOW DATABASES;
----
defaultdb root eu-north-1  {eu-north-1} zone
no_region_db root eu-north-1  {eu-north-1} zone
no_region_db_2 root eu-north-1  {eu-north-1} zone
postgres root eu-north-1  {eu-north-1} zone
system node <nil> <nil> {} <nil>

query-sql
USE no_region_db;
SELECT schema_name, table_name, type, owner, locality FROM [SHOW TABLES];
----
public t table root REGIONAL BY TABLE IN PRIMARY REGION

# Check we can restore without triggering the default primary region.
exec-sql
RESTORE DATABASE eu_central_db FROM LATEST IN 'nodelocal://1/eu_central_database_backup/';
----

query-sql
SHOW DATABASES;
----
defaultdb root eu-north-1  {eu-north-1} zone
eu_central_db root eu-central-1  {eu-central-1} zone
no_region_db root eu-north-1  {eu-north-1} zone
no_region_db_2 root eu-north-1  {eu-north-1} zone
postgres root eu-north-1  {eu-north-1} zone
system node <nil> <nil> {} <nil>
