Skip to content

Commit

Permalink
Test and fix Id as part of relation in migration engine
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhoule committed Mar 5, 2020
1 parent 234aaab commit dd9d247
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
Expand Up @@ -124,7 +124,17 @@ impl<'a> SqlSchemaCalculator<'a> {
.collect();

let primary_key = sql::PrimaryKey {
columns: model.id_fields().map(|field| field.db_name().to_owned()).collect(),
columns: model
.id_fields()
.flat_map(|field| {
field
.data_source_fields()
.into_iter()
.map(|s| s.name.clone())
.collect::<Vec<String>>()
.into_iter()
})
.collect(),
sequence: None,
};

Expand Down
Expand Up @@ -3,7 +3,7 @@ use datamodel::{
Datamodel, DefaultValue, Enum, Field, FieldArity, FieldType, IndexDefinition, Model, ScalarType,
WithDatabaseName,
},
EnumValue,
DataSourceField, EnumValue,
};

pub(crate) fn walk_models<'a>(datamodel: &'a Datamodel) -> impl Iterator<Item = ModelRef<'a>> + 'a {
Expand Down Expand Up @@ -115,6 +115,10 @@ impl<'a> FieldRef<'a> {
self.field.single_database_name().unwrap_or(self.name())
}

pub(super) fn data_source_fields(&self) -> &[DataSourceField] {
&self.field.data_source_fields
}

pub(super) fn default_value(&self) -> Option<&'a DefaultValue> {
self.field.default_value.as_ref()
}
Expand Down
79 changes: 79 additions & 0 deletions migration-engine/migration-engine-tests/tests/migrations/sql.rs
Expand Up @@ -199,3 +199,82 @@ async fn enum_defaults_must_work(api: &TestApi) -> TestResult {

Ok(())
}

#[test_each_connector(tags("sql"))]
async fn id_as_part_of_relation_must_work(api: &TestApi) -> TestResult {
let dm = r##"
model Cat {
nemesis Dog @id
}
model Dog {
id String @id
}
"##;

api.infer_apply(dm).send_assert().await?.assert_green()?;

api.assert_schema().await?.assert_table("Cat", |table| {
table
.assert_pk(|pk| pk.assert_columns(&["nemesis"]))?
.assert_fk_on_columns(&["nemesis"], |fk| fk.assert_references("Dog", &["id"]))
})?;

Ok(())
}

#[test_each_connector(tags("sql"), log = "debug")]
async fn multi_field_id_as_part_of_relation_must_work(api: &TestApi) -> TestResult {
let dm = r##"
model Cat {
nemesis Dog @id
}
model Dog {
name String
weight Int
@@id([name, weight])
}
"##;

api.infer_apply(dm).send_assert().await?.assert_green()?;

api.assert_schema().await?.assert_table("Cat", |table| {
table
.assert_pk(|pk| pk.assert_columns(&["nemesis_name", "nemesis_weight"]))?
.assert_fk_on_columns(&["nemesis_name", "nemesis_weight"], |fk| {
fk.assert_references("Dog", &["name", "weight"])
})
})?;

Ok(())
}

#[test_each_connector(tags("sql"), log = "debug")]
async fn remapped_multi_field_id_as_part_of_relation_must_work(api: &TestApi) -> TestResult {
let dm = r##"
model Cat {
nemesis Dog @map(["dogname", "dogweight"]) @id
}
model Dog {
name String
weight Int
@@id([name, weight])
}
"##;

api.infer_apply(dm).send_assert().await?.assert_green()?;

api.assert_schema().await?.assert_table("Cat", |table| {
table
.assert_pk(|pk| pk.assert_columns(&["dogname", "dogweight"]))?
.assert_fk_on_columns(&["dogname", "dogweight"], |fk| {
fk.assert_references("Dog", &["name", "weight"])
})
})?;

Ok(())
}

0 comments on commit dd9d247

Please sign in to comment.