Skip to content

Commit

Permalink
backupccl: remove fk revision history test
Browse files Browse the repository at this point in the history
This change removes the test that was added as part of
were correctly upgraded on restore to 20.x+ versions.
Backups taken one major release ago will no longer have
these old style fks and so this test serves better as a
regular test on this branch.

Release note: None
  • Loading branch information
adityamaru committed Mar 10, 2023
1 parent 7066e9e commit f25c02d
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 141 deletions.
107 changes: 107 additions & 0 deletions pkg/ccl/backupccl/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10876,3 +10876,110 @@ func TestExportResponseDataSizeZeroCPUPagination(t *testing.T) {
sqlDB.Exec(t, `BACKUP TABLE foo INTO 'nodelocal://1/foo'`)
require.Equal(t, 2, numRequests)
}

func TestBackupRestoreForeignKeys(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)

params := base.TestServerArgs{}
const numAccounts = 1000
_, sqlDB, _, cleanup := backupRestoreTestSetupWithParams(t, singleNode, numAccounts,
InitManualReplication, base.TestClusterArgs{ServerArgs: params})
defer cleanup()

sqlDB.Exec(t, `CREATE DATABASE test`)
sqlDB.Exec(t, `SET database = test`)
sqlDB.Exec(t, `
CREATE TABLE circular (k INT8 PRIMARY KEY, selfid INT8 UNIQUE);
ALTER TABLE circular ADD CONSTRAINT self_fk FOREIGN KEY (selfid) REFERENCES circular (selfid);
CREATE TABLE parent (k INT8 PRIMARY KEY, j INT8 UNIQUE);
CREATE TABLE child (k INT8 PRIMARY KEY, parent_i INT8 REFERENCES parent, parent_j INT8 REFERENCES parent (j));
CREATE TABLE child_pk (k INT8 PRIMARY KEY REFERENCES parent);
`)

sqlDB.Exec(t, `BACKUP INTO $1 WITH revision_history`, localFoo)

sqlDB.Exec(t, `CREATE TABLE rev_times (id INT PRIMARY KEY, logical_time DECIMAL);`)
sqlDB.Exec(t, `INSERT INTO rev_times VALUES (1, cluster_logical_timestamp());`)

sqlDB.Exec(t, `
CREATE USER newuser;
GRANT ALL ON circular TO newuser;
GRANT ALL ON parent TO newuser;
`)

sqlDB.Exec(t, `BACKUP INTO LATEST IN $1 WITH revision_history`, localFoo)
sqlDB.Exec(t, `INSERT INTO rev_times VALUES (2, cluster_logical_timestamp())`)

sqlDB.Exec(t, `GRANT ALL ON child TO newuser;`)

sqlDB.Exec(t, `BACKUP INTO LATEST IN $1 WITH revision_history`, localFoo)
sqlDB.Exec(t, `INSERT INTO rev_times VALUES (3, cluster_logical_timestamp())`)

sqlDB.Exec(t, `GRANT ALL ON child_pk TO newuser`)

sqlDB.Exec(t, `BACKUP INTO LATEST IN $1 WITH revision_history`, localFoo)

// Test that `SHOW BACKUP` displays the foreign keys correctly.
type testCase struct {
table string
expectedForeignKeyPattern string
}
for _, tc := range []testCase{
{
"circular",
"CONSTRAINT self_fk FOREIGN KEY \\(selfid\\) REFERENCES public\\.circular\\(selfid\\) NOT VALID",
},
{
"child",
"CONSTRAINT \\w+ FOREIGN KEY \\(\\w+\\) REFERENCES public\\.parent\\(\\w+\\)",
},
{
"child_pk",
"CONSTRAINT \\w+ FOREIGN KEY \\(\\w+\\) REFERENCES public\\.parent\\(\\w+\\)",
},
} {
results := sqlDB.QueryStr(t, `
SELECT
create_statement
FROM
[SHOW BACKUP SCHEMAS FROM LATEST IN $1]
WHERE
object_type = 'table' AND object_name = $2
`, localFoo, tc.table)
require.NotEmpty(t, results)
require.Regexp(t, regexp.MustCompile(tc.expectedForeignKeyPattern), results[0][0])
}
sqlDB.Exec(t, `DROP DATABASE test`)

// Test restoring different objects from the backup.
sqlDB.Exec(t, `CREATE DATABASE ts`)
sqlDB.Exec(t, `RESTORE test.rev_times FROM LATEST IN $1 WITH into_db = 'ts'`, localFoo)
for _, ts := range sqlDB.QueryStr(t, `SELECT logical_time FROM ts.rev_times`) {
sqlDB.Exec(t, fmt.Sprintf(`RESTORE DATABASE test FROM LATEST IN $1 AS OF SYSTEM TIME %s`, ts[0]), localFoo)
// Just rendering the constraints loads and validates schema.
sqlDB.Exec(t, `SELECT * FROM pg_catalog.pg_constraint`)
sqlDB.Exec(t, `DROP DATABASE test`)

// Restore a couple tables, including parent but not child_pk.
sqlDB.Exec(t, `CREATE DATABASE test`)
sqlDB.Exec(t, fmt.Sprintf(`RESTORE test.circular FROM LATEST IN $1 AS OF SYSTEM TIME %s`, ts[0]), localFoo)
require.Equal(t, [][]string{
{"test.public.circular", "CREATE TABLE public.circular (\n\tk INT8 NOT NULL,\n\tselfid INT8 NULL,\n\tCONSTRAINT circular_pkey PRIMARY KEY (k ASC),\n\tCONSTRAINT self_fk FOREIGN KEY (selfid) REFERENCES public.circular(selfid) NOT VALID,\n\tUNIQUE INDEX circular_selfid_key (selfid ASC)\n)"},
}, sqlDB.QueryStr(t, `SHOW CREATE TABLE test.circular`))
sqlDB.Exec(t, fmt.Sprintf(`RESTORE test.parent, test.child FROM LATEST IN $1 AS OF SYSTEM TIME %s `, ts[0]), localFoo)
sqlDB.Exec(t, `SELECT * FROM pg_catalog.pg_constraint`)
sqlDB.Exec(t, `DROP DATABASE test`)

// Now do each table on its own with skip_missing_foreign_keys.
sqlDB.Exec(t, `CREATE DATABASE test`)
for _, name := range []string{"child_pk", "child", "circular", "parent"} {
if name == "child" || name == "child_pk" {
sqlDB.ExpectErr(t, "cannot restore table.*without referenced table", fmt.Sprintf(`RESTORE test.%s FROM LATEST IN $1 AS OF SYSTEM TIME %s`, name, ts[0]), localFoo)
}
sqlDB.Exec(t, fmt.Sprintf(`RESTORE test.%s FROM LATEST IN $1 AS OF SYSTEM TIME %s WITH skip_missing_foreign_keys`, name, ts[0]), localFoo)
}
sqlDB.Exec(t, `SELECT * FROM pg_catalog.pg_constraint`)
sqlDB.Exec(t, `DROP DATABASE test`)
}
}
49 changes: 0 additions & 49 deletions pkg/ccl/backupccl/restore_old_versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ func TestRestoreOldVersions(t *testing.T) {
testdataBase := datapathutils.TestDataPath(t, "restore_old_versions")
var (
exportDirsWithoutInterleave = testdataBase + "/exports-without-interleaved"
fkRevDirs = testdataBase + "/fk-rev-history"
clusterDirs = testdataBase + "/cluster"
privilegeDirs = testdataBase + "/privileges"
multiRegionDirs = testdataBase + "/multi-region"
Expand All @@ -87,17 +86,6 @@ func TestRestoreOldVersions(t *testing.T) {
}
})

t.Run("fk-rev-restore", func(t *testing.T) {
dirs, err := os.ReadDir(fkRevDirs)
require.NoError(t, err)
for _, dir := range dirs {
require.True(t, dir.IsDir())
exportDir, err := filepath.Abs(filepath.Join(fkRevDirs, dir.Name()))
require.NoError(t, err)
t.Run(dir.Name(), restoreOldVersionFKRevTest(exportDir))
}
})

t.Run("cluster-restore", func(t *testing.T) {
dirs, err := os.ReadDir(clusterDirs)
require.NoError(t, err)
Expand Down Expand Up @@ -352,43 +340,6 @@ func restoreV201ZoneconfigPrivilegeTest(exportDir string) func(t *testing.T) {
}
}

func restoreOldVersionFKRevTest(exportDir string) func(t *testing.T) {
return func(t *testing.T) {
params := base.TestServerArgs{}
const numAccounts = 1000
_, sqlDB, dir, cleanup := backupRestoreTestSetupWithParams(t, singleNode, numAccounts,
InitManualReplication, base.TestClusterArgs{ServerArgs: params})
defer cleanup()
err := os.Symlink(exportDir, filepath.Join(dir, "foo"))
require.NoError(t, err)
sqlDB.Exec(t, `CREATE DATABASE ts`)
sqlDB.Exec(t, `RESTORE test.rev_times FROM $1 WITH into_db = 'ts'`, localFoo)
for _, ts := range sqlDB.QueryStr(t, `SELECT logical_time FROM ts.rev_times`) {

sqlDB.Exec(t, fmt.Sprintf(`RESTORE DATABASE test FROM $1 AS OF SYSTEM TIME %s`, ts[0]), localFoo)
// Just rendering the constraints loads and validates schema.
sqlDB.Exec(t, `SELECT * FROM pg_catalog.pg_constraint`)
sqlDB.Exec(t, `DROP DATABASE test`)

// Restore a couple tables, including parent but not child_pk.
sqlDB.Exec(t, `CREATE DATABASE test`)
sqlDB.Exec(t, fmt.Sprintf(`RESTORE test.circular FROM $1 AS OF SYSTEM TIME %s`, ts[0]), localFoo)
sqlDB.Exec(t, fmt.Sprintf(`RESTORE test.parent, test.child FROM $1 AS OF SYSTEM TIME %s WITH skip_missing_foreign_keys`, ts[0]), localFoo)
sqlDB.Exec(t, `SELECT * FROM pg_catalog.pg_constraint`)
sqlDB.Exec(t, `DROP DATABASE test`)

// Now do each table on its own with skip_missing_foreign_keys.
sqlDB.Exec(t, `CREATE DATABASE test`)
for _, name := range []string{"child_pk", "child", "circular", "parent"} {
sqlDB.Exec(t, fmt.Sprintf(`RESTORE test.%s FROM $1 AS OF SYSTEM TIME %s WITH skip_missing_foreign_keys`, name, ts[0]), localFoo)
}
sqlDB.Exec(t, `SELECT * FROM pg_catalog.pg_constraint`)
sqlDB.Exec(t, `DROP DATABASE test`)

}
}
}

func deprecatedRestoreOldVersionClusterTest(exportDir string) func(t *testing.T) {
return func(t *testing.T) {
externalDir, dirCleanup := testutils.TempDir(t)
Expand Down
62 changes: 0 additions & 62 deletions pkg/ccl/backupccl/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/catalog/desctestutils"
"github.com/cockroachdb/cockroach/pkg/sql/rowenc"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/testutils/datapathutils"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/testutils/skip"
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
Expand Down Expand Up @@ -715,67 +714,6 @@ func TestShowBackupPrivileges(t *testing.T) {
require.NoError(t, err)
}

func TestShowUpgradedForeignKeys(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)

var (
testdataBase = datapathutils.TestDataPath(t, "restore_old_versions")
fkRevDirs = testdataBase + "/fk-rev-history"
)

dirs, err := os.ReadDir(fkRevDirs)
require.NoError(t, err)
for _, dir := range dirs {
require.True(t, dir.IsDir())
exportDir, err := filepath.Abs(filepath.Join(fkRevDirs, dir.Name()))
require.NoError(t, err)
t.Run(dir.Name(), showUpgradedForeignKeysTest(exportDir))
}
}

func showUpgradedForeignKeysTest(exportDir string) func(t *testing.T) {
return func(t *testing.T) {
params := base.TestServerArgs{}
const numAccounts = 1000
_, sqlDB, dir, cleanup := backupRestoreTestSetupWithParams(t, singleNode, numAccounts,
InitManualReplication, base.TestClusterArgs{ServerArgs: params})
defer cleanup()
err := os.Symlink(exportDir, filepath.Join(dir, "foo"))
require.NoError(t, err)

type testCase struct {
table string
expectedForeignKeyPattern string
}
for _, tc := range []testCase{
{
"circular",
"CONSTRAINT self_fk FOREIGN KEY \\(selfid\\) REFERENCES public\\.circular\\(selfid\\) NOT VALID",
},
{
"child",
"CONSTRAINT \\w+ FOREIGN KEY \\(\\w+\\) REFERENCES public\\.parent\\(\\w+\\)",
},
{
"child_pk",
"CONSTRAINT \\w+ FOREIGN KEY \\(\\w+\\) REFERENCES public\\.parent\\(\\w+\\)",
},
} {
results := sqlDB.QueryStr(t, `
SELECT
create_statement
FROM
[SHOW BACKUP SCHEMAS $1]
WHERE
object_type = 'table' AND object_name = $2
`, localFoo, tc.table)
require.NotEmpty(t, results)
require.Regexp(t, regexp.MustCompile(tc.expectedForeignKeyPattern), results[0][0])
}
}
}

func TestShowBackupWithDebugIDs(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
Expand Down

This file was deleted.

Binary file not shown.
Binary file not shown.

0 comments on commit f25c02d

Please sign in to comment.