Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

temp: set foreign_key_checks=0 on mysql soft reset #4513

Closed
wants to merge 14 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use query_engine_tests::*;

// https://stackoverflow.com/questions/4380813/how-to-get-rid-of-mysql-error-prepared-statement-needs-to-be-re-prepared
// Looks like there's a bug with create view stmt on MariaDB
#[test_suite(schema(schema), exclude(MongoDb, MySQL("mariadb"), Vitess))]
#[test_suite(schema(schema), exclude(MongoDb, MySQL("mariadb"), MySql(5.6), Vitess))]
mod views {
use query_engine_tests::{connector_test, run_query, Runner};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ pub(crate) fn render_script(
// some steps don't render anything.
let mut is_first_step = true;

if let Some(fkc0) = flavour.render_foreign_key_checks_0() {
script.push_str(fkc0);
script.push('\n');
}

if let Some(begin) = flavour.render_begin_transaction() {
script.push_str(begin);
script.push('\n');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ pub(crate) trait SqlRenderer {
None
}

/// Render a foreign key checks = 0.
fn render_foreign_key_checks_0(&self) -> Option<&'static str> {
None
}

/// Render a `RenameForeignKey` step.
fn render_rename_foreign_key(&self, fks: MigrationPair<ForeignKeyWalker<'_>>) -> String;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
sql_migration::{AlterColumn, AlterEnum, AlterTable, RedefineTable, TableChange},
sql_schema_differ::ColumnChanges,
};
use indoc::indoc;
use once_cell::sync::Lazy;
use psl::builtin_connectors::MySqlType;
use regex::Regex;
Expand Down Expand Up @@ -343,6 +344,14 @@ impl SqlRenderer for MysqlFlavour {
fn render_rename_foreign_key(&self, _fks: MigrationPair<ForeignKeyWalker<'_>>) -> String {
unreachable!("render RenameForeignKey on MySQL")
}

fn render_foreign_key_checks_0(&self) -> Option<&'static str> {
let sql = indoc! { r#"
SET foreign_key_checks = 0;
"# };

Some(sql)
}
}

fn render_mysql_modify(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ fn basic_create_migration_works(api: TestApi) {
"#]]
} else if is_mysql {
expect![[r#"
SET foreign_key_checks = 0;

-- CreateTable
CREATE TABLE `Cat` (
`id` INTEGER NOT NULL,
Expand Down Expand Up @@ -158,6 +160,8 @@ fn creating_a_second_migration_should_have_the_previous_sql_schema_as_baseline(a
"#]]
} else if is_mysql {
expect![[r#"
SET foreign_key_checks = 0;

-- CreateTable
CREATE TABLE `Dog` (
`id` INTEGER NOT NULL,
Expand Down Expand Up @@ -382,6 +386,8 @@ fn create_enum_step_only_rendered_when_needed(api: TestApi) {
} else if is_mysql {
indoc! {
r#"
SET foreign_key_checks = 0;

-- CreateTable
CREATE TABLE `Cat` (
`id` INTEGER NOT NULL,
Expand Down Expand Up @@ -696,6 +702,8 @@ fn create_constraint_name_tests_w_implicit_names(api: TestApi) {
} else if is_mysql {
expect![[
r#"
SET foreign_key_checks = 0;

-- CreateTable
CREATE TABLE `A` (
`id` INTEGER NOT NULL,
Expand Down Expand Up @@ -933,6 +941,8 @@ fn create_constraint_name_tests_w_explicit_names(api: TestApi) {
} else if is_mysql {
expect![[
r#"
SET foreign_key_checks = 0;

-- CreateTable
CREATE TABLE `A` (
`id` INTEGER NOT NULL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn failing_migration_after_migration_dropping_data(api: TestApi) {

0: sql_schema_connector::apply_migration::apply_script
with migration_name=" 2"
at schema-engine/connectors/sql-schema-connector/src/apply_migration.rs:106
at schema-engine/connectors/sql-schema-connector/src/apply_migration.rs:111
1: schema_core::commands::apply_migrations::Applying migration
with migration_name=" 2"
at schema-engine/core/src/commands/apply_migrations.rs:91"#]];
Expand Down Expand Up @@ -89,7 +89,7 @@ fn failing_step_in_migration_dropping_data(api: TestApi) {

0: sql_schema_connector::apply_migration::apply_script
with migration_name=" 1"
at schema-engine/connectors/sql-schema-connector/src/apply_migration.rs:106
at schema-engine/connectors/sql-schema-connector/src/apply_migration.rs:111
1: schema_core::commands::apply_migrations::Applying migration
with migration_name=" 1"
at schema-engine/core/src/commands/apply_migrations.rs:91"#]];
Expand Down Expand Up @@ -203,7 +203,7 @@ fn syntax_errors_return_error_position(api: TestApi) {

0: sql_schema_connector::apply_migration::apply_script
with migration_name=" 0"
at schema-engine/connectors/sql-schema-connector/src/apply_migration.rs:106
at schema-engine/connectors/sql-schema-connector/src/apply_migration.rs:111
1: schema_core::commands::apply_migrations::Applying migration
with migration_name=" 0"
at schema-engine/core/src/commands/apply_migrations.rs:91"#]];
Expand Down
20 changes: 17 additions & 3 deletions schema-engine/sql-migration-tests/tests/migrations/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ fn default_current_timestamp_precision_follows_column_precision(api: TestApi) {

let expected_migration = indoc!(
r#"
SET foreign_key_checks = 0;

-- CreateTable
CREATE TABLE `A` (
`id` INTEGER NOT NULL,
Expand Down Expand Up @@ -317,6 +319,8 @@ fn datetime_dbgenerated_defaults(api: TestApi) {

let expected_migration = indoc!(
r#"
SET foreign_key_checks = 0;

-- CreateTable
CREATE TABLE `A` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
Expand All @@ -340,6 +344,8 @@ fn mysql_apply_migrations_errors_gives_the_failed_sql(api: TestApi) {
let migrations_directory = api.create_migrations_directory();

let migration = r#"
SET foreign_key_checks = 0;

CREATE TABLE `Cat` ( id INTEGER PRIMARY KEY );

DROP TABLE `Emu`;
Expand Down Expand Up @@ -378,7 +384,7 @@ fn mysql_apply_migrations_errors_gives_the_failed_sql(api: TestApi) {
Database error:
target: test.0.primary: vttablet: rpc error: code = InvalidArgument desc = Unknown table 'vt_test_0.Emu' (errno 1051) (sqlstate 42S02) (CallerID: userData1): Sql: "drop table Emu", BindVars: {}

Please check the query number 2 from the migration file.
Please check the query number 3 from the migration file.

"#]]
} else if cfg!(windows) {
Expand All @@ -392,7 +398,7 @@ fn mysql_apply_migrations_errors_gives_the_failed_sql(api: TestApi) {
Database error:
Unknown table 'mysql_apply_migrations_errors_gives_the_failed_sql.emu'

Please check the query number 2 from the migration file.
Please check the query number 3 from the migration file.

"#]]
} else {
Expand All @@ -406,7 +412,7 @@ fn mysql_apply_migrations_errors_gives_the_failed_sql(api: TestApi) {
Database error:
Unknown table 'mysql_apply_migrations_errors_gives_the_failed_sql.Emu'

Please check the query number 2 from the migration file.
Please check the query number 3 from the migration file.

"#]]
};
Expand Down Expand Up @@ -479,6 +485,8 @@ fn dropping_m2m_relation_from_datamodel_works() {
});

let expected = expect![[r#"
SET foreign_key_checks = 0;

-- DropForeignKey
ALTER TABLE `_puppyFriendships` DROP FOREIGN KEY `_puppyFriendships_A_fkey`;

Expand Down Expand Up @@ -551,6 +559,8 @@ fn alter_constraint_name(mut api: TestApi) {
let expected_script = if is_mysql_5_6 || is_mariadb {
expect![[
r#"
SET foreign_key_checks = 0;

-- DropForeignKey
ALTER TABLE `B` DROP FOREIGN KEY `B_aId_fkey`;

Expand All @@ -575,6 +585,8 @@ fn alter_constraint_name(mut api: TestApi) {
"#]]
} else {
expect![[r#"
SET foreign_key_checks = 0;

-- DropForeignKey
ALTER TABLE `B` DROP FOREIGN KEY `B_aId_fkey`;

Expand Down Expand Up @@ -613,6 +625,8 @@ fn bigint_defaults_work(api: TestApi) {
}
"#;
let sql = expect![[r#"
SET foreign_key_checks = 0;

-- CreateTable
CREATE TABLE `foo` (
`id` VARCHAR(191) NOT NULL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ model B {
}

// Expected Migration:
// SET foreign_key_checks = 0;
//
// -- CreateTable
// CREATE TABLE `A` (
// `id` INTEGER NOT NULL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ model B {
}

// Expected Migration:
// SET foreign_key_checks = 0;
//
// -- CreateTable
// CREATE TABLE `A` (
// `id` INTEGER NOT NULL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ enum Kind {
error
}
// Expected Migration:
// SET foreign_key_checks = 0;
//
// -- CreateTable
// CREATE TABLE `MyTable` (
// `id` INTEGER NOT NULL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2581,6 +2581,8 @@ fn mysql_foreign_key_on_delete_must_be_handled(api: TestApi) {
#[test_connector(tags(Mysql))]
fn mysql_join_table_unique_indexes_must_be_inferred(api: TestApi) {
let sql = r#"
SET foreign_key_checks = 0;

CREATE TABLE `Cat` (
id INTEGER AUTO_INCREMENT PRIMARY KEY,
name TEXT
Expand Down
Loading