-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sql: insert missing public schema namespace entry
When restoring a database, a namespace entry for the public schema was not created. Release note: None
- Loading branch information
1 parent
7167b22
commit 2e13cab
Showing
21 changed files
with
202 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
pkg/ccl/backupccl/insert_missing_public_schema_namespace_entry_restore_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright 2021 The Cockroach Authors. | ||
// | ||
// Licensed as a CockroachDB Enterprise file under the Cockroach Community | ||
// License (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt | ||
|
||
package backupccl_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/base" | ||
"github.com/cockroachdb/cockroach/pkg/clusterversion" | ||
"github.com/cockroachdb/cockroach/pkg/server" | ||
"github.com/cockroachdb/cockroach/pkg/testutils" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/testcluster" | ||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPublicSchemaMigration(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
ctx := context.Background() | ||
dir, cleanup := testutils.TempDir(t) | ||
defer cleanup() | ||
tc := testcluster.StartTestCluster(t, 3, base.TestClusterArgs{ | ||
ServerArgs: base.TestServerArgs{ | ||
ExternalIODir: dir, | ||
Knobs: base.TestingKnobs{ | ||
Server: &server.TestingKnobs{ | ||
DisableAutomaticVersionUpgrade: 1, | ||
BinaryVersionOverride: clusterversion.ByKey(clusterversion.InsertPublicSchemaNamespaceEntryOnRestore - 1), | ||
}, | ||
}, | ||
}, | ||
}) | ||
defer tc.Stopper().Stop(ctx) | ||
|
||
db := tc.ServerConn(0) | ||
defer db.Close() | ||
sqlDB := sqlutils.MakeSQLRunner(tc.Conns[0]) | ||
|
||
/* | ||
This backup was created by executing the following commands on a v21.1.1 | ||
cluster and performing a full cluster backup. | ||
CREATE DATABASE db1; | ||
CREATE TABLE db1.t(); | ||
CREATE SCHEMA db1.s; | ||
CREATE DATABASE db2; | ||
CREATE TABLE db2.t(x INT); | ||
INSERT INTO db2.t VALUES (1), (2); | ||
CREATE SCHEMA db2.s; | ||
CREATE TABLE db2.s.t(x INT); | ||
INSERT INTO db2.s.t VALUES (1), (2); | ||
*/ | ||
publicSchemaDir, err := filepath.Abs("./testdata/restore_old_versions/missing-public-schema-namespace/v21.1.1") | ||
require.NoError(t, err) | ||
err = os.Symlink(publicSchemaDir, filepath.Join(dir, "foo")) | ||
require.NoError(t, err) | ||
|
||
localFoo := "nodelocal://0/foo" | ||
|
||
_ = sqlDB.Exec(t, fmt.Sprintf("RESTORE DATABASE db1 FROM '%s'", localFoo)) | ||
_ = sqlDB.Exec(t, fmt.Sprintf("RESTORE DATABASE db2 FROM '%s'", localFoo)) | ||
|
||
var db1ID, db2ID int | ||
row := sqlDB.QueryRow(t, `SELECT id FROM system.namespace WHERE name = 'db1'`) | ||
row.Scan(&db1ID) | ||
row = sqlDB.QueryRow(t, `SELECT id FROM system.namespace WHERE name = 'db2'`) | ||
row.Scan(&db2ID) | ||
|
||
// Restore is bugged, the public schemas will not have entries in | ||
// system.namespace. | ||
sqlDB.CheckQueryResults(t, fmt.Sprintf(`SELECT id FROM system.namespace WHERE name = 'public' AND "parentID"=%d`, db1ID), [][]string{}) | ||
sqlDB.CheckQueryResults(t, fmt.Sprintf(`SELECT id FROM system.namespace WHERE name = 'public' AND "parentID"=%d`, db2ID), [][]string{}) | ||
|
||
// Migrate to the new version. | ||
_ = sqlDB.Exec(t, `SET CLUSTER SETTING version = $1`, | ||
clusterversion.ByKey(clusterversion.InsertPublicSchemaNamespaceEntryOnRestore).String()) | ||
require.NoError(t, err) | ||
|
||
sqlDB.CheckQueryResults(t, fmt.Sprintf(`SELECT id FROM system.namespace WHERE name = 'public' AND "parentID"=%d`, db1ID), [][]string{{"29"}}) | ||
sqlDB.CheckQueryResults(t, fmt.Sprintf(`SELECT id FROM system.namespace WHERE name = 'public' AND "parentID"=%d`, db2ID), [][]string{{"29"}}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+1014 Bytes
...tdata/restore_old_versions/missing-public-schema-namespace/v21.1.1/716846481902174209.sst
Binary file not shown.
Binary file added
BIN
+1.11 KB
...tdata/restore_old_versions/missing-public-schema-namespace/v21.1.1/716846481902206977.sst
Binary file not shown.
Binary file added
BIN
+1.28 KB
...tdata/restore_old_versions/missing-public-schema-namespace/v21.1.1/716846481902239745.sst
Binary file not shown.
Binary file added
BIN
+1.05 KB
...tdata/restore_old_versions/missing-public-schema-namespace/v21.1.1/716846482369970177.sst
Binary file not shown.
Binary file added
BIN
+1.12 KB
...tdata/restore_old_versions/missing-public-schema-namespace/v21.1.1/716846482426396673.sst
Binary file not shown.
Binary file added
BIN
+991 Bytes
...tdata/restore_old_versions/missing-public-schema-namespace/v21.1.1/716846482479906817.sst
Binary file not shown.
Binary file added
BIN
+953 Bytes
...tdata/restore_old_versions/missing-public-schema-namespace/v21.1.1/716846484028981249.sst
Binary file not shown.
Binary file added
BIN
+953 Bytes
...tdata/restore_old_versions/missing-public-schema-namespace/v21.1.1/716846484029112321.sst
Binary file not shown.
1 change: 1 addition & 0 deletions
1
...ons/missing-public-schema-namespace/v21.1.1/BACKUP-CHECKPOINT-716846479146844161-CHECKSUM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
���� |
1 change: 1 addition & 0 deletions
1
...a/restore_old_versions/missing-public-schema-namespace/v21.1.1/BACKUP-CHECKPOINT-CHECKSUM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
��3 |
Empty file.
Binary file added
BIN
+2.45 KB
...ccl/testdata/restore_old_versions/missing-public-schema-namespace/v21.1.1/BACKUP_MANIFEST
Binary file not shown.
1 change: 1 addition & 0 deletions
1
...ata/restore_old_versions/missing-public-schema-namespace/v21.1.1/BACKUP_MANIFEST-CHECKSUM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
O��, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
pkg/migration/migrations/insert_missing_public_schema_namespace_entry.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright 2021 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package migrations | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/clusterversion" | ||
"github.com/cockroachdb/cockroach/pkg/jobs" | ||
"github.com/cockroachdb/cockroach/pkg/keys" | ||
"github.com/cockroachdb/cockroach/pkg/kv" | ||
"github.com/cockroachdb/cockroach/pkg/migration" | ||
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catalogkeys" | ||
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" | ||
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descs" | ||
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree" | ||
) | ||
|
||
// insertMissingPublicSchemaNamespaceEntry creates a system.namespace entries | ||
// for public schemas that are missing a system.namespace entry. | ||
// This arises from restore where we mistakenly did not create system.namespace | ||
// entries for public schemas when restoring databases. | ||
func insertMissingPublicSchemaNamespaceEntry( | ||
ctx context.Context, _ clusterversion.ClusterVersion, d migration.TenantDeps, _ *jobs.Job, | ||
) error { | ||
// Get the ID of all databases where we're missing a public schema namespace | ||
// entry for. | ||
query := ` | ||
SELECT id | ||
FROM system.namespace | ||
WHERE id | ||
NOT IN ( | ||
SELECT ns_db.id | ||
FROM system.namespace AS ns_db | ||
INNER JOIN system.namespace | ||
AS ns_sc ON ( | ||
ns_db.id | ||
= ns_sc."parentID" | ||
) | ||
WHERE ns_db."parentSchemaID" = 0 | ||
AND ns_db."parentID" = 0 | ||
AND ns_sc."parentSchemaID" = 0 | ||
AND ns_sc.name = 'public' | ||
AND ns_sc.id = 29 | ||
) | ||
AND "parentID" = 0 | ||
ORDER BY id ASC; | ||
` | ||
rows, err := d.InternalExecutor.QueryIterator( | ||
ctx, "get_databases_without_public_schema_namespace_entry", nil /* txn */, query, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
var databaseIDs []descpb.ID | ||
for ok, err := rows.Next(ctx); ok; ok, err = rows.Next(ctx) { | ||
if err != nil { | ||
return err | ||
} | ||
id := descpb.ID(tree.MustBeDInt(rows.Cur()[0])) | ||
databaseIDs = append(databaseIDs, id) | ||
} | ||
|
||
return d.CollectionFactory.Txn(ctx, d.InternalExecutor, d.DB, func( | ||
ctx context.Context, txn *kv.Txn, descriptors *descs.Collection, | ||
) error { | ||
b := txn.NewBatch() | ||
for _, dbID := range databaseIDs { | ||
publicSchemaKey := catalogkeys.MakeSchemaNameKey(d.Codec, dbID, tree.PublicSchema) | ||
b.Put(publicSchemaKey, keys.PublicSchemaID) | ||
} | ||
return txn.Run(ctx, b) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters