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

Commit 11e3db7

Browse files
authored
Merge pull request #4347 from prisma/MigValueForFieldsWithIDType
Migrationvalue for fields with ID type
2 parents 3db4f2f + 80927ca commit 11e3db7

3 files changed

Lines changed: 65 additions & 22 deletions

File tree

server/connectors/deploy-connector-postgres/src/main/scala/com/prisma/deploy/connector/postgres/database/PostgresJdbcDeployDatabaseMutationBuilder.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ case class PostgresJdbcDeployDatabaseMutationBuilder(
153153
case (DateTimeGCValue(dateTime), params) => params.setTimestamp(jodaDateTimeToSqlTimestampUTC(dateTime))
154154
case (EnumGCValue(enum), params) => params.setString(enum)
155155
case (JsonGCValue(json), params) => params.setString(json.toString())
156-
case (UuidGCValue(uuid), params) => sys.error("")
156+
case (UuidGCValue(uuid), params) => params.setObject(uuid, -2)
157157
case _ => sys.error("")
158158
}
159159

server/connectors/deploy-connector/src/main/scala/com/prisma/deploy/connector/MigrationValueGenerator.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ trait MigrationValueGenerator {
1515
case TypeIdentifier.DateTime => DateTimeGCValue(new DateTime("1970-01-01T00:00:00Z"))
1616
case TypeIdentifier.Json => JsonGCValue(Json.parse("{}"))
1717
case TypeIdentifier.Enum => EnumGCValue(field.enum.get.values.head)
18-
case _ => sys.error("MigrationValue method should only be called on scalar fields.")
18+
case TypeIdentifier.Cuid => StringIdGCValue("DefaultCUIDMigrationValue")
19+
case TypeIdentifier.UUID => UuidGCValue.parse_!("550e8400-e29b-11d4-a716-446655440000")
1920
}
2021
}

server/integration-tests/integration-tests-mysql/src/test/scala/com/prisma/integration/PrefillingFieldsWithDefaultOrMigrationValueSpec.scala

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,12 @@
11
package com.prisma.integration
22

3-
import com.prisma.ConnectorTag
3+
import com.prisma.{ConnectorTag, IgnoreMongo, IgnoreMySql}
44
import com.prisma.ConnectorTag.{MySqlConnectorTag, PostgresConnectorTag}
55
import org.scalatest.{FlatSpec, Matchers}
66

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

10-
//Affected Deploy Actions
11-
// creating new required field -> set column to default/migValue for all rows -> createColumn
12-
// making field required -> set column to default/migValue for all rows where it is null -> updateColumn
13-
// changing type of a (newly) required field -> set column to default/migValue for all rows -> createColumn
14-
15-
//Necessary changes
16-
// Keep validations, create message that MV was used -> Done
17-
// Downgrade errors to warnings -> Done
18-
// Create shared MigvalueMatcher -> Done
19-
// Add tests -> Done
20-
// Change queries to insert default/migration value
21-
// Fix broken tests
22-
// find locations for MigrationValueGenerator and SetParameter
23-
24-
//Postgres -> Done
25-
//MySql -> Done
26-
//Sqlite -> No Changes
27-
//Mongo -> Keep Errors
28-
2910
"Creating a required Field" should "not error when there is no defaultValue but there are no nodes yet" in {
3011

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

28+
"Creating a required Field of type ID" should "not error when there is no defaultValue but there are no nodes yet" in {
29+
30+
val schema =
31+
"""type A {
32+
| name: String! @unique
33+
|}""".stripMargin
34+
35+
val (project, _) = setupProject(schema)
36+
37+
val schema2 =
38+
"""type A {
39+
| name: String! @unique
40+
| test: ID!
41+
|}""".stripMargin
42+
43+
deployServer.deploySchemaThatMustSucceed(project, schema2, 3)
44+
}
45+
46+
"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 {
47+
48+
val schema =
49+
"""type A {
50+
| name: String! @unique
51+
|}""".stripMargin
52+
53+
val (project, _) = setupProject(schema)
54+
55+
val schema2 =
56+
"""type A {
57+
| name: String! @unique
58+
| test: UUID!
59+
|}""".stripMargin
60+
61+
deployServer.deploySchemaThatMustSucceed(project, schema2, 3)
62+
}
63+
64+
"Creating a required Field of type UUID" should "not error when there is no defaultValue" taggedAs (IgnoreMongo, IgnoreMySql) in {
65+
66+
val schema =
67+
"""type A {
68+
| name: String! @unique
69+
|}""".stripMargin
70+
71+
val (project, _) = setupProject(schema)
72+
73+
apiServer.query("""mutation{createA(data:{name: "test"}){name}}""", project)
74+
75+
val schema1 =
76+
"""type A {
77+
| name: String! @unique
78+
| test: UUID!
79+
|}""".stripMargin
80+
81+
val res = deployServer.deploySchemaThatMustWarn(project, schema1, true)
82+
res.toString should include("""The fields will be pre-filled with the value `550e8400-e29b-11d4-a716-446655440000`.""")
83+
84+
val updatedProject = deployServer.deploySchema(project, schema1)
85+
apiServer.query("query{as{name, test}}", updatedProject).toString should be(
86+
"""{"data":{"as":[{"name":"test","test":"550e8400-e29b-11d4-a716-446655440000"}]}}""")
87+
}
88+
4789
"Adding a required field without default value" should "set the internal migration value" in {
4890

4991
val schema =

0 commit comments

Comments
 (0)