diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index eab0964668c3..ed17e9139124 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -255,12 +255,16 @@ jobs: shell: bash run: cargo +${{ matrix.rust }} test --manifest-path diesel_cli/Cargo.toml --no-default-features --features "${{ matrix.backend }}" + - name: Test diesel-cli (integer_primary_key_i64_for_sqlite_3_37) + shell: bash + run: cargo +${{ matrix.rust }} test --manifest-path diesel_cli/Cargo.toml --no-default-features --features "sqlite integer_primary_key_i64_for_sqlite_3_37" + - name: Test diesel examples shell: bash env: BACKEND: ${{ matrix.backend }} run: | - (cd examples/${{ matrix.backend }} && rustup run ${{ matrix.rust }} bash test_all) + (cd examples/${{ matrix.backend }} && rustup run ${{ matrix.rust }} bash test_all) - name: Test migrations-internals shell: bash diff --git a/diesel_cli/Cargo.toml b/diesel_cli/Cargo.toml index 9a9bfadd18e8..722104b72cf9 100644 --- a/diesel_cli/Cargo.toml +++ b/diesel_cli/Cargo.toml @@ -35,7 +35,7 @@ libsqlite3-sys = { version = ">=0.17.2, <0.29.0", optional = true } diffy = "0.3.0" regex = "1.0.6" serde_regex = "1.1" -diesel_table_macro_syntax = {version = "0.1", path = "../diesel_table_macro_syntax"} +diesel_table_macro_syntax = { version = "0.1", path = "../diesel_table_macro_syntax" } syn = { version = "2", features = ["visit"] } tracing = "0.1" tracing-subscriber = { version = "0.3.10", features = ["env-filter"] } @@ -61,6 +61,7 @@ postgres = ["diesel/postgres", "uses_information_schema"] sqlite = ["diesel/sqlite"] mysql = ["diesel/mysql", "uses_information_schema"] sqlite-bundled = ["sqlite", "libsqlite3-sys/bundled"] +integer_primary_key_i64_for_sqlite_3_37 = [] uses_information_schema = [] [[test]] diff --git a/diesel_cli/src/infer_schema_internals/inference.rs b/diesel_cli/src/infer_schema_internals/inference.rs index cf7650e0573c..7ab02fbda81b 100644 --- a/diesel_cli/src/infer_schema_internals/inference.rs +++ b/diesel_cli/src/infer_schema_internals/inference.rs @@ -185,10 +185,13 @@ fn get_column_information( fn determine_column_type( attr: &ColumnInformation, conn: &mut InferConnection, + #[allow(unused_variables)] table: &TableName, ) -> Result { match *conn { #[cfg(feature = "sqlite")] - InferConnection::Sqlite(_) => super::sqlite::determine_column_type(attr), + InferConnection::Sqlite(ref mut conn) => { + super::sqlite::determine_column_type(conn, attr, table) + } #[cfg(feature = "postgres")] InferConnection::Pg(ref mut conn) => { use crate::infer_schema_internals::information_schema::DefaultSchema; @@ -275,12 +278,12 @@ pub fn load_table_data( let primary_key = primary_key .iter() .map(|k| rust_name_for_sql_name(k)) - .collect(); + .collect::>(); let column_data = get_column_information(connection, &name, column_sorting)? .into_iter() .map(|c| { - let ty = determine_column_type(&c, connection)?; + let ty = determine_column_type(&c, connection, &name)?; let ColumnInformation { column_name, diff --git a/diesel_cli/src/infer_schema_internals/sqlite.rs b/diesel_cli/src/infer_schema_internals/sqlite.rs index 3fc16779abdf..f17d9b282584 100644 --- a/diesel_cli/src/infer_schema_internals/sqlite.rs +++ b/diesel_cli/src/infer_schema_internals/sqlite.rs @@ -176,6 +176,67 @@ impl QueryableByName for PrimaryKeyInformation { } } +#[cfg(feature = "integer_primary_key_i64_for_sqlite_3_37")] +struct WithoutRowIdInformation { + name: String, + without_row_id: bool, +} + +#[cfg(feature = "integer_primary_key_i64_for_sqlite_3_37")] +impl QueryableByName for WithoutRowIdInformation { + fn build<'a>(row: &impl NamedRow<'a, Sqlite>) -> deserialize::Result { + Ok(Self { + name: NamedRow::get::(row, "name")?, + without_row_id: NamedRow::get::(row, "wr")?, + }) + } +} + +#[cfg(feature = "integer_primary_key_i64_for_sqlite_3_37")] +pub fn column_is_row_id( + conn: &mut SqliteConnection, + table: &TableName, + column_name: &str, + type_name: &str, +) -> QueryResult { + let sqlite_version = get_sqlite_version(conn)?; + if sqlite_version < SqliteVersion::new(3, 37, 0) { + return Ok(false); + } + + if type_name != "integer" { + return Ok(false); + } + + let table_xinfo_query = format!("PRAGMA TABLE_XINFO('{}')", &table.sql_name); + let table_xinfo_results = sql_query(table_xinfo_query).load::(conn)?; + + let primary_keys = table_xinfo_results + .iter() + .filter(|pk_info| pk_info.primary_key) + .collect::>(); + + if primary_keys.len() != 1 { + return Ok(false); + } + + if primary_keys[0].name != column_name { + return Ok(false); + } + + let table_list_query = format!("PRAGMA TABLE_LIST('{}')", &table.sql_name); + let table_list_results = sql_query(table_list_query).load::(conn)?; + + match table_list_results + .iter() + .find(|wr_info| wr_info.name == table.sql_name) + .map(|wr_info| wr_info.without_row_id) + { + Some(false) => Ok(true), + _ => Ok(false), + } +} + #[derive(Queryable)] struct ForeignKeyListRow { _id: i32, @@ -232,8 +293,12 @@ pub fn get_primary_keys( Ok(collected) } -#[tracing::instrument] -pub fn determine_column_type(attr: &ColumnInformation) -> Result { +#[tracing::instrument(skip(conn))] +pub fn determine_column_type( + #[allow(unused_variables)] conn: &mut SqliteConnection, + attr: &ColumnInformation, + table: &TableName, +) -> Result { let mut type_name = attr.type_name.to_lowercase(); if type_name == "generated always" { type_name.clear(); @@ -246,7 +311,16 @@ pub fn determine_column_type(attr: &ColumnInformation) -> Result>(); + + let column_types = std::iter::zip(&tables, &column_infos) + .map(|(table, column_info)| { + determine_column_type(&mut conn, &column_info, &table) + .unwrap() + .sql_name + }) + .collect::>(); + + let expected_column_types = vec!["BigInt", "Integer", "Integer", "Integer"]; + + assert_eq!(column_types, expected_column_types); +} diff --git a/diesel_cli/src/migrations/diff_schema.rs b/diesel_cli/src/migrations/diff_schema.rs index 08c05f15d52d..dcd8f50ba5c1 100644 --- a/diesel_cli/src/migrations/diff_schema.rs +++ b/diesel_cli/src/migrations/diff_schema.rs @@ -514,11 +514,32 @@ where query_builder.push_sql(",\n"); } query_builder.push_sql("\t"); + + let is_only_primary_key = + primary_keys.contains(&column.rust_name) && primary_keys.len() == 1; + query_builder.push_identifier(&column.sql_name)?; - generate_column_type_name(query_builder, &column.ty); - if primary_keys.contains(&column.rust_name) && primary_keys.len() == 1 { + + if cfg!(all( + feature = "sqlite", + feature = "integer_primary_key_i64_for_sqlite_3_37" + )) && is_only_primary_key + && column.ty.sql_name.eq_ignore_ascii_case("BigInt") + { + let ty = ColumnType { + rust_name: "Integer".into(), + sql_name: "Integer".into(), + ..column.ty.clone() + }; + generate_column_type_name(query_builder, &ty); + } else { + generate_column_type_name(query_builder, &column.ty); + } + + if is_only_primary_key { query_builder.push_sql(" PRIMARY KEY"); } + if let Some((table, _, pk)) = foreign_keys.iter().find(|(_, k, _)| k == &column.rust_name) { foreign_key_list.push((column, table, pk)); } diff --git a/diesel_cli/tests/generate_migrations/diff_add_table/sqlite/schema_out.rs/expected_i64_pk.snap b/diesel_cli/tests/generate_migrations/diff_add_table/sqlite/schema_out.rs/expected_i64_pk.snap new file mode 100644 index 000000000000..0f8d10baa63e --- /dev/null +++ b/diesel_cli/tests/generate_migrations/diff_add_table/sqlite/schema_out.rs/expected_i64_pk.snap @@ -0,0 +1,12 @@ +--- +source: diesel_cli/tests/migration_generate.rs +description: "Test: diff_add_table" +--- +// @generated automatically by Diesel CLI. + +diesel::table! { + users (id) { + id -> BigInt, + name -> Text, + } +} diff --git a/diesel_cli/tests/generate_migrations/diff_add_table_all_the_types/sqlite/schema_out.rs/expected_i64_pk.snap b/diesel_cli/tests/generate_migrations/diff_add_table_all_the_types/sqlite/schema_out.rs/expected_i64_pk.snap new file mode 100644 index 000000000000..bd050c8d5eff --- /dev/null +++ b/diesel_cli/tests/generate_migrations/diff_add_table_all_the_types/sqlite/schema_out.rs/expected_i64_pk.snap @@ -0,0 +1,42 @@ +--- +source: diesel_cli/tests/migration_generate.rs +assertion_line: 354 +description: "Test: diff_add_table_all_the_types" +--- +// @generated automatically by Diesel CLI. + +diesel::table! { + all_the_types (id) { + id -> BigInt, + bool_column -> Bool, + integer_column -> Integer, + small_int_col -> SmallInt, + big_int_col -> BigInt, + binary_col -> Binary, + text_col -> Text, + double_col -> Double, + float_col -> Float, + numeric_col -> Double, + date_col -> Date, + timestamp_col -> Timestamp, + time_col -> Time, + float4_col -> Float, + small_int2_col -> SmallInt, + int2_col -> SmallInt, + int4_col -> Integer, + int8_col -> BigInt, + big_int2_col -> BigInt, + float8_col -> Float, + decimal_col -> Double, + varchar_col -> Text, + varchar2_col -> Text, + char_col -> Text, + tinytext_col -> Text, + mediumtext_col -> Text, + tinyblob_col -> Binary, + blob_col -> Binary, + longblob_col -> Binary, + mediumblob_col -> Binary, + varbinary_col -> Binary, + } +} diff --git a/diesel_cli/tests/generate_migrations/diff_add_table_with_fk/schema_i64_pk.rs b/diesel_cli/tests/generate_migrations/diff_add_table_with_fk/schema_i64_pk.rs new file mode 100644 index 000000000000..7e4dc051134d --- /dev/null +++ b/diesel_cli/tests/generate_migrations/diff_add_table_with_fk/schema_i64_pk.rs @@ -0,0 +1,17 @@ +diesel::table! { + users { + id -> BigInt, + name -> Text, + } +} + +diesel::table! { + posts { + id -> BigInt, + title -> Text, + body -> Nullable, + user_id -> Integer, + } +} + +diesel::joinable!(posts -> users (user_id)); diff --git a/diesel_cli/tests/generate_migrations/diff_add_table_with_fk/sqlite/schema_out.rs/expected_i64_pk.snap b/diesel_cli/tests/generate_migrations/diff_add_table_with_fk/sqlite/schema_out.rs/expected_i64_pk.snap new file mode 100644 index 000000000000..35434dc80624 --- /dev/null +++ b/diesel_cli/tests/generate_migrations/diff_add_table_with_fk/sqlite/schema_out.rs/expected_i64_pk.snap @@ -0,0 +1,29 @@ +--- +source: diesel_cli/tests/migration_generate.rs +assertion_line: 332 +description: "Test: diff_add_table_with_fk" +--- +// @generated automatically by Diesel CLI. + +diesel::table! { + posts (id) { + id -> BigInt, + title -> Text, + body -> Nullable, + user_id -> Integer, + } +} + +diesel::table! { + users (id) { + id -> BigInt, + name -> Text, + } +} + +diesel::joinable!(posts -> users (user_id)); + +diesel::allow_tables_to_appear_in_same_query!( + posts, + users, +); diff --git a/diesel_cli/tests/generate_migrations/diff_alter_table_add_column/schema_i64_pk.rs b/diesel_cli/tests/generate_migrations/diff_alter_table_add_column/schema_i64_pk.rs new file mode 100644 index 000000000000..20bbbbd32824 --- /dev/null +++ b/diesel_cli/tests/generate_migrations/diff_alter_table_add_column/schema_i64_pk.rs @@ -0,0 +1,7 @@ +table! { + users { + id -> BigInt, + name -> Text, + hair_color -> Nullable, + } +} diff --git a/diesel_cli/tests/generate_migrations/diff_alter_table_add_column/sqlite/schema_out.rs/expected_i64_pk.snap b/diesel_cli/tests/generate_migrations/diff_alter_table_add_column/sqlite/schema_out.rs/expected_i64_pk.snap new file mode 100644 index 000000000000..aa1aea96f9bd --- /dev/null +++ b/diesel_cli/tests/generate_migrations/diff_alter_table_add_column/sqlite/schema_out.rs/expected_i64_pk.snap @@ -0,0 +1,14 @@ +--- +source: diesel_cli/tests/migration_generate.rs +assertion_line: 327 +description: "Test: diff_alter_table_add_column" +--- +// @generated automatically by Diesel CLI. + +diesel::table! { + users (id) { + id -> BigInt, + name -> Text, + hair_color -> Nullable, + } +} diff --git a/diesel_cli/tests/generate_migrations/diff_alter_table_drop_column/schema_i64_pk.rs b/diesel_cli/tests/generate_migrations/diff_alter_table_drop_column/schema_i64_pk.rs new file mode 100644 index 000000000000..6551d0c9e01d --- /dev/null +++ b/diesel_cli/tests/generate_migrations/diff_alter_table_drop_column/schema_i64_pk.rs @@ -0,0 +1,5 @@ +table! { + users { + id -> BigInt, + } +} diff --git a/diesel_cli/tests/generate_migrations/diff_alter_table_drop_column/sqlite/schema_out.rs/expected_i64_pk.snap b/diesel_cli/tests/generate_migrations/diff_alter_table_drop_column/sqlite/schema_out.rs/expected_i64_pk.snap new file mode 100644 index 000000000000..ff22117bc35f --- /dev/null +++ b/diesel_cli/tests/generate_migrations/diff_alter_table_drop_column/sqlite/schema_out.rs/expected_i64_pk.snap @@ -0,0 +1,12 @@ +--- +source: diesel_cli/tests/migration_generate.rs +assertion_line: 327 +description: "Test: diff_alter_table_drop_column" +--- +// @generated automatically by Diesel CLI. + +diesel::table! { + users (id) { + id -> BigInt, + } +} diff --git a/diesel_cli/tests/generate_migrations/diff_drop_table_with_fk/schema_i64_pk.rs b/diesel_cli/tests/generate_migrations/diff_drop_table_with_fk/schema_i64_pk.rs new file mode 100644 index 000000000000..bcb1d8b16565 --- /dev/null +++ b/diesel_cli/tests/generate_migrations/diff_drop_table_with_fk/schema_i64_pk.rs @@ -0,0 +1,6 @@ +table! { + users { + id -> BigInt, + name -> Text, + } +} diff --git a/diesel_cli/tests/generate_migrations/diff_drop_table_with_fk/sqlite/schema_out.rs/expected_i64_pk.snap b/diesel_cli/tests/generate_migrations/diff_drop_table_with_fk/sqlite/schema_out.rs/expected_i64_pk.snap new file mode 100644 index 000000000000..bfecaec77f15 --- /dev/null +++ b/diesel_cli/tests/generate_migrations/diff_drop_table_with_fk/sqlite/schema_out.rs/expected_i64_pk.snap @@ -0,0 +1,13 @@ +--- +source: diesel_cli/tests/migration_generate.rs +assertion_line: 337 +description: "Test: diff_drop_table_with_fk" +--- +// @generated automatically by Diesel CLI. + +diesel::table! { + users (id) { + id -> BigInt, + name -> Text, + } +} diff --git a/diesel_cli/tests/generate_migrations/diff_except_tables/schema_i64_pk.rs b/diesel_cli/tests/generate_migrations/diff_except_tables/schema_i64_pk.rs new file mode 100644 index 000000000000..ccafcc8aea62 --- /dev/null +++ b/diesel_cli/tests/generate_migrations/diff_except_tables/schema_i64_pk.rs @@ -0,0 +1,6 @@ +diesel::table! { + table_a (id) { + id -> BigInt, + script -> Text, + } +} diff --git a/diesel_cli/tests/generate_migrations/diff_except_tables/sqlite/schema_out.rs/expected_i64_pk.snap b/diesel_cli/tests/generate_migrations/diff_except_tables/sqlite/schema_out.rs/expected_i64_pk.snap new file mode 100644 index 000000000000..897fa25e66f5 --- /dev/null +++ b/diesel_cli/tests/generate_migrations/diff_except_tables/sqlite/schema_out.rs/expected_i64_pk.snap @@ -0,0 +1,12 @@ +--- +source: diesel_cli/tests/migration_generate.rs +description: "Test: diff_except_tables" +--- +// @generated automatically by Diesel CLI. + +diesel::table! { + table_a (id) { + id -> BigInt, + script -> Text, + } +} diff --git a/diesel_cli/tests/generate_migrations/diff_only_tables/schema_i64_pk.rs b/diesel_cli/tests/generate_migrations/diff_only_tables/schema_i64_pk.rs new file mode 100644 index 000000000000..ccafcc8aea62 --- /dev/null +++ b/diesel_cli/tests/generate_migrations/diff_only_tables/schema_i64_pk.rs @@ -0,0 +1,6 @@ +diesel::table! { + table_a (id) { + id -> BigInt, + script -> Text, + } +} diff --git a/diesel_cli/tests/generate_migrations/diff_only_tables/sqlite/schema_out.rs/expected_i64_pk.snap b/diesel_cli/tests/generate_migrations/diff_only_tables/sqlite/schema_out.rs/expected_i64_pk.snap new file mode 100644 index 000000000000..698f99dd5662 --- /dev/null +++ b/diesel_cli/tests/generate_migrations/diff_only_tables/sqlite/schema_out.rs/expected_i64_pk.snap @@ -0,0 +1,12 @@ +--- +source: diesel_cli/tests/migration_generate.rs +description: "Test: diff_only_tables" +--- +// @generated automatically by Diesel CLI. + +diesel::table! { + table_a (id) { + id -> BigInt, + script -> Text, + } +} diff --git a/diesel_cli/tests/migration_generate.rs b/diesel_cli/tests/migration_generate.rs index 1c6deafa663b..e0cc3427319b 100644 --- a/diesel_cli/tests/migration_generate.rs +++ b/diesel_cli/tests/migration_generate.rs @@ -286,6 +286,29 @@ fn backend_file_path(test_name: &str, file: &str) -> PathBuf { .join(file) } +fn assert_expected_snapshot(snapshot_path: &Path, contents: &str) { + if cfg!(all( + feature = "sqlite", + feature = "integer_primary_key_i64_for_sqlite_3_37" + )) && std::fs::metadata(format!("{}/expected_i64_pk.snap", snapshot_path.display())).is_ok() + { + insta::assert_snapshot!("expected_i64_pk", contents); + } else { + insta::assert_snapshot!("expected", contents); + } +} + +fn schema_file() -> &'static str { + if cfg!(all( + feature = "sqlite", + feature = "integer_primary_key_i64_for_sqlite_3_37" + )) { + "schema_i64_pk.rs" + } else { + "schema.rs" + } +} + fn test_generate_migration(test_name: &str, args: Vec<&str>) { let p = project(test_name).build(); run_generate_migration_test(test_name, args, p); @@ -316,7 +339,13 @@ fn run_generate_migration_test(test_name: &str, args: Vec<&str>, p: Project) { db.execute(schema); } - let mut schema_rs = backend_file_path(test_name, "schema.rs"); + let mut schema_rs = backend_file_path(test_name, schema_file()); + if !schema_rs.exists() { + schema_rs = backend_file_path(test_name, "schema.rs"); + } + if !schema_rs.exists() { + schema_rs = backend_file_path(test_name, &format!("../{}", schema_file())); + } if !schema_rs.exists() { schema_rs = backend_file_path(test_name, "../schema.rs"); } @@ -352,9 +381,10 @@ fn run_generate_migration_test(test_name: &str, args: Vec<&str>, p: Project) { insta::assert_snapshot!("expected", up_sql); }); - setting.set_snapshot_path(backend_file_path(test_name, "down.sql")); + let snapshot_path = backend_file_path(test_name, "down.sql"); + setting.set_snapshot_path(&snapshot_path); setting.bind(|| { - insta::assert_snapshot!("expected", down_sql); + assert_expected_snapshot(&snapshot_path, &down_sql); }); // check that "up.sql" works @@ -371,13 +401,14 @@ fn run_generate_migration_test(test_name: &str, args: Vec<&str>, p: Project) { let result = result.stdout().replace("\r\n", "\n"); let mut setting = insta::Settings::new(); - setting.set_snapshot_path(backend_file_path(test_name, "schema_out.rs")); + let snapshot_path = backend_file_path(test_name, "schema_out.rs"); + setting.set_snapshot_path(&snapshot_path); setting.set_omit_expression(true); setting.set_description(format!("Test: {test_name}")); setting.set_prepend_module_to_snapshot(false); setting.bind(|| { - insta::assert_snapshot!("expected", result); + assert_expected_snapshot(&snapshot_path, &result); }); // revert the migration and compare the schema to the initial one diff --git a/diesel_cli/tests/print_schema.rs b/diesel_cli/tests/print_schema.rs index cdbdd16cafd1..15467eb4582e 100644 --- a/diesel_cli/tests/print_schema.rs +++ b/diesel_cli/tests/print_schema.rs @@ -142,9 +142,7 @@ fn print_schema_datetime_for_mysql() { #[test] #[cfg(not(windows))] fn print_schema_patch_file() { - let path_to_patch_file = backend_file_path("print_schema_patch_file", "schema.patch"); - let path = path_to_patch_file.display().to_string(); - test_print_schema("print_schema_patch_file", vec!["--patch-file", &path]); + test_print_schema("print_schema_patch_file", vec!["--patch-file"]); } #[test] @@ -356,6 +354,29 @@ fn backend_file_path(test_name: &str, file: &str) -> PathBuf { .join(file) } +fn assert_expected_snapshot(snapshot_path: &Path, contents: &str) { + if cfg!(all( + feature = "sqlite", + feature = "integer_primary_key_i64_for_sqlite_3_37" + )) && std::fs::metadata(format!("{}/expected_i64_pk.snap", snapshot_path.display())).is_ok() + { + insta::assert_snapshot!("expected_i64_pk", contents); + } else { + insta::assert_snapshot!("expected", contents); + } +} + +fn schema_patch_file() -> &'static str { + if cfg!(all( + feature = "sqlite", + feature = "integer_primary_key_i64_for_sqlite_3_37" + )) { + "schema_i64_pk.patch" + } else { + "schema.patch" + } +} + fn test_print_schema(test_name: &str, args: Vec<&str>) { let test_path = Path::new(env!("CARGO_MANIFEST_DIR")) .join("tests") @@ -369,30 +390,50 @@ fn test_print_schema(test_name: &str, args: Vec<&str>) { let schema = read_file(&backend_file_path(test_name, "schema.sql")); db.execute(&schema); - let result = p.command("print-schema").args(args).run(); + let mut command = p.command("print-schema").args(args.clone()); + + let schema_patch_file = if args == ["--patch-file"] { + let schema_patch_file = schema_patch_file(); + let path_to_patch_file = backend_file_path("print_schema_patch_file", schema_patch_file); + let path = path_to_patch_file.display().to_string(); + command = command.arg(path); + Some(schema_patch_file) + } else { + None + }; + + let result = command.run(); assert!(result.is_success(), "Result was unsuccessful {:?}", result); let result = result.stdout().replace("\r\n", "\n"); let mut setting = insta::Settings::new(); - setting.set_snapshot_path(backend_file_path(test_name, "")); + let snapshot_path = backend_file_path(test_name, ""); + setting.set_snapshot_path(&snapshot_path); setting.set_omit_expression(true); setting.set_description(format!("Test: {test_name}")); setting.set_prepend_module_to_snapshot(false); setting.bind(|| { - insta::assert_snapshot!("expected", result); - - test_print_schema_config(test_name, &test_path, schema); + assert_expected_snapshot(&snapshot_path, &result); + test_print_schema_config(test_name, &test_path, schema, schema_patch_file); }); } -fn test_print_schema_config(test_name: &str, test_path: &Path, schema: String) { +fn test_print_schema_config( + test_name: &str, + test_path: &Path, + schema: String, + schema_patch_file: Option<&str>, +) { + let snapshot_path = backend_file_path(test_name, ""); + let config = read_file(&test_path.join("diesel.toml")); let mut p = project(&format!("{}_config", test_name)).file("diesel.toml", &config); - let patch_file = backend_file_path(test_name, "schema.patch"); + let schema_patch_file = schema_patch_file.unwrap_or("schema.patch"); + let patch_file = backend_file_path(test_name, schema_patch_file); if patch_file.exists() { let patch_contents = read_file(&patch_file); p = p.file("schema.patch", &patch_contents); @@ -406,14 +447,13 @@ fn test_print_schema_config(test_name: &str, test_path: &Path, schema: String) { assert!(result.is_success(), "Result was unsuccessful {:?}", result); let schema = p.file_contents("src/schema.rs").replace("\r\n", "\n"); - insta::assert_snapshot!("expected", schema); + assert_expected_snapshot(&snapshot_path, &schema); let result = p.command("print-schema").run(); assert!(result.is_success(), "Result was unsuccessful {:?}", result); let result = result.stdout().replace("\r\n", "\n"); - - insta::assert_snapshot!("expected", result); + assert_expected_snapshot(&snapshot_path, &result); } fn read_file(path: &Path) -> String { diff --git a/diesel_cli/tests/print_schema/print_schema_column_order/sqlite/expected_i64_pk.snap b/diesel_cli/tests/print_schema/print_schema_column_order/sqlite/expected_i64_pk.snap new file mode 100644 index 000000000000..6850277700bb --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_column_order/sqlite/expected_i64_pk.snap @@ -0,0 +1,13 @@ +--- +source: diesel_cli/tests/print_schema.rs +description: "Test: print_schema_column_order" +--- +// @generated automatically by Diesel CLI. + +diesel::table! { + abc (a) { + a -> Nullable, + b -> Text, + c -> Nullable, + } +} diff --git a/diesel_cli/tests/print_schema/print_schema_column_renaming/sqlite/expected_i64_pk.snap b/diesel_cli/tests/print_schema/print_schema_column_renaming/sqlite/expected_i64_pk.snap new file mode 100644 index 000000000000..9952c75ec83b --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_column_renaming/sqlite/expected_i64_pk.snap @@ -0,0 +1,34 @@ +--- +source: diesel_cli/tests/print_schema.rs +description: "Test: print_schema_column_renaming" +--- +// @generated automatically by Diesel CLI. + +diesel::table! { + /// Representation of the `with_keywords` table. + /// + /// (Automatically generated by Diesel.) + with_keywords (fn_) { + /// The `fn` column of the `with_keywords` table. + /// + /// Its SQL type is `BigInt`. + /// + /// (Automatically generated by Diesel.) + #[sql_name = "fn"] + fn_ -> BigInt, + /// The `let` column of the `with_keywords` table. + /// + /// Its SQL type is `Integer`. + /// + /// (Automatically generated by Diesel.) + #[sql_name = "let"] + let_ -> Integer, + /// The `extern` column of the `with_keywords` table. + /// + /// Its SQL type is `Integer`. + /// + /// (Automatically generated by Diesel.) + #[sql_name = "extern"] + extern_ -> Integer, + } +} diff --git a/diesel_cli/tests/print_schema/print_schema_custom_types/sqlite/expected_i64_pk.snap b/diesel_cli/tests/print_schema/print_schema_custom_types/sqlite/expected_i64_pk.snap new file mode 100644 index 000000000000..292dda2e95f8 --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_custom_types/sqlite/expected_i64_pk.snap @@ -0,0 +1,28 @@ +--- +source: diesel_cli/tests/print_schema.rs +description: "Test: print_schema_custom_types" +--- +// @generated automatically by Diesel CLI. + +diesel::table! { + use foo::*; + use bar::*; + + users1 (id) { + id -> Nullable, + } +} + +diesel::table! { + use foo::*; + use bar::*; + + users2 (id) { + id -> Nullable, + } +} + +diesel::allow_tables_to_appear_in_same_query!( + users1, + users2, +); diff --git a/diesel_cli/tests/print_schema/print_schema_django_bool/sqlite/expected_i64_pk.snap b/diesel_cli/tests/print_schema/print_schema_django_bool/sqlite/expected_i64_pk.snap new file mode 100644 index 000000000000..73f1869bd8d2 --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_django_bool/sqlite/expected_i64_pk.snap @@ -0,0 +1,26 @@ +--- +source: diesel_cli/tests/print_schema.rs +assertion_line: 359 +description: "Test: print_schema_django_bool" +--- +// @generated automatically by Diesel CLI. + +diesel::table! { + /// Representation of the `users1` table. + /// + /// (Automatically generated by Diesel.) + users1 (id) { + /// The `id` column of the `users1` table. + /// + /// Its SQL type is `Nullable`. + /// + /// (Automatically generated by Diesel.) + id -> Nullable, + /// The `bool_field` column of the `users1` table. + /// + /// Its SQL type is `Bool`. + /// + /// (Automatically generated by Diesel.) + bool_field -> Bool, + } +} diff --git a/diesel_cli/tests/print_schema/print_schema_except_table_regexes/sqlite/expected_i64_pk.snap b/diesel_cli/tests/print_schema/print_schema_except_table_regexes/sqlite/expected_i64_pk.snap new file mode 100644 index 000000000000..53227150c68f --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_except_table_regexes/sqlite/expected_i64_pk.snap @@ -0,0 +1,19 @@ +--- +source: diesel_cli/tests/print_schema.rs +description: "Test: print_schema_except_table_regexes" +--- +// @generated automatically by Diesel CLI. + +diesel::table! { + /// Representation of the `users2` table. + /// + /// (Automatically generated by Diesel.) + users2 (id) { + /// The `id` column of the `users2` table. + /// + /// Its SQL type is `Nullable`. + /// + /// (Automatically generated by Diesel.) + id -> Nullable, + } +} diff --git a/diesel_cli/tests/print_schema/print_schema_except_tables/sqlite/expected_i64_pk.snap b/diesel_cli/tests/print_schema/print_schema_except_tables/sqlite/expected_i64_pk.snap new file mode 100644 index 000000000000..2b2c42bb84f4 --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_except_tables/sqlite/expected_i64_pk.snap @@ -0,0 +1,19 @@ +--- +source: diesel_cli/tests/print_schema.rs +description: "Test: print_schema_except_tables" +--- +// @generated automatically by Diesel CLI. + +diesel::table! { + /// Representation of the `users2` table. + /// + /// (Automatically generated by Diesel.) + users2 (id) { + /// The `id` column of the `users2` table. + /// + /// Its SQL type is `Nullable`. + /// + /// (Automatically generated by Diesel.) + id -> Nullable, + } +} diff --git a/diesel_cli/tests/print_schema/print_schema_generated_columns/sqlite/expected_i64_pk.snap b/diesel_cli/tests/print_schema/print_schema_generated_columns/sqlite/expected_i64_pk.snap new file mode 100644 index 000000000000..cbf237d1dc96 --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_generated_columns/sqlite/expected_i64_pk.snap @@ -0,0 +1,12 @@ +--- +source: diesel_cli/tests/print_schema.rs +description: "Test: print_schema_generated_columns" +--- +// @generated automatically by Diesel CLI. + +diesel::table! { + generated (id) { + id -> Nullable, + generated -> Nullable, + } +} diff --git a/diesel_cli/tests/print_schema/print_schema_generated_columns_generated_always/sqlite/expected_i64_pk.snap b/diesel_cli/tests/print_schema/print_schema_generated_columns_generated_always/sqlite/expected_i64_pk.snap new file mode 100644 index 000000000000..b038168312e4 --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_generated_columns_generated_always/sqlite/expected_i64_pk.snap @@ -0,0 +1,12 @@ +--- +source: diesel_cli/tests/print_schema.rs +description: "Test: print_schema_generated_columns_generated_always" +--- +// @generated automatically by Diesel CLI. + +diesel::table! { + generated (id) { + id -> Nullable, + generated -> Nullable, + } +} diff --git a/diesel_cli/tests/print_schema/print_schema_only_table_regexes/sqlite/expected_i64_pk.snap b/diesel_cli/tests/print_schema/print_schema_only_table_regexes/sqlite/expected_i64_pk.snap new file mode 100644 index 000000000000..5f044359982a --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_only_table_regexes/sqlite/expected_i64_pk.snap @@ -0,0 +1,19 @@ +--- +source: diesel_cli/tests/print_schema.rs +description: "Test: print_schema_only_table_regexes" +--- +// @generated automatically by Diesel CLI. + +diesel::table! { + /// Representation of the `users1` table. + /// + /// (Automatically generated by Diesel.) + users1 (id) { + /// The `id` column of the `users1` table. + /// + /// Its SQL type is `Nullable`. + /// + /// (Automatically generated by Diesel.) + id -> Nullable, + } +} diff --git a/diesel_cli/tests/print_schema/print_schema_only_tables/sqlite/expected_i64_pk.snap b/diesel_cli/tests/print_schema/print_schema_only_tables/sqlite/expected_i64_pk.snap new file mode 100644 index 000000000000..3001e3aa8df6 --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_only_tables/sqlite/expected_i64_pk.snap @@ -0,0 +1,19 @@ +--- +source: diesel_cli/tests/print_schema.rs +description: "Test: print_schema_only_tables" +--- +// @generated automatically by Diesel CLI. + +diesel::table! { + /// Representation of the `users1` table. + /// + /// (Automatically generated by Diesel.) + users1 (id) { + /// The `id` column of the `users1` table. + /// + /// Its SQL type is `Nullable`. + /// + /// (Automatically generated by Diesel.) + id -> Nullable, + } +} diff --git a/diesel_cli/tests/print_schema/print_schema_patch_file/sqlite/expected_i64_pk.snap b/diesel_cli/tests/print_schema/print_schema_patch_file/sqlite/expected_i64_pk.snap new file mode 100644 index 000000000000..82e67b582e58 --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_patch_file/sqlite/expected_i64_pk.snap @@ -0,0 +1,23 @@ +--- +source: diesel_cli/tests/print_schema.rs +description: "Test: print_schema_patch_file" +--- +// @generated automatically by Diesel CLI. + +diesel::table! { + users1 (id) { + id -> BigInt, + } +} + +diesel::table! { + users2 (myid) { + #[sql_name = "id"] + myid -> BigInt, + } +} + +diesel::allow_tables_to_appear_in_same_query!( + users1, + users2, +); diff --git a/diesel_cli/tests/print_schema/print_schema_patch_file/sqlite/schema_i64_pk.patch b/diesel_cli/tests/print_schema/print_schema_patch_file/sqlite/schema_i64_pk.patch new file mode 100644 index 000000000000..f82005717b95 --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_patch_file/sqlite/schema_i64_pk.patch @@ -0,0 +1,17 @@ +@@ -3,12 +3,13 @@ + diesel::table! { + users1 (id) { +- id -> Nullable, ++ id -> BigInt, + } + } + + diesel::table! { +- users2 (id) { +- id -> Nullable, ++ users2 (myid) { ++ #[sql_name = "id"] ++ myid -> BigInt, + } + } + diff --git a/diesel_cli/tests/print_schema/print_schema_quoted_table_name/sqlite/expected_i64_pk.snap b/diesel_cli/tests/print_schema/print_schema_quoted_table_name/sqlite/expected_i64_pk.snap new file mode 100644 index 000000000000..7a679a717c25 --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_quoted_table_name/sqlite/expected_i64_pk.snap @@ -0,0 +1,11 @@ +--- +source: diesel_cli/tests/print_schema.rs +description: "Test: print_schema_quoted_table_name" +--- +// @generated automatically by Diesel CLI. + +diesel::table! { + User (id) { + id -> BigInt, + } +} diff --git a/diesel_cli/tests/print_schema/print_schema_regression_test_for_2623/sqlite/expected_i64_pk.snap b/diesel_cli/tests/print_schema/print_schema_regression_test_for_2623/sqlite/expected_i64_pk.snap new file mode 100644 index 000000000000..861a030bb2df --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_regression_test_for_2623/sqlite/expected_i64_pk.snap @@ -0,0 +1,25 @@ +--- +source: diesel_cli/tests/print_schema.rs +description: "Test: print_schema_regression_test_for_2623" +--- +// @generated automatically by Diesel CLI. + +diesel::table! { + tab_key1 (id) { + id -> BigInt, + } +} + +diesel::table! { + tab_problem (id) { + id -> BigInt, + key1 -> BigInt, + } +} + +diesel::joinable!(tab_problem -> tab_key1 (key1)); + +diesel::allow_tables_to_appear_in_same_query!( + tab_key1, + tab_problem, +); diff --git a/diesel_cli/tests/print_schema/print_schema_reserved_name_mitigation_issue_3404/sqlite/expected_i64_pk.snap b/diesel_cli/tests/print_schema/print_schema_reserved_name_mitigation_issue_3404/sqlite/expected_i64_pk.snap new file mode 100644 index 000000000000..beb91a60b6ec --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_reserved_name_mitigation_issue_3404/sqlite/expected_i64_pk.snap @@ -0,0 +1,13 @@ +--- +source: diesel_cli/tests/print_schema.rs +description: "Test: print_schema_reserved_name_mitigation_issue_3404" +--- +// @generated automatically by Diesel CLI. + +diesel::table! { + reserved_names_columns (id) { + id -> Nullable, + #[sql_name = "is_nullable"] + is_nullable_ -> Nullable, + } +} diff --git a/diesel_cli/tests/print_schema/print_schema_simple/sqlite/expected_i64_pk.snap b/diesel_cli/tests/print_schema/print_schema_simple/sqlite/expected_i64_pk.snap new file mode 100644 index 000000000000..b39f8a4d418c --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_simple/sqlite/expected_i64_pk.snap @@ -0,0 +1,38 @@ +--- +source: diesel_cli/tests/print_schema.rs +description: "Test: print_schema_simple" +--- +// @generated automatically by Diesel CLI. + +diesel::table! { + /// Representation of the `users1` table. + /// + /// (Automatically generated by Diesel.) + users1 (id) { + /// The `id` column of the `users1` table. + /// + /// Its SQL type is `Nullable`. + /// + /// (Automatically generated by Diesel.) + id -> Nullable, + } +} + +diesel::table! { + /// Representation of the `users2` table. + /// + /// (Automatically generated by Diesel.) + users2 (id) { + /// The `id` column of the `users2` table. + /// + /// Its SQL type is `Nullable`. + /// + /// (Automatically generated by Diesel.) + id -> Nullable, + } +} + +diesel::allow_tables_to_appear_in_same_query!( + users1, + users2, +); diff --git a/diesel_cli/tests/print_schema/print_schema_simple_without_docs/sqlite/expected_i64_pk.snap b/diesel_cli/tests/print_schema/print_schema_simple_without_docs/sqlite/expected_i64_pk.snap new file mode 100644 index 000000000000..890835d21b83 --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_simple_without_docs/sqlite/expected_i64_pk.snap @@ -0,0 +1,22 @@ +--- +source: diesel_cli/tests/print_schema.rs +description: "Test: print_schema_simple_without_docs" +--- +// @generated automatically by Diesel CLI. + +diesel::table! { + users1 (id) { + id -> Nullable, + } +} + +diesel::table! { + users2 (id) { + id -> Nullable, + } +} + +diesel::allow_tables_to_appear_in_same_query!( + users1, + users2, +); diff --git a/diesel_cli/tests/print_schema/print_schema_sqlite_implicit_foreign_key_reference/sqlite/expected_i64_pk.snap b/diesel_cli/tests/print_schema/print_schema_sqlite_implicit_foreign_key_reference/sqlite/expected_i64_pk.snap new file mode 100644 index 000000000000..07c5e90485b8 --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_sqlite_implicit_foreign_key_reference/sqlite/expected_i64_pk.snap @@ -0,0 +1,28 @@ +--- +source: diesel_cli/tests/print_schema.rs +description: "Test: print_schema_sqlite_implicit_foreign_key_reference" +--- +// @generated automatically by Diesel CLI. + +diesel::table! { + accounts (id) { + id -> BigInt, + account -> Text, + data_center_id -> Integer, + auth_key -> Binary, + } +} + +diesel::table! { + data_centers (id) { + id -> BigInt, + name -> Text, + } +} + +diesel::joinable!(accounts -> data_centers (data_center_id)); + +diesel::allow_tables_to_appear_in_same_query!( + accounts, + data_centers, +); diff --git a/diesel_cli/tests/print_schema/print_schema_table_order/sqlite/expected_i64_pk.snap b/diesel_cli/tests/print_schema/print_schema_table_order/sqlite/expected_i64_pk.snap new file mode 100644 index 000000000000..05d7f4a86c3c --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_table_order/sqlite/expected_i64_pk.snap @@ -0,0 +1,53 @@ +--- +source: diesel_cli/tests/print_schema.rs +description: "Test: print_schema_table_order" +--- +// @generated automatically by Diesel CLI. + +diesel::table! { + /// Representation of the `abc` table. + /// + /// (Automatically generated by Diesel.) + abc (id) { + /// The `id` column of the `abc` table. + /// + /// Its SQL type is `Nullable`. + /// + /// (Automatically generated by Diesel.) + id -> Nullable, + } +} + +diesel::table! { + /// Representation of the `def` table. + /// + /// (Automatically generated by Diesel.) + def (id) { + /// The `id` column of the `def` table. + /// + /// Its SQL type is `Nullable`. + /// + /// (Automatically generated by Diesel.) + id -> Nullable, + } +} + +diesel::table! { + /// Representation of the `ghi` table. + /// + /// (Automatically generated by Diesel.) + ghi (id) { + /// The `id` column of the `ghi` table. + /// + /// Its SQL type is `Nullable`. + /// + /// (Automatically generated by Diesel.) + id -> Nullable, + } +} + +diesel::allow_tables_to_appear_in_same_query!( + abc, + def, + ghi, +); diff --git a/diesel_cli/tests/print_schema/print_schema_with_unmappable_names/sqlite/expected_i64_pk.snap b/diesel_cli/tests/print_schema/print_schema_with_unmappable_names/sqlite/expected_i64_pk.snap new file mode 100644 index 000000000000..a7bb6211380a --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_with_unmappable_names/sqlite/expected_i64_pk.snap @@ -0,0 +1,68 @@ +--- +source: diesel_cli/tests/print_schema.rs +description: "Test: print_schema_with_unmappable_names" +--- +// @generated automatically by Diesel CLI. + +diesel::table! { + /// Representation of the `self` table. + /// + /// (Automatically generated by Diesel.) + #[sql_name = "self"] + self_ (id) { + /// The `id` column of the `self` table. + /// + /// Its SQL type is `BigInt`. + /// + /// (Automatically generated by Diesel.) + id -> BigInt, + } +} + +diesel::table! { + /// Representation of the `user-has::complex>>>role` table. + /// + /// (Automatically generated by Diesel.) + #[sql_name = "user-has::complex>>>role"] + user_has_complex_role (id) { + /// The `user` column of the `user-has::complex>>>role` table. + /// + /// Its SQL type is `Integer`. + /// + /// (Automatically generated by Diesel.) + user -> Integer, + /// The `role` column of the `user-has::complex>>>role` table. + /// + /// Its SQL type is `Integer`. + /// + /// (Automatically generated by Diesel.) + role -> Integer, + /// The `id` column of the `user-has::complex>>>role` table. + /// + /// Its SQL type is `BigInt`. + /// + /// (Automatically generated by Diesel.) + id -> BigInt, + /// The `created at` column of the `user-has::complex>>>role` table. + /// + /// Its SQL type is `Timestamp`. + /// + /// (Automatically generated by Diesel.) + #[sql_name = "created at"] + created_at -> Timestamp, + /// The `expiry date` column of the `user-has::complex>>>role` table. + /// + /// Its SQL type is `Nullable`. + /// + /// (Automatically generated by Diesel.) + #[sql_name = "expiry date"] + expiry_date -> Nullable, + } +} + +diesel::joinable!(user_has_complex_role -> self_ (user)); + +diesel::allow_tables_to_appear_in_same_query!( + self_, + user_has_complex_role, +);