|
7 | 7 | "strings"
|
8 | 8 | "testing"
|
9 | 9 |
|
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + |
10 | 12 | "github.com/xataio/pgroll/internal/testutils"
|
11 | 13 | "github.com/xataio/pgroll/pkg/migrations"
|
12 | 14 | )
|
@@ -97,6 +99,80 @@ func TestCreateConstraint(t *testing.T) {
|
97 | 99 | }, testutils.UniqueViolationErrorCode)
|
98 | 100 | },
|
99 | 101 | },
|
| 102 | + { |
| 103 | + name: "create check constraint on single column", |
| 104 | + migrations: []migrations.Migration{ |
| 105 | + { |
| 106 | + Name: "01_add_table", |
| 107 | + Operations: migrations.Operations{ |
| 108 | + &migrations.OpCreateTable{ |
| 109 | + Name: "users", |
| 110 | + Columns: []migrations.Column{ |
| 111 | + { |
| 112 | + Name: "id", |
| 113 | + Type: "serial", |
| 114 | + Pk: ptr(true), |
| 115 | + }, |
| 116 | + { |
| 117 | + Name: "name", |
| 118 | + Type: "varchar(255)", |
| 119 | + Nullable: ptr(false), |
| 120 | + }, |
| 121 | + }, |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, |
| 125 | + { |
| 126 | + Name: "02_create_constraint", |
| 127 | + Operations: migrations.Operations{ |
| 128 | + &migrations.OpCreateConstraint{ |
| 129 | + Name: "name_letters", |
| 130 | + Table: "users", |
| 131 | + Type: "check", |
| 132 | + Check: ptr("name ~ '^[a-zA-Z]+$'"), |
| 133 | + Columns: []string{"name"}, |
| 134 | + Up: migrations.OpCreateConstraintUp(map[string]string{ |
| 135 | + "name": "regexp_replace(name, '\\d+', '', 'g')", |
| 136 | + }), |
| 137 | + Down: migrations.OpCreateConstraintDown(map[string]string{ |
| 138 | + "name": "name", |
| 139 | + }), |
| 140 | + }, |
| 141 | + }, |
| 142 | + }, |
| 143 | + }, |
| 144 | + afterStart: func(t *testing.T, db *sql.DB, schema string) { |
| 145 | + // The new (temporary) column should exist on the underlying table. |
| 146 | + ColumnMustExist(t, db, schema, "users", migrations.TemporaryName("name")) |
| 147 | + // The check constraint exists on the new table. |
| 148 | + CheckConstraintMustExist(t, db, schema, "users", "name_letters") |
| 149 | + // Inserting values into the old schema that violate the check constraint must succeed. |
| 150 | + MustInsert(t, db, schema, "01_add_table", "users", map[string]string{ |
| 151 | + "name": "alice11", |
| 152 | + }) |
| 153 | + |
| 154 | + // Inserting values into the new schema that violate the check constraint should fail. |
| 155 | + MustInsert(t, db, schema, "02_create_constraint", "users", map[string]string{ |
| 156 | + "name": "bob", |
| 157 | + }) |
| 158 | + MustNotInsert(t, db, schema, "02_create_constraint", "users", map[string]string{ |
| 159 | + "name": "bob2", |
| 160 | + }, testutils.CheckViolationErrorCode) |
| 161 | + }, |
| 162 | + afterRollback: func(t *testing.T, db *sql.DB, schema string) { |
| 163 | + // Functions, triggers and temporary columns are dropped. |
| 164 | + tableCleanedUp(t, db, schema, "users", "name") |
| 165 | + }, |
| 166 | + afterComplete: func(t *testing.T, db *sql.DB, schema string) { |
| 167 | + // Functions, triggers and temporary columns are dropped. |
| 168 | + tableCleanedUp(t, db, schema, "users", "name") |
| 169 | + |
| 170 | + // Inserting values into the new schema that violate the check constraint should fail. |
| 171 | + MustNotInsert(t, db, schema, "02_create_constraint", "users", map[string]string{ |
| 172 | + "name": "carol0", |
| 173 | + }, testutils.CheckViolationErrorCode) |
| 174 | + }, |
| 175 | + }, |
100 | 176 | {
|
101 | 177 | name: "create unique constraint on multiple columns",
|
102 | 178 | migrations: []migrations.Migration{
|
@@ -181,6 +257,104 @@ func TestCreateConstraint(t *testing.T) {
|
181 | 257 | // Complete is a no-op.
|
182 | 258 | },
|
183 | 259 | },
|
| 260 | + { |
| 261 | + name: "create check constraint on multiple columns", |
| 262 | + migrations: []migrations.Migration{ |
| 263 | + { |
| 264 | + Name: "01_add_table", |
| 265 | + Operations: migrations.Operations{ |
| 266 | + &migrations.OpCreateTable{ |
| 267 | + Name: "users", |
| 268 | + Columns: []migrations.Column{ |
| 269 | + { |
| 270 | + Name: "id", |
| 271 | + Type: "serial", |
| 272 | + Pk: ptr(true), |
| 273 | + }, |
| 274 | + { |
| 275 | + Name: "name", |
| 276 | + Type: "varchar(255)", |
| 277 | + Nullable: ptr(false), |
| 278 | + }, |
| 279 | + { |
| 280 | + Name: "email", |
| 281 | + Type: "varchar(255)", |
| 282 | + Nullable: ptr(false), |
| 283 | + }, |
| 284 | + }, |
| 285 | + }, |
| 286 | + }, |
| 287 | + }, |
| 288 | + { |
| 289 | + Name: "02_create_constraint", |
| 290 | + Operations: migrations.Operations{ |
| 291 | + &migrations.OpCreateConstraint{ |
| 292 | + Name: "check_name_email", |
| 293 | + Table: "users", |
| 294 | + Type: "check", |
| 295 | + Check: ptr("name != email"), |
| 296 | + Columns: []string{"name", "email"}, |
| 297 | + Up: migrations.OpCreateConstraintUp(map[string]string{ |
| 298 | + "name": "name", |
| 299 | + "email": "(SELECT CASE WHEN email ~ '@' THEN email ELSE email || '@example.com' END)", |
| 300 | + }), |
| 301 | + Down: migrations.OpCreateConstraintDown(map[string]string{ |
| 302 | + "name": "name", |
| 303 | + "email": "email", |
| 304 | + }), |
| 305 | + }, |
| 306 | + }, |
| 307 | + }, |
| 308 | + }, |
| 309 | + afterStart: func(t *testing.T, db *sql.DB, schema string) { |
| 310 | + // The new (temporary) column should exist on the underlying table. |
| 311 | + ColumnMustExist(t, db, schema, "users", migrations.TemporaryName("name")) |
| 312 | + // The new (temporary) column should exist on the underlying table. |
| 313 | + ColumnMustExist(t, db, schema, "users", migrations.TemporaryName("email")) |
| 314 | + // The check constraint exists on the new table. |
| 315 | + CheckConstraintMustExist(t, db, schema, "users", "check_name_email") |
| 316 | + |
| 317 | + // Inserting values into the old schema that the violate the check constraint must succeed. |
| 318 | + MustInsert(t, db, schema, "01_add_table", "users", map[string]string{ |
| 319 | + "name": "alice", |
| 320 | + "email": "alice", |
| 321 | + }) |
| 322 | + |
| 323 | + // Inserting values into the new schema that meet the check constraint should succeed. |
| 324 | + MustInsert(t, db, schema, "02_create_constraint", "users", map[string]string{ |
| 325 | + "name": "bob", |
| 326 | + |
| 327 | + }) |
| 328 | + MustNotInsert(t, db, schema, "02_create_constraint", "users", map[string]string{ |
| 329 | + "name": "bob", |
| 330 | + "email": "bob", |
| 331 | + }, testutils.CheckViolationErrorCode) |
| 332 | + }, |
| 333 | + afterRollback: func(t *testing.T, db *sql.DB, schema string) { |
| 334 | + // The check constraint must not exists on the table. |
| 335 | + CheckConstraintMustNotExist(t, db, schema, "users", "check_name_email") |
| 336 | + // Functions, triggers and temporary columns are dropped. |
| 337 | + tableCleanedUp(t, db, schema, "users", "name") |
| 338 | + tableCleanedUp(t, db, schema, "users", "email") |
| 339 | + }, |
| 340 | + afterComplete: func(t *testing.T, db *sql.DB, schema string) { |
| 341 | + // Functions, triggers and temporary columns are dropped. |
| 342 | + tableCleanedUp(t, db, schema, "users", "name") |
| 343 | + tableCleanedUp(t, db, schema, "users", "email") |
| 344 | + |
| 345 | + // Inserting values into the new schema that the violate the check constraint must fail. |
| 346 | + MustNotInsert(t, db, schema, "02_create_constraint", "users", map[string]string{ |
| 347 | + "name": "carol", |
| 348 | + "email": "carol", |
| 349 | + }, testutils.CheckViolationErrorCode) |
| 350 | + |
| 351 | + rows := MustSelect(t, db, schema, "02_create_constraint", "users") |
| 352 | + assert.Equal(t, []map[string]any{ |
| 353 | + { "id": 1, "name": "alice", "email": "[email protected]"}, |
| 354 | + { "id": 2, "name": "bob", "email": "[email protected]"}, |
| 355 | + }, rows) |
| 356 | + }, |
| 357 | + }, |
184 | 358 | {
|
185 | 359 | name: "invalid constraint name",
|
186 | 360 | migrations: []migrations.Migration{
|
@@ -270,6 +444,52 @@ func TestCreateConstraint(t *testing.T) {
|
270 | 444 | afterRollback: func(t *testing.T, db *sql.DB, schema string) {},
|
271 | 445 | afterComplete: func(t *testing.T, db *sql.DB, schema string) {},
|
272 | 446 | },
|
| 447 | + { |
| 448 | + name: "expression of check constraint is missing", |
| 449 | + migrations: []migrations.Migration{ |
| 450 | + { |
| 451 | + Name: "01_add_table", |
| 452 | + Operations: migrations.Operations{ |
| 453 | + &migrations.OpCreateTable{ |
| 454 | + Name: "users", |
| 455 | + Columns: []migrations.Column{ |
| 456 | + { |
| 457 | + Name: "id", |
| 458 | + Type: "serial", |
| 459 | + Pk: ptr(true), |
| 460 | + }, |
| 461 | + { |
| 462 | + Name: "name", |
| 463 | + Type: "varchar(255)", |
| 464 | + Nullable: ptr(false), |
| 465 | + }, |
| 466 | + }, |
| 467 | + }, |
| 468 | + }, |
| 469 | + }, |
| 470 | + { |
| 471 | + Name: "02_create_constraint_with_missing_migration", |
| 472 | + Operations: migrations.Operations{ |
| 473 | + &migrations.OpCreateConstraint{ |
| 474 | + Name: "check_name", |
| 475 | + Table: "users", |
| 476 | + Columns: []string{"name"}, |
| 477 | + Type: "check", |
| 478 | + Up: migrations.OpCreateConstraintUp(map[string]string{ |
| 479 | + "name": "name", |
| 480 | + }), |
| 481 | + Down: migrations.OpCreateConstraintDown(map[string]string{ |
| 482 | + "name": "name", |
| 483 | + }), |
| 484 | + }, |
| 485 | + }, |
| 486 | + }, |
| 487 | + }, |
| 488 | + wantStartErr: migrations.FieldRequiredError{Name: "check"}, |
| 489 | + afterStart: func(t *testing.T, db *sql.DB, schema string) {}, |
| 490 | + afterRollback: func(t *testing.T, db *sql.DB, schema string) {}, |
| 491 | + afterComplete: func(t *testing.T, db *sql.DB, schema string) {}, |
| 492 | + }, |
273 | 493 | })
|
274 | 494 | }
|
275 | 495 |
|
|
0 commit comments