Skip to content

Commit 86a8d45

Browse files
committed
remove comments, replace fdw table to native table
2 parents c1c5f04 + 022237e commit 86a8d45

File tree

5 files changed

+85
-26
lines changed

5 files changed

+85
-26
lines changed

CHANGELOG.md

+33
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,38 @@
11
# Changelog
22

3+
## 0.2.6 2018-03-11
4+
5+
* Fix regular expressions to support schema qualification changes in 10.3
6+
7+
8+
## 0.2.5 2017-11-15
9+
10+
* Filter out indices belonging partitioned tables
11+
12+
13+
## 0.2.4 2017-11-02
14+
15+
* Remove pg_buffercache extension if present (its only used for statistics purposes)
16+
* Remove extension comments if present - they can prevent non-superusers from
17+
restoring the tables, and are never used together with Rails anyway
18+
19+
20+
## 0.2.3 2017-10-21
21+
22+
* pg 10.x adds AS Integer to structure.sql format [Nathan Woodhull](https://github.com/woodhull)
23+
24+
25+
## 0.2.2 2017-08-05
26+
27+
* Support Rails 5.1 primary key UUIDs that rely on gen_random_uuid()
28+
29+
30+
## 0.2.1 2017-06-30
31+
32+
* Allow primary keys to be the last column of a table [Clemens Kofler](https://github.com/clemens)
33+
- Special thanks to [Jon Mohrbacher](https://github.com/johnnymo87) who submitted a similar earlier change
34+
35+
336
## 0.2.0 2017-03-20
437

538
* Reduce dependencies to only require ActiveRecord [Mario Uher](https://github.com/ream88)

Gemfile.lock

+14-11
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,39 @@
11
PATH
22
remote: .
33
specs:
4-
activerecord-clean-db-structure (0.2.1)
4+
activerecord-clean-db-structure (0.2.6)
55
activerecord (>= 4.2)
66

77
GEM
88
remote: https://rubygems.org/
99
specs:
10-
activemodel (5.1.2)
11-
activesupport (= 5.1.2)
12-
activerecord (5.1.2)
13-
activemodel (= 5.1.2)
14-
activesupport (= 5.1.2)
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)
1515
arel (~> 8.0)
16-
activesupport (5.1.2)
16+
activesupport (5.1.5)
1717
concurrent-ruby (~> 1.0, >= 1.0.2)
1818
i18n (~> 0.7)
1919
minitest (~> 5.1)
2020
tzinfo (~> 1.1)
2121
arel (8.0.0)
2222
concurrent-ruby (1.0.5)
23-
i18n (0.8.4)
24-
minitest (5.10.2)
23+
i18n (0.9.5)
24+
concurrent-ruby (~> 1.0)
25+
minitest (5.11.3)
26+
rake (0.9.6)
2527
thread_safe (0.3.6)
26-
tzinfo (1.2.3)
28+
tzinfo (1.2.5)
2729
thread_safe (~> 0.1)
2830

2931
PLATFORMS
3032
ruby
3133

3234
DEPENDENCIES
3335
activerecord-clean-db-structure!
36+
rake (~> 0)
3437

3538
BUNDLED WITH
36-
1.15.1
39+
1.16.1

activerecord-clean-db-structure.gemspec

+2
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ Gem::Specification.new do |s|
1616
s.license = 'MIT'
1717

1818
s.add_dependency('activerecord', '>= 4.2')
19+
20+
s.add_development_dependency 'rake', '~> 0'
1921
end

lib/activerecord-clean-db-structure/clean_dump.rb

+35-14
Original file line numberDiff line numberDiff line change
@@ -18,40 +18,61 @@ def run
1818

1919
# Remove pg_stat_statements extension (its not relevant to the code)
2020
dump.gsub!(/^CREATE EXTENSION IF NOT EXISTS pg_stat_statements.*/, '')
21-
dump.gsub!(/^COMMENT ON EXTENSION pg_stat_statements.*/, '')
2221
dump.gsub!(/^-- Name: (EXTENSION )?pg_stat_statements;.*/, '')
2322

23+
# Remove pg_buffercache extension (its not relevant to the code)
24+
dump.gsub!(/^CREATE EXTENSION IF NOT EXISTS pg_buffercache.*/, '')
25+
dump.gsub!(/^-- Name: (EXTENSION )?pg_buffercache;.*/, '')
26+
27+
# Remove comments on extensions, they create problems if the extension is owned by another user
28+
dump.gsub!(/^COMMENT ON EXTENSION .*/, '')
29+
2430
# Remove useless, version-specific parts of comments
2531
dump.gsub!(/^-- (.*); Schema: (public|-); Owner: -.*/, '-- \1')
2632

2733
# Remove useless comment lines
2834
dump.gsub!(/^--$/, '')
2935

30-
# Mask user mapping
31-
dump.gsub!(/^CREATE USER MAPPING FOR \w+ SERVER (\w+) .*?;/m, 'CREATE USER MAPPING FOR some_user SERVER \1;')
32-
dump.gsub!(/^-- Name: USER MAPPING \w+ SERVER (\w+); Type: USER MAPPING/, '-- Name: USER MAPPING some_user SERVER \1; Type: USER MAPPING')
36+
# remove comments
37+
dump.gsub!(/^-- Name.+ Type: (COMMENT|EXTENSION|INDEX|TABLE|TYPE)$/, '')
38+
39+
### NEED FOR TEST DATABASE `rails db:setup`
40+
# remove server and user for FDW
41+
dump.gsub!(/^-- Name: [\w ]+; Type: SERVER.+?;/m, '')
42+
dump.gsub!(/^-- Name: [\w ]+; Type: USER MAPPING.+?;/m, '')
43+
44+
# FDW table to native table
45+
dump.gsub!(/^CREATE FOREIGN TABLE (.+?)\nSERVER.+?;/m, "CREATE TABLE \\1;")
46+
###
3347

3448
# Reduce noise for id fields by making them SERIAL instead of integer+sequence stuff
3549
#
3650
# This is a bit optimistic, but works as long as you don't have an id field thats not a sequence/uuid
37-
dump.gsub!(/^CREATE TABLE (\w+) \(\n id integer NOT NULL(.*?);/m, "CREATE TABLE \\1 (\n id SERIAL PRIMARY KEY\\2;")
38-
dump.gsub!(/^CREATE TABLE (\w+) \(\n id bigint NOT NULL(.*?);/m, "CREATE TABLE \\1 (\n id BIGSERIAL PRIMARY KEY\\2;")
39-
40-
dump.gsub!(/^ id uuid DEFAULT uuid_generate_v4\(\) NOT NULL,$/, ' id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,')
41-
dump.gsub!(/^CREATE SEQUENCE \w+_id_seq\s+START WITH 1\s+INCREMENT BY 1\s+NO MINVALUE\s+NO MAXVALUE\s+CACHE 1;$/, '')
42-
dump.gsub!(/^ALTER SEQUENCE \w+_id_seq OWNED BY .*;$/, '')
43-
dump.gsub!(/^ALTER TABLE ONLY \w+ ALTER COLUMN id SET DEFAULT nextval\('\w+_id_seq'::regclass\);$/, '')
44-
dump.gsub!(/^ALTER TABLE ONLY \w+\s+ADD CONSTRAINT \w+_pkey PRIMARY KEY \(id\);$/, '')
51+
dump.gsub!(/^CREATE TABLE ([\w.]+) \(\n id integer NOT NULL(.*?);/m, "CREATE TABLE \\1 (\n id SERIAL PRIMARY KEY\\2;")
52+
dump.gsub!(/^CREATE TABLE ([\w.]+) \(\n id bigint NOT NULL(.*?);/m, "CREATE TABLE \\1 (\n id BIGSERIAL PRIMARY KEY\\2;")
53+
54+
dump.gsub!(/^ id uuid DEFAULT (public\.)?uuid_generate_v4\(\) NOT NULL(,)?$/, ' id uuid DEFAULT \1uuid_generate_v4() PRIMARY KEY\2')
55+
dump.gsub!(/^ id uuid DEFAULT (public\.)?gen_random_uuid\(\) NOT NULL(,)?$/, ' id uuid DEFAULT \1gen_random_uuid() PRIMARY KEY\2')
56+
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;$/, '')
57+
dump.gsub!(/^ALTER SEQUENCE [\w\.]+_id_seq OWNED BY .*;$/, '')
58+
dump.gsub!(/^ALTER TABLE ONLY [\w\.]+ ALTER COLUMN id SET DEFAULT nextval\('[\w\.]+_id_seq'::regclass\);$/, '')
59+
dump.gsub!(/^ALTER TABLE ONLY [\w\.]+\s+ADD CONSTRAINT [\w\.]+_pkey PRIMARY KEY \(id\);$/, '')
4560
dump.gsub!(/^-- Name: (\w+\s+)?id; Type: DEFAULT$/, '')
4661
dump.gsub!(/^-- .*_id_seq; Type: SEQUENCE.*/, '')
4762
dump.gsub!(/^-- Name: (\w+\s+)?\w+_pkey; Type: CONSTRAINT$/, '')
4863

4964
# Remove inherited tables
50-
inherited_tables_regexp = /-- Name: ([\w_]+); Type: TABLE\n\n[^;]+?INHERITS \([\w_]+\);/m
65+
inherited_tables_regexp = /-- Name: ([\w_\.]+); Type: TABLE\n\n[^;]+?INHERITS \([\w_\.]+\);/m
5166
inherited_tables = dump.scan(inherited_tables_regexp).map(&:first)
5267
dump.gsub!(inherited_tables_regexp, '')
5368
inherited_tables.each do |inherited_table|
54-
dump.gsub!(/ALTER TABLE ONLY #{inherited_table}[^;]+;/, '')
69+
dump.gsub!(/ALTER TABLE ONLY (public\.)?#{inherited_table}[^;]+;/, '')
70+
71+
index_regexp = /CREATE INDEX ([\w_]+) ON (public\.)?#{inherited_table}[^;]+;/m
72+
dump.scan(index_regexp).map(&:first).each do |inherited_table_index|
73+
dump.gsub!("-- Name: #{inherited_table_index}; Type: INDEX", '')
74+
end
75+
dump.gsub!(index_regexp, '')
5576
end
5677

5778
# Remove whitespace between schema migration INSERTS to make editing easier
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module ActiveRecordCleanDbStructure
2-
VERSION = '0.2.1'
2+
VERSION = '0.2.6'
33
end

0 commit comments

Comments
 (0)