Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## [Unreleased]

- Remove no-op `NOT VALID` setting when inlining constraints

## [0.3.0] - 2025-03-15

- Introduce StatementAppender for better spacing between statements
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,9 @@ sequence indeed has default settings.

### InlineConstraints

Inline non-foreign key constraints into table declaration
Inline non-foreign key constraints into table declaration.

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.

### MoveIndicesAfterCreateTable

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def match_alter_column_statement(raw_statement)

def add_constraint!(raw_statement, constraint)
raw_statement.stmt.create_stmt.table_elts << PgQuery::Node.from(
PgQuery::Constraint.new(constraint)
PgQuery::Constraint.new(constraint.merge(skip_validation: nil))
)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,34 @@
;
SQL
end

it "removes NO VALID when inlining since that's a no-op when inlined" do
formatter = ActiveRecordPgFormatDbStructure::Formatter.new(
transforms: [described_class]
)

source = +<<~SQL
CREATE TABLE public.posts (
id bigint NOT NULL,
score int NOT NULL,
created_at timestamp(6) without time zone NOT NULL,
updated_at timestamp(6) without time zone NOT NULL
);

ALTER TABLE ONLY public.posts ADD CONSTRAINT postive_score CHECK (score > 0) NOT VALID;
SQL

expect(formatter.format(source)).to eq(<<~SQL)
-- Name: posts; Type: TABLE;

CREATE TABLE public.posts (
id bigint NOT NULL,
score int NOT NULL,
created_at timestamp(6) NOT NULL,
updated_at timestamp(6) NOT NULL,
CONSTRAINT postive_score CHECK (score > 0)
);
SQL
end
end
end