activerecord-pg-format-db-structure 0.2.0 → 0.3.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: 2a0b4f6730ac7a280afddd46b476a8443e4cbbb3eac4004b92692223437f432c
4
- data.tar.gz: b23c3a6ed71d6c46efa516d21f4c546dbdd3d2638f7c60046af0ff015d576f4d
3
+ metadata.gz: a18ecc82be3d91f7163c58a3e585c2fa4792767cf07467d3bee7e741fac2281b
4
+ data.tar.gz: 33def417b36f7f11eba8fce6d57f270aaf29b1320e6cf75b3a183719493bb2b5
5
5
  SHA512:
6
- metadata.gz: f74568c5dcdecd2b0fe20d0823167e97a59da8593d534f877370b8e91b333a1a1c397c37913d5c81e91d8ccdfd254fc594f930ea1a0dab1ada589815ed8c38b5
7
- data.tar.gz: 0eec46264e79d1ff729e12bef71bf6fd90291dbaff13ed88ae259f4e26b15cf7584ddb91a581d05d00df6c5b1ce4add028b4dc5f1318b32f89ef34dbbb8b0ddb
6
+ metadata.gz: a9a16f498480f55da503970918cf3a6df4e1899dd0c9331faa684b12ddbb32f1bde3935d7becae37f3e7f9e15190e0939f9587348c5786031c1c1edbf7cd7cfb
7
+ data.tar.gz: a1cead05ba2829d0fee3364e097495f7c100cc84cee09bfe2ae08e8b8a23d99ec5a16500af9d0611f5bf4f9db2b3fcc70ed492e1acc508aa2aa038e5db55de8e
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.3.0] - 2025-03-15
4
+
5
+ - Introduce StatementAppender for better spacing between statements
6
+
7
+ ## [0.2.1] - 2025-02-15
8
+
9
+ - Add transform to remove SET commands with default values
10
+
3
11
  ## [0.2.0] - 2025-02-15
4
12
 
5
13
  - Remove preprocessors (no longer relevant now that we don't reuse the source string in the output)
data/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Automatically cleans up your PostgreSQL `structure.sql` file after each rails migration.
7
7
 
8
- Say good-bye to small those small diffs you get between coworkers!
8
+ Say good-bye to those pesky diffs you get between coworkers!
9
9
 
10
10
  By default, it will:
11
11
 
@@ -191,8 +191,6 @@ INSERT INTO "schema_migrations" (version) VALUES
191
191
  into this normalize (and much more compatch & readable) version:
192
192
 
193
193
  ```sql
194
-
195
-
196
194
  CREATE EXTENSION IF NOT EXISTS pgcrypto SCHEMA public;
197
195
 
198
196
 
@@ -265,6 +263,7 @@ If you want to configure which transforms to use, you can configure the library
265
263
  Rails.application.configure do
266
264
  config.activerecord_pg_format_db_structure.transforms = [
267
265
  ActiveRecordPgFormatDbStructure::Transforms::RemoveCommentsOnExtensions,
266
+ ActiveRecordPgFormatDbStructure::Transforms::RemoveDefaultsSetCommands,
268
267
  ActiveRecordPgFormatDbStructure::Transforms::SortSchemaMigrations,
269
268
  ActiveRecordPgFormatDbStructure::Transforms::InlinePrimaryKeys,
270
269
  # ActiveRecordPgFormatDbStructure::Transforms::InlineForeignKeys,
@@ -295,6 +294,27 @@ File.write("db/structure.sql", formatted)
295
294
 
296
295
  Remove COMMENT statement applied to extensions
297
296
 
297
+ ### RemoveDefaultsSetCommands
298
+
299
+ Remove SET commands that apply default values to postgres settings. By default, the following defaults are handled:
300
+
301
+ ```ruby
302
+ ActiveRecordPgFormatDbStructure::Transforms::RemoveDefaultsSetCommands.postgres_config_defaults = {
303
+ default_table_access_method: "heap",
304
+ default_with_oids: false,
305
+ idle_in_transaction_session_timeout: 0,
306
+ lock_timeout: 0,
307
+ statement_timeout: 0,
308
+ transaction_timeout: 0,
309
+ standard_conforming_strings: true,
310
+ xmloption: "content"
311
+ }
312
+ ```
313
+
314
+ Which are the default values since Postgres 9.1. You can make changes
315
+ to the above config in case you want to handle more cases.
316
+
317
+
298
318
  ### SortSchemaMigrations
299
319
 
300
320
  Sort schema_migrations inserts to be in chronological order, helps with reducing merge conflicts.
@@ -32,42 +32,42 @@ module ActiveRecordPgFormatDbStructure
32
32
  private
33
33
 
34
34
  def deparse_stmt_generic(stmt)
35
- generic_str = +"\n\n"
35
+ generic_str = +""
36
36
  generic_str << deparse_stmt_and_indent(stmt)
37
37
  generic_str << ";"
38
38
  generic_str
39
39
  end
40
40
 
41
41
  def deparse_stmt_compact(stmt)
42
- compact_str = +"\n"
42
+ compact_str = +""
43
43
  compact_str << deparse_stmt(stmt)
44
44
  compact_str << ";"
45
45
  compact_str
46
46
  end
47
47
 
48
48
  def deparse_insert_stmt(stmt)
49
- insert_str = +"\n\n\n"
49
+ insert_str = +""
50
50
  insert_str << deparse_stmt_and_indent(stmt)
51
51
  insert_str << "\n;"
52
52
  insert_str
53
53
  end
54
54
 
55
55
  def deparse_create_stmt(stmt)
56
- table_str = "\n\n\n-- Name: #{stmt.relation.relname}; Type: TABLE;\n\n"
56
+ table_str = "-- Name: #{stmt.relation.relname}; Type: TABLE;\n\n"
57
57
  table_str << deparse_stmt_and_indent(stmt)
58
58
  table_str << ";"
59
59
  table_str
60
60
  end
61
61
 
62
62
  def deparse_view_stmt(stmt)
63
- table_str = "\n\n\n-- Name: #{stmt.view.relname}; Type: VIEW;\n\n"
63
+ table_str = "-- Name: #{stmt.view.relname}; Type: VIEW;\n\n"
64
64
  table_str << deparse_stmt_and_indent(stmt)
65
65
  table_str << ";"
66
66
  table_str
67
67
  end
68
68
 
69
69
  def deparse_create_table_as_stmt(stmt)
70
- table_str = "\n\n\n-- Name: #{stmt.into.rel.relname}; Type: MATERIALIZED VIEW;\n\n"
70
+ table_str = "-- Name: #{stmt.into.rel.relname}; Type: MATERIALIZED VIEW;\n\n"
71
71
  table_str << deparse_stmt_and_indent(stmt)
72
72
 
73
73
  # couldn't find a better solution for this, but probably an OK workaround?
@@ -6,14 +6,16 @@ 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
9
+ attr_reader :transforms, :deparser, :statement_appender
10
10
 
11
11
  def initialize(
12
12
  transforms: DEFAULT_TRANSFORMS,
13
- deparser: DEFAULT_DEPARSER
13
+ deparser: DEFAULT_DEPARSER,
14
+ statement_appender: DEFAULT_STATEMENT_APPENDER
14
15
  )
15
16
  @transforms = transforms
16
17
  @deparser = deparser
18
+ @statement_appender = statement_appender
17
19
  end
18
20
 
19
21
  def format(source)
@@ -23,9 +25,17 @@ module ActiveRecordPgFormatDbStructure
23
25
  transform.new(raw_statements).transform!
24
26
  end
25
27
 
26
- raw_statements.map do |raw_statement|
27
- deparser.new(source).deparse_raw_statement(raw_statement)
28
- end.compact.join
28
+ appender = statement_appender.new
29
+ raw_statements.each do |raw_statement|
30
+ statement = deparser.new(source).deparse_raw_statement(raw_statement)
31
+ appender.append_statement!(
32
+ statement,
33
+ statement_kind: PgQuery::Node.inner_class_to_name(
34
+ raw_statement.stmt.inner.class
35
+ )
36
+ )
37
+ end
38
+ appender.output
29
39
  end
30
40
  end
31
41
  end
@@ -6,6 +6,7 @@ module ActiveRecordPgFormatDbStructure
6
6
  config.activerecord_pg_format_db_structure = ActiveSupport::OrderedOptions.new
7
7
  config.activerecord_pg_format_db_structure.transforms = DEFAULT_TRANSFORMS.dup
8
8
  config.activerecord_pg_format_db_structure.deparser = DEFAULT_DEPARSER
9
+ config.activerecord_pg_format_db_structure.statement_appender = DEFAULT_STATEMENT_APPENDER
9
10
 
10
11
  rake_tasks do
11
12
  load "activerecord-pg-format-db-structure/tasks/clean_db_structure.rake"
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pg_query"
4
+ require_relative "indenter"
5
+
6
+ module ActiveRecordPgFormatDbStructure
7
+ # Appends statements with reasonable spacing in-between
8
+ class StatementAppender
9
+ attr_reader :output
10
+
11
+ def initialize(output = +"")
12
+ @output = output
13
+ @previous_statement_kind = nil
14
+ end
15
+
16
+ def append_statement!(statement, statement_kind:)
17
+ output.chomp!
18
+ output << newlines_separator(
19
+ previous_kind: @previous_statement_kind,
20
+ current_kind: statement_kind
21
+ )
22
+ @previous_statement_kind = statement_kind
23
+ output << statement
24
+ output << "\n"
25
+ end
26
+
27
+ private
28
+
29
+ def newlines_separator(previous_kind:, current_kind:)
30
+ case [
31
+ previous_kind,
32
+ current_kind
33
+ ]
34
+ in [
35
+ nil,
36
+ _
37
+ ]
38
+ ""
39
+ in [
40
+ _,
41
+ :insert_stmt | :create_stmt | :view_stmt | :create_table_as_stmt
42
+ ]
43
+ "\n\n\n"
44
+ in [
45
+ :create_stmt | :view_stmt | :create_table_as_stmt | :index_stmt,
46
+ :index_stmt
47
+ ] | [
48
+ :variable_set_stmt,
49
+ :variable_set_stmt
50
+ ]
51
+ "\n"
52
+ else
53
+ "\n\n"
54
+ end
55
+ end
56
+ end
57
+ end
@@ -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.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
@@ -3,6 +3,7 @@
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/statement_appender"
6
7
  require_relative "activerecord-pg-format-db-structure/transforms/remove_comments_on_extensions"
7
8
  require_relative "activerecord-pg-format-db-structure/transforms/inline_serials"
8
9
  require_relative "activerecord-pg-format-db-structure/transforms/inline_primary_keys"
@@ -10,12 +11,14 @@ require_relative "activerecord-pg-format-db-structure/transforms/inline_foreign_
10
11
  require_relative "activerecord-pg-format-db-structure/transforms/move_indices_after_create_table"
11
12
  require_relative "activerecord-pg-format-db-structure/transforms/inline_constraints"
12
13
  require_relative "activerecord-pg-format-db-structure/transforms/group_alter_table_statements"
14
+ require_relative "activerecord-pg-format-db-structure/transforms/remove_defaults_set_commands"
13
15
  require_relative "activerecord-pg-format-db-structure/transforms/sort_schema_migrations"
14
16
  require_relative "activerecord-pg-format-db-structure/transforms/sort_table_columns"
15
17
 
16
18
  module ActiveRecordPgFormatDbStructure
17
19
  DEFAULT_TRANSFORMS = [
18
20
  Transforms::RemoveCommentsOnExtensions,
21
+ Transforms::RemoveDefaultsSetCommands,
19
22
  Transforms::SortSchemaMigrations,
20
23
  Transforms::InlinePrimaryKeys,
21
24
  # Transforms::InlineForeignKeys,
@@ -27,6 +30,7 @@ module ActiveRecordPgFormatDbStructure
27
30
  ].freeze
28
31
 
29
32
  DEFAULT_DEPARSER = Deparser
33
+ DEFAULT_STATEMENT_APPENDER = StatementAppender
30
34
  end
31
35
 
32
36
  # :nocov:
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.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jell
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-02-15 00:00:00.000000000 Z
10
+ date: 2025-03-15 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: pg_query
@@ -43,6 +43,7 @@ files:
43
43
  - lib/activerecord-pg-format-db-structure/formatter.rb
44
44
  - lib/activerecord-pg-format-db-structure/indenter.rb
45
45
  - lib/activerecord-pg-format-db-structure/railtie.rb
46
+ - lib/activerecord-pg-format-db-structure/statement_appender.rb
46
47
  - lib/activerecord-pg-format-db-structure/tasks/clean_db_structure.rake
47
48
  - lib/activerecord-pg-format-db-structure/transforms/base.rb
48
49
  - lib/activerecord-pg-format-db-structure/transforms/group_alter_table_statements.rb
@@ -52,6 +53,7 @@ files:
52
53
  - lib/activerecord-pg-format-db-structure/transforms/inline_serials.rb
53
54
  - lib/activerecord-pg-format-db-structure/transforms/move_indices_after_create_table.rb
54
55
  - lib/activerecord-pg-format-db-structure/transforms/remove_comments_on_extensions.rb
56
+ - lib/activerecord-pg-format-db-structure/transforms/remove_defaults_set_commands.rb
55
57
  - lib/activerecord-pg-format-db-structure/transforms/sort_schema_migrations.rb
56
58
  - lib/activerecord-pg-format-db-structure/transforms/sort_table_columns.rb
57
59
  - lib/activerecord-pg-format-db-structure/version.rb