@@ -1810,267 +1810,6 @@ CREATE TABLE t.test (k INT PRIMARY KEY, v INT);
18101810 }
18111811}
18121812
1813- // TestSchemaChangeReverseMutations tests that schema changes get reversed
1814- // correctly when one of them violates a constraint.
1815- func TestSchemaChangeReverseMutations (t * testing.T ) {
1816- defer leaktest .AfterTest (t )()
1817- defer log .Scope (t ).Close (t )
1818- // TODO (lucy): This test needs more complicated schema changer knobs than
1819- // currently implemented. What this test should be doing is starting a schema
1820- // change, and, before it hits a violation that requires a rollback, starting
1821- // another schema change that depends on the successful completion of the
1822- // first. At the end, all the dependent schema change jobs should have been
1823- // rolled back. Right now we're just testing that schema changes fail
1824- // correctly when run sequentially, which is not as interesting. The previous
1825- // comments are kept around to describe the intended results of the test, but
1826- // they don't reflect what's happening now.
1827- params , _ := createTestServerParams ()
1828- const chunkSize = 200
1829- // Disable synchronous schema change processing so that the mutations get
1830- // processed asynchronously.
1831- var enableAsyncSchemaChanges uint32
1832- params .Knobs = base.TestingKnobs {
1833- SQLSchemaChanger : & sql.SchemaChangerTestingKnobs {
1834- BackfillChunkSize : chunkSize ,
1835- },
1836- }
1837- s , sqlDB , kvDB := serverutils .StartServer (t , params )
1838- defer s .Stopper ().Stop (context .Background ())
1839- codec := s .ApplicationLayer ().Codec ()
1840-
1841- // Disable strict GC TTL enforcement because we're going to shove a zero-value
1842- // TTL into the system with AddImmediateGCZoneConfig.
1843- defer sqltestutils .DisableGCTTLStrictEnforcement (t , sqlDB )()
1844-
1845- // Create a k-v table.
1846- if _ , err := sqlDB .Exec (`
1847- CREATE DATABASE t;
1848- CREATE TABLE t.test (k INT PRIMARY KEY, v INT8);
1849- ` ); err != nil {
1850- t .Fatal (err )
1851- }
1852-
1853- // Add some data
1854- const maxValue = chunkSize + 1
1855- if err := sqltestutils .BulkInsertIntoTable (sqlDB , maxValue ); err != nil {
1856- t .Fatal (err )
1857- }
1858-
1859- tableDesc := desctestutils .TestingGetPublicTableDescriptor (kvDB , keys .SystemSQLCodec , "t" , "test" )
1860-
1861- testCases := []struct {
1862- sql string
1863- errString string
1864- }{
1865- // Create a column that is not NULL. This schema change doesn't return an
1866- // error only because we've turned off the synchronous execution path; it
1867- // will eventually fail when run by the asynchronous path.
1868- {`ALTER TABLE t.public.test ADD COLUMN a INT8 UNIQUE DEFAULT 0, ADD COLUMN c INT8` ,
1869- "violates unique constraint" },
1870- // Add an index over a column that will be purged. This index will
1871- // eventually not get added. The column aa will also be dropped as
1872- // a result.
1873- {`ALTER TABLE t.public.test ADD COLUMN aa INT8, ADD CONSTRAINT foo UNIQUE (a)` ,
1874- "column \" a\" does not exist" },
1875-
1876- // The purge of column 'a' doesn't influence these schema changes.
1877-
1878- // Drop column 'v' moves along just fine.
1879- {`ALTER TABLE t.public.test DROP COLUMN v` ,
1880- "" },
1881- // Add unique column 'b' moves along creating column b and the index on
1882- // it.
1883- {`ALTER TABLE t.public.test ADD COLUMN b INT8 UNIQUE` ,
1884- "" },
1885- // #27033: Add a column followed by an index on the column.
1886- {`ALTER TABLE t.public.test ADD COLUMN d STRING NOT NULL DEFAULT 'something'` ,
1887- "" },
1888-
1889- {`CREATE INDEX ON t.public.test (d)` ,
1890- "" },
1891-
1892- // Add an index over a column 'c' that will be purged. This index will
1893- // eventually not get added. The column bb will also be dropped as
1894- // a result.
1895- {`ALTER TABLE t.public.test ADD COLUMN bb INT8, ADD CONSTRAINT bar UNIQUE (never_existed)` ,
1896- "column \" never_existed\" does not exist" },
1897- // Cascading of purges. column 'c' -> column 'bb' -> constraint 'idx_bb'.
1898- {`ALTER TABLE t.public.test ADD CONSTRAINT idx_bb UNIQUE (bb)` ,
1899- "column \" bb\" does not exist" },
1900- }
1901-
1902- for _ , tc := range testCases {
1903- _ , err := sqlDB .Exec (tc .sql )
1904- if tc .errString == "" {
1905- if err != nil {
1906- t .Fatalf ("%s: %v" , tc .sql , err )
1907- }
1908- } else {
1909- if err == nil {
1910- t .Fatalf ("%s: expected error" , tc .sql )
1911- }
1912- if ! strings .Contains (err .Error (), tc .errString ) {
1913- t .Fatalf ("%s: %v" , tc .sql , err )
1914- }
1915- }
1916- }
1917-
1918- // Enable async schema change processing for purged schema changes.
1919- atomic .StoreUint32 (& enableAsyncSchemaChanges , 1 )
1920-
1921- expectedCols := []string {"k" , "b" , "d" }
1922- // Wait until all the mutations have been processed.
1923- testutils .SucceedsSoon (t , func () error {
1924- tableDesc = desctestutils .TestingGetPublicTableDescriptor (kvDB , keys .SystemSQLCodec , "t" , "test" )
1925- if len (tableDesc .AllMutations ()) > 0 {
1926- return errors .Errorf ("%d mutations remaining" , len (tableDesc .AllMutations ()))
1927- }
1928- return nil
1929- })
1930-
1931- // Verify that t.public.test has the expected data. Read the table data while
1932- // ensuring that the correct table lease is in use.
1933- rows , err := sqlDB .Query (`SELECT * from t.test` )
1934- if err != nil {
1935- t .Fatal (err )
1936- }
1937- defer rows .Close ()
1938- cols , err := rows .Columns ()
1939- if err != nil {
1940- t .Fatal (err )
1941- }
1942-
1943- // Ensure that sql is using the correct table lease.
1944- if len (cols ) != len (expectedCols ) {
1945- t .Fatalf ("incorrect columns: %v, expected: %v" , cols , expectedCols )
1946- }
1947- if cols [0 ] != expectedCols [0 ] || cols [1 ] != expectedCols [1 ] {
1948- t .Fatalf ("incorrect columns: %v" , cols )
1949- }
1950-
1951- // rows contains the data; verify that it's the right data.
1952- vals := make ([]interface {}, len (expectedCols ))
1953- for i := range vals {
1954- vals [i ] = new (interface {})
1955- }
1956- var count int64
1957- for ; rows .Next (); count ++ {
1958- if err := rows .Scan (vals ... ); err != nil {
1959- t .Errorf ("row %d scan failed: %s" , count , err )
1960- continue
1961- }
1962- for j , v := range vals {
1963- switch j {
1964- case 0 :
1965- if val := * v .(* interface {}); val != nil {
1966- switch k := val .(type ) {
1967- case int64 :
1968- if count != k {
1969- t .Errorf ("k = %d, expected %d" , k , count )
1970- }
1971-
1972- default :
1973- t .Errorf ("error input of type %T" , k )
1974- }
1975- } else {
1976- t .Error ("received NULL value for column 'k'" )
1977- }
1978-
1979- case 1 :
1980- if val := * v .(* interface {}); val != nil {
1981- t .Error ("received non NULL value for column 'b'" )
1982- }
1983-
1984- case 2 :
1985- if val := * v .(* interface {}); val == nil {
1986- t .Error ("received NULL value for column 'd'" )
1987- }
1988- }
1989- }
1990- }
1991- if err := rows .Err (); err != nil {
1992- t .Fatal (err )
1993- }
1994- if eCount := int64 (maxValue + 1 ); eCount != count {
1995- t .Fatalf ("read the wrong number of rows: e = %d, v = %d" , eCount , count )
1996- }
1997-
1998- // Check that the index on b eventually goes live even though a schema
1999- // change in front of it in the queue got purged.
2000- rows , err = sqlDB .Query (`SELECT * from t.test@test_b_key` )
2001- if err != nil {
2002- t .Fatal (err )
2003- }
2004- defer rows .Close ()
2005- count = 0
2006- for ; rows .Next (); count ++ {
2007- }
2008- if err := rows .Err (); err != nil {
2009- t .Fatal (err )
2010- }
2011- if eCount := int64 (maxValue + 1 ); eCount != count {
2012- t .Fatalf ("read the wrong number of rows: e = %d, v = %d" , eCount , count )
2013- }
2014-
2015- // Check that the index on c gets purged.
2016- if _ , err := sqlDB .Query (`SELECT * from t.test@foo` ); err == nil {
2017- t .Fatal ("SELECT over index 'foo' works" )
2018- }
2019-
2020- if err := sqlutils .RunScrub (sqlDB , "t" , "test" ); err != nil {
2021- t .Fatal (err )
2022- }
2023-
2024- // Add immediate GC TTL to allow index creation purge to complete.
2025- if _ , err := sqltestutils .AddImmediateGCZoneConfig (sqlDB , tableDesc .GetID ()); err != nil {
2026- t .Fatal (err )
2027- }
2028-
2029- ctx := context .Background ()
2030-
2031- testutils .SucceedsSoon (t , func () error {
2032- // Check that the number of k-v pairs is accurate.
2033- return sqltestutils .CheckTableKeyCount (ctx , kvDB , codec , 3 , maxValue )
2034- })
2035-
2036- // State of jobs table
2037- skip .WithIssue (t , 51796 , "TODO(pbardea): The following fails due to causes seemingly unrelated to GC" )
2038- runner := sqlutils.SQLRunner {DB : sqlDB }
2039- // TODO (lucy): This test API should use an offset starting from the
2040- // most recent job instead.
2041- const migrationJobOffset = 0
2042- for i , tc := range testCases {
2043- status := jobs .StatusSucceeded
2044- if tc .errString != "" {
2045- status = jobs .StatusFailed
2046- }
2047- if err := jobutils .VerifySystemJob (t , & runner , migrationJobOffset + i , jobspb .TypeSchemaChange , status , jobs.Record {
2048- Username : username .RootUserName (),
2049- Description : tc .sql ,
2050- DescriptorIDs : descpb.IDs {
2051- tableDesc .GetID (),
2052- },
2053- }); err != nil {
2054- t .Fatal (err )
2055- }
2056- }
2057-
2058- jobRolledBack := 0
2059- jobID := jobutils .GetJobID (t , & runner , jobRolledBack )
2060-
2061- // Roll back job.
2062- if err := jobutils .VerifySystemJob (t , & runner , len (testCases ), jobspb .TypeSchemaChange , jobs .StatusSucceeded , jobs.Record {
2063- Username : username .RootUserName (),
2064- Description : fmt .Sprintf ("ROLL BACK JOB %d: %s" , jobID , testCases [jobRolledBack ].sql ),
2065- DescriptorIDs : descpb.IDs {
2066- tableDesc .GetID (),
2067- },
2068- }); err != nil {
2069- t .Fatal (err )
2070- }
2071-
2072- }
2073-
20741813// This test checks backward compatibility with old data that contains
20751814// sentinel kv pairs at the start of each table row. Cockroachdb used
20761815// to write table rows with sentinel values in the past. When a new column
0 commit comments