|
| 1 | +/* |
| 2 | +Copyright 2026 The Vitess Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package sessionvariable |
| 18 | + |
| 19 | +import ( |
| 20 | + "flag" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + "path" |
| 24 | + "strings" |
| 25 | + "testing" |
| 26 | + "time" |
| 27 | + |
| 28 | + "github.com/stretchr/testify/assert" |
| 29 | + "github.com/stretchr/testify/require" |
| 30 | + |
| 31 | + "vitess.io/vitess/go/mysql" |
| 32 | + "vitess.io/vitess/go/test/endtoend/cluster" |
| 33 | + "vitess.io/vitess/go/test/endtoend/onlineddl" |
| 34 | + "vitess.io/vitess/go/vt/schema" |
| 35 | +) |
| 36 | + |
| 37 | +var ( |
| 38 | + clusterInstance *cluster.LocalProcessCluster |
| 39 | + shards []cluster.Shard |
| 40 | + vtParams mysql.ConnParams |
| 41 | + |
| 42 | + hostname = "localhost" |
| 43 | + keyspaceName = "ks" |
| 44 | + cell = "zone1" |
| 45 | + schemaChangeDirectory = "" |
| 46 | + migrationWaitTimeout = 60 * time.Second |
| 47 | + |
| 48 | + // Zero-date defaults are rejected under the default MySQL sql_mode. Setting |
| 49 | + // sql_mode via --session-variable is what makes this CREATE succeed. |
| 50 | + createZeroDateTable = ` |
| 51 | + CREATE TABLE %s ( |
| 52 | + id INT NOT NULL, |
| 53 | + d DATE DEFAULT '0000-00-00', |
| 54 | + PRIMARY KEY (id) |
| 55 | + ) ENGINE=InnoDB` |
| 56 | + createBaseTable = ` |
| 57 | + CREATE TABLE %s ( |
| 58 | + id INT NOT NULL, |
| 59 | + PRIMARY KEY (id) |
| 60 | + ) ENGINE=InnoDB` |
| 61 | + // Adding a zero-date default must fail during VReplication shadow-table ALTER |
| 62 | + // unless sql_mode allows invalid dates. |
| 63 | + alterAddZeroDateColumn = `ALTER TABLE %s ADD COLUMN d DATE DEFAULT '0000-00-00'` |
| 64 | + dropTable = `DROP TABLE IF EXISTS %s` |
| 65 | + |
| 66 | + sessionVariableStrategy = "direct --session-variable sql_mode=ALLOW_INVALID_DATES" |
| 67 | + onlineSessionStrategy = "vitess --session-variable sql_mode=ALLOW_INVALID_DATES" |
| 68 | +) |
| 69 | + |
| 70 | +func TestMain(m *testing.M) { |
| 71 | + flag.Parse() |
| 72 | + |
| 73 | + exitcode, err := func() (int, error) { |
| 74 | + clusterInstance = cluster.NewCluster(cell, hostname) |
| 75 | + schemaChangeDirectory = path.Join("/tmp", fmt.Sprintf("schema_change_dir_%d", clusterInstance.GetAndReserveTabletUID())) |
| 76 | + defer os.RemoveAll(schemaChangeDirectory) |
| 77 | + defer clusterInstance.Teardown() |
| 78 | + |
| 79 | + if _, err := os.Stat(schemaChangeDirectory); os.IsNotExist(err) { |
| 80 | + _ = os.Mkdir(schemaChangeDirectory, 0o700) |
| 81 | + } |
| 82 | + |
| 83 | + clusterInstance.VtctldExtraArgs = []string{ |
| 84 | + "--schema-change-dir", schemaChangeDirectory, |
| 85 | + "--schema-change-controller", "local", |
| 86 | + "--schema-change-check-interval", "1s", |
| 87 | + } |
| 88 | + clusterInstance.VtTabletExtraArgs = []string{ |
| 89 | + "--heartbeat-interval", "250ms", |
| 90 | + "--migration-check-interval", "2s", |
| 91 | + } |
| 92 | + |
| 93 | + if err := clusterInstance.StartTopo(); err != nil { |
| 94 | + return 1, err |
| 95 | + } |
| 96 | + |
| 97 | + keyspace := &cluster.Keyspace{Name: keyspaceName} |
| 98 | + if err := clusterInstance.StartUnshardedKeyspace(*keyspace, 1, false, cell); err != nil { |
| 99 | + return 1, err |
| 100 | + } |
| 101 | + |
| 102 | + vtgateInstance := clusterInstance.NewVtgateInstance() |
| 103 | + if err := vtgateInstance.Setup(); err != nil { |
| 104 | + return 1, err |
| 105 | + } |
| 106 | + clusterInstance.VtgateProcess = *vtgateInstance |
| 107 | + vtParams = mysql.ConnParams{ |
| 108 | + Host: clusterInstance.Hostname, |
| 109 | + Port: clusterInstance.VtgateMySQLPort, |
| 110 | + } |
| 111 | + |
| 112 | + return m.Run(), nil |
| 113 | + }() |
| 114 | + if err != nil { |
| 115 | + fmt.Printf("%v\n", err) |
| 116 | + os.Exit(1) |
| 117 | + } |
| 118 | + os.Exit(exitcode) |
| 119 | +} |
| 120 | + |
| 121 | +// TestVtctldclientDirectSessionVariable verifies ApplySchema --ddl-strategy with |
| 122 | +// --session-variable applies SESSION sql_mode before direct DDL. |
| 123 | +func TestVtctldclientDirectSessionVariable(t *testing.T) { |
| 124 | + tableName := "vtctldclient_session_var" |
| 125 | + createSQL := fmt.Sprintf(createZeroDateTable, tableName) |
| 126 | + dropSQL := fmt.Sprintf(dropTable, tableName) |
| 127 | + t.Cleanup(func() { |
| 128 | + _, _ = clusterInstance.VtctldClientProcess.ApplySchemaWithOutput( |
| 129 | + keyspaceName, |
| 130 | + dropSQL, |
| 131 | + cluster.ApplySchemaParams{DDLStrategy: "direct"}, |
| 132 | + ) |
| 133 | + }) |
| 134 | + |
| 135 | + t.Run("without session variable", func(t *testing.T) { |
| 136 | + output, err := clusterInstance.VtctldClientProcess.ApplySchemaWithOutput( |
| 137 | + keyspaceName, |
| 138 | + createSQL, |
| 139 | + cluster.ApplySchemaParams{DDLStrategy: "direct"}, |
| 140 | + ) |
| 141 | + require.Error(t, err) |
| 142 | + assert.True(t, |
| 143 | + strings.Contains(output, "Invalid default value") || strings.Contains(err.Error(), "Invalid default value"), |
| 144 | + "expected zero-date rejection, got output=%q err=%v", output, err, |
| 145 | + ) |
| 146 | + }) |
| 147 | + |
| 148 | + t.Run("with session variable", func(t *testing.T) { |
| 149 | + _, err := clusterInstance.VtctldClientProcess.ApplySchemaWithOutput( |
| 150 | + keyspaceName, |
| 151 | + createSQL, |
| 152 | + cluster.ApplySchemaParams{DDLStrategy: sessionVariableStrategy}, |
| 153 | + ) |
| 154 | + require.NoError(t, err) |
| 155 | + assertTableExists(t, tableName) |
| 156 | + }) |
| 157 | +} |
| 158 | + |
| 159 | +// TestVtgateOnlineSessionVariable verifies @@ddl_strategy --session-variable is |
| 160 | +// applied on the VReplication shadow-table path (initVreplicationOriginalMigration), |
| 161 | +// not only on CREATE TABLE which executes directly. |
| 162 | +func TestVtgateOnlineSessionVariable(t *testing.T) { |
| 163 | + require.NoError(t, clusterInstance.WaitForTabletsToHealthyInVtgate()) |
| 164 | + shards = clusterInstance.Keyspaces[0].Shards |
| 165 | + |
| 166 | + tableName := "vtgate_session_var" |
| 167 | + createSQL := fmt.Sprintf(createBaseTable, tableName) |
| 168 | + alterSQL := fmt.Sprintf(alterAddZeroDateColumn, tableName) |
| 169 | + dropSQL := fmt.Sprintf(dropTable, tableName) |
| 170 | + t.Cleanup(func() { |
| 171 | + _, _ = clusterInstance.VtctldClientProcess.ApplySchemaWithOutput( |
| 172 | + keyspaceName, |
| 173 | + dropSQL, |
| 174 | + cluster.ApplySchemaParams{DDLStrategy: "direct"}, |
| 175 | + ) |
| 176 | + }) |
| 177 | + |
| 178 | + _, err := clusterInstance.VtctldClientProcess.ApplySchemaWithOutput( |
| 179 | + keyspaceName, |
| 180 | + createSQL, |
| 181 | + cluster.ApplySchemaParams{DDLStrategy: "direct"}, |
| 182 | + ) |
| 183 | + require.NoError(t, err) |
| 184 | + assertTableExists(t, tableName) |
| 185 | + |
| 186 | + t.Run("without session variable", func(t *testing.T) { |
| 187 | + uuid := submitOnlineDDL(t, "vitess", alterSQL) |
| 188 | + status := onlineddl.WaitForMigrationStatus( |
| 189 | + t, &vtParams, shards, uuid, migrationWaitTimeout, |
| 190 | + schema.OnlineDDLStatusComplete, schema.OnlineDDLStatusFailed, |
| 191 | + ) |
| 192 | + require.Equal(t, schema.OnlineDDLStatusFailed, status) |
| 193 | + assertColumnMissing(t, tableName, "d") |
| 194 | + }) |
| 195 | + |
| 196 | + t.Run("with session variable", func(t *testing.T) { |
| 197 | + uuid := submitOnlineDDL(t, onlineSessionStrategy, alterSQL) |
| 198 | + status := onlineddl.WaitForMigrationStatus( |
| 199 | + t, &vtParams, shards, uuid, migrationWaitTimeout, |
| 200 | + schema.OnlineDDLStatusComplete, schema.OnlineDDLStatusFailed, |
| 201 | + ) |
| 202 | + require.Equal(t, schema.OnlineDDLStatusComplete, status) |
| 203 | + assertColumnExists(t, tableName, "d") |
| 204 | + }) |
| 205 | +} |
| 206 | + |
| 207 | +func submitOnlineDDL(t *testing.T, ddlStrategy, sql string) string { |
| 208 | + t.Helper() |
| 209 | + row := onlineddl.VtgateExecDDL(t, &vtParams, ddlStrategy, sql, "").Named().Row() |
| 210 | + require.NotNil(t, row) |
| 211 | + uuid := strings.TrimSpace(row.AsString("uuid", "")) |
| 212 | + require.NotEmpty(t, uuid) |
| 213 | + return uuid |
| 214 | +} |
| 215 | + |
| 216 | +func assertTableExists(t *testing.T, tableName string) { |
| 217 | + t.Helper() |
| 218 | + qr, err := onlineddl.VtgateExecQuery(t.Context(), &vtParams, "show tables like '"+tableName+"'") |
| 219 | + require.NoError(t, err) |
| 220 | + require.Len(t, qr.Rows, 1, "expected table %s to exist", tableName) |
| 221 | +} |
| 222 | + |
| 223 | +func assertColumnExists(t *testing.T, tableName, columnName string) { |
| 224 | + t.Helper() |
| 225 | + qr, err := onlineddl.VtgateExecQuery( |
| 226 | + t.Context(), |
| 227 | + &vtParams, |
| 228 | + fmt.Sprintf("show columns from %s like '%s'", tableName, columnName), |
| 229 | + ) |
| 230 | + require.NoError(t, err) |
| 231 | + require.Len(t, qr.Rows, 1, "expected column %s.%s to exist", tableName, columnName) |
| 232 | +} |
| 233 | + |
| 234 | +func assertColumnMissing(t *testing.T, tableName, columnName string) { |
| 235 | + t.Helper() |
| 236 | + qr, err := onlineddl.VtgateExecQuery( |
| 237 | + t.Context(), |
| 238 | + &vtParams, |
| 239 | + fmt.Sprintf("show columns from %s like '%s'", tableName, columnName), |
| 240 | + ) |
| 241 | + require.NoError(t, err) |
| 242 | + require.Empty(t, qr.Rows, "expected column %s.%s to be missing", tableName, columnName) |
| 243 | +} |
0 commit comments