Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release-21.2: restoreccl: insert system.namespace entry for synthetic public schemas during restore #74759

Merged
merged 2 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions pkg/ccl/backupccl/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9005,3 +9005,70 @@ insert into A (a) VALUES ('foo');
checkRows()
})
}

// Verify that upon restoring a database, there is a namespace entry for its
// public schema.
func TestRestoreSyntheticPublicSchemaNamespaceEntry(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
const numAccounts = 100
params := base.TestClusterArgs{}

_, _, sqlDB, _, cleanup := backupRestoreTestSetupWithParams(t, singleNode, numAccounts,
InitManualReplication, params)
defer cleanup()

sqlDB.Exec(t, "CREATE DATABASE d")
sqlDB.Exec(t, "BACKUP DATABASE d TO $1", LocalFoo)
sqlDB.Exec(t, "DROP DATABASE d")

sqlDB.Exec(t, fmt.Sprintf("RESTORE DATABASE d FROM '%s'", LocalFoo))

var dbID int
row := sqlDB.QueryRow(t, `SELECT id FROM system.namespace WHERE name = 'd'`)
row.Scan(&dbID)

sqlDB.CheckQueryResults(t, fmt.Sprintf(`SELECT id FROM system.namespace WHERE name = 'public' AND "parentID"=%d`, dbID), [][]string{{"29"}})
}

// Verify that if a database restore fails, the cleanup removes the database's
// public namespace entry.
func TestRestoreSyntheticPublicSchemaNamespaceEntryCleanupOnFail(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
const numAccounts = 100
params := base.TestClusterArgs{}
_, _, _, dataDir, cleanupFn := BackupRestoreTestSetup(t, singleNode, numAccounts, InitManualReplication)
defer cleanupFn()
_, tc, sqlDB, cleanup := backupRestoreTestSetupEmpty(t, singleNode, dataDir,
InitManualReplication, params)
defer cleanup()

for _, server := range tc.Servers {
registry := server.JobRegistry().(*jobs.Registry)
registry.TestingResumerCreationKnobs = map[jobspb.Type]func(raw jobs.Resumer) jobs.Resumer{
jobspb.TypeRestore: func(raw jobs.Resumer) jobs.Resumer {
r := raw.(*restoreResumer)
r.testingKnobs.beforePublishingDescriptors = func() error {
return errors.New("boom")
}
return r
},
}
}

// Drop the default databases so only the system database remains.
sqlDB.Exec(t, "DROP DATABASE defaultdb")
sqlDB.Exec(t, "DROP DATABASE postgres")

sqlDB.Exec(t, "CREATE DATABASE d")
sqlDB.Exec(t, "BACKUP DATABASE d TO $1", LocalFoo)
sqlDB.Exec(t, "DROP DATABASE d")

restoreQuery := fmt.Sprintf("RESTORE DATABASE d FROM '%s'", LocalFoo)
sqlDB.ExpectErr(t, "boom", restoreQuery)

// We should have no non-system database with a public schema name space
// entry with id 29.
sqlDB.CheckQueryResults(t, `SELECT id FROM system.namespace WHERE name = 'public' AND id=29 AND "parentID"!=1`, [][]string{})
}
6 changes: 6 additions & 0 deletions pkg/ccl/backupccl/restore_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ func WriteDescriptors(
return err
}
b.CPut(catalogkeys.EncodeNameKey(codec, desc), desc.GetID(), nil)

// We also have to put a system.namespace entry for the public schema.
b.CPut(catalogkeys.MakeSchemaNameKey(codec, desc.GetID(), tree.PublicSchema), keys.PublicSchemaID, nil)
}

// Write namespace and descriptor entries for each schema.
Expand Down Expand Up @@ -2169,6 +2172,9 @@ func (r *restoreResumer) dropDescriptors(

descKey := catalogkeys.MakeDescMetadataKey(codec, db.GetID())
b.Del(descKey)

b.Del(catalogkeys.MakeSchemaNameKey(codec, db.GetID(), tree.PublicSchema))

nameKey := catalogkeys.MakeDatabaseNameKey(codec, db.GetName())
b.Del(nameKey)
descsCol.AddDeletedDescriptor(db)
Expand Down