github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/sqle/enginetest/dolt_queries_revert.go (about)

     1  // Copyright 2023 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package enginetest
    16  
    17  import (
    18  	"github.com/dolthub/go-mysql-server/enginetest/queries"
    19  	"github.com/dolthub/go-mysql-server/sql"
    20  )
    21  
    22  var RevertScripts = []queries.ScriptTest{
    23  	{
    24  		SkipPrepared: true, // https://github.com/dolthub/dolt/issues/6300
    25  		Name:         "dolt_revert() reverts HEAD",
    26  		SetUpScript: []string{
    27  			"create table test (pk int primary key, c0 int)",
    28  			"insert into test values (1,1),(2,2),(3,3);",
    29  			"call dolt_commit('-Am', 'seed table');",
    30  			"update test set c0 = 42 where pk = 2;",
    31  			"call dolt_commit('-am', 'answer of the universe: 42');",
    32  			"call dolt_revert('HEAD');",
    33  		},
    34  		Assertions: []queries.ScriptTestAssertion{
    35  			{
    36  				Query:    "select * from test as of 'HEAD' where pk = 2;",
    37  				Expected: []sql.Row{{2, 2}},
    38  			},
    39  			{
    40  				Query:    "select * from test as of 'HEAD~1' where pk = 2;",
    41  				Expected: []sql.Row{{2, 42}},
    42  			},
    43  		},
    44  	},
    45  	{
    46  		SkipPrepared: true, // https://github.com/dolthub/dolt/issues/6300
    47  		Name:         "dolt_revert() reverts HEAD~1",
    48  		SetUpScript: []string{
    49  			"create table test (pk int primary key, c0 int)",
    50  			"insert into test values (1,1),(2,2),(3,3);",
    51  			"call dolt_commit('-Am', 'seed table');",
    52  			"update test set c0 = 42 where pk = 2;",
    53  			"call dolt_commit('-am', 'answer of the universe');",
    54  			"update test set c0 = 23 where pk = 3;",
    55  			"call dolt_commit('-am', 'answer of the universe');",
    56  			"call dolt_revert('HEAD~1');",
    57  		},
    58  		Assertions: []queries.ScriptTestAssertion{
    59  			{
    60  				Query:    "select * from test as of 'HEAD' where pk = 2;",
    61  				Expected: []sql.Row{{2, 2}},
    62  			},
    63  			{
    64  				Query:    "select * from test as of 'HEAD~2' where pk = 2;",
    65  				Expected: []sql.Row{{2, 42}},
    66  			},
    67  			{
    68  				Query:    "select * from test as of 'HEAD' where pk = 3;",
    69  				Expected: []sql.Row{{3, 23}},
    70  			},
    71  		},
    72  	},
    73  	{
    74  		Name: "dolt_revert() detects conflicts",
    75  		SetUpScript: []string{
    76  			"create table test (pk int primary key, c0 int)",
    77  			"insert into test values (1,1),(2,2),(3,3);",
    78  			"call dolt_commit('-Am', 'seed table');",
    79  			"update test set c0 = 42 where pk = 2;",
    80  			"call dolt_commit('-am', 'first change');",
    81  			"update test set c0 = 23 where pk = 2;",
    82  			"call dolt_commit('-am', 'second change');",
    83  		},
    84  		Assertions: []queries.ScriptTestAssertion{
    85  			{
    86  				Query:          "call dolt_revert('HEAD~1');",
    87  				ExpectedErrStr: "revert currently does not handle conflicts",
    88  			},
    89  		},
    90  	},
    91  	{
    92  		Name: "dolt_revert() fails with untracked tables",
    93  		SetUpScript: []string{
    94  			"create table test (pk int primary key, c0 int)",
    95  			"insert into test values (1,1),(2,2),(3,3)",
    96  			"call dolt_commit('-Am', 'seed table')",
    97  			"update test set c0 = 42 where pk = 2",
    98  			"call dolt_commit('-am', 'answer of the universe: 42')",
    99  			"create table dont_track (pk int primary key)",
   100  			"insert into dont_track values (1)",
   101  		},
   102  		Assertions: []queries.ScriptTestAssertion{
   103  			{
   104  				Query:          "call dolt_revert('HEAD')",
   105  				ExpectedErrStr: "You must commit any changes before using revert",
   106  			},
   107  		},
   108  	},
   109  	{
   110  		SkipPrepared: true, // https://github.com/dolthub/dolt/issues/6300
   111  		Name:         "dolt_revert() respects dolt_ignore",
   112  		SetUpScript: []string{
   113  			"create table test (pk int primary key, c0 int)",
   114  			"insert into test values (1,1),(2,2),(3,3)",
   115  			"insert into dolt_ignore values ('dont_*', 1)",
   116  			"call dolt_commit('-Am', 'seed table')",
   117  			"update test set c0 = 42 where pk = 2",
   118  			"call dolt_commit('-am', 'answer of the universe: 42')",
   119  			"create table dont_track (id int primary key)",
   120  			"insert into dont_track values (1)",
   121  			"call dolt_revert('HEAD')",
   122  		},
   123  		Assertions: []queries.ScriptTestAssertion{
   124  			{
   125  				Query:    "select * from test as of 'HEAD' where pk = 2;",
   126  				Expected: []sql.Row{{2, 2}},
   127  			},
   128  			{
   129  				Query:    "select * from test as of 'HEAD~1' where pk = 2;",
   130  				Expected: []sql.Row{{2, 42}},
   131  			},
   132  			{
   133  				Query:          "select * from dont_track as of 'HEAD'",
   134  				ExpectedErrStr: "table not found: dont_track",
   135  			},
   136  			{
   137  				Query:    "select * from dolt_status",
   138  				Expected: []sql.Row{{"dont_track", false, "new table"}},
   139  			},
   140  		},
   141  	},
   142  	{
   143  		Name: "dolt_revert() detects not null violation (issue #4527)",
   144  		SetUpScript: []string{
   145  			"create table test2 (pk int primary key, c0 int)",
   146  			"insert into test2 values (1,1),(2,NULL),(3,3);",
   147  			"call dolt_commit('-Am', 'new table with NULL value');",
   148  			"delete from test2 where pk = 2;",
   149  			"call dolt_commit('-am', 'deleted row with NULL value');",
   150  			"alter table test2 modify c0 int not null",
   151  			"call dolt_commit('-am', 'modified column c0 to not null');",
   152  		},
   153  		Assertions: []queries.ScriptTestAssertion{
   154  			{
   155  				Query:          "call dolt_revert('head~1');",
   156  				ExpectedErrStr: "revert currently does not handle constraint violations",
   157  			},
   158  		},
   159  	},
   160  	{
   161  		Name: "dolt_revert() automatically resolves some conflicts",
   162  		SetUpScript: []string{
   163  			"create table tableA (id int primary key, col1 varchar(255));",
   164  			"call dolt_add('.');",
   165  			"call dolt_commit('-m', 'new table');",
   166  			"insert into tableA values (1, 'test')",
   167  			"call dolt_add('.');",
   168  			"call dolt_commit('-m', 'new row');",
   169  			"call dolt_branch('new_row');",
   170  			"ALTER TABLE tableA MODIFY col1 TEXT",
   171  			"call dolt_add('.');",
   172  			"call dolt_commit('-m', 'change col');",
   173  			"call dolt_revert('new_row');",
   174  		},
   175  		Assertions: []queries.ScriptTestAssertion{
   176  			{
   177  				Query:    "select * from tableA",
   178  				Expected: []sql.Row{},
   179  			},
   180  		},
   181  	},
   182  }