activerecord-pg-format-db-structure 0.1.5 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +12 -17
- data/lib/activerecord-pg-format-db-structure/formatter.rb +1 -7
- data/lib/activerecord-pg-format-db-structure/railtie.rb +0 -1
- data/lib/activerecord-pg-format-db-structure/version.rb +1 -1
- data/lib/activerecord-pg-format-db-structure.rb +0 -5
- metadata +2 -3
- data/lib/activerecord-pg-format-db-structure/preprocessors/remove_whitespaces.rb +0 -27
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2a0b4f6730ac7a280afddd46b476a8443e4cbbb3eac4004b92692223437f432c
|
4
|
+
data.tar.gz: b23c3a6ed71d6c46efa516d21f4c546dbdd3d2638f7c60046af0ff015d576f4d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f74568c5dcdecd2b0fe20d0823167e97a59da8593d534f877370b8e91b333a1a1c397c37913d5c81e91d8ccdfd254fc594f930ea1a0dab1ada589815ed8c38b5
|
7
|
+
data.tar.gz: 0eec46264e79d1ff729e12bef71bf6fd90291dbaff13ed88ae259f4e26b15cf7584ddb91a581d05d00df6c5b1ce4add028b4dc5f1318b32f89ef34dbbb8b0ddb
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -3,7 +3,9 @@
|
|
3
3
|

|
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
|
-
*
|
19
|
+
* Format and indent the entire file consistently
|
20
|
+
|
21
|
+
It can also optionally inline foreign key declarations (see below).
|
18
22
|
|
19
|
-
|
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
|
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
|
-
|
234
|
-
getting random diffs between machines after each
|
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-
|
253
|
+
gem 'activerecord-pg-format-db-structure'
|
249
254
|
```
|
250
255
|
|
251
256
|
## Usage
|
@@ -258,10 +263,6 @@ 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,
|
267
268
|
ActiveRecordPgFormatDbStructure::Transforms::SortSchemaMigrations,
|
@@ -288,12 +289,6 @@ formatted = ActiveRecordPgFormatDbStructure::Formatter.new.format(structure)
|
|
288
289
|
File.write("db/structure.sql", formatted)
|
289
290
|
```
|
290
291
|
|
291
|
-
## Preprocessors
|
292
|
-
|
293
|
-
### RemoveWhitespaces
|
294
|
-
|
295
|
-
Remove unnecessary comment, whitespase and empty lines.
|
296
|
-
|
297
292
|
## Transformers
|
298
293
|
|
299
294
|
### RemoveCommentsOnExtensions
|
@@ -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 :
|
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
|
|
@@ -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"
|
@@ -15,10 +14,6 @@ require_relative "activerecord-pg-format-db-structure/transforms/sort_schema_mig
|
|
15
14
|
require_relative "activerecord-pg-format-db-structure/transforms/sort_table_columns"
|
16
15
|
|
17
16
|
module ActiveRecordPgFormatDbStructure
|
18
|
-
DEFAULT_PREPROCESSORS = [
|
19
|
-
Preprocessors::RemoveWhitespaces
|
20
|
-
].freeze
|
21
|
-
|
22
17
|
DEFAULT_TRANSFORMS = [
|
23
18
|
Transforms::RemoveCommentsOnExtensions,
|
24
19
|
Transforms::SortSchemaMigrations,
|
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.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jell
|
8
8
|
bindir: exe
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-02-
|
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
|
@@ -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
|