From 365eebab50420390ae6cbdf66ace3ffbaf9a04d2 Mon Sep 17 00:00:00 2001 From: Matthias Oertel Date: Wed, 10 Apr 2019 13:02:52 +0200 Subject: [PATCH 1/2] support uuid and cuid default values in deploy --- ...resJdbcDeployDatabaseMutationBuilder.scala | 2 +- .../connector/MigrationValueGenerator.scala | 3 +- ...ieldsWithDefaultOrMigrationValueSpec.scala | 57 ++++++++++++------- 3 files changed, 40 insertions(+), 22 deletions(-) diff --git a/server/connectors/deploy-connector-postgres/src/main/scala/com/prisma/deploy/connector/postgres/database/PostgresJdbcDeployDatabaseMutationBuilder.scala b/server/connectors/deploy-connector-postgres/src/main/scala/com/prisma/deploy/connector/postgres/database/PostgresJdbcDeployDatabaseMutationBuilder.scala index d4785a1c42..0f9f8fea38 100644 --- a/server/connectors/deploy-connector-postgres/src/main/scala/com/prisma/deploy/connector/postgres/database/PostgresJdbcDeployDatabaseMutationBuilder.scala +++ b/server/connectors/deploy-connector-postgres/src/main/scala/com/prisma/deploy/connector/postgres/database/PostgresJdbcDeployDatabaseMutationBuilder.scala @@ -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("") } diff --git a/server/connectors/deploy-connector/src/main/scala/com/prisma/deploy/connector/MigrationValueGenerator.scala b/server/connectors/deploy-connector/src/main/scala/com/prisma/deploy/connector/MigrationValueGenerator.scala index b7c4f36513..7a2fa82c78 100644 --- a/server/connectors/deploy-connector/src/main/scala/com/prisma/deploy/connector/MigrationValueGenerator.scala +++ b/server/connectors/deploy-connector/src/main/scala/com/prisma/deploy/connector/MigrationValueGenerator.scala @@ -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") } } diff --git a/server/integration-tests/integration-tests-mysql/src/test/scala/com/prisma/integration/PrefillingFieldsWithDefaultOrMigrationValueSpec.scala b/server/integration-tests/integration-tests-mysql/src/test/scala/com/prisma/integration/PrefillingFieldsWithDefaultOrMigrationValueSpec.scala index b92133ed2a..7a51150e19 100644 --- a/server/integration-tests/integration-tests-mysql/src/test/scala/com/prisma/integration/PrefillingFieldsWithDefaultOrMigrationValueSpec.scala +++ b/server/integration-tests/integration-tests-mysql/src/test/scala/com/prisma/integration/PrefillingFieldsWithDefaultOrMigrationValueSpec.scala @@ -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 = @@ -44,6 +25,42 @@ 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) + } + "Adding a required field without default value" should "set the internal migration value" in { val schema = From 80927ca8be196feb61de85d3dae558b2348f841e Mon Sep 17 00:00:00 2001 From: Matthias Oertel Date: Wed, 10 Apr 2019 13:21:36 +0200 Subject: [PATCH 2/2] add additional test --- ...ieldsWithDefaultOrMigrationValueSpec.scala | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/server/integration-tests/integration-tests-mysql/src/test/scala/com/prisma/integration/PrefillingFieldsWithDefaultOrMigrationValueSpec.scala b/server/integration-tests/integration-tests-mysql/src/test/scala/com/prisma/integration/PrefillingFieldsWithDefaultOrMigrationValueSpec.scala index 7a51150e19..8cf6f1fc62 100644 --- a/server/integration-tests/integration-tests-mysql/src/test/scala/com/prisma/integration/PrefillingFieldsWithDefaultOrMigrationValueSpec.scala +++ b/server/integration-tests/integration-tests-mysql/src/test/scala/com/prisma/integration/PrefillingFieldsWithDefaultOrMigrationValueSpec.scala @@ -61,6 +61,31 @@ class PrefillingFieldsWithDefaultOrMigrationValueSpec extends FlatSpec with Matc 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 =