Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #4347 from prisma/MigValueForFieldsWithIDType
Browse files Browse the repository at this point in the history
Migrationvalue for fields with ID type
  • Loading branch information
mavilein authored Apr 11, 2019
2 parents 3db4f2f + 80927ca commit 11e3db7
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ case class PostgresJdbcDeployDatabaseMutationBuilder(
case (DateTimeGCValue(dateTime), params) => params.setTimestamp(jodaDateTimeToSqlTimestampUTC(dateTime))
case (EnumGCValue(enum), params) => params.setString(enum)
case (JsonGCValue(json), params) => params.setString(json.toString())
case (UuidGCValue(uuid), params) => sys.error("")
case (UuidGCValue(uuid), params) => params.setObject(uuid, -2)
case _ => sys.error("")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ trait MigrationValueGenerator {
case TypeIdentifier.DateTime => DateTimeGCValue(new DateTime("1970-01-01T00:00:00Z"))
case TypeIdentifier.Json => JsonGCValue(Json.parse("{}"))
case TypeIdentifier.Enum => EnumGCValue(field.enum.get.values.head)
case _ => sys.error("MigrationValue method should only be called on scalar fields.")
case TypeIdentifier.Cuid => StringIdGCValue("DefaultCUIDMigrationValue")
case TypeIdentifier.UUID => UuidGCValue.parse_!("550e8400-e29b-11d4-a716-446655440000")
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,12 @@
package com.prisma.integration

import com.prisma.ConnectorTag
import com.prisma.{ConnectorTag, IgnoreMongo, IgnoreMySql}
import com.prisma.ConnectorTag.{MySqlConnectorTag, PostgresConnectorTag}
import org.scalatest.{FlatSpec, Matchers}

class PrefillingFieldsWithDefaultOrMigrationValueSpec extends FlatSpec with Matchers with IntegrationBaseSpec {
override def runOnlyForConnectors: Set[ConnectorTag] = Set(MySqlConnectorTag, PostgresConnectorTag)

//Affected Deploy Actions
// creating new required field -> set column to default/migValue for all rows -> createColumn
// making field required -> set column to default/migValue for all rows where it is null -> updateColumn
// changing type of a (newly) required field -> set column to default/migValue for all rows -> createColumn

//Necessary changes
// Keep validations, create message that MV was used -> Done
// Downgrade errors to warnings -> Done
// Create shared MigvalueMatcher -> Done
// Add tests -> Done
// Change queries to insert default/migration value
// Fix broken tests
// find locations for MigrationValueGenerator and SetParameter

//Postgres -> Done
//MySql -> Done
//Sqlite -> No Changes
//Mongo -> Keep Errors

"Creating a required Field" should "not error when there is no defaultValue but there are no nodes yet" in {

val schema =
Expand All @@ -44,6 +25,67 @@ class PrefillingFieldsWithDefaultOrMigrationValueSpec extends FlatSpec with Matc
deployServer.deploySchemaThatMustSucceed(project, schema2, 3)
}

"Creating a required Field of type ID" should "not error when there is no defaultValue but there are no nodes yet" in {

val schema =
"""type A {
| name: String! @unique
|}""".stripMargin

val (project, _) = setupProject(schema)

val schema2 =
"""type A {
| name: String! @unique
| test: ID!
|}""".stripMargin

deployServer.deploySchemaThatMustSucceed(project, schema2, 3)
}

"Creating a required Field of type UUID" should "not error when there is no defaultValue but there are no nodes yet" taggedAs (IgnoreMongo, IgnoreMySql) in {

val schema =
"""type A {
| name: String! @unique
|}""".stripMargin

val (project, _) = setupProject(schema)

val schema2 =
"""type A {
| name: String! @unique
| test: UUID!
|}""".stripMargin

deployServer.deploySchemaThatMustSucceed(project, schema2, 3)
}

"Creating a required Field of type UUID" should "not error when there is no defaultValue" taggedAs (IgnoreMongo, IgnoreMySql) in {

val schema =
"""type A {
| name: String! @unique
|}""".stripMargin

val (project, _) = setupProject(schema)

apiServer.query("""mutation{createA(data:{name: "test"}){name}}""", project)

val schema1 =
"""type A {
| name: String! @unique
| test: UUID!
|}""".stripMargin

val res = deployServer.deploySchemaThatMustWarn(project, schema1, true)
res.toString should include("""The fields will be pre-filled with the value `550e8400-e29b-11d4-a716-446655440000`.""")

val updatedProject = deployServer.deploySchema(project, schema1)
apiServer.query("query{as{name, test}}", updatedProject).toString should be(
"""{"data":{"as":[{"name":"test","test":"550e8400-e29b-11d4-a716-446655440000"}]}}""")
}

"Adding a required field without default value" should "set the internal migration value" in {

val schema =
Expand Down

0 comments on commit 11e3db7

Please sign in to comment.