@@ -51,9 +51,9 @@ func TestAccDatabase_collationChange(t *testing.T) {
51
51
CheckDestroy : testAccDatabaseCheckDestroy (dbName ),
52
52
Steps : []resource.TestStep {
53
53
{
54
- Config : testAccDatabaseConfigFull (dbName , charset1 , collation1 ),
54
+ Config : testAccDatabaseConfigFull (dbName , charset1 , collation1 , "" ),
55
55
Check : resource .ComposeTestCheckFunc (
56
- testAccDatabaseCheckFull ("mysql_database.test" , dbName , charset1 , collation1 ),
56
+ testAccDatabaseCheckFull ("mysql_database.test" , dbName , charset1 , collation1 , "" ),
57
57
),
58
58
},
59
59
{
@@ -70,20 +70,58 @@ func TestAccDatabase_collationChange(t *testing.T) {
70
70
71
71
db .Exec (fmt .Sprintf ("ALTER DATABASE %s CHARACTER SET %s COLLATE %s" , dbName , charset2 , collation2 ))
72
72
},
73
- Config : testAccDatabaseConfigFull (dbName , charset1 , collation1 ),
73
+ Config : testAccDatabaseConfigFull (dbName , charset1 , collation1 , "" ),
74
74
Check : resource .ComposeTestCheckFunc (
75
- testAccDatabaseCheckFull (resourceName , dbName , charset1 , collation1 ),
75
+ testAccDatabaseCheckFull (resourceName , dbName , charset1 , collation1 , "" ),
76
+ ),
77
+ },
78
+ },
79
+ })
80
+ }
81
+
82
+ func TestAccDatabase_placementPolicyChange (t * testing.T ) {
83
+ dbName := "terraform_acceptance_test"
84
+
85
+ charset1 := "latin1"
86
+ collation1 := "latin1_bin"
87
+ placementPolicy1 := "test_policy"
88
+ placementPolicy2 := "test_policy_v2"
89
+ placementPolicyResourceName := "mysql_ti_placement_policy.test.name"
90
+
91
+ resource .Test (t , resource.TestCase {
92
+ PreCheck : func () {
93
+ testAccPreCheckSkipNotTiDB (t )
94
+ },
95
+ ProviderFactories : testAccProviderFactories ,
96
+ CheckDestroy : testAccDatabaseCheckDestroy (dbName ),
97
+ Steps : []resource.TestStep {
98
+ {
99
+ Config : testAccDatabaseAndPlacementPolicy (dbName , charset1 , collation1 , placementPolicy1 , placementPolicyResourceName ),
100
+ Check : resource .ComposeTestCheckFunc (
101
+ testAccDatabaseCheckFull ("mysql_database.test" , dbName , charset1 , collation1 , placementPolicy1 ),
102
+ ),
103
+ },
104
+ {
105
+ Config : testAccDatabaseAndPlacementPolicy (dbName , charset1 , collation1 , placementPolicy1 , "" ),
106
+ Check : resource .ComposeTestCheckFunc (
107
+ testAccDatabaseCheckFull ("mysql_database.test" , dbName , charset1 , collation1 , "" ),
108
+ ),
109
+ },
110
+ {
111
+ Config : testAccDatabaseAndPlacementPolicy (dbName , charset1 , collation1 , placementPolicy2 , placementPolicyResourceName ),
112
+ Check : resource .ComposeTestCheckFunc (
113
+ testAccDatabaseCheckFull ("mysql_database.test" , dbName , charset1 , collation1 , placementPolicy2 ),
76
114
),
77
115
},
78
116
},
79
117
})
80
118
}
81
119
82
120
func testAccDatabaseCheckBasic (rn string , name string ) resource.TestCheckFunc {
83
- return testAccDatabaseCheckFull (rn , name , "utf8mb4" , "utf8mb4_bin" )
121
+ return testAccDatabaseCheckFull (rn , name , "utf8mb4" , "utf8mb4_bin" , "" )
84
122
}
85
123
86
- func testAccDatabaseCheckFull (rn string , name string , charset string , collation string ) resource.TestCheckFunc {
124
+ func testAccDatabaseCheckFull (rn string , name string , charset string , collation string , placementPolicy string ) resource.TestCheckFunc {
87
125
return func (s * terraform.State ) error {
88
126
rs , ok := s .RootModule ().Resources [rn ]
89
127
if ! ok {
@@ -123,6 +161,12 @@ func testAccDatabaseCheckFull(rn string, name string, charset string, collation
123
161
}
124
162
}
125
163
164
+ if ! strings .Contains (createSQL , fmt .Sprintf ("PLACEMENT POLICY=`%s`" , placementPolicy )) && placementPolicy != "" {
165
+ return fmt .Errorf ("placement policy expected %s" , placementPolicy )
166
+ } else if strings .Contains (createSQL , "PLACEMENT POLICY=" ) && placementPolicy == "" {
167
+ return fmt .Errorf ("placement policy expected to be empty" )
168
+ }
169
+
126
170
return nil
127
171
}
128
172
}
@@ -150,14 +194,28 @@ func testAccDatabaseCheckDestroy(name string) resource.TestCheckFunc {
150
194
}
151
195
152
196
func testAccDatabaseConfigBasic (name string ) string {
153
- return testAccDatabaseConfigFull (name , "utf8mb4" , "utf8mb4_bin" )
197
+ return testAccDatabaseConfigFull (name , "utf8mb4" , "utf8mb4_bin" , "" )
154
198
}
155
199
156
- func testAccDatabaseConfigFull (name string , charset string , collation string ) string {
200
+ func testAccDatabaseConfigFull (name string , charset string , collation string , placementPolicy string ) string {
201
+ placementPolicyConfig := ""
202
+ if placementPolicy != "" {
203
+ placementPolicyConfig = fmt .Sprintf (`placement_policy = %s` , placementPolicy )
204
+ }
205
+
157
206
return fmt .Sprintf (`
158
207
resource "mysql_database" "test" {
159
208
name = "%s"
160
209
default_character_set = "%s"
161
210
default_collation = "%s"
162
- }` , name , charset , collation )
211
+ %s
212
+ }` , name , charset , collation , placementPolicyConfig )
213
+ }
214
+
215
+ func testAccDatabaseAndPlacementPolicy (name string , charset string , collation string , placementPolicy string , databasePlacementPolicy string ) string {
216
+ return fmt .Sprintf (
217
+ "%s\n %s" ,
218
+ testAccPlacementPolicyConfigBasic (placementPolicy ),
219
+ testAccDatabaseConfigFull (name , charset , collation , databasePlacementPolicy ),
220
+ )
163
221
}
0 commit comments