activerecord-clean-db-structure 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4d348caa1f27c948da0884c8ee3529031ead8486
4
+ data.tar.gz: a2f4f3a742cc2314d501d5e271d0e08299794d5c
5
+ SHA512:
6
+ metadata.gz: 7c375392618769115389a416fa27387e567f89fe16f01cd1823b3b88e8a7cad4ffdd63ee86d97d8a42e78129a8d785ba753b1b17b14d1ef5fe243ffadf90e3ac
7
+ data.tar.gz: 05e165a2a0dd1479cabb35999fa0720154a7229f302834ec4ec4303621561ad85a9cd759f995eee43e137f77aefe4e7aca063fbfc8779c645129a2ae88c0cdff
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ ## 0.0.1 2017-02-12
4
+
5
+ * Initial release.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,116 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ activerecord-clean-db-structure (0.1.0)
5
+ rails (>= 4.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actioncable (5.0.1)
11
+ actionpack (= 5.0.1)
12
+ nio4r (~> 1.2)
13
+ websocket-driver (~> 0.6.1)
14
+ actionmailer (5.0.1)
15
+ actionpack (= 5.0.1)
16
+ actionview (= 5.0.1)
17
+ activejob (= 5.0.1)
18
+ mail (~> 2.5, >= 2.5.4)
19
+ rails-dom-testing (~> 2.0)
20
+ actionpack (5.0.1)
21
+ actionview (= 5.0.1)
22
+ activesupport (= 5.0.1)
23
+ rack (~> 2.0)
24
+ rack-test (~> 0.6.3)
25
+ rails-dom-testing (~> 2.0)
26
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
27
+ actionview (5.0.1)
28
+ activesupport (= 5.0.1)
29
+ builder (~> 3.1)
30
+ erubis (~> 2.7.0)
31
+ rails-dom-testing (~> 2.0)
32
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
33
+ activejob (5.0.1)
34
+ activesupport (= 5.0.1)
35
+ globalid (>= 0.3.6)
36
+ activemodel (5.0.1)
37
+ activesupport (= 5.0.1)
38
+ activerecord (5.0.1)
39
+ activemodel (= 5.0.1)
40
+ activesupport (= 5.0.1)
41
+ arel (~> 7.0)
42
+ activesupport (5.0.1)
43
+ concurrent-ruby (~> 1.0, >= 1.0.2)
44
+ i18n (~> 0.7)
45
+ minitest (~> 5.1)
46
+ tzinfo (~> 1.1)
47
+ arel (7.1.4)
48
+ builder (3.2.3)
49
+ concurrent-ruby (1.0.4)
50
+ erubis (2.7.0)
51
+ globalid (0.3.7)
52
+ activesupport (>= 4.1.0)
53
+ i18n (0.8.0)
54
+ loofah (2.0.3)
55
+ nokogiri (>= 1.5.9)
56
+ mail (2.6.4)
57
+ mime-types (>= 1.16, < 4)
58
+ method_source (0.8.2)
59
+ mime-types (3.1)
60
+ mime-types-data (~> 3.2015)
61
+ mime-types-data (3.2016.0521)
62
+ mini_portile2 (2.1.0)
63
+ minitest (5.10.1)
64
+ nio4r (1.2.1)
65
+ nokogiri (1.7.0.1)
66
+ mini_portile2 (~> 2.1.0)
67
+ rack (2.0.1)
68
+ rack-test (0.6.3)
69
+ rack (>= 1.0)
70
+ rails (5.0.1)
71
+ actioncable (= 5.0.1)
72
+ actionmailer (= 5.0.1)
73
+ actionpack (= 5.0.1)
74
+ actionview (= 5.0.1)
75
+ activejob (= 5.0.1)
76
+ activemodel (= 5.0.1)
77
+ activerecord (= 5.0.1)
78
+ activesupport (= 5.0.1)
79
+ bundler (>= 1.3.0, < 2.0)
80
+ railties (= 5.0.1)
81
+ sprockets-rails (>= 2.0.0)
82
+ rails-dom-testing (2.0.2)
83
+ activesupport (>= 4.2.0, < 6.0)
84
+ nokogiri (~> 1.6)
85
+ rails-html-sanitizer (1.0.3)
86
+ loofah (~> 2.0)
87
+ railties (5.0.1)
88
+ actionpack (= 5.0.1)
89
+ activesupport (= 5.0.1)
90
+ method_source
91
+ rake (>= 0.8.7)
92
+ thor (>= 0.18.1, < 2.0)
93
+ rake (12.0.0)
94
+ sprockets (3.7.1)
95
+ concurrent-ruby (~> 1.0)
96
+ rack (> 1, < 3)
97
+ sprockets-rails (3.2.0)
98
+ actionpack (>= 4.0)
99
+ activesupport (>= 4.0)
100
+ sprockets (>= 3.0.0)
101
+ thor (0.19.4)
102
+ thread_safe (0.3.5)
103
+ tzinfo (1.2.2)
104
+ thread_safe (~> 0.1)
105
+ websocket-driver (0.6.5)
106
+ websocket-extensions (>= 0.1.0)
107
+ websocket-extensions (0.1.2)
108
+
109
+ PLATFORMS
110
+ ruby
111
+
112
+ DEPENDENCIES
113
+ activerecord-clean-db-structure!
114
+
115
+ BUNDLED WITH
116
+ 1.12.5
data/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ Copyright (c) 2017, Lukas Fittl
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice, this
8
+ list of conditions and the following disclaimer.
9
+
10
+ * Redistributions in binary form must reproduce the above copyright notice,
11
+ this list of conditions and the following disclaimer in the documentation
12
+ and/or other materials provided with the distribution.
13
+
14
+ * Neither the name of Lukas Fittl nor the names of its contributors may be used
15
+ to endorse or promote products derived from this software without specific
16
+ prior written permission.
17
+
18
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
+ POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,42 @@
1
+ ## activerecord-clean-db-structure [ ![](https://img.shields.io/gem/v/activerecord-clean-db-structure.svg)](https://rubygems.org/gems/activerecord-clean-db-structure) [ ![](https://img.shields.io/gem/dt/activerecord-clean-db-structure.svg)](https://rubygems.org/gems/activerecord-clean-db-structure)
2
+
3
+ Ever been annoyed at a constantly changing `db/structure.sql` file when using ActiveRecord and Postgres?
4
+
5
+ Spent hours trying to decipher why that one team member keeps changing the file?
6
+
7
+ This library is here to help!
8
+
9
+ It cleans away all the unnecessary output in the file every time its updated automatically. This helps avoid merge conflicts, as well as increase readability.
10
+
11
+ ## Installation
12
+
13
+ Add the following to your Gemfile:
14
+
15
+ ```ruby
16
+ gem 'activerecord-clean-db-structure'
17
+ ```
18
+
19
+ This will automatically hook the library into your `rake db:migrate` task.
20
+
21
+ ## Supported Rails versions
22
+
23
+ Whilst there is no reason this shouldn't work on earlier versions, this has only been tested on Rails 4.2 and newer.
24
+
25
+ It also assumes you use ActiveRecord with PostgreSQL - other ORMs or databases are not supported.
26
+
27
+ ## Caveats
28
+
29
+ Currently the library assumes all your `id` columns are either SERIAL, BIGSERIAL or uuid. It also assumes the `id` is the primary key.
30
+
31
+ Multi-column primary keys, as well as tables that don't have `id` as the primary key are not supported right now, and might lead to wrong output.
32
+
33
+ Patches and test cases welcome!
34
+
35
+ ## Authors
36
+
37
+ * [Lukas Fittl](https://github.com/lfittl)
38
+
39
+ ## License
40
+
41
+ Copyright (c) 2017, Lukas Fittl<br>
42
+ activerecord-clean-db-structure is licensed under the 3-clause BSD license, see LICENSE file for details.
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'bundler/gem_tasks'
@@ -0,0 +1,19 @@
1
+ $:.push File.expand_path('../lib', __FILE__)
2
+ require 'activerecord-clean-db-structure/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'activerecord-clean-db-structure'
6
+ s.version = ActiveRecordCleanDbStructure::VERSION
7
+ s.summary = 'Automatic cleanup for the Rails db/structure.sql file (ActiveRecord/PostgreSQL)'
8
+ s.description = 'Never worry about weird diffs and merge conflicts again'
9
+ s.authors = ['Lukas Fittl']
10
+ s.email = 'lukas@fittl.com'
11
+
12
+ s.files = `git ls-files`.split("\n")
13
+ s.test_files = `git ls-files -- {spec}/*`.split("\n")
14
+ s.require_paths = ['lib']
15
+ s.homepage = 'https://github.com/lfittl/activerecord-clean-db-structure'
16
+ s.license = 'MIT'
17
+
18
+ s.add_dependency('rails','>= 4.2')
19
+ end
@@ -0,0 +1 @@
1
+ require 'activerecord-clean-db-structure/railtie' if defined?(Rails)
@@ -0,0 +1,59 @@
1
+ module ActiveRecordCleanDbStructure
2
+ class CleanDump
3
+ attr_reader :dump
4
+ def initialize(dump)
5
+ @dump = dump
6
+ end
7
+
8
+ def run
9
+ # Remove trailing whitespace
10
+ dump.gsub!(/[ \t]+$/, '')
11
+ dump.gsub!(/\A\n/, '')
12
+ dump.gsub!(/\n\n\z/, "\n")
13
+
14
+ # Remove version-specific output
15
+ dump.gsub!(/^-- Dumped.*/, '')
16
+ dump.gsub!(/^SET row_security = off;$/, '') # 9.5
17
+ dump.gsub!(/^SET idle_in_transaction_session_timeout = 0;$/, '') # 9.6
18
+
19
+ # Remove pg_stat_statements extension (its not relevant to the code)
20
+ dump.gsub!(/^CREATE EXTENSION IF NOT EXISTS pg_stat_statements.*/, '')
21
+ dump.gsub!(/^COMMENT ON EXTENSION pg_stat_statements.*/, '')
22
+ dump.gsub!(/^-- Name: (EXTENSION )?pg_stat_statements;.*/, '')
23
+
24
+ # Remove useless, version-specific parts of comments
25
+ dump.gsub!(/^-- (.*); Schema: (public|-); Owner: -.*/, '-- \1')
26
+
27
+ # Remove useless comment lines
28
+ dump.gsub!(/^--$/, '')
29
+
30
+ # Reduce noise for id fields by making them SERIAL instead of integer+sequence stuff
31
+ #
32
+ # This is a bit optimistic, but works as long as you don't have an id field thats not a sequence/uuid
33
+ dump.gsub!(/^ id integer NOT NULL,$/, ' id SERIAL PRIMARY KEY,')
34
+ dump.gsub!(/^ id bigint NOT NULL,$/, ' id BIGSERIAL PRIMARY KEY,')
35
+ dump.gsub!(/^ id uuid DEFAULT uuid_generate_v4\(\) NOT NULL,$/, ' id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,')
36
+ dump.gsub!(/^CREATE SEQUENCE \w+_id_seq\s+START WITH 1\s+INCREMENT BY 1\s+NO MINVALUE\s+NO MAXVALUE\s+CACHE 1;$/, '')
37
+ dump.gsub!(/^ALTER SEQUENCE \w+_id_seq OWNED BY .*;$/, '')
38
+ dump.gsub!(/^ALTER TABLE ONLY \w+ ALTER COLUMN id SET DEFAULT nextval\('\w+_id_seq'::regclass\);$/, '')
39
+ dump.gsub!(/^ALTER TABLE ONLY \w+\s+ADD CONSTRAINT \w+_pkey PRIMARY KEY \(id\);$/, '')
40
+ dump.gsub!(/^-- Name: [\w_]+ id; Type: DEFAULT$/, '')
41
+ dump.gsub!(/^-- .*_id_seq; Type: SEQUENCE.*/, '')
42
+ dump.gsub!(/^-- Name: [\w_]+ \w+_pkey; Type: CONSTRAINT$/, '')
43
+
44
+ # Remove inherited tables
45
+ inherited_tables_regexp = /-- Name: ([\w_]+); Type: TABLE\n\n[^;]+?INHERITS \([\w_]+\);/m
46
+ inherited_tables = dump.scan(inherited_tables_regexp).map(&:first)
47
+ dump.gsub!(inherited_tables_regexp, '')
48
+ inherited_tables.each do |inherited_table|
49
+ dump.gsub!(/ALTER TABLE ONLY #{inherited_table}[^;]+;/, '')
50
+ end
51
+
52
+ # Remove whitespace between schema migration INSERTS to make editing easier
53
+ dump.gsub!(/^(INSERT INTO schema_migrations .*)\n\n/, "\\1\n")
54
+
55
+ # Reduce 2+ lines of whitespace to one line of whitespace
56
+ dump.gsub!(/\n{2,}/m, "\n\n")
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,7 @@
1
+ module ActiveRecordCleanDbStructure
2
+ class Railtie < Rails::Railtie
3
+ rake_tasks do
4
+ load 'activerecord-clean-db-structure/tasks/clean_db_structure.rake'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,13 @@
1
+ require 'activerecord-clean-db-structure/clean_dump'
2
+
3
+ Rake::Task['db:structure:dump'].enhance do
4
+ Rake::Task['clean_db_structure'].invoke
5
+ end
6
+
7
+ task :clean_db_structure do
8
+ filename = ENV['DB_STRUCTURE'] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, 'structure.sql')
9
+
10
+ cleaner = ActiveRecordCleanDbStructure::CleanDump.new(File.read(filename))
11
+ cleaner.run
12
+ File.write(filename, cleaner.dump)
13
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveRecordCleanDbStructure
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-clean-db-structure
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Lukas Fittl
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-02-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ description: Never worry about weird diffs and merge conflicts again
28
+ email: lukas@fittl.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - CHANGELOG.md
34
+ - Gemfile
35
+ - Gemfile.lock
36
+ - LICENSE
37
+ - README.md
38
+ - Rakefile
39
+ - activerecord-clean-db-structure.gemspec
40
+ - lib/activerecord-clean-db-structure.rb
41
+ - lib/activerecord-clean-db-structure/clean_dump.rb
42
+ - lib/activerecord-clean-db-structure/railtie.rb
43
+ - lib/activerecord-clean-db-structure/tasks/clean_db_structure.rake
44
+ - lib/activerecord-clean-db-structure/version.rb
45
+ homepage: https://github.com/lfittl/activerecord-clean-db-structure
46
+ licenses:
47
+ - MIT
48
+ metadata: {}
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project:
65
+ rubygems_version: 2.4.5.1
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: Automatic cleanup for the Rails db/structure.sql file (ActiveRecord/PostgreSQL)
69
+ test_files: []