Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
DBDiff/tests/Unit/GeneratedColumnPlanTest.php at master · DBDiff/DBDiff · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
DBDiff
DBDiff
Repository navigation
Code
Issues
3
(3)
Pull requests
1
(1)
Actions
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
DBDiff
/
tests
/
Unit
/
GeneratedColumnPlanTest.php
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
172 lines (144 loc) · 7.84 KB
master
Breadcrumbs
DBDiff
/
tests
/
Unit
/
GeneratedColumnPlanTest.php
Copy path
Top
File metadata and controls
Code
Blame
172 lines (144 loc) · 7.84 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
namespace
Tests
\
Unit
;
use
DBDiff
\
DB
\
Schema
\
GeneratedColumnPlan
;
use
DBDiff
\
Diff
\
AlterTableChangeColumn
;
use
DBDiff
\
SQLGen
\
Dialect
\
PostgresDialect
;
use
DBDiff
\
SQLGen
\
DiffToSQL
\
AlterTableChangeColumnSQL
;
use
Diff
\
DiffOp
\
DiffOpAdd
;
use
Diff
\
DiffOp
\
DiffOpChange
;
use
Diff
\
DiffOp
\
DiffOpRemove
;
use
PHPUnit
\
Framework
\
TestCase
;
/**
* How changing stored generated columns are applied (issue #233), and how an
* inherited column's type change is left to its parent (issue #232).
*/
class
GeneratedColumnPlanTest
extends
TestCase
{
private
const
A_OLD
=
'
"a" numeric(10,2)
'
;
private
const
A_NEW
=
'
"a" numeric(12,2)
'
;
public
function
testAGeneratedColumnChangingWithTheColumnItReadsIsAttachedToThatChange
():
void
{
$
plan
= GeneratedColumnPlan::
plan
([
'
a
'
=>
new
DiffOpChange
(
self
::
A_OLD
,
self
::
A_NEW
),
'
g
'
=>
new
DiffOpChange
(
'
"g" numeric GENERATED ALWAYS AS ((a * 2)) STORED
'
,
'
"g" numeric GENERATED ALWAYS AS ((a * 3)) STORED
'
),
]);
$
this
->
assertSame
([
'
g
'
=>
'
a
'
],
$
plan
[
'
attach
'
]);
$
this
->
assertSame
([],
$
plan
[
'
regenerate
'
]);
}
public
function
testAnExpressionChangeAloneIsRegenerated
():
void
{
// Before, only a stray DROP NOT NULL was emitted for this.
$
plan
= GeneratedColumnPlan::
plan
([
'
g
'
=>
new
DiffOpChange
(
'
"g" numeric GENERATED ALWAYS AS ((a * 2)) STORED
'
,
'
"g" numeric GENERATED ALWAYS AS ((a * 3)) STORED
'
),
]);
$
this
->
assertSame
([
'
g
'
=>
true
],
$
plan
[
'
regenerate
'
]);
}
public
function
testAColumnCeasingToBeGeneratedIsPlannedForItsDown
():
void
{
// The UP is a DROP EXPRESSION; the DOWN gives a plain column an
// expression, which only a drop and re-add can.
$
plan
= GeneratedColumnPlan::
plan
([
'
g
'
=>
new
DiffOpChange
(
'
"g" numeric GENERATED ALWAYS AS ((a * 2)) STORED
'
,
'
"g" numeric
'
),
]);
$
this
->
assertSame
([
'
g
'
=>
true
],
$
plan
[
'
regenerate
'
]);
}
public
function
testEachDirectionDecidesWhetherItRegenerates
():
void
{
$
diff
=
new
AlterTableChangeColumn
(
'
t
'
,
'
g
'
,
new
DiffOpChange
(
'
"g" numeric GENERATED ALWAYS AS ((a * 2)) STORED
'
,
'
"g" numeric
'
));
$
diff
->
regenerated
=
true
;
$
diff
->
dependants
= [
'
views
'
=> [],
'
policies
'
=> [],
'
triggers
'
=> [],
'
defaultGrantees
'
=> [],
'
generated
'
=> [[
'
schema
'
=>
'
public
'
,
'
table
'
=>
'
t
'
,
'
name
'
=>
'
g
'
,
'
type
'
=>
'
numeric
'
,
'
collation
'
=>
null
,
'
expression
'
=>
'
(a * 2)
'
,
'
notNull
'
=>
false
,
]]];
$
sql
=
new
AlterTableChangeColumnSQL
(
$
diff
,
new
PostgresDialect
());
$
this
->
assertSame
(
'
ALTER TABLE "t" ALTER COLUMN "g" DROP EXPRESSION;
'
,
$
sql
->
getUp
());
$
this
->
assertStringContainsString
(
'
ALTER TABLE "t" DROP COLUMN "g";
'
,
$
sql
->
getDown
());
$
this
->
assertStringContainsString
(
'
GENERATED ALWAYS AS ((a * 2)) STORED
'
,
$
sql
->
getDown
());
}
public
function
testANullabilityChangeAloneIsNotRegenerated
():
void
{
$
plan
= GeneratedColumnPlan::
plan
([
'
g
'
=>
new
DiffOpChange
(
'
"g" numeric GENERATED ALWAYS AS ((a * 2)) STORED
'
,
'
"g" numeric GENERATED ALWAYS AS ((a * 2)) STORED NOT NULL
'
),
]);
$
this
->
assertSame
([
'
attach
'
=> [],
'
regenerate
'
=> [],
'
dropFirst
'
=> []],
$
plan
);
}
public
function
testARemovedGeneratedColumnReadingARetypedColumnIsDroppedFirst
():
void
{
$
plan
= GeneratedColumnPlan::
plan
([
'
a
'
=>
new
DiffOpChange
(
self
::
A_OLD
,
self
::
A_NEW
),
'
g
'
=>
new
DiffOpRemove
(
'
"g" numeric GENERATED ALWAYS AS ((a * 2)) STORED
'
),
]);
$
this
->
assertSame
([
'
g
'
=>
true
],
$
plan
[
'
dropFirst
'
]);
}
public
function
testADefaultChangeOnTheReadColumnDoesNotForceAnything
():
void
{
$
plan
= GeneratedColumnPlan::
plan
([
'
a
'
=>
new
DiffOpChange
(
'
"a" numeric(10,2) DEFAULT 1
'
,
'
"a" numeric(10,2) DEFAULT 2
'
),
'
g
'
=>
new
DiffOpRemove
(
'
"g" numeric GENERATED ALWAYS AS ((a * 2)) STORED
'
),
'
h
'
=>
new
DiffOpAdd
(
'
"h" numeric GENERATED ALWAYS AS ((a * 4)) STORED
'
),
]);
$
this
->
assertSame
([
'
attach
'
=> [],
'
regenerate
'
=> [],
'
dropFirst
'
=> []],
$
plan
);
}
private
function
sqlFor
(
AlterTableChangeColumn
$
change
):
AlterTableChangeColumnSQL
{
return
new
AlterTableChangeColumnSQL
(
$
change
,
new
PostgresDialect
());
}
public
function
testAnInheritedColumnKeepsEverythingButTheTypeChange
():
void
{
$
change
=
new
AlterTableChangeColumn
(
'
p1
'
,
'
a
'
,
new
DiffOpChange
(
'
"a" numeric(10,2) DEFAULT 1
'
,
'
"a" numeric(12,2) DEFAULT 5
'
));
$
change
->
typeInherited
=
true
;
$
this
->
assertSame
(
'
ALTER TABLE "p1" ALTER COLUMN "a" SET DEFAULT 5;
'
,
$
this
->
sqlFor
(
$
change
)->
getUp
());
$
this
->
assertSame
(
'
ALTER TABLE "p1" ALTER COLUMN "a" SET DEFAULT 1;
'
,
$
this
->
sqlFor
(
$
change
)->
getDown
());
}
public
function
testAnInheritedColumnWithOnlyATypeChangeEmitsNothing
():
void
{
$
change
=
new
AlterTableChangeColumn
(
'
p1
'
,
'
a
'
,
new
DiffOpChange
(
self
::
A_OLD
,
self
::
A_NEW
));
$
change
->
typeInherited
=
true
;
$
this
->
assertSame
(
''
,
$
this
->
sqlFor
(
$
change
)->
getUp
());
}
private
function
generated
(
array
$
extra
= []):
array
{
return
$
extra
+ [
'
schema
'
=>
'
public
'
,
'
table
'
=>
'
t
'
,
'
name
'
=>
'
g
'
,
'
type
'
=>
'
numeric
'
,
'
collation
'
=>
null
,
'
expression
'
=>
'
(a * (2)::numeric)
'
,
'
notNull
'
=>
true
,
'
comment
'
=>
"
'doubled'
"
,
'
indexes
'
=> [
'
CREATE INDEX t_g ON public.t USING btree (g)
'
],
'
constraints
'
=> [
'
ALTER TABLE t ADD CONSTRAINT g_nonneg CHECK ((g >= (0)::numeric))
'
],
'
grants
'
=> [[
'
grantee
'
=>
'
reader
'
,
'
privilege
'
=>
'
SELECT
'
,
'
grantable
'
=>
false
]],
];
}
public
function
testAGeneratedDependantIsDroppedBeforeAndReAddedWholeAfter
():
void
{
$
change
=
new
AlterTableChangeColumn
(
'
t
'
,
'
a
'
,
new
DiffOpChange
(
self
::
A_OLD
,
self
::
A_NEW
));
$
change
->
dependants
= [
'
views
'
=> [],
'
policies
'
=> [],
'
triggers
'
=> [],
'
defaultGrantees
'
=> [],
'
generated
'
=> [
$
this
->
generated
()]];
$
sql
=
$
this
->
sqlFor
(
$
change
)->
getUp
();
$
drop
=
strpos
(
$
sql
,
'
ALTER TABLE "t" DROP COLUMN "g";
'
);
$
alter
=
strpos
(
$
sql
,
'
ALTER COLUMN "a" TYPE numeric(12,2)
'
);
$
add
=
strpos
(
$
sql
,
'
ALTER TABLE "t" ADD COLUMN "g" numeric GENERATED ALWAYS AS ((a * (2)::numeric)) STORED NOT NULL;
'
);
$
this
->
assertTrue
(
$
drop
!==
false
&&
$
alter
!==
false
&&
$
add
!==
false
,
$
sql
);
$
this
->
assertLessThan
(
$
alter
,
$
drop
);
$
this
->
assertGreaterThan
(
$
alter
,
$
add
);
foreach
([
'
CREATE INDEX t_g
'
,
'
ADD CONSTRAINT g_nonneg
'
,
"
COMMENT ON COLUMN
\"
t
\"
.
\"
g
\"
IS 'doubled';
"
,
'
GRANT SELECT ("g") ON "t" TO reader;
'
]
as
$
part
) {
$
this
->
assertStringContainsString
(
$
part
,
$
sql
);
}
$
this
->
assertStringNotContainsString
(
'
CASCADE
'
,
$
sql
);
}
public
function
testARegeneratedColumnComesBackAsTheSourceDefinesItOnTheWayUpOnly
():
void
{
$
change
=
new
AlterTableChangeColumn
(
'
t
'
,
'
g
'
,
new
DiffOpChange
(
'
"g" numeric GENERATED ALWAYS AS ((a * (2)::numeric)) STORED
'
,
'
"g" numeric GENERATED ALWAYS AS ((a * (3)::numeric)) STORED
'
));
$
change
->
regenerated
=
true
;
$
change
->
dependants
= [
'
views
'
=> [],
'
policies
'
=> [],
'
triggers
'
=> [],
'
defaultGrantees
'
=> [],
'
generated
'
=> [
$
this
->
generated
([
'
notNull
'
=>
false
,
'
upDefinition
'
=>
'
"g" numeric GENERATED ALWAYS AS ((a * (3)::numeric)) STORED
'
])]];
$
up
=
$
this
->
sqlFor
(
$
change
)->
getUp
();
$
down
=
$
this
->
sqlFor
(
$
change
)->
getDown
();
$
this
->
assertStringContainsString
(
'
ADD COLUMN "g" numeric GENERATED ALWAYS AS ((a * (3)::numeric)) STORED;
'
,
$
up
);
$
this
->
assertStringContainsString
(
'
ADD COLUMN "g" numeric GENERATED ALWAYS AS ((a * (2)::numeric)) STORED;
'
,
$
down
);
$
this
->
assertStringNotContainsString
(
'
ALTER COLUMN
'
,
$
up
,
'
no ALTER: dropping and re-adding is the change
'
);
}
}
You can’t perform that action at this time.