Skip to content

Fix regex handling of tables with WITH storage parameters#65

Open
MathijsK93 wants to merge 66 commits into
lfittl:masterfrom
payt:fix-with-storage-parameters
Open

Fix regex handling of tables with WITH storage parameters#65
MathijsK93 wants to merge 66 commits into
lfittl:masterfrom
payt:fix-with-storage-parameters

Conversation

@MathijsK93
Copy link
Copy Markdown

Problem

Tables with WITH (...) storage parameters (e.g. autovacuum settings) end their CREATE TABLE block as:

```sql
)
WITH (autovacuum_vacuum_scale_factor='0.01', ...);
```

Two regexes in CleanDump only recognized ); as the table terminator, breaking when WITH (...) was present.

Bug 1 — indexes_after_tables

The end-of-table pattern (\);\n|PARTITION.+\);\n) didn't match )\nWITH (...);\n. The ) line was consumed by the preceding (:?[^;\n]*\n)+ group, leaving WITH (...);\n unmatched. Indexes were extracted and deleted from their original position but never reinserted after the table → all indexes for that table vanished from the dump.

Bug 2 — order_column_definitions

The lookahead (?=\n(\);|\)\nPARTITION.+\);)$) also didn't terminate at )\nWITH (...);\n. The lazy (?<columns>.+?) overshot past the storage parameters into the next CREATE TABLE statement, stealing its columns into the current table's column list.

Fix

Both patterns now also accept )\nWITH [^\n]+; as a valid table terminator.

Test

Added storage_parameters_test.rb that verifies:

  • indexes are placed after the correct table (not lost)
  • columns don't bleed from the next table into the one with WITH (...)
  • the subsequent table's columns remain intact

🤖 Generated with Claude Code

nvanoorschot and others added 30 commits November 13, 2019 16:08
Split primary_keys and sequences cleanups
Add support for Rails 6.1
Move unique constraint to create table definitions
Keep pg_stat_statements extension for view
Add support for (MATERIALIZED) VIEWS indexes
…rted

Fix/allow partitioned tables to be sorted
bobvanoorschot and others added 28 commits March 26, 2024 13:43
Add options to ignore some partitions in removal
…r-constraints

Add composite primary key support
Handle new PG 18 not null constraints in pks
Tables with WITH (...) storage parameters (e.g. autovacuum settings) end
with `)` on one line followed by `WITH (...);\n` on the next. Two regexes
didn't handle this terminator:

- `indexes_after_tables`: end-of-table pattern only matched `);` or
  `PARTITION...);`, so indexes were extracted and deleted but never
  reinserted after the table.
- `order_column_definitions`: lookahead only terminated on `);` or
  `)\nPARTITION...);`, causing the lazy column match to overshoot past
  the WITH clause into the next CREATE TABLE, stealing its columns.

Both patterns now also match `)\nWITH [^\n]+;` as a valid table end.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…orage parameters

The sub pattern \n\);\z only matched the normal table end `);`.
With WITH storage parameters the table ends as `)` + newline +
`WITH (...);\n`, so the `)` preceding `;\z` is inside the WITH clause,
not the table-closing `)`. The sub never matched, causing constraints
to be deleted from their ALTER TABLE location but never inserted inline.

New pattern `(\n\)(?:;|\nWITH [^\n]+;))\z` anchors correctly on the
table-closing `)` followed by either `;` or `\nWITH (...);\z`.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@seanlinsley
Copy link
Copy Markdown
Collaborator

Hi @MathijsK93, thanks for the PR! Could you rebase on the upstream master branch so only your changes show up in the diff?

@MathijsK93
Copy link
Copy Markdown
Author

Hi @MathijsK93, thanks for the PR! Could you rebase on the upstream master branch so only your changes show up in the diff?

This PR was created by accident, but if you like I can push this fix upstream by creating a new PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants