Skip to content

Commit 4e070f1

Browse files
authored
Merge pull request #10 from ReifyAB/jl/remove-no-validate-when-inlining-contraints
Remove skip validation setting when inlining constraint
2 parents 9449489 + 16ad196 commit 4e070f1

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## [Unreleased]
22

3+
- Remove no-op `NOT VALID` setting when inlining constraints
4+
35
## [0.3.0] - 2025-03-15
46

57
- Introduce StatementAppender for better spacing between statements

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,9 @@ sequence indeed has default settings.
347347

348348
### InlineConstraints
349349

350-
Inline non-foreign key constraints into table declaration
350+
Inline non-foreign key constraints into table declaration.
351+
352+
Note that this also remove the `NOT VALID` setting if present, since that's a no-op when the constraint is created at the same time as the table.
351353

352354
### MoveIndicesAfterCreateTable
353355

lib/activerecord-pg-format-db-structure/transforms/inline_constraints.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def match_alter_column_statement(raw_statement)
6464

6565
def add_constraint!(raw_statement, constraint)
6666
raw_statement.stmt.create_stmt.table_elts << PgQuery::Node.from(
67-
PgQuery::Constraint.new(constraint)
67+
PgQuery::Constraint.new(constraint.merge(skip_validation: nil))
6868
)
6969
end
7070
end

spec/activerecord-pg-format-db-structure/transforms/inline_constraints_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,34 @@
9797
;
9898
SQL
9999
end
100+
101+
it "removes NO VALID when inlining since that's a no-op when inlined" do
102+
formatter = ActiveRecordPgFormatDbStructure::Formatter.new(
103+
transforms: [described_class]
104+
)
105+
106+
source = +<<~SQL
107+
CREATE TABLE public.posts (
108+
id bigint NOT NULL,
109+
score int NOT NULL,
110+
created_at timestamp(6) without time zone NOT NULL,
111+
updated_at timestamp(6) without time zone NOT NULL
112+
);
113+
114+
ALTER TABLE ONLY public.posts ADD CONSTRAINT postive_score CHECK (score > 0) NOT VALID;
115+
SQL
116+
117+
expect(formatter.format(source)).to eq(<<~SQL)
118+
-- Name: posts; Type: TABLE;
119+
120+
CREATE TABLE public.posts (
121+
id bigint NOT NULL,
122+
score int NOT NULL,
123+
created_at timestamp(6) NOT NULL,
124+
updated_at timestamp(6) NOT NULL,
125+
CONSTRAINT postive_score CHECK (score > 0)
126+
);
127+
SQL
128+
end
100129
end
101130
end

0 commit comments

Comments
 (0)