activerecord-pg-format-db-structure 0.2.1 → 0.4.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 +4 -4
- data/.rubocop.yml +3 -1
- data/CHANGELOG.md +8 -0
- data/README.md +4 -4
- data/lib/activerecord-pg-format-db-structure/deparser.rb +6 -6
- data/lib/activerecord-pg-format-db-structure/formatter.rb +15 -5
- data/lib/activerecord-pg-format-db-structure/railtie.rb +1 -0
- data/lib/activerecord-pg-format-db-structure/statement_appender.rb +57 -0
- data/lib/activerecord-pg-format-db-structure/transforms/inline_constraints.rb +1 -1
- data/lib/activerecord-pg-format-db-structure/version.rb +1 -1
- data/lib/activerecord-pg-format-db-structure.rb +2 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 74eff8ba4546f71002cc784146845e66b74a38cfdfe6f2604b6ee34994269564
|
4
|
+
data.tar.gz: 330f056b6883d6dbbfc253ac9230794756f0a45b19527ecdb8af3005b01dc55e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 64f5bcc1ca4e2b6033b83acc918372e20c19765315f7c03bfb8666190d255a7e9c8bb4fde73c28ce2c2af109423bc739af0938e638c003bb6dabdcb277a12861
|
7
|
+
data.tar.gz: 30fb3d9e684ac251b20f6eee82a791c075173cf2385f8e8c69b14e726ceda554cc6ab768f8bca0fea6f4fedc1b08d7b34a71c64d0002fbff95b8d32f8951ae06
|
data/.rubocop.yml
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
plugins:
|
2
2
|
- rubocop-rake
|
3
3
|
- rubocop-rspec
|
4
4
|
|
@@ -119,6 +119,8 @@ Lint/UnexpectedBlockArity: # new in 1.5
|
|
119
119
|
Enabled: true
|
120
120
|
Lint/UnmodifiedReduceAccumulator: # new in 1.1
|
121
121
|
Enabled: true
|
122
|
+
Lint/UselessConstantScoping:
|
123
|
+
Enabled: false
|
122
124
|
Lint/UselessDefined: # new in 1.69
|
123
125
|
Enabled: true
|
124
126
|
Lint/UselessNumericOperation: # new in 1.66
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.4.0] - 2025-03-21
|
4
|
+
|
5
|
+
- Remove no-op `NOT VALID` setting when inlining constraints
|
6
|
+
|
7
|
+
## [0.3.0] - 2025-03-15
|
8
|
+
|
9
|
+
- Introduce StatementAppender for better spacing between statements
|
10
|
+
|
3
11
|
## [0.2.1] - 2025-02-15
|
4
12
|
|
5
13
|
- 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
|
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
|
|
@@ -349,7 +347,9 @@ sequence indeed has default settings.
|
|
349
347
|
|
350
348
|
### InlineConstraints
|
351
349
|
|
352
|
-
Inline non-foreign key constraints into table declaration
|
350
|
+
Inline non-foreign key constraints into table declaration.
|
351
|
+
|
352
|
+
Note that this also remove the `NOT VALID` setting if present, since that's a no-op when the constraint is created at the same time as the table.
|
353
353
|
|
354
354
|
### MoveIndicesAfterCreateTable
|
355
355
|
|
@@ -32,42 +32,42 @@ module ActiveRecordPgFormatDbStructure
|
|
32
32
|
private
|
33
33
|
|
34
34
|
def deparse_stmt_generic(stmt)
|
35
|
-
generic_str = +"
|
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 = +"
|
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 = +"
|
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 = "
|
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 = "
|
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 = "
|
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
|
-
|
27
|
-
|
28
|
-
|
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
|
@@ -64,7 +64,7 @@ module ActiveRecordPgFormatDbStructure
|
|
64
64
|
|
65
65
|
def add_constraint!(raw_statement, constraint)
|
66
66
|
raw_statement.stmt.create_stmt.table_elts << PgQuery::Node.from(
|
67
|
-
PgQuery::Constraint.new(constraint)
|
67
|
+
PgQuery::Constraint.new(constraint.merge(skip_validation: nil))
|
68
68
|
)
|
69
69
|
end
|
70
70
|
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.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jell
|
8
8
|
bindir: exe
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-
|
10
|
+
date: 2025-03-21 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
|