Skip to content

Commit 0931bfd

Browse files
authored
Merge pull request #12 from ReifyAB/jl/add-preprocessors-to-handle-latest-pg_dump
Add preprocessor to remove \restrict and \unrestrict pragmas
2 parents 754a1ea + b20f3e4 commit 0931bfd

File tree

9 files changed

+135
-2
lines changed

9 files changed

+135
-2
lines changed

CHANGELOG.md

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

3+
- Add preprocessors config that allows applying text transforms to the input before parsing
4+
- Add preprocessor to remove `\restrict` and `\unrestrict` pragmas that appeared in newer versions of pg_dump
5+
36
## [0.5.0] - 2025-08-04
47

58
- Upgrade pg_query to 6.1 to add support for latest mac OS version

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Say good-bye to those pesky diffs you get between coworkers!
99

1010
By default, it will:
1111

12+
* remove `\restrict` and `\unrestrict` pragmas
1213
* Inline primary key declarations
1314
* Inline SERIAL type declarations
1415
* Inline table constraints
@@ -27,6 +28,8 @@ As an example, the task will transform this raw `structure.sql`:
2728
<summary>Click to expand</summary>
2829

2930
```sql
31+
\restrict 1234
32+
3033
--
3134
-- Name: pgcrypto; Type: EXTENSION; Schema: -; Owner: -
3235
--
@@ -261,6 +264,10 @@ If you want to configure which transforms to use, you can configure the library
261264

262265
```ruby
263266
Rails.application.configure do
267+
config.activerecord_pg_format_db_structure.preprocessors = [
268+
Preprocessors::RemoveRestrictPragmas
269+
]
270+
264271
config.activerecord_pg_format_db_structure.transforms = [
265272
ActiveRecordPgFormatDbStructure::Transforms::RemoveCommentsOnExtensions,
266273
ActiveRecordPgFormatDbStructure::Transforms::RemoveDefaultsSetCommands,
@@ -271,7 +278,7 @@ Rails.application.configure do
271278
ActiveRecordPgFormatDbStructure::Transforms::InlineConstraints,
272279
ActiveRecordPgFormatDbStructure::Transforms::MoveIndicesAfterCreateTable,
273280
ActiveRecordPgFormatDbStructure::Transforms::GroupAlterTableStatements,
274-
ActiveRecordPgFormatDbStructure::Transforms::SortTableColumns,
281+
ActiveRecordPgFormatDbStructure::Transforms::SortTableColumns
275282
]
276283

277284
config.activerecord_pg_format_db_structure.deparser = ActiveRecordPgFormatDbStructure::Deparser
@@ -287,6 +294,13 @@ structure = File.read("db/structure.sql")
287294
formatted = ActiveRecordPgFormatDbStructure::Formatter.new.format(structure)
288295
File.write("db/structure.sql", formatted)
289296
```
297+
## Preprocessors
298+
299+
### RemoveRestrictPragmas
300+
301+
Remove `\restrict` and `\unrestrict` pragmas that were added in newer
302+
versions of `pg_dump`, since they are not valid SQL syntax and
303+
therefore prevent parsing the file as SQL.
290304

291305
## Transformers
292306

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
require_relative "activerecord-pg-format-db-structure/deparser"
66
require_relative "activerecord-pg-format-db-structure/statement_appender"
7+
require_relative "activerecord-pg-format-db-structure/preprocessors/remove_restrict_pragmas"
78
require_relative "activerecord-pg-format-db-structure/transforms/remove_comments_on_extensions"
89
require_relative "activerecord-pg-format-db-structure/transforms/inline_serials"
910
require_relative "activerecord-pg-format-db-structure/transforms/inline_primary_keys"
@@ -16,6 +17,10 @@
1617
require_relative "activerecord-pg-format-db-structure/transforms/sort_table_columns"
1718

1819
module ActiveRecordPgFormatDbStructure
20+
DEFAULT_PREPROCESSORS = [
21+
Preprocessors::RemoveRestrictPragmas
22+
].freeze
23+
1924
DEFAULT_TRANSFORMS = [
2025
Transforms::RemoveCommentsOnExtensions,
2126
Transforms::RemoveDefaultsSetCommands,

lib/activerecord-pg-format-db-structure/formatter.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,25 @@
66
module ActiveRecordPgFormatDbStructure
77
# Formats & normalizes in place the given SQL string
88
class Formatter
9-
attr_reader :transforms, :deparser, :statement_appender
9+
attr_reader :preprocessors, :transforms, :deparser, :statement_appender
1010

1111
def initialize(
12+
preprocessors: DEFAULT_PREPROCESSORS,
1213
transforms: DEFAULT_TRANSFORMS,
1314
deparser: DEFAULT_DEPARSER,
1415
statement_appender: DEFAULT_STATEMENT_APPENDER
1516
)
17+
@preprocessors = preprocessors
1618
@transforms = transforms
1719
@deparser = deparser
1820
@statement_appender = statement_appender
1921
end
2022

2123
def format(source)
24+
preprocessors.each do |preprocessor|
25+
preprocessor.new(source).preprocess!
26+
end
27+
2228
raw_statements = PgQuery.parse(source).tree.stmts
2329

2430
transforms.each do |transform|
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
require "pg_query"
4+
5+
module ActiveRecordPgFormatDbStructure
6+
module Preprocessors
7+
# :nodoc:
8+
class Base
9+
attr_reader :source
10+
11+
def initialize(source)
12+
@source = source
13+
end
14+
end
15+
end
16+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "base"
4+
5+
module ActiveRecordPgFormatDbStructure
6+
module Preprocessors
7+
# Inline non-foreign key constraints into table declaration
8+
class RemoveRestrictPragmas < Base
9+
def preprocess!
10+
source.gsub!(/^(\\restrict .*)$/, "")
11+
source.gsub!(/^(\\unrestrict .*)$/, "")
12+
end
13+
end
14+
end
15+
end

lib/activerecord-pg-format-db-structure/railtie.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module ActiveRecordPgFormatDbStructure
44
# Setup for Rails
55
class Railtie < Rails::Railtie
66
config.activerecord_pg_format_db_structure = ActiveSupport::OrderedOptions.new
7+
config.activerecord_pg_format_db_structure.preprocessors = DEFAULT_PREPROCESSORS.dup
78
config.activerecord_pg_format_db_structure.transforms = DEFAULT_TRANSFORMS.dup
89
config.activerecord_pg_format_db_structure.deparser = DEFAULT_DEPARSER
910
config.activerecord_pg_format_db_structure.statement_appender = DEFAULT_STATEMENT_APPENDER

spec/activerecord-pg-format-db-structure/formatter_spec.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,5 +229,39 @@
229229
;
230230
SQL
231231
end
232+
233+
it "handles pg_dump's \restrict commands" do
234+
formatter = described_class.new
235+
236+
source = +<<~SQL
237+
\\restrict 1234
238+
239+
--
240+
-- Name: comments; Type: TABLE; Schema: public; Owner: -
241+
--
242+
243+
CREATE TABLE public.comments (
244+
id bigserial PRIMARY KEY,
245+
user_id bigint NOT NULL,
246+
post_id bigint NOT NULL,
247+
created_at timestamp(6) without time zone NOT NULL,
248+
updated_at timestamp(6) without time zone NOT NULL
249+
);
250+
251+
\\unrestrict 1234
252+
SQL
253+
254+
expect(formatter.format(source)).to eq(<<~SQL)
255+
-- Name: comments; Type: TABLE;
256+
257+
CREATE TABLE public.comments (
258+
id bigserial PRIMARY KEY,
259+
post_id bigint NOT NULL,
260+
user_id bigint NOT NULL,
261+
created_at timestamp(6) NOT NULL,
262+
updated_at timestamp(6) NOT NULL
263+
);
264+
SQL
265+
end
232266
end
233267
end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe ActiveRecordPgFormatDbStructure::Preprocessors::RemoveRestrictPragmas do
4+
describe "#preprocess!" do
5+
it 'removes \restrict and \unrestrict pragmas' do
6+
source = +<<~SQL
7+
\\restrict 1234
8+
9+
CREATE TABLE public.comments (
10+
id bigint NOT NULL,
11+
user_id bigint NOT NULL,
12+
post_id bigint NOT NULL,
13+
created_at timestamp(6) without time zone NOT NULL,
14+
updated_at timestamp(6) without time zone NOT NULL
15+
);
16+
17+
\\unrestrict 1234
18+
SQL
19+
20+
described_class.new(
21+
source
22+
).preprocess!
23+
24+
expect(source).to eq(<<~SQL)
25+
26+
27+
CREATE TABLE public.comments (
28+
id bigint NOT NULL,
29+
user_id bigint NOT NULL,
30+
post_id bigint NOT NULL,
31+
created_at timestamp(6) without time zone NOT NULL,
32+
updated_at timestamp(6) without time zone NOT NULL
33+
);
34+
35+
36+
SQL
37+
end
38+
end
39+
end

0 commit comments

Comments
 (0)