Skip to content

Commit 1ff7f99

Browse files
committed
Initial release.
0 parents  commit 1ff7f99

File tree

12 files changed

+299
-0
lines changed

12 files changed

+299
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Changelog
2+
3+
## 0.0.1 2017-02-12
4+
5+
* Initial release.

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source 'https://rubygems.org'
2+
3+
gemspec

Gemfile.lock

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

LICENSE

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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.

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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.

Rakefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require 'rubygems'
2+
require 'bundler/setup'
3+
require 'bundler/gem_tasks'
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require 'activerecord-clean-db-structure/railtie' if defined?(Rails)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 commit comments

Comments
 (0)