Skip to content

Commit

Permalink
Set FK checks off while creating schema in MoveTables atomic mode.
Browse files Browse the repository at this point in the history
Signed-off-by: Rohit Nayak <[email protected]>
  • Loading branch information
rohit-nayak-ps committed Mar 11, 2024
1 parent 2830a07 commit 7c23bae
Show file tree
Hide file tree
Showing 13 changed files with 744 additions and 630 deletions.
27 changes: 26 additions & 1 deletion go/test/endtoend/vreplication/fk_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,19 @@ create table child(id int, parent_id int, name varchar(128), primary key(id), fo
create view vparent as select * from parent;
create table t1(id int, name varchar(128), primary key(id)) engine=innodb;
create table t2(id int, t1id int, name varchar(128), primary key(id), foreign key(t1id) references t1(id) on delete cascade) engine=innodb;
create table t11 (id int primary key, i int);
create table t12 (id int primary key, i int);
alter table t11 add constraint f11 foreign key (i) references t12 (id);
alter table t12 add constraint f12 foreign key (i) references t11 (id);
`
initialFKData = `
insert into parent values(1, 'parent1'), (2, 'parent2');
insert into child values(1, 1, 'child11'), (2, 1, 'child21'), (3, 2, 'child32');
insert into t1 values(1, 't11'), (2, 't12');
insert into t2 values(1, 1, 't21'), (2, 1, 't22'), (3, 2, 't23');
insert into t11 values(1, null);
insert into t12 values(1, 1);
update t11 set i = 1 where id = 1;
`

initialFKSourceVSchema = `
Expand All @@ -37,7 +44,9 @@ insert into t2 values(1, 1, 't21'), (2, 1, 't22'), (3, 2, 't23');
"parent": {},
"child": {},
"t1": {},
"t2": {}
"t2": {},
"t11": {},
"t12": {}
}
}
`
Expand Down Expand Up @@ -82,6 +91,22 @@ insert into t2 values(1, 1, 't21'), (2, 1, 't22'), (3, 2, 't23');
"name": "reverse_bits"
}
]
},
"t11": {
"column_vindexes": [
{
"column": "id",
"name": "reverse_bits"
}
]
},
"t12": {
"column_vindexes": [
{
"column": "id",
"name": "reverse_bits"
}
]
}
}
}
Expand Down
19 changes: 12 additions & 7 deletions go/test/endtoend/vreplication/fk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import (

const testWorkflowFlavor = workflowFlavorRandom

var counter int = 100

// TestFKWorkflow runs a MoveTables workflow with atomic copy for a db with foreign key constraints.
// It inserts initial data, then simulates load. We insert both child rows with foreign keys and those without,
// i.e. with foreign_key_checks=0.
Expand Down Expand Up @@ -106,7 +108,9 @@ func TestFKWorkflow(t *testing.T) {
require.NotNil(t, targetTab)
catchup(t, targetTab, workflowName, "MoveTables")
vdiff(t, targetKeyspace, workflowName, cellName, true, false, nil)
ls.waitForAdditionalRows(200)
if withLoad {
ls.waitForAdditionalRows(200)
}
vdiff(t, targetKeyspace, workflowName, cellName, true, false, nil)
if withLoad {
cancel()
Expand All @@ -121,9 +125,7 @@ func TestFKWorkflow(t *testing.T) {
ls = newFKLoadSimulator(t, ctx)
defer cancel()
go ls.simulateLoad()
}
ls.waitForAdditionalRows(200)
if withLoad {
ls.waitForAdditionalRows(200)
cancel()
<-ch
}
Expand All @@ -137,9 +139,7 @@ func insertInitialFKData(t *testing.T) {
sourceKeyspace := "fksource"
shard := "0"
db := fmt.Sprintf("%s:%s", sourceKeyspace, shard)
log.Infof("Inserting initial FK data")
execMultipleQueries(t, vtgateConn, db, initialFKData)
log.Infof("Done inserting initial FK data")
waitForRowCount(t, vtgateConn, db, "parent", 2)
waitForRowCount(t, vtgateConn, db, "child", 3)
waitForRowCount(t, vtgateConn, db, "t1", 2)
Expand Down Expand Up @@ -193,8 +193,13 @@ func (ls *fkLoadSimulator) simulateLoad() {
default: // 20% chance to delete
ls.delete()
}
for _, table := range []string{"t11", "t12"} {
query := fmt.Sprintf("insert /*+ SET_VAR(foreign_key_checks=0) */ into fksource.%s values(%d, %d)", table, counter, counter)
ls.exec(query)
counter++
}
require.NoError(t, err)
time.Sleep(1 * time.Millisecond)
time.Sleep(10 * time.Millisecond)
}
}

Expand Down
4 changes: 4 additions & 0 deletions go/vt/mysqlctl/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,10 @@ func (mysqld *Mysqld) ApplySchemaChange(ctx context.Context, dbName string, chan
sql = "SET sql_log_bin = 0;\n" + sql
}

if change.SetForeignKeyChecksOff {
sql = "SET foreign_key_checks = 0;\n" + sql

Check warning on line 540 in go/vt/mysqlctl/schema.go

View check run for this annotation

Codecov / codecov/patch

go/vt/mysqlctl/schema.go#L539-L540

Added lines #L539 - L540 were not covered by tests
}

// add a 'use XXX' in front of the SQL
sql = fmt.Sprintf("USE %s;\n%s", sqlescape.EscapeID(dbName), sql)

Expand Down
13 changes: 7 additions & 6 deletions go/vt/mysqlctl/tmutils/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,13 @@ func DiffSchemaToArray(leftName string, left *tabletmanagerdatapb.SchemaDefiniti
// SchemaChange contains all necessary information to apply a schema change.
// It should not be sent over the wire, it's just a set of parameters.
type SchemaChange struct {
SQL string
Force bool
AllowReplication bool
BeforeSchema *tabletmanagerdatapb.SchemaDefinition
AfterSchema *tabletmanagerdatapb.SchemaDefinition
SQLMode string
SQL string
Force bool
AllowReplication bool
BeforeSchema *tabletmanagerdatapb.SchemaDefinition
AfterSchema *tabletmanagerdatapb.SchemaDefinition
SQLMode string
SetForeignKeyChecksOff bool
}

// Equal compares two SchemaChange objects.
Expand Down
Loading

0 comments on commit 7c23bae

Please sign in to comment.