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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make implicit relation rules explicit with prisma-fmt #433

Merged
merged 17 commits into from Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -137,7 +137,7 @@ pub(crate) fn calculate_scalar_field(
debug!("Handling column {:?}", column);
let field_type = calculate_field_type(&schema, &column, &table);
let arity = match column.tpe.arity {
_ if column.auto_increment && field_type == FieldType::Base(ScalarType::Int) => {
_ if column.auto_increment && field_type == FieldType::Base(ScalarType::Int, None) => {
FieldArity::Required
}
ColumnArity::Required => FieldArity::Required,
Expand Down Expand Up @@ -436,15 +436,15 @@ pub(crate) fn calculate_field_type(
_ => {
debug!("Found no corresponding foreign key");
match &column.tpe.family {
ColumnTypeFamily::Boolean => FieldType::Base(ScalarType::Boolean),
ColumnTypeFamily::DateTime => FieldType::Base(ScalarType::DateTime),
ColumnTypeFamily::Float => FieldType::Base(ScalarType::Float),
ColumnTypeFamily::Int => FieldType::Base(ScalarType::Int),
ColumnTypeFamily::String => FieldType::Base(ScalarType::String),
ColumnTypeFamily::Boolean => FieldType::Base(ScalarType::Boolean, None),
ColumnTypeFamily::DateTime => FieldType::Base(ScalarType::DateTime, None),
ColumnTypeFamily::Float => FieldType::Base(ScalarType::Float, None),
ColumnTypeFamily::Int => FieldType::Base(ScalarType::Int, None),
ColumnTypeFamily::String => FieldType::Base(ScalarType::String, None),
ColumnTypeFamily::Enum(name) => FieldType::Enum(name.clone()),
// XXX: We made a conscious decision to punt on mapping of ColumnTypeFamily
// variants that don't yet have corresponding PrismaType variants
_ => FieldType::Base(ScalarType::String),
_ => FieldType::Base(ScalarType::String, None),
}
}
}
Expand Down
@@ -1,7 +1,7 @@
use datamodel::{
common::{ScalarType, ScalarValue},
dml, Datamodel, DefaultValue as DMLDefault, Field, FieldArity, FieldType, IndexDefinition, Model, OnDeleteStrategy,
RelationInfo, ValueGenerator,
dml, Datamodel, DefaultValue as DMLDefault, Field, FieldArity, FieldType, IndexDefinition,
Model, OnDeleteStrategy, RelationInfo, ValueGenerator,
};
use pretty_assertions::assert_eq;
use sql_introspection_connector::calculate_datamodel::calculate_model;
Expand Down Expand Up @@ -41,14 +41,14 @@ fn a_data_model_can_be_generated_from_a_schema() {
.iter()
.map(|col_type| {
let field_type = match col_type {
ColumnTypeFamily::Boolean => FieldType::Base(ScalarType::Boolean),
ColumnTypeFamily::DateTime => FieldType::Base(ScalarType::DateTime),
ColumnTypeFamily::Float => FieldType::Base(ScalarType::Float),
ColumnTypeFamily::Int => FieldType::Base(ScalarType::Int),
ColumnTypeFamily::String => FieldType::Base(ScalarType::String),
ColumnTypeFamily::Boolean => FieldType::Base(ScalarType::Boolean, None),
ColumnTypeFamily::DateTime => FieldType::Base(ScalarType::DateTime, None),
ColumnTypeFamily::Float => FieldType::Base(ScalarType::Float, None),
ColumnTypeFamily::Int => FieldType::Base(ScalarType::Int, None),
ColumnTypeFamily::String => FieldType::Base(ScalarType::String, None),
// XXX: We made a conscious decision to punt on mapping of ColumnTypeFamily
// variants that don't yet have corresponding PrismaType variants
_ => FieldType::Base(ScalarType::String),
_ => FieldType::Base(ScalarType::String, None),
};
Field {
name: col_type.to_string(),
Expand Down Expand Up @@ -114,7 +114,7 @@ fn arity_is_preserved_when_generating_data_model_from_a_schema() {
Field {
name: "optional".to_string(),
arity: FieldArity::Optional,
field_type: FieldType::Base(ScalarType::Int),
field_type: FieldType::Base(ScalarType::Int, None),
database_names: Vec::new(),
default_value: None,
is_unique: false,
Expand All @@ -128,9 +128,11 @@ fn arity_is_preserved_when_generating_data_model_from_a_schema() {
Field {
name: "required".to_string(),
arity: FieldArity::Required,
field_type: FieldType::Base(ScalarType::Int),
field_type: FieldType::Base(ScalarType::Int, None),
database_names: Vec::new(),
default_value: Some(DMLDefault::Expression(ValueGenerator::new_autoincrement())),
default_value: Some(
DMLDefault::Expression(ValueGenerator::new_autoincrement()),
),
is_unique: false,
is_id: true,
documentation: None,
Expand All @@ -142,7 +144,7 @@ fn arity_is_preserved_when_generating_data_model_from_a_schema() {
Field {
name: "list".to_string(),
arity: FieldArity::List,
field_type: FieldType::Base(ScalarType::Int),
field_type: FieldType::Base(ScalarType::Int, None),
database_names: Vec::new(),
default_value: None,
is_unique: false,
Expand Down Expand Up @@ -224,7 +226,7 @@ fn defaults_are_preserved_when_generating_data_model_from_a_schema() {
Field {
name: "no_default".to_string(),
arity: FieldArity::Optional,
field_type: FieldType::Base(ScalarType::Int),
field_type: FieldType::Base(ScalarType::Int, None),
database_names: Vec::new(),
default_value: None,
is_unique: false,
Expand All @@ -238,7 +240,7 @@ fn defaults_are_preserved_when_generating_data_model_from_a_schema() {
Field {
name: "int_default".to_string(),
arity: FieldArity::Optional,
field_type: FieldType::Base(ScalarType::Int),
field_type: FieldType::Base(ScalarType::Int, None),
database_names: Vec::new(),
default_value: Some(dml::DefaultValue::Single(ScalarValue::Int(1))),
is_unique: false,
Expand All @@ -252,7 +254,7 @@ fn defaults_are_preserved_when_generating_data_model_from_a_schema() {
Field {
name: "bool_default".to_string(),
arity: FieldArity::Optional,
field_type: FieldType::Base(ScalarType::Boolean),
field_type: FieldType::Base(ScalarType::Boolean, None),
database_names: Vec::new(),
default_value: Some(dml::DefaultValue::Single(ScalarValue::Boolean(true))),
is_unique: false,
Expand All @@ -266,7 +268,7 @@ fn defaults_are_preserved_when_generating_data_model_from_a_schema() {
Field {
name: "float_default".to_string(),
arity: FieldArity::Optional,
field_type: FieldType::Base(ScalarType::Float),
field_type: FieldType::Base(ScalarType::Float, None),
database_names: Vec::new(),
default_value: Some(dml::DefaultValue::Single(ScalarValue::Float(1.0))),
is_unique: false,
Expand All @@ -280,9 +282,11 @@ fn defaults_are_preserved_when_generating_data_model_from_a_schema() {
Field {
name: "string_default".to_string(),
arity: FieldArity::Optional,
field_type: FieldType::Base(ScalarType::String),
field_type: FieldType::Base(ScalarType::String, None),
database_names: Vec::new(),
default_value: Some(dml::DefaultValue::Single(ScalarValue::String("default".to_string()))),
default_value: Some(dml::DefaultValue::Single(ScalarValue::String(
"default".to_string(),
))),
is_unique: false,
is_id: false,
documentation: None,
Expand Down Expand Up @@ -388,9 +392,11 @@ fn primary_key_is_preserved_when_generating_data_model_from_a_schema() {
fields: vec![Field {
name: "primary".to_string(),
arity: FieldArity::Required,
field_type: FieldType::Base(ScalarType::Int),
field_type: FieldType::Base(ScalarType::Int, None),
database_names: Vec::new(),
default_value: Some(DMLDefault::Expression(ValueGenerator::new_autoincrement())),
default_value: Some(
DMLDefault::Expression(ValueGenerator::new_autoincrement()),
),
is_unique: false,
is_id: true,
documentation: None,
Expand All @@ -413,7 +419,7 @@ fn primary_key_is_preserved_when_generating_data_model_from_a_schema() {
fields: vec![Field {
name: "primary".to_string(),
arity: FieldArity::Required,
field_type: FieldType::Base(ScalarType::Int),
field_type: FieldType::Base(ScalarType::Int, None),
database_names: Vec::new(),
default_value: None,
is_unique: false,
Expand All @@ -438,9 +444,11 @@ fn primary_key_is_preserved_when_generating_data_model_from_a_schema() {
fields: vec![Field {
name: "primary".to_string(),
arity: FieldArity::Required,
field_type: FieldType::Base(ScalarType::Int),
field_type: FieldType::Base(ScalarType::Int, None),
database_names: Vec::new(),
default_value: Some(DMLDefault::Expression(ValueGenerator::new_autoincrement())),
default_value: Some(
DMLDefault::Expression(ValueGenerator::new_autoincrement()),
),
is_unique: false,
is_id: true,
documentation: None,
Expand Down Expand Up @@ -542,7 +550,7 @@ fn uniqueness_is_preserved_when_generating_data_model_from_a_schema() {
Field {
name: "non_unique".to_string(),
arity: FieldArity::Optional,
field_type: FieldType::Base(ScalarType::Int),
field_type: FieldType::Base(ScalarType::Int, None),
database_names: Vec::new(),
default_value: None,
is_unique: false,
Expand All @@ -556,7 +564,7 @@ fn uniqueness_is_preserved_when_generating_data_model_from_a_schema() {
Field {
name: "unique".to_string(),
arity: FieldArity::Required,
field_type: FieldType::Base(ScalarType::Int),
field_type: FieldType::Base(ScalarType::Int, None),
database_names: Vec::new(),
default_value: None,
is_unique: true,
Expand Down Expand Up @@ -631,9 +639,11 @@ fn compound_foreign_keys_are_preserved_when_generating_data_model_from_a_schema(
Field {
name: "id".to_string(),
arity: FieldArity::Required,
field_type: FieldType::Base(ScalarType::Int),
field_type: FieldType::Base(ScalarType::Int, None),
database_names: Vec::new(),
default_value: Some(DMLDefault::Expression(ValueGenerator::new_autoincrement())),
default_value: Some(DMLDefault::Expression(
ValueGenerator::new_autoincrement(),
)),
is_unique: false,
is_id: true,
documentation: None,
Expand All @@ -645,7 +655,7 @@ fn compound_foreign_keys_are_preserved_when_generating_data_model_from_a_schema(
Field {
name: "name".to_string(),
arity: FieldArity::Required,
field_type: FieldType::Base(ScalarType::String),
field_type: FieldType::Base(ScalarType::String, None),
database_names: Vec::new(),
default_value: None,
is_unique: false,
Expand All @@ -671,7 +681,7 @@ fn compound_foreign_keys_are_preserved_when_generating_data_model_from_a_schema(
Field {
name: "id".to_string(),
arity: FieldArity::Required,
field_type: FieldType::Base(ScalarType::Int),
field_type: FieldType::Base(ScalarType::Int, None),
database_names: Vec::new(),
default_value: None,
is_unique: false,
Expand Down Expand Up @@ -829,9 +839,11 @@ fn multi_field_uniques_are_preserved_when_generating_data_model_from_a_schema()
Field {
name: "id".to_string(),
arity: FieldArity::Required,
field_type: FieldType::Base(ScalarType::Int),
field_type: FieldType::Base(ScalarType::Int, None),
database_names: Vec::new(),
default_value: Some(DMLDefault::Expression(ValueGenerator::new_autoincrement())),
default_value: Some(
DMLDefault::Expression(ValueGenerator::new_autoincrement()),
),
is_unique: false,
is_id: true,
documentation: None,
Expand All @@ -843,7 +855,7 @@ fn multi_field_uniques_are_preserved_when_generating_data_model_from_a_schema()
Field {
name: "name".to_string(),
arity: FieldArity::Required,
field_type: FieldType::Base(ScalarType::String),
field_type: FieldType::Base(ScalarType::String, None),
database_names: Vec::new(),
default_value: None,
is_unique: false,
Expand All @@ -857,7 +869,7 @@ fn multi_field_uniques_are_preserved_when_generating_data_model_from_a_schema()
Field {
name: "lastname".to_string(),
arity: FieldArity::Required,
field_type: FieldType::Base(ScalarType::String),
field_type: FieldType::Base(ScalarType::String, None),
database_names: Vec::new(),
default_value: None,
is_unique: false,
Expand Down Expand Up @@ -948,9 +960,11 @@ fn foreign_keys_are_preserved_when_generating_data_model_from_a_schema() {
Field {
name: "id".to_string(),
arity: FieldArity::Required,
field_type: FieldType::Base(ScalarType::Int),
field_type: FieldType::Base(ScalarType::Int, None),
database_names: Vec::new(),
default_value: Some(DMLDefault::Expression(ValueGenerator::new_autoincrement())),
default_value: Some(DMLDefault::Expression(
ValueGenerator::new_autoincrement(),
)),
is_unique: false,
is_id: true,
documentation: None,
Expand All @@ -962,7 +976,7 @@ fn foreign_keys_are_preserved_when_generating_data_model_from_a_schema() {
Field {
name: "name".to_string(),
arity: FieldArity::Required,
field_type: FieldType::Base(ScalarType::String),
field_type: FieldType::Base(ScalarType::String, None),
database_names: Vec::new(),
default_value: None,
is_unique: false,
Expand Down Expand Up @@ -1007,9 +1021,11 @@ fn foreign_keys_are_preserved_when_generating_data_model_from_a_schema() {
Field {
name: "id".to_string(),
arity: FieldArity::Required,
field_type: FieldType::Base(ScalarType::Int),
field_type: FieldType::Base(ScalarType::Int, None),
database_names: Vec::new(),
default_value: Some(DMLDefault::Expression(ValueGenerator::new_autoincrement())),
default_value: Some(DMLDefault::Expression(
ValueGenerator::new_autoincrement(),
)),
is_unique: false,
is_id: true,
documentation: None,
Expand Down
Expand Up @@ -76,13 +76,13 @@ async fn introspecting_two_one_to_one_relations_between_the_same_models_should_w
let dm = r#"
model Post {
id Int @id @default(autoincrement())
user_id User @relation("Post_user_idToUser")
user_id User @relation("Post_user_idToUser", references: [id])
user User? @relation("PostToUser_post_id")
}

model User {
id Int @id @default(autoincrement())
post_id Post @relation("PostToUser_post_id")
post_id Post @relation("PostToUser_post_id", references: [id])
post Post? @relation("Post_user_idToUser")
}
"#;
Expand Down Expand Up @@ -522,7 +522,7 @@ async fn introspecting_id_fields_with_foreign_key_should_work(api: &TestApi) {
let dm = r#"
model Post {
test String
user_id User @id
user_id User @id @relation(references: [id])
}

model User {
Expand Down
@@ -1,5 +1,6 @@
use crate::*;
use barrel::types;
use pretty_assertions::assert_eq;
use test_harness::*;

#[test_each_connector(tags("mysql"))]
Expand Down Expand Up @@ -281,7 +282,7 @@ async fn introspecting_a_table_without_uniques_should_comment_it_out(api: &TestA
})
.await;

let dm = "/// The underlying table does not contain a unique identifier and can therefore currently not be handled.\n// model Post {\n // id Int\n // user_id User\n\n // @@index([user_id], name: \"user_id\")\n// }\n\nmodel User {\n id Int @default(autoincrement()) @id\n}";
let dm = "// The underlying table does not contain a unique identifier and can therefore currently not be handled.\n// model Post {\n // id Int\n // user_id User @relation(references: [id])\n\n // @@index([user_id], name: \"user_id\")\n// }\n\nmodel User {\n id Int @default(autoincrement()) @id\n}";

let result = dbg!(api.introspect().await);
assert_eq!(&result, dm);
Expand Down