Skip to content

Commit

Permalink
sql: fix formatting of import, backup and create tenant
Browse files Browse the repository at this point in the history
Informs: #99185

This commit cherry-picked changes from #107723 and add fixes
for formatting `EXPORT` options.

Release note: None
  • Loading branch information
chengxiong-ruan committed Jul 31, 2023
1 parent e57e974 commit 2468ed7
Show file tree
Hide file tree
Showing 29 changed files with 271 additions and 1,238 deletions.
4 changes: 2 additions & 2 deletions pkg/ccl/backupccl/alter_backup_schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ INSERT INTO t1 values (1), (10), (100);
require.Equal(t, []string{"PAUSED: Waiting for initial backup to complete", "ACTIVE"}, statuses)
require.Equal(t, []string{"@daily", "@weekly"}, schedules)
require.Equal(t, []string{
"BACKUP TABLE mydb.public.t1 INTO LATEST IN 'nodelocal://1/backup/alter-schedule' WITH detached",
"BACKUP TABLE mydb.public.t1 INTO 'nodelocal://1/backup/alter-schedule' WITH detached",
"BACKUP TABLE mydb.public.t1 INTO LATEST IN 'nodelocal://1/backup/alter-schedule' WITH OPTIONS (detached)",
"BACKUP TABLE mydb.public.t1 INTO 'nodelocal://1/backup/alter-schedule' WITH OPTIONS (detached)",
},
backupStmts)

Expand Down
16 changes: 8 additions & 8 deletions pkg/ccl/backupccl/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ func TestBackupAndRestoreJobDescription(t *testing.T) {
sqlDB.Exec(t, "BACKUP TO ($1,$2,$3) INCREMENTAL FROM $4", append(incrementals, backups[0])...)
sqlDB.Exec(t, "BACKUP INTO ($1, $2, $3)", collections...)
sqlDB.Exec(t, "BACKUP INTO LATEST IN ($1, $2, $3)", collections...)
sqlDB.Exec(t, "BACKUP INTO LATEST IN ($1, $2, $3) WITH incremental_location = ($4, $5, $6)",
sqlDB.Exec(t, "BACKUP INTO LATEST IN ($1, $2, $3) WITH OPTIONS (incremental_location = ($4, $5, $6))",
append(collections, incrementals...)...)

sqlDB.ExpectErr(t, "the incremental_location option must contain the same number of locality",
Expand Down Expand Up @@ -848,7 +848,7 @@ func TestBackupAndRestoreJobDescription(t *testing.T) {
collections[1], collections[2])},
{fmt.Sprintf("BACKUP INTO '%s' IN ('%s', '%s', '%s')", full1,
collections[0], collections[1], collections[2])},
{fmt.Sprintf("BACKUP INTO '%s' IN ('%s', '%s', '%s') WITH incremental_location = ('%s', '%s', '%s')",
{fmt.Sprintf("BACKUP INTO '%s' IN ('%s', '%s', '%s') WITH OPTIONS (incremental_location = ('%s', '%s', '%s'))",
full1, collections[0], collections[1], collections[2], incrementals[0],
incrementals[1], incrementals[2])},
{fmt.Sprintf("BACKUP INTO '%s' IN ('%s', '%s', '%s') AS OF SYSTEM TIME '-1s'", asOf1, collections[0],
Expand Down Expand Up @@ -904,7 +904,7 @@ func TestBackupAndRestoreJobDescription(t *testing.T) {
{fmt.Sprintf("RESTORE DATABASE data FROM ('%s', '%s', '%s')",
resolvedCollectionURIs[0], resolvedCollectionURIs[1],
resolvedCollectionURIs[2])},
{fmt.Sprintf("RESTORE DATABASE data FROM ('%s', '%s', '%s') WITH incremental_location = ('%s', '%s', '%s')",
{fmt.Sprintf("RESTORE DATABASE data FROM ('%s', '%s', '%s') WITH OPTIONS (incremental_location = ('%s', '%s', '%s'))",
resolvedCollectionURIs[0], resolvedCollectionURIs[1], resolvedCollectionURIs[2],
resolvedIncURIs[0], resolvedIncURIs[1], resolvedIncURIs[2])},
{fmt.Sprintf("RESTORE DATABASE data FROM ('%s', '%s', '%s')",
Expand Down Expand Up @@ -1308,7 +1308,7 @@ func TestBackupRestoreSystemJobs(t *testing.T) {
if err := jobutils.VerifySystemJob(t, sqlDB, 0, jobspb.TypeRestore, jobs.StatusSucceeded, jobs.Record{
Username: username.RootUserName(),
Description: fmt.Sprintf(
`RESTORE TABLE bank FROM '%s', '%s' WITH into_db = 'restoredb'`,
`RESTORE TABLE bank FROM '%s', '%s' WITH OPTIONS (into_db = 'restoredb')`,
sanitizedFullDir+"redacted", sanitizedIncDir+"redacted",
),
DescriptorIDs: descpb.IDs{
Expand Down Expand Up @@ -1395,7 +1395,7 @@ func TestEncryptedBackupRestoreSystemJobs(t *testing.T) {
jobs.Record{
Username: username.RootUserName(),
Description: fmt.Sprintf(
`BACKUP DATABASE data TO '%s' WITH %s`,
`BACKUP DATABASE data TO '%s' WITH OPTIONS (%s)`,
backupLoc1, sanitizedEncryptionOption),
DescriptorIDs: descpb.IDs{
descpb.ID(backupDatabaseID),
Expand All @@ -1414,7 +1414,7 @@ into_db='restoredb', %s)`, encryptionOption), backupLoc1)
if err := jobutils.VerifySystemJob(t, sqlDB, 0, jobspb.TypeRestore, jobs.StatusSucceeded, jobs.Record{
Username: username.RootUserName(),
Description: fmt.Sprintf(
`RESTORE TABLE data.bank FROM '%s' WITH %s, into_db = 'restoredb'`,
`RESTORE TABLE data.bank FROM '%s' WITH OPTIONS (%s, into_db = 'restoredb')`,
backupLoc1, sanitizedEncryptionOption,
),
DescriptorIDs: descpb.IDs{
Expand Down Expand Up @@ -5700,8 +5700,8 @@ func TestBackupRestoreShowJob(t *testing.T) {
sqlDB.CheckQueryResults(
t, "SELECT description FROM [SHOW JOBS] WHERE job_type != 'MIGRATION' AND description != 'updating privileges' ORDER BY description",
[][]string{
{"BACKUP DATABASE data TO 'nodelocal://1/foo' WITH revision_history = true"},
{"RESTORE TABLE data.bank FROM 'nodelocal://1/foo' WITH into_db = 'data 2', skip_missing_foreign_keys"},
{"BACKUP DATABASE data TO 'nodelocal://1/foo' WITH OPTIONS (revision_history = true)"},
{"RESTORE TABLE data.bank FROM 'nodelocal://1/foo' WITH OPTIONS (into_db = 'data 2', skip_missing_foreign_keys)"},
},
)
}
Expand Down
72 changes: 36 additions & 36 deletions pkg/ccl/backupccl/create_scheduled_backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,57 +251,57 @@ CREATE TABLE other_db.t1(a int);
{
name: "fully-qualified-table-name",
query: "CREATE SCHEDULE FOR BACKUP mydb.public.t1 INTO $1 RECURRING '@hourly'",
expectedBackupStmt: "BACKUP TABLE mydb.public.t1 INTO '%s' WITH detached",
expectedBackupStmt: "BACKUP TABLE mydb.public.t1 INTO '%s' WITH OPTIONS (detached)",
},
{
name: "schema-qualified-table-name",
query: "CREATE SCHEDULE FOR BACKUP public.t1 INTO $1 RECURRING '@hourly'",
expectedBackupStmt: "BACKUP TABLE mydb.public.t1 INTO '%s' WITH detached",
expectedBackupStmt: "BACKUP TABLE mydb.public.t1 INTO '%s' WITH OPTIONS (detached)",
},
{
name: "uds-qualified-table-name",
query: "CREATE SCHEDULE FOR BACKUP myschema.mytbl INTO $1 RECURRING '@hourly'",
expectedBackupStmt: "BACKUP TABLE mydb.myschema.mytbl INTO '%s' WITH detached",
expectedBackupStmt: "BACKUP TABLE mydb.myschema.mytbl INTO '%s' WITH OPTIONS (detached)",
},
{
name: "db-qualified-table-name",
query: "CREATE SCHEDULE FOR BACKUP mydb.t1 INTO $1 RECURRING '@hourly'",
expectedBackupStmt: "BACKUP TABLE mydb.public.t1 INTO '%s' WITH detached",
expectedBackupStmt: "BACKUP TABLE mydb.public.t1 INTO '%s' WITH OPTIONS (detached)",
},
{
name: "unqualified-table-name",
query: "CREATE SCHEDULE FOR BACKUP t1 INTO $1 RECURRING '@hourly'",
expectedBackupStmt: "BACKUP TABLE mydb.public.t1 INTO '%s' WITH detached",
expectedBackupStmt: "BACKUP TABLE mydb.public.t1 INTO '%s' WITH OPTIONS (detached)",
},
{
name: "unqualified-table-name-with-symbols",
query: `CREATE SCHEDULE FOR BACKUP "my.tbl" INTO $1 RECURRING '@hourly'`,
expectedBackupStmt: `BACKUP TABLE mydb.public."my.tbl" INTO '%s' WITH detached`,
expectedBackupStmt: `BACKUP TABLE mydb.public."my.tbl" INTO '%s' WITH OPTIONS (detached)`,
},
{
name: "table-names-from-different-db",
query: "CREATE SCHEDULE FOR BACKUP t1, other_db.t1 INTO $1 RECURRING '@hourly'",
expectedBackupStmt: "BACKUP TABLE mydb.public.t1, other_db.public.t1 INTO '%s' WITH detached",
expectedBackupStmt: "BACKUP TABLE mydb.public.t1, other_db.public.t1 INTO '%s' WITH OPTIONS (detached)",
},
{
name: "unqualified-all-tables-selectors",
query: "CREATE SCHEDULE FOR BACKUP * INTO $1 RECURRING '@hourly'",
expectedBackupStmt: "BACKUP TABLE mydb.public.* INTO '%s' WITH detached",
expectedBackupStmt: "BACKUP TABLE mydb.public.* INTO '%s' WITH OPTIONS (detached)",
},
{
name: "all-tables-selectors-with-user-defined-schema",
query: "CREATE SCHEDULE FOR BACKUP myschema.* INTO $1 RECURRING '@hourly'",
expectedBackupStmt: "BACKUP TABLE mydb.myschema.* INTO '%s' WITH detached",
expectedBackupStmt: "BACKUP TABLE mydb.myschema.* INTO '%s' WITH OPTIONS (detached)",
},
{
name: "partially-qualified-all-tables-selectors-with-different-db",
query: "CREATE SCHEDULE FOR BACKUP other_db.* INTO $1 RECURRING '@hourly'",
expectedBackupStmt: "BACKUP TABLE other_db.public.* INTO '%s' WITH detached",
expectedBackupStmt: "BACKUP TABLE other_db.public.* INTO '%s' WITH OPTIONS (detached)",
},
{
name: "fully-qualified-all-tables-selectors-with-multiple-dbs",
query: "CREATE SCHEDULE FOR BACKUP *, other_db.* INTO $1 RECURRING '@hourly'",
expectedBackupStmt: "BACKUP TABLE mydb.public.*, other_db.public.* INTO '%s' WITH detached",
expectedBackupStmt: "BACKUP TABLE mydb.public.*, other_db.public.* INTO '%s' WITH OPTIONS (detached)",
},
}

Expand Down Expand Up @@ -359,8 +359,8 @@ func TestSerializesScheduledBackupExecutionArgs(t *testing.T) {
expectedSchedules: []expectedSchedule{
{
nameRe: "BACKUP .+",
backupStmt: "BACKUP INTO 'nodelocal://1/backup?AWS_SECRET_ACCESS_KEY=neverappears' WITH detached",
shownStmt: "BACKUP INTO 'nodelocal://1/backup?AWS_SECRET_ACCESS_KEY=redacted' WITH detached",
backupStmt: "BACKUP INTO 'nodelocal://1/backup?AWS_SECRET_ACCESS_KEY=neverappears' WITH OPTIONS (detached)",
shownStmt: "BACKUP INTO 'nodelocal://1/backup?AWS_SECRET_ACCESS_KEY=redacted' WITH OPTIONS (detached)",
period: time.Hour,
},
},
Expand All @@ -372,7 +372,7 @@ func TestSerializesScheduledBackupExecutionArgs(t *testing.T) {
expectedSchedules: []expectedSchedule{
{
nameRe: "my-backup",
backupStmt: "BACKUP INTO 'nodelocal://1/backup' WITH detached",
backupStmt: "BACKUP INTO 'nodelocal://1/backup' WITH OPTIONS (detached)",
period: time.Hour,
},
},
Expand All @@ -384,7 +384,7 @@ func TestSerializesScheduledBackupExecutionArgs(t *testing.T) {
expectedSchedules: []expectedSchedule{
{
nameRe: "BACKUP .+",
backupStmt: "BACKUP INTO 'nodelocal://1/backup' WITH detached",
backupStmt: "BACKUP INTO 'nodelocal://1/backup' WITH OPTIONS (detached)",
period: time.Hour,
},
},
Expand All @@ -396,14 +396,14 @@ func TestSerializesScheduledBackupExecutionArgs(t *testing.T) {
expectedSchedules: []expectedSchedule{
{
nameRe: "BACKUP .*",
backupStmt: "BACKUP INTO LATEST IN 'nodelocal://1/backup' WITH detached",
backupStmt: "BACKUP INTO LATEST IN 'nodelocal://1/backup' WITH OPTIONS (detached)",
period: time.Hour,
paused: true,
chainProtectedTimestampRecord: true,
},
{
nameRe: "BACKUP .+",
backupStmt: "BACKUP INTO 'nodelocal://1/backup' WITH detached",
backupStmt: "BACKUP INTO 'nodelocal://1/backup' WITH OPTIONS (detached)",
period: 24 * time.Hour,
runsNow: true,
chainProtectedTimestampRecord: true,
Expand All @@ -417,14 +417,14 @@ func TestSerializesScheduledBackupExecutionArgs(t *testing.T) {
expectedSchedules: []expectedSchedule{
{
nameRe: "my-backup",
backupStmt: "BACKUP INTO LATEST IN 'nodelocal://1/backup' WITH detached",
backupStmt: "BACKUP INTO LATEST IN 'nodelocal://1/backup' WITH OPTIONS (detached)",
period: time.Hour,
paused: true,
chainProtectedTimestampRecord: true,
},
{
nameRe: "my-backup",
backupStmt: "BACKUP INTO 'nodelocal://1/backup' WITH detached",
backupStmt: "BACKUP INTO 'nodelocal://1/backup' WITH OPTIONS (detached)",
period: 24 * time.Hour,
runsNow: true,
chainProtectedTimestampRecord: true,
Expand All @@ -438,7 +438,7 @@ func TestSerializesScheduledBackupExecutionArgs(t *testing.T) {
expectedSchedules: []expectedSchedule{
{
nameRe: "BACKUP .+",
backupStmt: "BACKUP INTO 'nodelocal://1/backup' WITH revision_history = true, detached",
backupStmt: "BACKUP INTO 'nodelocal://1/backup' WITH OPTIONS (revision_history = true, detached)",
period: time.Hour,
},
},
Expand All @@ -450,14 +450,14 @@ func TestSerializesScheduledBackupExecutionArgs(t *testing.T) {
expectedSchedules: []expectedSchedule{
{
nameRe: "BACKUP .*",
backupStmt: "BACKUP INTO LATEST IN 'nodelocal://1/backup' WITH detached, incremental_location = 'nodelocal://1/incremental'",
backupStmt: "BACKUP INTO LATEST IN 'nodelocal://1/backup' WITH OPTIONS (detached, incremental_location = 'nodelocal://1/incremental')",
period: time.Hour,
paused: true,
chainProtectedTimestampRecord: true,
},
{
nameRe: "BACKUP .+",
backupStmt: "BACKUP INTO 'nodelocal://1/backup' WITH detached",
backupStmt: "BACKUP INTO 'nodelocal://1/backup' WITH OPTIONS (detached)",
period: 24 * time.Hour,
runsNow: true,
chainProtectedTimestampRecord: true,
Expand All @@ -475,15 +475,15 @@ func TestSerializesScheduledBackupExecutionArgs(t *testing.T) {
nameRe: "BACKUP .*",
backupStmt: "BACKUP TABLE system.public.jobs, " +
"system.public.scheduled_jobs INTO LATEST IN 'nodelocal://1/backup' WITH" +
" revision_history = true, detached",
" OPTIONS (revision_history = true, detached)",
period: time.Hour,
paused: true,
chainProtectedTimestampRecord: true,
},
{
nameRe: "BACKUP .+",
backupStmt: "BACKUP TABLE system.public.jobs, " +
"system.public.scheduled_jobs INTO 'nodelocal://1/backup' WITH revision_history = true, detached",
"system.public.scheduled_jobs INTO 'nodelocal://1/backup' WITH OPTIONS (revision_history = true, detached)",
period: 24 * time.Hour,
runsNow: true,
chainProtectedTimestampRecord: true,
Expand All @@ -499,14 +499,14 @@ func TestSerializesScheduledBackupExecutionArgs(t *testing.T) {
expectedSchedules: []expectedSchedule{
{
nameRe: "BACKUP .*",
backupStmt: "BACKUP DATABASE system INTO LATEST IN 'nodelocal://1/backup' WITH revision_history = true, detached",
backupStmt: "BACKUP DATABASE system INTO LATEST IN 'nodelocal://1/backup' WITH OPTIONS (revision_history = true, detached)",
period: time.Hour,
paused: true,
chainProtectedTimestampRecord: true,
},
{
nameRe: "BACKUP .+",
backupStmt: "BACKUP DATABASE system INTO 'nodelocal://1/backup' WITH revision_history = true, detached",
backupStmt: "BACKUP DATABASE system INTO 'nodelocal://1/backup' WITH OPTIONS (revision_history = true, detached)",
period: 24 * time.Hour,
runsNow: true,
chainProtectedTimestampRecord: true,
Expand All @@ -522,14 +522,14 @@ func TestSerializesScheduledBackupExecutionArgs(t *testing.T) {
expectedSchedules: []expectedSchedule{
{
nameRe: "BACKUP .*",
backupStmt: "BACKUP TABLE system.public.* INTO LATEST IN 'nodelocal://1/backup' WITH revision_history = true, detached",
backupStmt: "BACKUP TABLE system.public.* INTO LATEST IN 'nodelocal://1/backup' WITH OPTIONS (revision_history = true, detached)",
period: time.Hour,
paused: true,
chainProtectedTimestampRecord: true,
},
{
nameRe: "BACKUP .+",
backupStmt: "BACKUP TABLE system.public.* INTO 'nodelocal://1/backup' WITH revision_history = true, detached",
backupStmt: "BACKUP TABLE system.public.* INTO 'nodelocal://1/backup' WITH OPTIONS (revision_history = true, detached)",
period: 24 * time.Hour,
runsNow: true,
chainProtectedTimestampRecord: true,
Expand Down Expand Up @@ -562,14 +562,14 @@ func TestSerializesScheduledBackupExecutionArgs(t *testing.T) {
expectedSchedules: []expectedSchedule{
{
nameRe: "my_backup_name",
backupStmt: "BACKUP INTO LATEST IN 'nodelocal://1/backup' WITH revision_history = true, detached",
backupStmt: "BACKUP INTO LATEST IN 'nodelocal://1/backup' WITH OPTIONS (revision_history = true, detached)",
period: time.Hour,
paused: true,
chainProtectedTimestampRecord: true,
},
{
nameRe: "my_backup_name",
backupStmt: "BACKUP INTO 'nodelocal://1/backup' WITH revision_history = true, detached",
backupStmt: "BACKUP INTO 'nodelocal://1/backup' WITH OPTIONS (revision_history = true, detached)",
period: 24 * time.Hour,
runsNow: true,
chainProtectedTimestampRecord: true,
Expand All @@ -584,14 +584,14 @@ func TestSerializesScheduledBackupExecutionArgs(t *testing.T) {
expectedSchedules: []expectedSchedule{
{
nameRe: "my_backup_name",
backupStmt: "BACKUP INTO LATEST IN 'nodelocal://1/backup' WITH revision_history = true, detached",
backupStmt: "BACKUP INTO LATEST IN 'nodelocal://1/backup' WITH OPTIONS (revision_history = true, detached)",
period: time.Hour,
paused: true,
chainProtectedTimestampRecord: true,
},
{
nameRe: "my_backup_name",
backupStmt: "BACKUP INTO 'nodelocal://1/backup' WITH revision_history = true, detached",
backupStmt: "BACKUP INTO 'nodelocal://1/backup' WITH OPTIONS (revision_history = true, detached)",
period: 24 * time.Hour,
runsNow: true,
chainProtectedTimestampRecord: true,
Expand All @@ -609,10 +609,10 @@ func TestSerializesScheduledBackupExecutionArgs(t *testing.T) {
nameRe: "BACKUP .*",
backupStmt: "BACKUP TABLE system.public.jobs, " +
"system.public.scheduled_jobs INTO 'nodelocal://1/backup' WITH" +
" revision_history = true, encryption_passphrase = 'secret', detached",
" OPTIONS (revision_history = true, encryption_passphrase = 'secret', detached)",
shownStmt: "BACKUP TABLE system.public.jobs, " +
"system.public.scheduled_jobs INTO 'nodelocal://1/backup' WITH" +
" revision_history = true, encryption_passphrase = '*****', detached",
" OPTIONS (revision_history = true, encryption_passphrase = '*****', detached)",
period: 7 * 24 * time.Hour,
},
},
Expand All @@ -634,7 +634,7 @@ func TestSerializesScheduledBackupExecutionArgs(t *testing.T) {
nameRe: "BACKUP .+",
backupStmt: "BACKUP DATABASE system INTO " +
"('nodelocal://1/backup?COCKROACH_LOCALITY=x%3Dy', 'nodelocal://1/backup2?COCKROACH_LOCALITY=default') " +
"WITH revision_history = true, detached",
"WITH OPTIONS (revision_history = true, detached)",
period: 24 * time.Hour,
},
},
Expand All @@ -655,7 +655,7 @@ func TestSerializesScheduledBackupExecutionArgs(t *testing.T) {
{
nameRe: "BACKUP .+",
backupStmt: "BACKUP DATABASE system INTO 'nodelocal://1/backup' " +
"WITH revision_history = true, detached, execution locality = 'region=of-france'",
"WITH OPTIONS (revision_history = true, detached, execution locality = 'region=of-france')",
period: 24 * time.Hour,
},
},
Expand Down
Loading

0 comments on commit 2468ed7

Please sign in to comment.