activerecord-pg-format-db-structure 0.1.5 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 231e1ce74a64d5b68e27b122465855bf382cb24c5f573da3983b2ab1e11b3c23
4
- data.tar.gz: 3341fb271ea14d6762cb879f28b6b526d51a76c37c83163fe26ce2955d9b1b83
3
+ metadata.gz: 476baa5e7f08de7b010ef98240bf4dc9d5f92d8c381759682dfdcbdd33ba5723
4
+ data.tar.gz: 8a9337bff151ccadbfc77cb638aca3b1097831979206cf971d4e3af4b3258c7c
5
5
  SHA512:
6
- metadata.gz: 2f06ac5c8140c6766f4dd77909ba62f60fb2b86b2453d4fad3eb190221de590025eb079e6c2e74999ff3e39fcf90dcac8b1751d52fa33bf9a33ec26ea9c05b91
7
- data.tar.gz: 790f40bbc56b3b0062e72b7793a9c1645ea286f9a3f557f180aea1fc261dc52401ef721ee2320901cf2ec30d1dc6b763eb0662d30906ab6e351e0b4ab33b1377
6
+ metadata.gz: a6c728fe8aba0d6c943ff8ac5cf16d386099ea85f4a35c101eed701b899ab3ec266b9004313007bba2d5e2567273f5aedfff857567743ea64c1a2d5bdaa1961c
7
+ data.tar.gz: 17b9b5722ac383a92969640cf30a6a68aaee073eec784cbe8545eae6f85d983a5fc7a5b2bebd66755bcf097340a8e931a4e831cc298647759f039078b51663f4
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.1] - 2025-02-15
4
+
5
+ - Add transform to remove SET commands with default values
6
+
7
+ ## [0.2.0] - 2025-02-15
8
+
9
+ - Remove preprocessors (no longer relevant now that we don't reuse the source string in the output)
10
+
3
11
  ## [0.1.5] - 2025-02-08
4
12
 
5
13
  - Sort table columns
data/README.md CHANGED
@@ -3,7 +3,9 @@
3
3
  ![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/ReifyAB/activerecord-pg-format-db-structure/main.yml)
4
4
 
5
5
 
6
- Automatically cleans up your `structure.sql` file after each rails migration.
6
+ Automatically cleans up your PostgreSQL `structure.sql` file after each rails migration.
7
+
8
+ Say good-bye to small those small diffs you get between coworkers!
7
9
 
8
10
  By default, it will:
9
11
 
@@ -14,9 +16,11 @@ By default, it will:
14
16
  * Group `ALTER TABLE` statements into a single statement per table
15
17
  * Sorts table column declarations (primary key / foreign keys / data / timestamp / constraints)
16
18
  * Sorts `schema_migrations` inserts
17
- * Removes unnecessary whitespace
19
+ * Format and indent the entire file consistently
20
+
21
+ It can also optionally inline foreign key declarations (see below).
18
22
 
19
- The task will transform this raw `structure.sql`:
23
+ As an example, the task will transform this raw `structure.sql`:
20
24
 
21
25
  <details>
22
26
 
@@ -184,7 +188,7 @@ INSERT INTO "schema_migrations" (version) VALUES
184
188
  ```
185
189
  </details>
186
190
 
187
- into this much more compact and normalized version:
191
+ into this normalize (and much more compatch & readable) version:
188
192
 
189
193
  ```sql
190
194
 
@@ -230,8 +234,9 @@ INSERT INTO schema_migrations (version) VALUES
230
234
  ;
231
235
  ```
232
236
 
233
- which is a lot more compact, easier to read, and reduces the risk of
234
- getting random diffs between machines after each migration.
237
+ The goal is to make your `structure.sql` file easier to read and to
238
+ reduce the risk of getting random diffs between machines after each
239
+ migration.
235
240
 
236
241
  Those transformations are made by manipulating the SQL AST directly
237
242
  using [pg_query](https://github.com/pganalyze/pg_query), and each
@@ -245,7 +250,7 @@ You can also add your own transforms (see below).
245
250
  Add the following to your Gemfile:
246
251
 
247
252
  ```ruby
248
- gem 'activerecord-clean-db-structure'
253
+ gem 'activerecord-pg-format-db-structure'
249
254
  ```
250
255
 
251
256
  ## Usage
@@ -258,12 +263,9 @@ If you want to configure which transforms to use, you can configure the library
258
263
 
259
264
  ```ruby
260
265
  Rails.application.configure do
261
- config.activerecord_pg_format_db_structure.preprocessors = [
262
- ActiveRecordPgFormatDbStructure::Preprocessors::RemoveWhitespaces
263
- ]
264
-
265
266
  config.activerecord_pg_format_db_structure.transforms = [
266
267
  ActiveRecordPgFormatDbStructure::Transforms::RemoveCommentsOnExtensions,
268
+ ActiveRecordPgFormatDbStructure::Transforms::RemoveDefaultsSetCommands,
267
269
  ActiveRecordPgFormatDbStructure::Transforms::SortSchemaMigrations,
268
270
  ActiveRecordPgFormatDbStructure::Transforms::InlinePrimaryKeys,
269
271
  # ActiveRecordPgFormatDbStructure::Transforms::InlineForeignKeys,
@@ -288,18 +290,33 @@ formatted = ActiveRecordPgFormatDbStructure::Formatter.new.format(structure)
288
290
  File.write("db/structure.sql", formatted)
289
291
  ```
290
292
 
291
- ## Preprocessors
292
-
293
- ### RemoveWhitespaces
294
-
295
- Remove unnecessary comment, whitespase and empty lines.
296
-
297
293
  ## Transformers
298
294
 
299
295
  ### RemoveCommentsOnExtensions
300
296
 
301
297
  Remove COMMENT statement applied to extensions
302
298
 
299
+ ### RemoveDefaultsSetCommands
300
+
301
+ Remove SET commands that apply default values to postgres settings. By default, the following defaults are handled:
302
+
303
+ ```ruby
304
+ ActiveRecordPgFormatDbStructure::Transforms::RemoveDefaultsSetCommands.postgres_config_defaults = {
305
+ default_table_access_method: "heap",
306
+ default_with_oids: false,
307
+ idle_in_transaction_session_timeout: 0,
308
+ lock_timeout: 0,
309
+ statement_timeout: 0,
310
+ transaction_timeout: 0,
311
+ standard_conforming_strings: true,
312
+ xmloption: "content"
313
+ }
314
+ ```
315
+
316
+ Which are the default values since Postgres 9.1. You can make changes
317
+ to the above config in case you want to handle more cases.
318
+
319
+
303
320
  ### SortSchemaMigrations
304
321
 
305
322
  Sort schema_migrations inserts to be in chronological order, helps with reducing merge conflicts.
@@ -6,23 +6,17 @@ 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 :preprocessors, :transforms, :deparser
9
+ attr_reader :transforms, :deparser
10
10
 
11
11
  def initialize(
12
- preprocessors: DEFAULT_PREPROCESSORS,
13
12
  transforms: DEFAULT_TRANSFORMS,
14
13
  deparser: DEFAULT_DEPARSER
15
14
  )
16
- @preprocessors = preprocessors
17
15
  @transforms = transforms
18
16
  @deparser = deparser
19
17
  end
20
18
 
21
19
  def format(source)
22
- preprocessors.each do |preprocessor|
23
- preprocessor.new(source).preprocess!
24
- end
25
-
26
20
  raw_statements = PgQuery.parse(source).tree.stmts
27
21
 
28
22
  transforms.each do |transform|
@@ -4,7 +4,6 @@ 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
8
7
  config.activerecord_pg_format_db_structure.transforms = DEFAULT_TRANSFORMS.dup
9
8
  config.activerecord_pg_format_db_structure.deparser = DEFAULT_DEPARSER
10
9
 
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "base"
4
+
5
+ module ActiveRecordPgFormatDbStructure
6
+ module Transforms
7
+ # Remove SET commands that apply default values to postgres settings
8
+ class RemoveDefaultsSetCommands < Base
9
+ class << self
10
+ attr_accessor :postgres_config_defaults
11
+ end
12
+
13
+ self.postgres_config_defaults = {
14
+ default_table_access_method: "heap",
15
+ default_with_oids: false,
16
+ idle_in_transaction_session_timeout: 0,
17
+ lock_timeout: 0,
18
+ statement_timeout: 0,
19
+ transaction_timeout: 0,
20
+ standard_conforming_strings: true,
21
+ xmloption: "content"
22
+ }
23
+
24
+ def transform!
25
+ raw_statements.delete_if do |raw_statement|
26
+ next unless raw_statement.stmt.to_h in variable_set_stmt: {kind: :VAR_SET_VALUE, name:, args: [{a_const:}]}
27
+
28
+ next unless self.class.postgres_config_defaults.key?(name.to_sym)
29
+
30
+ pattern = value_to_pattern(self.class.postgres_config_defaults[name.to_sym])
31
+
32
+ val_from_a_const(a_const) in ^pattern
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def value_to_pattern(value)
39
+ case value
40
+ in false
41
+ Set.new(["false", "no", "off", 0])
42
+ in true
43
+ Set.new(["true", "yes", "on", 1])
44
+ else
45
+ value
46
+ end
47
+ end
48
+
49
+ def val_from_a_const(a_const)
50
+ case a_const
51
+ in ival:
52
+ ival.fetch(:ival, 0)
53
+ in sval:
54
+ sval.fetch(:sval, "").downcase
55
+ else
56
+ a_const.values.first
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecordPgFormatDbStructure
4
- VERSION = "0.1.5"
4
+ VERSION = "0.2.1"
5
5
  end
@@ -3,7 +3,6 @@
3
3
  require_relative "activerecord-pg-format-db-structure/version"
4
4
 
5
5
  require_relative "activerecord-pg-format-db-structure/deparser"
6
- require_relative "activerecord-pg-format-db-structure/preprocessors/remove_whitespaces"
7
6
  require_relative "activerecord-pg-format-db-structure/transforms/remove_comments_on_extensions"
8
7
  require_relative "activerecord-pg-format-db-structure/transforms/inline_serials"
9
8
  require_relative "activerecord-pg-format-db-structure/transforms/inline_primary_keys"
@@ -11,16 +10,14 @@ require_relative "activerecord-pg-format-db-structure/transforms/inline_foreign_
11
10
  require_relative "activerecord-pg-format-db-structure/transforms/move_indices_after_create_table"
12
11
  require_relative "activerecord-pg-format-db-structure/transforms/inline_constraints"
13
12
  require_relative "activerecord-pg-format-db-structure/transforms/group_alter_table_statements"
13
+ require_relative "activerecord-pg-format-db-structure/transforms/remove_defaults_set_commands"
14
14
  require_relative "activerecord-pg-format-db-structure/transforms/sort_schema_migrations"
15
15
  require_relative "activerecord-pg-format-db-structure/transforms/sort_table_columns"
16
16
 
17
17
  module ActiveRecordPgFormatDbStructure
18
- DEFAULT_PREPROCESSORS = [
19
- Preprocessors::RemoveWhitespaces
20
- ].freeze
21
-
22
18
  DEFAULT_TRANSFORMS = [
23
19
  Transforms::RemoveCommentsOnExtensions,
20
+ Transforms::RemoveDefaultsSetCommands,
24
21
  Transforms::SortSchemaMigrations,
25
22
  Transforms::InlinePrimaryKeys,
26
23
  # Transforms::InlineForeignKeys,
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.1.5
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jell
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-02-08 00:00:00.000000000 Z
10
+ date: 2025-02-15 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: pg_query
@@ -42,7 +42,6 @@ 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/remove_whitespaces.rb
46
45
  - lib/activerecord-pg-format-db-structure/railtie.rb
47
46
  - lib/activerecord-pg-format-db-structure/tasks/clean_db_structure.rake
48
47
  - lib/activerecord-pg-format-db-structure/transforms/base.rb
@@ -53,6 +52,7 @@ files:
53
52
  - lib/activerecord-pg-format-db-structure/transforms/inline_serials.rb
54
53
  - lib/activerecord-pg-format-db-structure/transforms/move_indices_after_create_table.rb
55
54
  - lib/activerecord-pg-format-db-structure/transforms/remove_comments_on_extensions.rb
55
+ - lib/activerecord-pg-format-db-structure/transforms/remove_defaults_set_commands.rb
56
56
  - lib/activerecord-pg-format-db-structure/transforms/sort_schema_migrations.rb
57
57
  - lib/activerecord-pg-format-db-structure/transforms/sort_table_columns.rb
58
58
  - lib/activerecord-pg-format-db-structure/version.rb
@@ -1,27 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ActiveRecordPgFormatDbStructure
4
- module Preprocessors
5
- # Remove whitespace and SQL comments from an SQL string
6
- class RemoveWhitespaces
7
- attr_reader :source
8
-
9
- def initialize(source)
10
- @source = source
11
- end
12
-
13
- def preprocess!
14
- # Remove trailing whitespace
15
- source.gsub!(/[ \t]+$/, "")
16
- source.gsub!(/\A\n/, "")
17
- source.gsub!(/\n\n\z/, "\n")
18
-
19
- # Remove useless comment lines
20
- source.gsub!(/^--\n/, "")
21
-
22
- # Remove useless, version-specific parts of comments
23
- source.gsub!(/^-- (.*); Schema: ([\w.]+|-); Owner: -.*/, '-- \1')
24
- end
25
- end
26
- end
27
- end