activerecord-pg-format-db-structure 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 951bd79c320d7f072e3127b173f0f10a62e3790ec723898560b96c20be48687a
4
- data.tar.gz: 3dfe0017a38748c7514db4354f39d4c12e6c3ae19a2828414798f6ec24dd7e32
3
+ metadata.gz: b916a4b16cf31a2799f9513691052d87b71eadd8fe9e96897f9a94123f4d670d
4
+ data.tar.gz: c86d19f089cc15288cb9a664da548f93e95c451827b6d030229d5c59dd86ffc3
5
5
  SHA512:
6
- metadata.gz: 17a295ed4ffc4052905dc7ef1b40ee85038598b6114ea5c3eef766fe77580ca52f06cf956652abbddd285823e9c2706eee90891890f1b0ce3a1bb5c747a922c8
7
- data.tar.gz: e04dc537cd946efe5dc0064e19f933ffa9fb6315bd5f10a2102318d4149f35479cf31ef06d2c6a2abeeae623f30dbaa07edd5fe7e01f3c8b6a723c2180994093
6
+ metadata.gz: a4c57555371d9aa30151ffb5fd329f6e1bbee46a7bec5be0956eee482b2a7b8bdf9740c9a12466f03c8e57d604d802f82abce1cd95818615b255c8fcc2619a7c
7
+ data.tar.gz: ed0865a617ce28462019287d4a1d1db62ea42f34c98e13d4cad8a80bcd5ab92a9b78599e7912691102e2698bc79d2055e68e01cafc721829b0b7f7e126e68aea
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.6.0] - 2025-08-21
4
+
5
+ - Add preprocessors config that allows applying text transforms to the input before parsing
6
+ - Add preprocessor to remove `\restrict` and `\unrestrict` pragmas that appeared in newer versions of pg_dump
7
+
3
8
  ## [0.5.0] - 2025-08-04
4
9
 
5
10
  - Upgrade pg_query to 6.1 to add support for latest mac OS version
data/README.md CHANGED
@@ -9,6 +9,7 @@ Say good-bye to those pesky diffs you get between coworkers!
9
9
 
10
10
  By default, it will:
11
11
 
12
+ * remove `\restrict` and `\unrestrict` pragmas
12
13
  * Inline primary key declarations
13
14
  * Inline SERIAL type declarations
14
15
  * Inline table constraints
@@ -27,6 +28,8 @@ As an example, the task will transform this raw `structure.sql`:
27
28
  <summary>Click to expand</summary>
28
29
 
29
30
  ```sql
31
+ \restrict 1234
32
+
30
33
  --
31
34
  -- Name: pgcrypto; Type: EXTENSION; Schema: -; Owner: -
32
35
  --
@@ -261,6 +264,10 @@ If you want to configure which transforms to use, you can configure the library
261
264
 
262
265
  ```ruby
263
266
  Rails.application.configure do
267
+ config.activerecord_pg_format_db_structure.preprocessors = [
268
+ Preprocessors::RemoveRestrictPragmas
269
+ ]
270
+
264
271
  config.activerecord_pg_format_db_structure.transforms = [
265
272
  ActiveRecordPgFormatDbStructure::Transforms::RemoveCommentsOnExtensions,
266
273
  ActiveRecordPgFormatDbStructure::Transforms::RemoveDefaultsSetCommands,
@@ -271,7 +278,7 @@ Rails.application.configure do
271
278
  ActiveRecordPgFormatDbStructure::Transforms::InlineConstraints,
272
279
  ActiveRecordPgFormatDbStructure::Transforms::MoveIndicesAfterCreateTable,
273
280
  ActiveRecordPgFormatDbStructure::Transforms::GroupAlterTableStatements,
274
- ActiveRecordPgFormatDbStructure::Transforms::SortTableColumns,
281
+ ActiveRecordPgFormatDbStructure::Transforms::SortTableColumns
275
282
  ]
276
283
 
277
284
  config.activerecord_pg_format_db_structure.deparser = ActiveRecordPgFormatDbStructure::Deparser
@@ -287,6 +294,13 @@ structure = File.read("db/structure.sql")
287
294
  formatted = ActiveRecordPgFormatDbStructure::Formatter.new.format(structure)
288
295
  File.write("db/structure.sql", formatted)
289
296
  ```
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.
290
304
 
291
305
  ## Transformers
292
306
 
@@ -6,19 +6,25 @@ require_relative "../activerecord-pg-format-db-structure"
6
6
  module ActiveRecordPgFormatDbStructure
7
7
  # Formats & normalizes in place the given SQL string
8
8
  class Formatter
9
- attr_reader :transforms, :deparser, :statement_appender
9
+ attr_reader :preprocessors, :transforms, :deparser, :statement_appender
10
10
 
11
11
  def initialize(
12
+ preprocessors: DEFAULT_PREPROCESSORS,
12
13
  transforms: DEFAULT_TRANSFORMS,
13
14
  deparser: DEFAULT_DEPARSER,
14
15
  statement_appender: DEFAULT_STATEMENT_APPENDER
15
16
  )
17
+ @preprocessors = preprocessors
16
18
  @transforms = transforms
17
19
  @deparser = deparser
18
20
  @statement_appender = statement_appender
19
21
  end
20
22
 
21
23
  def format(source)
24
+ preprocessors.each do |preprocessor|
25
+ preprocessor.new(source).preprocess!
26
+ end
27
+
22
28
  raw_statements = PgQuery.parse(source).tree.stmts
23
29
 
24
30
  transforms.each do |transform|
@@ -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
@@ -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
@@ -4,6 +4,7 @@ module ActiveRecordPgFormatDbStructure
4
4
  # Setup for Rails
5
5
  class Railtie < Rails::Railtie
6
6
  config.activerecord_pg_format_db_structure = ActiveSupport::OrderedOptions.new
7
+ config.activerecord_pg_format_db_structure.preprocessors = DEFAULT_PREPROCESSORS.dup
7
8
  config.activerecord_pg_format_db_structure.transforms = DEFAULT_TRANSFORMS.dup
8
9
  config.activerecord_pg_format_db_structure.deparser = DEFAULT_DEPARSER
9
10
  config.activerecord_pg_format_db_structure.statement_appender = DEFAULT_STATEMENT_APPENDER
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecordPgFormatDbStructure
4
- VERSION = "0.5.0"
4
+ VERSION = "0.6.0"
5
5
  end
@@ -4,6 +4,7 @@ require_relative "activerecord-pg-format-db-structure/version"
4
4
 
5
5
  require_relative "activerecord-pg-format-db-structure/deparser"
6
6
  require_relative "activerecord-pg-format-db-structure/statement_appender"
7
+ require_relative "activerecord-pg-format-db-structure/preprocessors/remove_restrict_pragmas"
7
8
  require_relative "activerecord-pg-format-db-structure/transforms/remove_comments_on_extensions"
8
9
  require_relative "activerecord-pg-format-db-structure/transforms/inline_serials"
9
10
  require_relative "activerecord-pg-format-db-structure/transforms/inline_primary_keys"
@@ -16,6 +17,10 @@ require_relative "activerecord-pg-format-db-structure/transforms/sort_schema_mig
16
17
  require_relative "activerecord-pg-format-db-structure/transforms/sort_table_columns"
17
18
 
18
19
  module ActiveRecordPgFormatDbStructure
20
+ DEFAULT_PREPROCESSORS = [
21
+ Preprocessors::RemoveRestrictPragmas
22
+ ].freeze
23
+
19
24
  DEFAULT_TRANSFORMS = [
20
25
  Transforms::RemoveCommentsOnExtensions,
21
26
  Transforms::RemoveDefaultsSetCommands,
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-pg-format-db-structure
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jell
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-08-04 00:00:00.000000000 Z
10
+ date: 2025-08-21 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: pg_query
@@ -42,6 +42,8 @@ files:
42
42
  - lib/activerecord-pg-format-db-structure/deparser.rb
43
43
  - lib/activerecord-pg-format-db-structure/formatter.rb
44
44
  - lib/activerecord-pg-format-db-structure/indenter.rb
45
+ - lib/activerecord-pg-format-db-structure/preprocessors/base.rb
46
+ - lib/activerecord-pg-format-db-structure/preprocessors/remove_restrict_pragmas.rb
45
47
  - lib/activerecord-pg-format-db-structure/railtie.rb
46
48
  - lib/activerecord-pg-format-db-structure/statement_appender.rb
47
49
  - lib/activerecord-pg-format-db-structure/tasks/clean_db_structure.rake