new-cluster name=s1 allow-implicit-access
----

# Create test schedules.

exec-sql
create schedule datatest for backup into 'nodelocal://1/example-schedule' recurring '@daily' full backup '@weekly';
----

let $fullID $incID
with schedules as (show schedules) select id from schedules where label='datatest' order by command->>'backup_type' asc;
----

query-sql
with schedules as (show schedules) select id, state, recurrence, owner, command from schedules where label='datatest' order by command->>'backup_type' asc;
----
$fullID <nil> @weekly root {"backup_statement": "BACKUP INTO 'nodelocal://1/example-schedule' WITH OPTIONS (detached)", "chain_protected_timestamp_records": true, "dependent_schedule_id": $incID, "unpause_on_success": $incID}
$incID Waiting for initial backup to complete @daily root {"backup_statement": "BACKUP INTO LATEST IN 'nodelocal://1/example-schedule' WITH OPTIONS (detached)", "backup_type": 1, "chain_protected_timestamp_records": true, "dependent_schedule_id": $fullID}

# Disable incremental backup.

exec-sql
alter backup schedule $fullID set full backup always;
----

query-sql
with schedules as (show schedules) select id, state, recurrence, owner, command from schedules where label='datatest' order by command->>'backup_type' asc;
----
$fullID <nil> @daily root {"backup_statement": "BACKUP INTO 'nodelocal://1/example-schedule' WITH OPTIONS (detached)", "chain_protected_timestamp_records": true}

# Verify idempotency.

exec-sql
alter backup schedule $fullID set full backup always;
alter backup schedule $fullID set full backup always;
alter backup schedule $fullID set full backup always;
----

query-sql
with schedules as (show schedules) select id, state, recurrence, owner, command from schedules where label='datatest' order by command->>'backup_type' asc;
----
$fullID <nil> @daily root {"backup_statement": "BACKUP INTO 'nodelocal://1/example-schedule' WITH OPTIONS (detached)", "chain_protected_timestamp_records": true}

# Change cadence (of full backup.)

exec-sql
alter backup schedule $fullID set recurring '@weekly';
----

query-sql
with schedules as (show schedules) select id, state, recurrence, owner, command from schedules where label='datatest' order by command->>'backup_type' asc;
----
$fullID <nil> @weekly root {"backup_statement": "BACKUP INTO 'nodelocal://1/example-schedule' WITH OPTIONS (detached)", "chain_protected_timestamp_records": true}

# Verify idempotency.

exec-sql
alter backup schedule $fullID set recurring '@weekly';
alter backup schedule $fullID set recurring '@weekly';
alter backup schedule $fullID set recurring '@weekly';
----

query-sql
with schedules as (show schedules) select id, state, recurrence, owner, command from schedules where label='datatest' order by command->>'backup_type' asc;
----
$fullID <nil> @weekly root {"backup_statement": "BACKUP INTO 'nodelocal://1/example-schedule' WITH OPTIONS (detached)", "chain_protected_timestamp_records": true}

# Add incremental backup.

exec-sql
alter backup schedule $fullID set full backup '0 0 1 * *';
----

let $incID
with schedules as (show schedules) select id from schedules where label='datatest' and id != $fullID;
----

query-sql
with schedules as (show schedules) select id, state, recurrence, owner, command from schedules where label='datatest' order by command->>'backup_type' asc;
----
$fullID <nil> 0 0 1 * * root {"backup_statement": "BACKUP INTO 'nodelocal://1/example-schedule' WITH OPTIONS (detached)", "chain_protected_timestamp_records": true, "dependent_schedule_id": $incID, "unpause_on_success": $incID}
$incID Waiting for initial backup to complete @weekly root {"backup_statement": "BACKUP INTO LATEST IN 'nodelocal://1/example-schedule' WITH OPTIONS (detached)", "backup_type": 1, "chain_protected_timestamp_records": true, "dependent_schedule_id": $fullID}

# Verify idempotency.

exec-sql
alter backup schedule $fullID set full backup '0 0 1 * *';
alter backup schedule $fullID set full backup '0 0 1 * *';
alter backup schedule $fullID set full backup '0 0 1 * *';
----

query-sql
with schedules as (show schedules) select id, state, recurrence, owner, command from schedules where label='datatest' order by command->>'backup_type' asc;
----
$fullID <nil> 0 0 1 * * root {"backup_statement": "BACKUP INTO 'nodelocal://1/example-schedule' WITH OPTIONS (detached)", "chain_protected_timestamp_records": true, "dependent_schedule_id": $incID, "unpause_on_success": $incID}
$incID Waiting for initial backup to complete @weekly root {"backup_statement": "BACKUP INTO LATEST IN 'nodelocal://1/example-schedule' WITH OPTIONS (detached)", "backup_type": 1, "chain_protected_timestamp_records": true, "dependent_schedule_id": $fullID}

# Change cadence (of incremental backup.)

exec-sql
-- Using full ID instead of incremental, as it should be fully interchangeable.
alter backup schedule $fullID set recurring '@daily';
----

query-sql
with schedules as (show schedules) select id, state, recurrence, owner, command from schedules where label='datatest' order by command->>'backup_type' asc;
----
$fullID <nil> 0 0 1 * * root {"backup_statement": "BACKUP INTO 'nodelocal://1/example-schedule' WITH OPTIONS (detached)", "chain_protected_timestamp_records": true, "dependent_schedule_id": $incID, "unpause_on_success": $incID}
$incID Waiting for initial backup to complete @daily root {"backup_statement": "BACKUP INTO LATEST IN 'nodelocal://1/example-schedule' WITH OPTIONS (detached)", "backup_type": 1, "chain_protected_timestamp_records": true, "dependent_schedule_id": $fullID}

# Verify idempotency

exec-sql
alter backup schedule $fullID set recurring '@daily';
alter backup schedule $fullID set recurring '@daily';
alter backup schedule $fullID set recurring '@daily';
----

query-sql
with schedules as (show schedules) select id, state, recurrence, owner, command from schedules where label='datatest' order by command->>'backup_type' asc;
----
$fullID <nil> 0 0 1 * * root {"backup_statement": "BACKUP INTO 'nodelocal://1/example-schedule' WITH OPTIONS (detached)", "chain_protected_timestamp_records": true, "dependent_schedule_id": $incID, "unpause_on_success": $incID}
$incID Waiting for initial backup to complete @daily root {"backup_statement": "BACKUP INTO LATEST IN 'nodelocal://1/example-schedule' WITH OPTIONS (detached)", "backup_type": 1, "chain_protected_timestamp_records": true, "dependent_schedule_id": $fullID}

# Change cadence (of full backup, while incremental exists.)

exec-sql
-- Using incremental ID instead of full, as it should be fully interchangeable.
alter backup schedule $incID set full backup '@weekly';
----

query-sql
with schedules as (show schedules) select id, state, recurrence, owner, command from schedules where label='datatest' order by command->>'backup_type' asc;
----
$fullID <nil> @weekly root {"backup_statement": "BACKUP INTO 'nodelocal://1/example-schedule' WITH OPTIONS (detached)", "chain_protected_timestamp_records": true, "dependent_schedule_id": $incID, "unpause_on_success": $incID}
$incID Waiting for initial backup to complete @daily root {"backup_statement": "BACKUP INTO LATEST IN 'nodelocal://1/example-schedule' WITH OPTIONS (detached)", "backup_type": 1, "chain_protected_timestamp_records": true, "dependent_schedule_id": $fullID}

# Verify idempotency.

exec-sql
alter backup schedule $incID set full backup '@weekly';
alter backup schedule $incID set full backup '@weekly';
alter backup schedule $incID set full backup '@weekly';
----

query-sql
with schedules as (show schedules) select id, state, recurrence, owner, command from schedules where label='datatest' order by command->>'backup_type' asc;
----
$fullID <nil> @weekly root {"backup_statement": "BACKUP INTO 'nodelocal://1/example-schedule' WITH OPTIONS (detached)", "chain_protected_timestamp_records": true, "dependent_schedule_id": $incID, "unpause_on_success": $incID}
$incID Waiting for initial backup to complete @daily root {"backup_statement": "BACKUP INTO LATEST IN 'nodelocal://1/example-schedule' WITH OPTIONS (detached)", "backup_type": 1, "chain_protected_timestamp_records": true, "dependent_schedule_id": $fullID}

# Alter full and incremental cadence in the same command.

exec-sql
alter backup schedule $incID set full backup '0 0 1 * *', set recurring '@weekly';
----

query-sql
with schedules as (show schedules) select id, state, recurrence, owner, command from schedules where label='datatest' order by command->>'backup_type' asc;
----
$fullID <nil> 0 0 1 * * root {"backup_statement": "BACKUP INTO 'nodelocal://1/example-schedule' WITH OPTIONS (detached)", "chain_protected_timestamp_records": true, "dependent_schedule_id": $incID, "unpause_on_success": $incID}
$incID Waiting for initial backup to complete @weekly root {"backup_statement": "BACKUP INTO LATEST IN 'nodelocal://1/example-schedule' WITH OPTIONS (detached)", "backup_type": 1, "chain_protected_timestamp_records": true, "dependent_schedule_id": $fullID}

# Verify idempotency.

exec-sql
alter backup schedule $incID set full backup '0 0 1 * *', set recurring '@weekly';
alter backup schedule $incID set full backup '0 0 1 * *', set recurring '@weekly';
alter backup schedule $incID set full backup '0 0 1 * *', set recurring '@weekly';
----

query-sql
with schedules as (show schedules) select id, state, recurrence, owner, command from schedules where label='datatest' order by command->>'backup_type' asc;
----
$fullID <nil> 0 0 1 * * root {"backup_statement": "BACKUP INTO 'nodelocal://1/example-schedule' WITH OPTIONS (detached)", "chain_protected_timestamp_records": true, "dependent_schedule_id": $incID, "unpause_on_success": $incID}
$incID Waiting for initial backup to complete @weekly root {"backup_statement": "BACKUP INTO LATEST IN 'nodelocal://1/example-schedule' WITH OPTIONS (detached)", "backup_type": 1, "chain_protected_timestamp_records": true, "dependent_schedule_id": $fullID}

# Can't set incremental schedule to be slower than full.

exec-sql expect-error-regex=(incremental backups must occur more often than full backups)
alter backup schedule $fullID set recurring '@weekly', set full backup '@daily';
----
regex matches error

# Remove incremental backup and change full cadence in the same command.

exec-sql
alter backup schedule $fullID set full backup always, set recurring '@daily';
----

query-sql
with schedules as (show schedules) select id, state, recurrence, owner, command from schedules where label='datatest' order by command->>'backup_type' asc;
----
$fullID <nil> @daily root {"backup_statement": "BACKUP INTO 'nodelocal://1/example-schedule' WITH OPTIONS (detached)", "chain_protected_timestamp_records": true}

# Verify idempotency.

exec-sql
alter backup schedule $fullID set full backup always, set recurring '@daily';
alter backup schedule $fullID set full backup always, set recurring '@daily';
alter backup schedule $fullID set full backup always, set recurring '@daily';
----

query-sql
with schedules as (show schedules) select id, state, recurrence, owner, command from schedules where label='datatest' order by command->>'backup_type' asc;
----
$fullID <nil> @daily root {"backup_statement": "BACKUP INTO 'nodelocal://1/example-schedule' WITH OPTIONS (detached)", "chain_protected_timestamp_records": true}
