Skip to content

Multi-line check constraints incorrectly have commas after each line when reordering structure files #19

@aristocrates

Description

@aristocrates

Multi-line check constraints incorrectly have commas after each line when reordering structure files.

Example:

SCHEMA_FILENAME = 'table_schema_for_repro.sql'

PgColumnBytePacker::PgDump.sort_columns_for_definition_file(
  SCHEMA_FILENAME,
  connection: ActiveRecord::Base.connection,
)
CREATE TABLE public.bug_repro_values (
    id bigint NOT NULL,
    condition_category text NOT NULL,
    condition_value integer NOT NULL
    CONSTRAINT contrived_condition_check CHECK (
CASE
    WHEN (condition_category = 'must_be_positive') THEN (condition_value > 0)
    WHEN (condition_category = 'must_be_negative') THEN (condition_value < 0)
    ELSE true
END)
);

The following diff occurs, which correctly reorders condition_value integer and condition_category text, but incorrectly adds commas to the end of all the check constraint lines:

--- table_schema_for_repro.sql	2023-08-31 04:44:55.438589724 +0000
+++ table_schema_for_repro_copy.sql	2023-08-31 04:53:12.714570483 +0000
@@ -1,11 +1,11 @@
 CREATE TABLE public.bug_repro_values (
     id bigint NOT NULL,
+    condition_value integer NOT NULL,
     condition_category text NOT NULL,
-    condition_value integer NOT NULL
-    CONSTRAINT contrived_condition_check CHECK (
-CASE
-    WHEN (condition_category = 'must_be_positive') THEN (condition_value > 0)
-    WHEN (condition_category = 'must_be_negative') THEN (condition_value < 0)
-    ELSE true
+    CONSTRAINT contrived_condition_check CHECK (,
+CASE,
+    WHEN (condition_category = 'must_be_positive') THEN (condition_value > 0),
+    WHEN (condition_category = 'must_be_negative') THEN (condition_value < 0),
+    ELSE true,
 END)
 );

Full repro Dockerfile and supporting files: https://github.com/aristocrates/pg_column_byte_packer_comma_bug_repro

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions