activerecord-pg-format-db-structure 0.2.1 → 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: 476baa5e7f08de7b010ef98240bf4dc9d5f92d8c381759682dfdcbdd33ba5723
4
- data.tar.gz: 8a9337bff151ccadbfc77cb638aca3b1097831979206cf971d4e3af4b3258c7c
3
+ metadata.gz: a18ecc82be3d91f7163c58a3e585c2fa4792767cf07467d3bee7e741fac2281b
4
+ data.tar.gz: 33def417b36f7f11eba8fce6d57f270aaf29b1320e6cf75b3a183719493bb2b5
5
5
  SHA512:
6
- metadata.gz: a6c728fe8aba0d6c943ff8ac5cf16d386099ea85f4a35c101eed701b899ab3ec266b9004313007bba2d5e2567273f5aedfff857567743ea64c1a2d5bdaa1961c
7
- data.tar.gz: 17b9b5722ac383a92969640cf30a6a68aaee073eec784cbe8545eae6f85d983a5fc7a5b2bebd66755bcf097340a8e931a4e831cc298647759f039078b51663f4
6
+ metadata.gz: a9a16f498480f55da503970918cf3a6df4e1899dd0c9331faa684b12ddbb32f1bde3935d7becae37f3e7f9e15190e0939f9587348c5786031c1c1edbf7cd7cfb
7
+ data.tar.gz: a1cead05ba2829d0fee3364e097495f7c100cc84cee09bfe2ae08e8b8a23d99ec5a16500af9d0611f5bf4f9db2b3fcc70ed492e1acc508aa2aa038e5db55de8e
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.3.0] - 2025-03-15
4
+
5
+ - Introduce StatementAppender for better spacing between statements
6
+
3
7
  ## [0.2.1] - 2025-02-15
4
8
 
5
9
  - Add transform to remove SET commands with default values
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
 
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecordPgFormatDbStructure
4
- VERSION = "0.2.1"
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"
@@ -29,6 +30,7 @@ module ActiveRecordPgFormatDbStructure
29
30
  ].freeze
30
31
 
31
32
  DEFAULT_DEPARSER = Deparser
33
+ DEFAULT_STATEMENT_APPENDER = StatementAppender
32
34
  end
33
35
 
34
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.1
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