@@ -25,6 +25,7 @@ import (
2525 "testing"
2626 "time"
2727
28+ "github.com/dolthub/vitess/go/sqltypes"
2829 "github.com/pmezard/go-difflib/difflib"
2930 "github.com/stretchr/testify/assert"
3031 "github.com/stretchr/testify/require"
@@ -981,6 +982,70 @@ func TestTimestampBindingsCanBeCompared(t *testing.T) {
981982 require .Equal (t , 1 , count )
982983}
983984
985+ // TestAlterTableWithBadSchema is a backwards compatibility test that
986+ // ensures tables made with old versions of the engine can be altered.
987+ func TestAlterTableWithBadSchema (t * testing.T ) {
988+ harness := enginetest .NewDefaultMemoryHarness ()
989+ pro := harness .Provider ()
990+ harness .NewDatabases ("mydb" )
991+ ctx := harness .NewContext ()
992+ sqlDb , err := pro .Database (ctx , "mydb" )
993+ require .NoError (t , err )
994+ db := sqlDb .(* memory.HistoryDatabase )
995+
996+ sch := sql .NewPrimaryKeySchema (sql.Schema {
997+ {Name : "a" , Type : types .MustCreateStringWithDefaults (sqltypes .VarChar , 16383 ), Source : "mytable" },
998+ {Name : "b" , Type : types .MustCreateStringWithDefaults (sqltypes .VarChar , 16383 ), Source : "mytable" },
999+ {Name : "c" , Type : types .MustCreateStringWithDefaults (sqltypes .VarChar , 16383 ), Source : "mytable" },
1000+ })
1001+
1002+ harness .NewTableAsOf (db , "mytable" , sch , nil )
1003+
1004+ engine , err := harness .NewEngine (t )
1005+ require .NoError (t , err )
1006+
1007+ tests := []struct {
1008+ name string
1009+ q string
1010+ err bool
1011+ }{
1012+ {
1013+ name : "noop modify triggers validation" ,
1014+ q : "alter table mytable rename column a to d" ,
1015+ err : true ,
1016+ },
1017+ {
1018+ name : "partial update with invalid final schema fails" ,
1019+ q : "alter table mytable modify column a varchar(100), modify column b varchar(100)" ,
1020+ err : true ,
1021+ },
1022+ {
1023+ name : "update with valid final schema succeeds" ,
1024+ q : "alter table mytable modify column a varchar(100), modify column b varchar(100), modify column c varchar(100)" ,
1025+ err : false ,
1026+ },
1027+ {
1028+ name : "mixed update add with invalid final schema fails" ,
1029+ q : "alter table mytable modify column a varchar(100), modify column b varchar(100), modify column c varchar(100), add column d varchar(max)" ,
1030+ err : true ,
1031+ },
1032+ }
1033+ for _ , tt := range tests {
1034+ t .Run (tt .name , func (t * testing.T ) {
1035+ ctx := harness .NewContext ()
1036+ _ , iter , err := engine .Query (ctx , tt .q )
1037+ // errors should be analyze time, not execution time
1038+ if tt .err {
1039+ require .Error (t , err )
1040+ } else {
1041+ require .NoError (t , err )
1042+ _ , err = sql .RowIterToRows (ctx , iter )
1043+ require .NoError (t , err )
1044+ }
1045+ })
1046+ }
1047+ }
1048+
9841049func newDatabase () (* sql2.DB , func ()) {
9851050 // Grab an empty port so that tests do not fail if a specific port is already in use
9861051 listener , err := net .Listen ("tcp" , ":0" )
0 commit comments