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

     1  // Copyright 2021 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  	gmstypes "github.com/dolthub/go-mysql-server/sql/types"
    22  )
    23  
    24  var DoltProcedureTests = []queries.ScriptTest{
    25  	{
    26  		Name: "dolt_commit in a loop",
    27  		SetUpScript: []string{
    28  			"create table t(a int primary key auto_increment, b int);",
    29  			"call dolt_commit('-Am', 'new table');",
    30  			`create procedure commit_many()
    31  begin
    32    declare i int default 1;
    33  	commits: loop
    34  		insert into t(b) values (i);
    35  		call dolt_commit('-am', concat('inserted row ', cast (i as char)));
    36  		if i >= 10 then
    37  			leave commits;
    38  		end if;
    39  		set i = i + 1;
    40  	end loop commits;
    41  end
    42  `,
    43  		},
    44  		Assertions: []queries.ScriptTestAssertion{
    45  			{
    46  				Query:            "call commit_many();",
    47  				SkipResultsCheck: true, // return value is a bit odd, needs investigation
    48  			},
    49  			{
    50  				Query:    "select * from t",
    51  				Expected: []sql.Row{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}, {10, 10}},
    52  			},
    53  			{
    54  				Query:    "select count(*) from dolt_log;",
    55  				Expected: []sql.Row{{13}}, // init, setup for test harness, initial commit in setup script, 10 commits in procedure
    56  			},
    57  			{
    58  				Query:    "select * from t as of `HEAD~5`",
    59  				Expected: []sql.Row{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}},
    60  			},
    61  		},
    62  	},
    63  	{
    64  		Name: "dolt_branch in a loop",
    65  		SetUpScript: []string{
    66  			"create table t(a int primary key auto_increment, b int);",
    67  			"call dolt_commit('-Am', 'new table');",
    68  			`create procedure branches()
    69  begin
    70    declare i int default 1;
    71  	commits: loop
    72  		insert into t(b) values (i);
    73  		call dolt_branch(concat('branch', i));
    74  		if i >= 4 then
    75  			leave commits;
    76  		end if;
    77  		set i = i + 1;
    78  	end loop commits;
    79  end
    80  `,
    81  		},
    82  		Assertions: []queries.ScriptTestAssertion{
    83  			{
    84  				Query:            "call branches();",
    85  				SkipResultsCheck: true, // return value is a bit odd, needs investigation
    86  			},
    87  			{
    88  				Query:    "select name from dolt_branches order by 1",
    89  				Expected: []sql.Row{{"branch1"}, {"branch2"}, {"branch3"}, {"branch4"}, {"main"}},
    90  			},
    91  			{
    92  				Query:    "select * from t order by 1",
    93  				Expected: []sql.Row{{1, 1}, {2, 2}, {3, 3}, {4, 4}},
    94  			},
    95  		},
    96  	},
    97  	{
    98  		Name: "dolt_branch in a loop, insert after branch",
    99  		SetUpScript: []string{
   100  			"create table t(a int primary key auto_increment, b int);",
   101  			"call dolt_commit('-Am', 'new table');",
   102  			`create procedure branches()
   103  begin
   104    declare i int default 1;
   105  	commits: loop
   106  		call dolt_branch(concat('branch', i));
   107  		insert into t(b) values (i);
   108  		if i >= 4 then
   109  			leave commits;
   110  		end if;
   111  		set i = i + 1;
   112  	end loop commits;
   113  end
   114  `,
   115  		},
   116  		Assertions: []queries.ScriptTestAssertion{
   117  			{
   118  				Query:            "call branches();",
   119  				SkipResultsCheck: true, // return value is a bit odd, needs investigation
   120  			},
   121  			{
   122  				Query:    "select name from dolt_branches order by 1",
   123  				Expected: []sql.Row{{"branch1"}, {"branch2"}, {"branch3"}, {"branch4"}, {"main"}},
   124  			},
   125  			{
   126  				Query:    "select * from t order by 1",
   127  				Expected: []sql.Row{{1, 1}, {2, 2}, {3, 3}, {4, 4}},
   128  			},
   129  		},
   130  	},
   131  	{
   132  		Name: "dolt_branch in conditional chain",
   133  		SetUpScript: []string{
   134  			"create table t(a int primary key auto_increment, b int);",
   135  			"call dolt_commit('-Am', 'new table');",
   136  			`create procedure branches()
   137  begin
   138    declare i int default 1;
   139  	commits: loop
   140  		if i = 1 then
   141  			insert into t(b) values (i);
   142  			call dolt_branch('branch1');
   143  		elseif i = 2 then
   144  			call dolt_branch('branch2');
   145  			insert into t(b) values (i);
   146  		elseif i = 3 then
   147  			insert into t(b) values (i);
   148  			call dolt_branch('branch3');
   149  		else 
   150  			call dolt_branch('branch4');
   151  			insert into t(b) values (i);
   152  		end if;
   153  		if i >= 4 then
   154  			leave commits;
   155  		end if;
   156  		set i = i + 1;
   157  	end loop commits;
   158  end
   159  `,
   160  		},
   161  		Assertions: []queries.ScriptTestAssertion{
   162  			{
   163  				Query:            "call branches();",
   164  				SkipResultsCheck: true, // return value is a bit odd, needs investigation
   165  			},
   166  			{
   167  				Query:    "select name from dolt_branches order by 1",
   168  				Expected: []sql.Row{{"branch1"}, {"branch2"}, {"branch3"}, {"branch4"}, {"main"}},
   169  			},
   170  			{
   171  				Query:    "select * from t order by 1",
   172  				Expected: []sql.Row{{1, 1}, {2, 2}, {3, 3}, {4, 4}},
   173  			},
   174  		},
   175  	},
   176  	{
   177  		Name: "dolt_branch in case statement",
   178  		SetUpScript: []string{
   179  			"create table t(a int primary key auto_increment, b int);",
   180  			"call dolt_commit('-Am', 'new table');",
   181  			`create procedure branches()
   182  begin
   183    declare i int default 1;
   184  	commits: loop
   185  		case i
   186  	  when 1 then
   187  			insert into t(b) values (i);
   188  			call dolt_branch('branch1');
   189  		when 2 then
   190  			call dolt_branch('branch2');
   191  			insert into t(b) values (i);
   192  		when 3 then
   193  			insert into t(b) values (i);
   194  			call dolt_branch('branch3');
   195  		else 
   196  			call dolt_branch('branch4');
   197  			insert into t(b) values (i);
   198  		end case;
   199  		if i >= 4 then
   200  			leave commits;
   201  		end if;
   202  		set i = i + 1;
   203  	end loop commits;
   204  end
   205  `,
   206  		},
   207  		Assertions: []queries.ScriptTestAssertion{
   208  			{
   209  				Query:            "call branches();",
   210  				SkipResultsCheck: true, // return value is a bit odd, needs investigation
   211  			},
   212  			{
   213  				Query:    "select name from dolt_branches order by 1",
   214  				Expected: []sql.Row{{"branch1"}, {"branch2"}, {"branch3"}, {"branch4"}, {"main"}},
   215  			},
   216  			{
   217  				Query:    "select * from t order by 1",
   218  				Expected: []sql.Row{{1, 1}, {2, 2}, {3, 3}, {4, 4}},
   219  			},
   220  		},
   221  	},
   222  	{
   223  		Name: "dolt_branch in trigger",
   224  		SetUpScript: []string{
   225  			"create table t(a int primary key auto_increment, b int);",
   226  			"create table t2(a int primary key auto_increment, b int);",
   227  			"call dolt_commit('-Am', 'new table');",
   228  			`create trigger branch_trigger after insert on t for each row
   229  begin
   230    declare i int default 1;
   231  	commits: loop
   232  		case i
   233  	  when 1 then
   234  			insert into t2(b) values (i);
   235  			call dolt_branch('branch1');
   236  		when 2 then
   237  			call dolt_branch('branch2');
   238  			insert into t2(b) values (i);
   239  		when 3 then
   240  			insert into t2(b) values (i);
   241  			call dolt_branch('branch3');
   242  		else 
   243  			call dolt_branch('branch4');
   244  			insert into t2(b) values (i);
   245  		end case;
   246  		if i >= 4 then
   247  			leave commits;
   248  		end if;
   249  		set i = i + 1;
   250  	end loop commits;
   251  end
   252  `,
   253  		},
   254  		Assertions: []queries.ScriptTestAssertion{
   255  			{
   256  				Query: "insert into t values (1, 1);",
   257  				Expected: []sql.Row{
   258  					{gmstypes.OkResult{RowsAffected: 1, InsertID: 1}},
   259  				},
   260  			},
   261  			{
   262  				Query: "select name from dolt_branches order by 1",
   263  				Expected: []sql.Row{
   264  					{"branch1"},
   265  					{"branch2"},
   266  					{"branch3"},
   267  					{"branch4"},
   268  					{"main"},
   269  				},
   270  			},
   271  			{
   272  				// For some reason, calling stored procedures disables inserts
   273  				Skip:  true,
   274  				Query: "select * from t2 order by 1",
   275  				Expected: []sql.Row{
   276  					{1, 1},
   277  					{2, 2},
   278  					{3, 3},
   279  					{4, 4},
   280  				},
   281  			},
   282  		},
   283  	},
   284  	{
   285  		Name: "checkout new branch, insert, and commit in procedure",
   286  		SetUpScript: []string{
   287  			"create table t(a int primary key auto_increment, b int);",
   288  			"call dolt_commit('-Am', 'new table');",
   289  			`create procedure edit_on_branch()
   290  begin
   291  	call dolt_checkout('-b', 'branch1');
   292  	insert into t(b) values (100);
   293  	call dolt_commit('-am', 'new row');
   294  	call dolt_checkout('main');
   295  end
   296  `,
   297  		},
   298  		Assertions: []queries.ScriptTestAssertion{
   299  			{
   300  				Query:            "call edit_on_branch();",
   301  				Skip:             true,
   302  				SkipResultsCheck: true, // return value is a bit odd, needs investigation
   303  			},
   304  			{
   305  				Query:    "select active_branch()",
   306  				Skip:     true,
   307  				Expected: []sql.Row{{"main"}},
   308  			},
   309  			{
   310  				Query:    "select * from t order by 1",
   311  				Skip:     true,
   312  				Expected: []sql.Row{},
   313  			},
   314  			{
   315  				Query:    "select name from dolt_branches order by 1",
   316  				Skip:     true,
   317  				Expected: []sql.Row{{"branch1"}, {"main"}},
   318  			},
   319  			{
   320  				Query:    "select * from `mydb/branch1`.t order by 1",
   321  				Skip:     true,
   322  				Expected: []sql.Row{{1, 100}},
   323  			},
   324  		},
   325  	},
   326  	{
   327  		Name: "checkout existing branch and commit in procedure",
   328  		SetUpScript: []string{
   329  			"create table t(a int primary key auto_increment, b int);",
   330  			"call dolt_commit('-Am', 'new table');",
   331  			"call dolt_branch('branch1');",
   332  			`create procedure edit_on_branch()
   333  begin
   334  	call dolt_checkout('branch1');
   335  	insert into t(b) values (100);
   336  	call dolt_commit('-am', 'new row');
   337  	call dolt_checkout('main');
   338  end
   339  `,
   340  		},
   341  		Assertions: []queries.ScriptTestAssertion{
   342  			{
   343  				Query:            "call edit_on_branch();",
   344  				Skip:             true,
   345  				SkipResultsCheck: true, // return value is a bit odd, needs investigation
   346  			},
   347  			{
   348  				Query:    "select active_branch()",
   349  				Skip:     true,
   350  				Expected: []sql.Row{{"main"}},
   351  			},
   352  			{
   353  				Query:    "select * from t order by 1",
   354  				Skip:     true,
   355  				Expected: []sql.Row{},
   356  			},
   357  			{
   358  				Query:    "select name from dolt_branches order by 1",
   359  				Skip:     true,
   360  				Expected: []sql.Row{{"branch1"}, {"main"}},
   361  			},
   362  			{
   363  				Query:    "select * from `mydb/branch1`.t order by 1",
   364  				Skip:     true,
   365  				Expected: []sql.Row{{1, 100}},
   366  			},
   367  		},
   368  	},
   369  	{
   370  		Name: "merge in procedure",
   371  		SetUpScript: []string{
   372  			"create table t(a int primary key auto_increment, b int);",
   373  			"call dolt_commit('-Am', 'new table');",
   374  			"call dolt_branch('branch1');",
   375  			"insert into t(a, b) values (1, 100);",
   376  			"call dolt_commit('-am', 'new row');",
   377  			"call dolt_checkout('branch1');",
   378  			"insert into t(a, b) values (2, 200);",
   379  			"call dolt_commit('-am', 'new row on branch1');",
   380  			"call dolt_checkout('main');",
   381  			`create procedure merge_branch(branchName varchar(255))
   382  begin
   383  	call dolt_checkout(branchName);
   384  	call dolt_merge('--no-ff', 'main');
   385  	call dolt_checkout('main');
   386  end
   387  `,
   388  		},
   389  		Assertions: []queries.ScriptTestAssertion{
   390  			{
   391  				Query:            "call merge_branch('branch1');",
   392  				SkipResultsCheck: true, // return value is a bit odd, needs investigation
   393  			},
   394  			{
   395  				Query:    "select active_branch()",
   396  				Expected: []sql.Row{{"main"}},
   397  			},
   398  			{
   399  				Query:    "select * from t order by 1",
   400  				Expected: []sql.Row{{1, 100}},
   401  			},
   402  			{
   403  				Query:    "select name from dolt_branches order by 1",
   404  				Expected: []sql.Row{{"branch1"}, {"main"}},
   405  			},
   406  			{
   407  				Query:    "select * from `mydb/branch1`.t order by 1",
   408  				Expected: []sql.Row{{1, 100}, {2, 200}},
   409  			},
   410  		},
   411  	},
   412  }