File: cli-migrate-lint-modify-nullability.txt

package info (click to toggle)
golang-ariga-atlas 0.7.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 5,676 kB
  • sloc: javascript: 592; sql: 404; makefile: 10
file content (67 lines) | stat: -rw-r--r-- 2,333 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
only postgres14

atlas migrate lint --dir file://migrations1 --dev-url URL --latest=1 > got.txt
cmp got.txt expected1.txt

atlas migrate lint --dir file://migrations2 --dev-url URL --latest=1 > got.txt
cmp got.txt expected2.txt

atlas migrate lint --dir file://migrations3 --dev-url URL --latest=1 > got.txt
cmp got.txt empty.txt

atlas migrate lint --dir file://migrations4 --dev-url URL --latest=1 > got.txt
cmp got.txt expected4.txt

-- empty.txt --
-- migrations1/1.sql --
CREATE TABLE users (id int);

-- migrations1/2.sql --
ALTER TABLE users ADD COLUMN "rank" int NULL DEFAULT 1;
ALTER TABLE users ALTER COLUMN "id" SET NOT NULL;

-- expected1.txt --
2.sql: data dependent changes detected:

	L2: Modifying nullable column "id" to non-nullable might fail in case it contains NULL values

-- migrations2/1.sql --
CREATE TABLE users (id int);

-- migrations2/2.sql --
-- Add (a, b, c), backfill (a, b) and then modify all to not-null.
ALTER TABLE users ADD COLUMN "a" int, ADD COLUMN "b" int, ADD COLUMN "c" int;
UPDATE users SET "a" = 1;
UPDATE users SET "b" = 1 WHERE "b" IS NULL;
ALTER TABLE users ALTER COLUMN "a" SET NOT NULL, ALTER COLUMN "b" SET NOT NULL, ALTER COLUMN "c" SET NOT NULL;

-- expected2.txt --
2.sql: data dependent changes detected:

	L5: Modifying nullable column "c" to non-nullable might fail in case it contains NULL values

-- migrations3/1.sql --
CREATE TABLE users (id int);
ALTER TABLE users ALTER COLUMN "id" SET NOT NULL;

-- migrations4/1.sql --
CREATE TABLE users (id int);
CREATE TABLE pets (id int);

-- migrations4/2.sql --
ALTER TABLE users ADD COLUMN name varchar(255), ADD COLUMN age float;
UPDATE users SET name = 'Unknown', age = 0;
-- No diagnostics.
ALTER TABLE users ALTER COLUMN name SET NOT NULL, ALTER COLUMN age SET NOT NULL;

ALTER TABLE pets ADD COLUMN name varchar(255), ADD COLUMN age float;
UPDATE pets SET name = 'Unknown', age = 0 WHERE random() > 0.5;
-- With diagnostics as statement above cannot be sure NULL values are back-filled.
ALTER TABLE pets ALTER COLUMN name SET NOT NULL, ALTER COLUMN age SET NOT NULL;

-- expected4.txt --
2.sql: data dependent changes detected:

	L9: Modifying nullable column "name" to non-nullable might fail in case it contains NULL values
	L9: Modifying nullable column "age" to non-nullable might fail in case it contains NULL values