activerecord-clean-db-structure 0.2.6 → 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
- SHA256:
3
- metadata.gz: d36c390340cb86023df909cb5566610d47881767c746a118307f64c697e1153d
4
- data.tar.gz: bc70ca9657712e25e5b925cc4184142831a83ffd36e103df6043c3713168005e
2
+ SHA1:
3
+ metadata.gz: 6ee2c62ab134b71d0ccce4e02928038c983c4403
4
+ data.tar.gz: 6264e28734c7ca2066fefbd2a6ef3dc68e68c1c1
5
5
  SHA512:
6
- metadata.gz: ffa80995355e8d0e5404ea8f51eb82646e3b41a6c9ab95bd29db194a5fbbcdedce3e71c9f1da67f9ab8f6ca8b32ee336dc701e9f6a732e63b3c86e2a885ec8a7
7
- data.tar.gz: b810eecc4cae33f4a1c0785188282264e0ad43a6975b28bf0bde9a3d1594e624f78f327ec343b9e679debe726d72856b323aef4a3004fb448e5683889d4f512b
6
+ metadata.gz: bf88f74415c68337f2b9f409a358085d4d71c64be4696a942b973a2d34c420024f9bde791f3edb120c1df26357d8c3d0f5d47ed94892437b63ef72b388ae5175
7
+ data.tar.gz: 1b086ae9152601942d7999ab0491c2d5d4e812e34208688f5bd8d8437530d74c868b0ca6a5639b237b2cb9cdd80c3caa1cd7871c601100b586ee748a86fe5def
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.3.0 2019-05-07
4
+
5
+ * Add "ignore_ids" option to allow disabling of primary key substitution logic [#12](https://github.com/lfittl/activerecord-clean-db-structure/pull/12) [Vladimir Dementyev](https://github.com/palkan)
6
+ * Compatibility with Rails 6 multi-database configuration
7
+
8
+
3
9
  ## 0.2.6 2018-03-11
4
10
 
5
11
  * Fix regular expressions to support schema qualification changes in 10.3
data/Gemfile.lock CHANGED
@@ -1,26 +1,26 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- activerecord-clean-db-structure (0.2.6)
4
+ activerecord-clean-db-structure (0.3.0)
5
5
  activerecord (>= 4.2)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- activemodel (5.1.5)
11
- activesupport (= 5.1.5)
12
- activerecord (5.1.5)
13
- activemodel (= 5.1.5)
14
- activesupport (= 5.1.5)
15
- arel (~> 8.0)
16
- activesupport (5.1.5)
10
+ activemodel (5.2.3)
11
+ activesupport (= 5.2.3)
12
+ activerecord (5.2.3)
13
+ activemodel (= 5.2.3)
14
+ activesupport (= 5.2.3)
15
+ arel (>= 9.0)
16
+ activesupport (5.2.3)
17
17
  concurrent-ruby (~> 1.0, >= 1.0.2)
18
- i18n (~> 0.7)
18
+ i18n (>= 0.7, < 2)
19
19
  minitest (~> 5.1)
20
20
  tzinfo (~> 1.1)
21
- arel (8.0.0)
22
- concurrent-ruby (1.0.5)
23
- i18n (0.9.5)
21
+ arel (9.0.0)
22
+ concurrent-ruby (1.1.5)
23
+ i18n (1.6.0)
24
24
  concurrent-ruby (~> 1.0)
25
25
  minitest (5.11.3)
26
26
  rake (0.9.6)
data/README.md CHANGED
@@ -30,7 +30,13 @@ Currently the library assumes all your `id` columns are either SERIAL, BIGSERIAL
30
30
 
31
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
32
 
33
- Patches and test cases welcome!
33
+ You can disable this part of the _cleaning_ process in your `config/environments/<environment>.rb` (or `config/application.rb`):
34
+
35
+ ```ruby
36
+ Rails.application.configure do
37
+ config.activerecord_clean_db_structure.ignore_ids = true
38
+ end
39
+ ```
34
40
 
35
41
  ## Authors
36
42
 
@@ -1,8 +1,10 @@
1
1
  module ActiveRecordCleanDbStructure
2
2
  class CleanDump
3
- attr_reader :dump
4
- def initialize(dump)
3
+ attr_reader :dump, :options
4
+
5
+ def initialize(dump, options = {})
5
6
  @dump = dump
7
+ @options = options
6
8
  end
7
9
 
8
10
  def run
@@ -33,20 +35,22 @@ module ActiveRecordCleanDbStructure
33
35
  # Remove useless comment lines
34
36
  dump.gsub!(/^--$/, '')
35
37
 
36
- # Reduce noise for id fields by making them SERIAL instead of integer+sequence stuff
37
- #
38
- # This is a bit optimistic, but works as long as you don't have an id field thats not a sequence/uuid
39
- dump.gsub!(/^ id integer NOT NULL(,)?$/, ' id SERIAL PRIMARY KEY\1')
40
- dump.gsub!(/^ id bigint NOT NULL(,)?$/, ' id BIGSERIAL PRIMARY KEY\1')
41
- dump.gsub!(/^ id uuid DEFAULT (public\.)?uuid_generate_v4\(\) NOT NULL(,)?$/, ' id uuid DEFAULT \1uuid_generate_v4() PRIMARY KEY\2')
42
- dump.gsub!(/^ id uuid DEFAULT (public\.)?gen_random_uuid\(\) NOT NULL(,)?$/, ' id uuid DEFAULT \1gen_random_uuid() PRIMARY KEY\2')
43
- dump.gsub!(/^CREATE SEQUENCE [\w\.]+_id_seq\s+(AS integer\s+)?START WITH 1\s+INCREMENT BY 1\s+NO MINVALUE\s+NO MAXVALUE\s+CACHE 1;$/, '')
44
- dump.gsub!(/^ALTER SEQUENCE [\w\.]+_id_seq OWNED BY .*;$/, '')
45
- dump.gsub!(/^ALTER TABLE ONLY [\w\.]+ ALTER COLUMN id SET DEFAULT nextval\('[\w\.]+_id_seq'::regclass\);$/, '')
46
- dump.gsub!(/^ALTER TABLE ONLY [\w\.]+\s+ADD CONSTRAINT [\w\.]+_pkey PRIMARY KEY \(id\);$/, '')
47
- dump.gsub!(/^-- Name: (\w+\s+)?id; Type: DEFAULT$/, '')
48
- dump.gsub!(/^-- .*_id_seq; Type: SEQUENCE.*/, '')
49
- dump.gsub!(/^-- Name: (\w+\s+)?\w+_pkey; Type: CONSTRAINT$/, '')
38
+ unless options[:ignore_ids] == true
39
+ # Reduce noise for id fields by making them SERIAL instead of integer+sequence stuff
40
+ #
41
+ # This is a bit optimistic, but works as long as you don't have an id field thats not a sequence/uuid
42
+ dump.gsub!(/^ id integer NOT NULL(,)?$/, ' id SERIAL PRIMARY KEY\1')
43
+ dump.gsub!(/^ id bigint NOT NULL(,)?$/, ' id BIGSERIAL PRIMARY KEY\1')
44
+ dump.gsub!(/^ id uuid DEFAULT (public\.)?uuid_generate_v4\(\) NOT NULL(,)?$/, ' id uuid DEFAULT \1uuid_generate_v4() PRIMARY KEY\2')
45
+ dump.gsub!(/^ id uuid DEFAULT (public\.)?gen_random_uuid\(\) NOT NULL(,)?$/, ' id uuid DEFAULT \1gen_random_uuid() PRIMARY KEY\2')
46
+ dump.gsub!(/^CREATE SEQUENCE [\w\.]+_id_seq\s+(AS integer\s+)?START WITH 1\s+INCREMENT BY 1\s+NO MINVALUE\s+NO MAXVALUE\s+CACHE 1;$/, '')
47
+ dump.gsub!(/^ALTER SEQUENCE [\w\.]+_id_seq OWNED BY .*;$/, '')
48
+ dump.gsub!(/^ALTER TABLE ONLY [\w\.]+ ALTER COLUMN id SET DEFAULT nextval\('[\w\.]+_id_seq'::regclass\);$/, '')
49
+ dump.gsub!(/^ALTER TABLE ONLY [\w\.]+\s+ADD CONSTRAINT [\w\.]+_pkey PRIMARY KEY \(id\);$/, '')
50
+ dump.gsub!(/^-- Name: (\w+\s+)?id; Type: DEFAULT$/, '')
51
+ dump.gsub!(/^-- .*_id_seq; Type: SEQUENCE.*/, '')
52
+ dump.gsub!(/^-- Name: (\w+\s+)?\w+_pkey; Type: CONSTRAINT$/, '')
53
+ end
50
54
 
51
55
  # Remove inherited tables
52
56
  inherited_tables_regexp = /-- Name: ([\w_\.]+); Type: TABLE\n\n[^;]+?INHERITS \([\w_\.]+\);/m
@@ -62,6 +66,27 @@ module ActiveRecordCleanDbStructure
62
66
  dump.gsub!(index_regexp, '')
63
67
  end
64
68
 
69
+ # Remove partitioned tables
70
+ partitioned_tables_regexp = /-- Name: ([\w_\.]+); Type: TABLE\n\n[^;]+?PARTITION OF [\w_\.]+\n[^;]+?;/m
71
+ partitioned_tables = dump.scan(partitioned_tables_regexp).map(&:first)
72
+ dump.gsub!(partitioned_tables_regexp, '')
73
+ partitioned_tables.each do |partitioned_table|
74
+ dump.gsub!(/ALTER TABLE ONLY (public\.)?#{partitioned_table}[^;]+;/, '')
75
+ dump.gsub!(/-- Name: #{partitioned_table} [^;]+; Type: DEFAULT/, '')
76
+
77
+ index_regexp = /CREATE INDEX ([\w_]+) ON (public\.)?#{partitioned_table}[^;]+;/m
78
+ dump.scan(index_regexp).map(&:first).each do |partitioned_table_index|
79
+ dump.gsub!("-- Name: #{partitioned_table_index}; Type: INDEX ATTACH", '')
80
+ dump.gsub!("-- Name: #{partitioned_table_index}; Type: INDEX", '')
81
+ dump.gsub!(/ALTER INDEX ([\w_\.]+) ATTACH PARTITION (public\.)?#{partitioned_table_index};/, '')
82
+ end
83
+ dump.gsub!(index_regexp, '')
84
+
85
+ dump.gsub!(/-- Name: #{partitioned_table}_pkey; Type: INDEX ATTACH\n\n[^;]+?ATTACH PARTITION (public\.)?#{partitioned_table}_pkey;/, '')
86
+ end
87
+ # This is mostly done to allow restoring Postgres 11 output on Postgres 10
88
+ dump.gsub!(/CREATE INDEX ([\w_]+) ON ONLY/, 'CREATE INDEX \\1 ON')
89
+
65
90
  # Remove whitespace between schema migration INSERTS to make editing easier
66
91
  dump.gsub!(/^(INSERT INTO schema_migrations .*)\n\n/, "\\1\n")
67
92
 
@@ -1,5 +1,7 @@
1
1
  module ActiveRecordCleanDbStructure
2
2
  class Railtie < Rails::Railtie
3
+ config.activerecord_clean_db_structure = ActiveSupport::OrderedOptions.new
4
+
3
5
  rake_tasks do
4
6
  load 'activerecord-clean-db-structure/tasks/clean_db_structure.rake'
5
7
  end
@@ -2,12 +2,24 @@ require 'activerecord-clean-db-structure/clean_dump'
2
2
 
3
3
  Rake::Task['db:structure:dump'].enhance do
4
4
  filenames = ENV['DB_STRUCTURE']
5
+
6
+ if ActiveRecord::VERSION::MAJOR >= 6
7
+ ActiveRecord::Tasks::DatabaseTasks.for_each do |spec_name|
8
+ filenames ||= Rails.application.config.paths['db'].map do |path|
9
+ File.join(path, spec_name + '_structure.sql')
10
+ end
11
+ end
12
+ end
13
+
5
14
  filenames ||= Rails.application.config.paths['db'].map do |path|
6
15
  File.join(path, 'structure.sql')
7
16
  end
8
17
 
9
18
  filenames.each do |filename|
10
- cleaner = ActiveRecordCleanDbStructure::CleanDump.new(File.read(filename))
19
+ cleaner = ActiveRecordCleanDbStructure::CleanDump.new(
20
+ File.read(filename),
21
+ **Rails.application.config.activerecord_clean_db_structure
22
+ )
11
23
  cleaner.run
12
24
  File.write(filename, cleaner.dump)
13
25
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveRecordCleanDbStructure
2
- VERSION = '0.2.6'
2
+ VERSION = '0.3.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-clean-db-structure
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lukas Fittl
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-12 00:00:00.000000000 Z
11
+ date: 2019-05-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -77,7 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
77
  version: '0'
78
78
  requirements: []
79
79
  rubyforge_project:
80
- rubygems_version: 2.7.6
80
+ rubygems_version: 2.6.13
81
81
  signing_key:
82
82
  specification_version: 4
83
83
  summary: Automatic cleanup for the Rails db/structure.sql file (ActiveRecord/PostgreSQL)