diff --git a/CHANGELOG.md b/CHANGELOG.md index bee471c..2bd8aa4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index dec7eed..5dc9bbb 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/activerecord-pg-format-db-structure/transforms/inline_constraints.rb b/lib/activerecord-pg-format-db-structure/transforms/inline_constraints.rb index ef6bbd5..73a44b1 100644 --- a/lib/activerecord-pg-format-db-structure/transforms/inline_constraints.rb +++ b/lib/activerecord-pg-format-db-structure/transforms/inline_constraints.rb @@ -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 diff --git a/spec/activerecord-pg-format-db-structure/transforms/inline_constraints_spec.rb b/spec/activerecord-pg-format-db-structure/transforms/inline_constraints_spec.rb index 7968186..c21b395 100644 --- a/spec/activerecord-pg-format-db-structure/transforms/inline_constraints_spec.rb +++ b/spec/activerecord-pg-format-db-structure/transforms/inline_constraints_spec.rb @@ -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