sample_data_dump_postgres_data_store 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 489de668db9b90f244c7e897499e2987c1ecc102
4
+ data.tar.gz: a9839c61819eb279acf7e7aee4b517eaaf9aae07
5
+ SHA512:
6
+ metadata.gz: be99ce59d1cea6ea0914bd82889b10531a14c2a9f1a56ac88239932746af76e1ea5c2a8143f77fff08bb82da8fa7c35052f00efc9b7c2f0e99fa078b4be23104
7
+ data.tar.gz: 9e2f23e31c0d216bcd5bc211090632d19d00158b47f3d4e1c3fb0a3ccbdf0bb97e2a6920b37a2ec34d34c747d39065313a8426d442a694b5cd69c227ec098647
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ coverage/
2
+ .rspec_status
3
+ tmp
data/.reek.yml ADDED
@@ -0,0 +1,32 @@
1
+ ---
2
+ detectors:
3
+ IrresponsibleModule:
4
+ enabled: false
5
+ TooManyStatements:
6
+ enabled: true
7
+ max_statements: 10
8
+ NilCheck:
9
+ enabled: false
10
+ directories:
11
+ app/controllers:
12
+ NestedIterators:
13
+ max_allowed_nesting: 2
14
+ UnusedPrivateMethod:
15
+ enabled: false
16
+ app/helpers:
17
+ UtilityFunction:
18
+ enabled: false
19
+ exclude_paths:
20
+ - app/assets
21
+ - bin
22
+ - client/node_modules
23
+ - config
24
+ - coverage
25
+ - data
26
+ - db
27
+ - dw
28
+ - log
29
+ - phrase
30
+ - public
31
+ - tmp
32
+ - vendor
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,61 @@
1
+ ---
2
+ Documentation:
3
+ Enabled: false
4
+ Rails:
5
+ Enabled: true
6
+ AllCops:
7
+ Include:
8
+ - app/**/*.rb
9
+ - lib/**/*.rb
10
+ - spec/**/*.rb
11
+ Exclude:
12
+ - app/assets/**/*
13
+ - bin/**/*
14
+ - client/node_modules/**/*
15
+ - config/**/*
16
+ - coverage/**/*
17
+ - data/**/*
18
+ - db/**/*
19
+ - db_*/**/*
20
+ - dw/**/*
21
+ - log/**/*
22
+ - phrase/**/*
23
+ - public/**/*
24
+ - tmp/**/*
25
+ - vendor/**/*
26
+ TargetRubyVersion: 2.4
27
+ Metrics/LineLength:
28
+ Max: 100
29
+ Layout/MultilineMethodCallIndentation:
30
+ EnforcedStyle: indented
31
+ Style/PercentLiteralDelimiters:
32
+ PreferredDelimiters:
33
+ "%w": "[]"
34
+ RSpec/ExampleLength:
35
+ Enabled: false
36
+ Max: 10
37
+ RSpec/MultipleExpectations:
38
+ Enabled: false
39
+ Max: 10
40
+ RSpec/NestedGroups:
41
+ Enabled: false
42
+ Max: 10
43
+ RSpec/MessageExpectation:
44
+ Enabled: false
45
+ RSpec/MissingExampleGroupArgument:
46
+ Enabled: false
47
+ require:
48
+ - rubocop-rspec
49
+ - test_prof/rubocop
50
+ Metrics/BlockLength:
51
+ Enabled: false
52
+ RSpec/MessageSpies:
53
+ Enabled: false
54
+ RSpec/ExpectInHook:
55
+ Enabled: false
56
+ RSpec/AggregateFailures:
57
+ Enabled: true
58
+ Include:
59
+ - spec/**/*.rb
60
+ Rails/InverseOf:
61
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.4.5
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ 0.0.1
2
+
3
+ * First release
data/Gemfile ADDED
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in bellroy-products.gemspec
6
+ gemspec
7
+
8
+ group :development, :test do
9
+ gem 'guard-rspec'
10
+ gem 'pry-byebug'
11
+ gem 'rb-fsevent', require: false
12
+ gem 'rb-readline'
13
+ gem 'reek'
14
+ gem 'rspec'
15
+ gem 'rubocop'
16
+ gem 'rubocop-rspec'
17
+ gem 'stackprof'
18
+ gem 'timecop'
19
+ end
20
+
21
+ group :development do
22
+ gem 'pry'
23
+ end
24
+
25
+ group :test do
26
+ gem 'simplecov'
27
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,240 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ sample_data_dump_postgres_data_store (0.0.1)
5
+ activerecord
6
+ dry-matcher
7
+ dry-monads
8
+ dry-struct
9
+ dry-types
10
+ duckface-interfaces
11
+ pg
12
+ railties
13
+ sample_data_dump
14
+
15
+ GEM
16
+ remote: https://rubygems.org/
17
+ specs:
18
+ actionpack (5.2.1.1)
19
+ actionview (= 5.2.1.1)
20
+ activesupport (= 5.2.1.1)
21
+ rack (~> 2.0)
22
+ rack-test (>= 0.6.3)
23
+ rails-dom-testing (~> 2.0)
24
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
25
+ actionview (5.2.1.1)
26
+ activesupport (= 5.2.1.1)
27
+ builder (~> 3.1)
28
+ erubi (~> 1.4)
29
+ rails-dom-testing (~> 2.0)
30
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
31
+ activemodel (5.2.1.1)
32
+ activesupport (= 5.2.1.1)
33
+ activerecord (5.2.1.1)
34
+ activemodel (= 5.2.1.1)
35
+ activesupport (= 5.2.1.1)
36
+ arel (>= 9.0)
37
+ activesupport (5.2.1.1)
38
+ concurrent-ruby (~> 1.0, >= 1.0.2)
39
+ i18n (>= 0.7, < 2)
40
+ minitest (~> 5.1)
41
+ tzinfo (~> 1.1)
42
+ arel (9.0.0)
43
+ ast (2.4.0)
44
+ axiom-types (0.1.1)
45
+ descendants_tracker (~> 0.0.4)
46
+ ice_nine (~> 0.11.0)
47
+ thread_safe (~> 0.3, >= 0.3.1)
48
+ builder (3.2.3)
49
+ byebug (10.0.2)
50
+ codeclimate-engine-rb (0.4.1)
51
+ virtus (~> 1.0)
52
+ coderay (1.1.2)
53
+ coercible (1.0.0)
54
+ descendants_tracker (~> 0.0.1)
55
+ concurrent-ruby (1.1.3)
56
+ crass (1.0.4)
57
+ descendants_tracker (0.0.4)
58
+ thread_safe (~> 0.3, >= 0.3.1)
59
+ diff-lcs (1.3)
60
+ docile (1.3.1)
61
+ dry-configurable (0.7.0)
62
+ concurrent-ruby (~> 1.0)
63
+ dry-container (0.6.0)
64
+ concurrent-ruby (~> 1.0)
65
+ dry-configurable (~> 0.1, >= 0.1.3)
66
+ dry-core (0.4.7)
67
+ concurrent-ruby (~> 1.0)
68
+ dry-equalizer (0.2.1)
69
+ dry-inflector (0.1.2)
70
+ dry-logic (0.4.2)
71
+ dry-container (~> 0.2, >= 0.2.6)
72
+ dry-core (~> 0.2)
73
+ dry-equalizer (~> 0.2)
74
+ dry-matcher (0.7.0)
75
+ dry-monads (1.1.0)
76
+ concurrent-ruby (~> 1.0)
77
+ dry-core (~> 0.4, >= 0.4.4)
78
+ dry-equalizer
79
+ dry-struct (0.6.0)
80
+ dry-core (~> 0.4, >= 0.4.3)
81
+ dry-equalizer (~> 0.2)
82
+ dry-types (~> 0.13)
83
+ ice_nine (~> 0.11)
84
+ dry-types (0.13.3)
85
+ concurrent-ruby (~> 1.0)
86
+ dry-container (~> 0.3)
87
+ dry-core (~> 0.4, >= 0.4.4)
88
+ dry-equalizer (~> 0.2)
89
+ dry-inflector (~> 0.1, >= 0.1.2)
90
+ dry-logic (~> 0.4, >= 0.4.2)
91
+ duckface-interfaces (0.0.3)
92
+ equalizer (0.0.11)
93
+ erubi (1.7.1)
94
+ ffi (1.9.25)
95
+ formatador (0.2.5)
96
+ guard (2.15.0)
97
+ formatador (>= 0.2.4)
98
+ listen (>= 2.7, < 4.0)
99
+ lumberjack (>= 1.0.12, < 2.0)
100
+ nenv (~> 0.1)
101
+ notiffany (~> 0.0)
102
+ pry (>= 0.9.12)
103
+ shellany (~> 0.0)
104
+ thor (>= 0.18.1)
105
+ guard-compat (1.2.1)
106
+ guard-rspec (4.7.3)
107
+ guard (~> 2.1)
108
+ guard-compat (~> 1.1)
109
+ rspec (>= 2.99.0, < 4.0)
110
+ i18n (1.1.1)
111
+ concurrent-ruby (~> 1.0)
112
+ ice_nine (0.11.2)
113
+ jaro_winkler (1.5.1)
114
+ json (2.1.0)
115
+ kwalify (0.7.2)
116
+ listen (3.1.5)
117
+ rb-fsevent (~> 0.9, >= 0.9.4)
118
+ rb-inotify (~> 0.9, >= 0.9.7)
119
+ ruby_dep (~> 1.2)
120
+ loofah (2.2.3)
121
+ crass (~> 1.0.2)
122
+ nokogiri (>= 1.5.9)
123
+ lumberjack (1.0.13)
124
+ method_source (0.9.2)
125
+ mini_portile2 (2.3.0)
126
+ minitest (5.11.3)
127
+ nenv (0.3.0)
128
+ nokogiri (1.8.5)
129
+ mini_portile2 (~> 2.3.0)
130
+ notiffany (0.1.1)
131
+ nenv (~> 0.1)
132
+ shellany (~> 0.0)
133
+ parallel (1.12.1)
134
+ parser (2.5.3.0)
135
+ ast (~> 2.4.0)
136
+ pg (1.1.3)
137
+ powerpack (0.1.2)
138
+ pry (0.12.2)
139
+ coderay (~> 1.1.0)
140
+ method_source (~> 0.9.0)
141
+ pry-byebug (3.6.0)
142
+ byebug (~> 10.0)
143
+ pry (~> 0.10)
144
+ rack (2.0.6)
145
+ rack-test (1.1.0)
146
+ rack (>= 1.0, < 3)
147
+ rails-dom-testing (2.0.3)
148
+ activesupport (>= 4.2.0)
149
+ nokogiri (>= 1.6)
150
+ rails-html-sanitizer (1.0.4)
151
+ loofah (~> 2.2, >= 2.2.2)
152
+ railties (5.2.1.1)
153
+ actionpack (= 5.2.1.1)
154
+ activesupport (= 5.2.1.1)
155
+ method_source
156
+ rake (>= 0.8.7)
157
+ thor (>= 0.19.0, < 2.0)
158
+ rainbow (3.0.0)
159
+ rake (12.3.1)
160
+ rb-fsevent (0.10.3)
161
+ rb-inotify (0.9.10)
162
+ ffi (>= 0.5.0, < 2)
163
+ rb-readline (0.5.5)
164
+ reek (5.2.0)
165
+ codeclimate-engine-rb (~> 0.4.0)
166
+ kwalify (~> 0.7.0)
167
+ parser (>= 2.5.0.0, < 2.6, != 2.5.1.1)
168
+ rainbow (>= 2.0, < 4.0)
169
+ rspec (3.8.0)
170
+ rspec-core (~> 3.8.0)
171
+ rspec-expectations (~> 3.8.0)
172
+ rspec-mocks (~> 3.8.0)
173
+ rspec-core (3.8.0)
174
+ rspec-support (~> 3.8.0)
175
+ rspec-expectations (3.8.2)
176
+ diff-lcs (>= 1.2.0, < 2.0)
177
+ rspec-support (~> 3.8.0)
178
+ rspec-mocks (3.8.0)
179
+ diff-lcs (>= 1.2.0, < 2.0)
180
+ rspec-support (~> 3.8.0)
181
+ rspec-support (3.8.0)
182
+ rubocop (0.60.0)
183
+ jaro_winkler (~> 1.5.1)
184
+ parallel (~> 1.10)
185
+ parser (>= 2.5, != 2.5.1.1)
186
+ powerpack (~> 0.1)
187
+ rainbow (>= 2.2.2, < 4.0)
188
+ ruby-progressbar (~> 1.7)
189
+ unicode-display_width (~> 1.4.0)
190
+ rubocop-rspec (1.30.1)
191
+ rubocop (>= 0.60.0)
192
+ ruby-progressbar (1.10.0)
193
+ ruby_dep (1.5.0)
194
+ sample_data_dump (0.0.2)
195
+ dry-matcher
196
+ dry-monads
197
+ dry-struct
198
+ dry-types
199
+ duckface-interfaces
200
+ shellany (0.0.1)
201
+ simplecov (0.16.1)
202
+ docile (~> 1.1)
203
+ json (>= 1.8, < 3)
204
+ simplecov-html (~> 0.10.0)
205
+ simplecov-html (0.10.2)
206
+ stackprof (0.2.12)
207
+ thor (0.20.3)
208
+ thread_safe (0.3.6)
209
+ timecop (0.9.1)
210
+ tzinfo (1.2.5)
211
+ thread_safe (~> 0.1)
212
+ unicode-display_width (1.4.0)
213
+ virtus (1.0.5)
214
+ axiom-types (~> 0.1)
215
+ coercible (~> 1.0)
216
+ descendants_tracker (~> 0.0, >= 0.0.3)
217
+ equalizer (~> 0.0, >= 0.0.9)
218
+
219
+ PLATFORMS
220
+ ruby
221
+
222
+ DEPENDENCIES
223
+ bundler (>= 1.13)
224
+ guard-rspec
225
+ pry
226
+ pry-byebug
227
+ rake (>= 10.0)
228
+ rb-fsevent
229
+ rb-readline
230
+ reek
231
+ rspec
232
+ rubocop
233
+ rubocop-rspec
234
+ sample_data_dump_postgres_data_store!
235
+ simplecov
236
+ stackprof
237
+ timecop
238
+
239
+ BUNDLED WITH
240
+ 1.17.1
data/Guardfile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ guard :rspec, cmd: 'bundle exec rspec' do
4
+ require 'guard/rspec/dsl'
5
+ dsl = Guard::RSpec::Dsl.new(self)
6
+
7
+ # RSpec files
8
+ rspec = dsl.rspec
9
+ watch(rspec.spec_helper) { rspec.spec_dir }
10
+ watch(rspec.spec_support) { rspec.spec_dir }
11
+ watch(rspec.spec_files)
12
+
13
+ # Ruby files
14
+ ruby = dsl.ruby
15
+ dsl.watch_spec_files_for(ruby.lib_files)
16
+ end
data/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # Sample Data Dump - Postgres Data Store
2
+
3
+ This gem provides:
4
+ - A PostgreSQL data storage gateway for the `sample_data_dump` gem
5
+
6
+ ## Usage
7
+
8
+ See the [sample_data_dump README](https://github.com/tricycle/sample_data_dump) for more
9
+ instructions.
10
+
11
+ ```
12
+ settings = SampleDataDumpPostgresDataStore::Settings.new(your_settings)
13
+ SampleDataDumpPostgresDataStore::Gateway.new(settings)
14
+ ```
15
+
16
+ ## Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ ```ruby
21
+ gem 'sample_data_dump_postgres_data_store', git: 'git@github.com:tricycle/sample_data_dump_postgres_data_store.git'
22
+ ```
23
+
24
+ And then execute:
25
+
26
+ $ bundle install
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sample_data_dump_postgres_data_store/version'
@@ -0,0 +1,230 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry/monads/all'
4
+ require 'duckface'
5
+ require 'rails'
6
+ require 'sample_data_dump/entities/table_configuration'
7
+ require 'sample_data_dump/interfaces/data_store_gateway'
8
+ require 'sample_data_dump_postgres_data_store/settings'
9
+
10
+ module SampleDataDumpPostgresDataStore
11
+ class Gateway
12
+ implements_interface SampleDataDump::Interfaces::DataStoreGateway
13
+
14
+ def initialize(postgresql_adapter, settings)
15
+ @squished_sql_runner = SquishedSqlRunner.new(postgresql_adapter)
16
+ @settings = settings
17
+ end
18
+
19
+ def dump_to_local_file(table_configuration)
20
+ dump_file = SampleDataDump::Helpers::DumpFile.new(table_configuration, @settings)
21
+ uncompressed_file_path = dump_file.local_dump_file_path
22
+
23
+ sql = extraction_sql(table_configuration)
24
+ results = @squished_sql_runner.run(sql)
25
+ export_results_to_sql(results, table_configuration, uncompressed_file_path)
26
+ Dry::Monads::Success(uncompressed_file_path)
27
+ end
28
+
29
+ def load_dump_file(table_configuration)
30
+ raise 'DO NOT LOAD OBFUSCATED DUMPS IN PRODUCTION!' if Rails.env.production?
31
+
32
+ dump_file = SampleDataDump::Helpers::DumpFile.new(table_configuration, @settings)
33
+ source_file_path = dump_file.local_dump_file_path
34
+ unless File.exist?(source_file_path)
35
+ return Dry::Monads::Failure("File #{source_file_path} does not exist for loading!")
36
+ end
37
+
38
+ sql = File.read(source_file_path)
39
+ @squished_sql_runner.run(sql)
40
+ Dry::Monads::Success(true)
41
+ end
42
+
43
+ def reset_sequence(table_configuration)
44
+ table_name = table_configuration.qualified_table_name
45
+ sql = "SELECT setval('#{table_name}_id_seq', coalesce((SELECT MAX(id) FROM #{table_name}),1))"
46
+ @squished_sql_runner.run(sql)
47
+ Dry::Monads::Success(true)
48
+ end
49
+
50
+ def valid?(table_configuration)
51
+ TableConfigurationValidator.new(table_configuration, @squished_sql_runner).validation_result
52
+ end
53
+
54
+ def wipe_table(table_configuration)
55
+ qualified_table_name = table_configuration.qualified_table_name
56
+ @squished_sql_runner.run "DELETE FROM #{qualified_table_name} CASCADE"
57
+ Dry::Monads::Success(true)
58
+ end
59
+
60
+ private
61
+
62
+ attr_reader :settings
63
+ delegate :compacted_dump_directory, :lorem_ipsum_function_schema, to: :settings
64
+
65
+ class SquishedSqlRunner
66
+ def initialize(postgresql_adapter)
67
+ @postgresql_adapter = postgresql_adapter
68
+ end
69
+
70
+ def run(sql)
71
+ @postgresql_adapter.execute sql.squish
72
+ end
73
+ end
74
+
75
+ class TableConfigurationValidator
76
+ include Dry::Monads::Do.for(:validation_result)
77
+
78
+ def initialize(table_configuration, squished_sql_runner)
79
+ @table_configuration = table_configuration
80
+ @squished_sql_runner = squished_sql_runner
81
+ end
82
+
83
+ def validation_result
84
+ yield schema_existence_result
85
+ yield table_existence_result
86
+ yield dump_where_condition_validity_result
87
+ yield obfuscate_columns_validity_result
88
+ Dry::Monads::Success(true)
89
+ end
90
+
91
+ private
92
+
93
+ attr_reader :table_configuration
94
+ delegate :dump_where,
95
+ :obfuscate_columns,
96
+ :qualified_table_name,
97
+ :schema_name,
98
+ :table_name,
99
+ to: :table_configuration
100
+
101
+ def schema_existence_result
102
+ sql = <<~SQL
103
+ SELECT EXISTS (
104
+ SELECT *
105
+ FROM pg_catalog.pg_namespace
106
+ WHERE nspname = '#{schema_name}'
107
+ );
108
+ SQL
109
+ results = @squished_sql_runner.run(sql)
110
+
111
+ return Dry::Monads::Success(true) if results.first['exists']
112
+
113
+ Dry::Monads::Failure("schema #{schema_name} does not exist")
114
+ end
115
+
116
+ def table_existence_result
117
+ sql = <<~SQL
118
+ SELECT EXISTS (
119
+ SELECT 1
120
+ FROM information_schema.tables
121
+ WHERE table_schema = '#{schema_name}'
122
+ AND table_name = '#{table_name}'
123
+ );
124
+ SQL
125
+ results = @squished_sql_runner.run(sql)
126
+
127
+ return Dry::Monads::Success(true) if results.first['exists']
128
+
129
+ Dry::Monads::Failure("#{qualified_table_name} does not exist")
130
+ end
131
+
132
+ def dump_where_condition_validity_result
133
+ sql = <<~SQL
134
+ SELECT * FROM #{qualified_table_name}
135
+ WHERE #{dump_where}
136
+ LIMIT 1
137
+ SQL
138
+ @squished_sql_runner.run(sql)
139
+ Dry::Monads::Success(true)
140
+ rescue ActiveRecord::StatementInvalid
141
+ Dry::Monads::Failure("dump_where for #{qualified_table_name} invalid")
142
+ end
143
+
144
+ def obfuscate_columns_validity_result
145
+ success_result = Dry::Monads::Success(true)
146
+ return success_result if obfuscate_columns.empty?
147
+
148
+ sql = "SELECT #{obfuscate_columns.join(', ')} FROM #{qualified_table_name} LIMIT 1"
149
+ @squished_sql_runner.run(sql)
150
+ success_result
151
+ rescue ActiveRecord::StatementInvalid
152
+ Dry::Monads::Failure("obfuscate_columns for #{qualified_table_name} invalid")
153
+ end
154
+ end
155
+
156
+ def extraction_sql(table_configuration)
157
+ columns = normal_columns(table_configuration) + lorem_ipsum_columns(table_configuration)
158
+ <<~SQL
159
+ SELECT #{columns.join(', ')}
160
+ FROM #{table_configuration.qualified_table_name}
161
+ WHERE #{table_configuration.dump_where}
162
+ LIMIT 100000
163
+ SQL
164
+ end
165
+
166
+ def normal_columns(table_configuration)
167
+ columns = table_columns(table_configuration) - table_configuration.obfuscate_columns
168
+ columns.map { |column| "\"#{column}\"" }
169
+ end
170
+
171
+ def table_columns(table_configuration)
172
+ sql = <<~SQL
173
+ SELECT column_name FROM information_schema.columns
174
+ WHERE table_schema = '#{table_configuration.schema_name}'
175
+ AND table_name = '#{table_configuration.table_name}'
176
+ SQL
177
+ @squished_sql_runner.run(sql).map { |row| row['column_name'] }
178
+ end
179
+
180
+ def lorem_ipsum_columns(table_configuration)
181
+ table_configuration.obfuscate_columns.map do |column_name|
182
+ "#{lorem_ipsum_function_schema}.lorem_ipsum(3) AS \"#{column_name}\""
183
+ end
184
+ end
185
+
186
+ def export_results_to_sql(results, table_configuration, sql_file_path)
187
+ row_count = results.ntuples
188
+ if row_count.zero?
189
+ `echo "SELECT 'No rows to load'" >> #{sql_file_path}`
190
+ else
191
+ File.open(sql_file_path, 'w+') do |file|
192
+ table_name = table_configuration.qualified_table_name
193
+ file.puts "DELETE FROM #{table_name};"
194
+ insert_columns = table_columns(table_configuration)
195
+ columns_in_quotes = insert_columns.map { |col| "\"#{col}\"" }.join(', ')
196
+ file.puts "INSERT INTO #{table_name} (#{columns_in_quotes})"
197
+ file.puts 'VALUES'
198
+ results.each_with_index do |result, index|
199
+ comma = index == row_count - 1 ? '' : ','
200
+ values = columns_with_reserved_words_replaced(insert_columns).map { |col| result[col] }
201
+ values = '(' + valid_postgres_values(values).join(', ') + ')' + comma
202
+ file.puts(values)
203
+ end
204
+ end
205
+ end
206
+ end
207
+
208
+ def columns_with_reserved_words_replaced(columns)
209
+ columns.map do |column|
210
+ if column == 'user'
211
+ 'current_user'
212
+ else
213
+ column
214
+ end
215
+ end
216
+ end
217
+
218
+ def valid_postgres_values(original_values)
219
+ original_values.map do |value|
220
+ if value.nil?
221
+ 'NULL'
222
+ elsif value.is_a?(String) || value.is_a?(Date) || value.is_a?(Time)
223
+ "'#{value.to_s.gsub("'", "''")}'"
224
+ else
225
+ value
226
+ end
227
+ end
228
+ end
229
+ end
230
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry/struct'
4
+ require 'sample_data_dump/entities/settings'
5
+ require 'sample_data_dump/types'
6
+
7
+ module SampleDataDumpPostgresDataStore
8
+ class Settings < SampleDataDump::Entities::Settings
9
+ attribute :lorem_ipsum_function_schema,
10
+ SampleDataDump::Types::Strict::String.default('public')
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SampleDataDumpPostgresDataStore
4
+ VERSION = '0.0.1'
5
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'sample_data_dump_postgres_data_store/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'sample_data_dump_postgres_data_store'
9
+ spec.version = SampleDataDumpPostgresDataStore::VERSION
10
+ spec.authors = ['Bellroy Dev Team']
11
+ spec.email = ['techsupport@bellroy.com']
12
+
13
+ spec.summary = 'Data storage for `sample_data_dump` gem - PostgreSQL'
14
+ spec.description = ''
15
+ spec.homepage = 'http://www.bellroy.com'
16
+
17
+ if spec.respond_to?(:metadata)
18
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
19
+ else
20
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
21
+ 'public gem pushes.'
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
25
+ f.match(%r{^(test|spec|features)/})
26
+ end
27
+ spec.bindir = 'exe'
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ['lib']
30
+
31
+ spec.add_dependency 'activerecord'
32
+ spec.add_dependency 'dry-matcher'
33
+ spec.add_dependency 'dry-monads'
34
+ spec.add_dependency 'dry-struct'
35
+ spec.add_dependency 'dry-types'
36
+ spec.add_dependency 'duckface-interfaces'
37
+ spec.add_dependency 'pg'
38
+ spec.add_dependency 'railties'
39
+ spec.add_dependency 'sample_data_dump'
40
+
41
+ spec.add_development_dependency 'bundler', '>= 1.13'
42
+ spec.add_development_dependency 'rake', '>= 10.0'
43
+ spec.add_development_dependency 'rspec', '>= 3.0'
44
+ end
metadata ADDED
@@ -0,0 +1,227 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sample_data_dump_postgres_data_store
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Bellroy Dev Team
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-12-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: dry-matcher
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: dry-monads
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: dry-struct
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: dry-types
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: duckface-interfaces
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pg
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: railties
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: sample_data_dump
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: bundler
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '1.13'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '1.13'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rake
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '10.0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '10.0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: rspec
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '3.0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '3.0'
181
+ description: ''
182
+ email:
183
+ - techsupport@bellroy.com
184
+ executables: []
185
+ extensions: []
186
+ extra_rdoc_files: []
187
+ files:
188
+ - ".gitignore"
189
+ - ".reek.yml"
190
+ - ".rspec"
191
+ - ".rubocop.yml"
192
+ - ".ruby-version"
193
+ - CHANGELOG.md
194
+ - Gemfile
195
+ - Gemfile.lock
196
+ - Guardfile
197
+ - README.md
198
+ - lib/sample_data_dump_postgres_data_store.rb
199
+ - lib/sample_data_dump_postgres_data_store/gateway.rb
200
+ - lib/sample_data_dump_postgres_data_store/settings.rb
201
+ - lib/sample_data_dump_postgres_data_store/version.rb
202
+ - sample_data_dump_postgres_data_store.gemspec
203
+ homepage: http://www.bellroy.com
204
+ licenses: []
205
+ metadata:
206
+ allowed_push_host: https://rubygems.org
207
+ post_install_message:
208
+ rdoc_options: []
209
+ require_paths:
210
+ - lib
211
+ required_ruby_version: !ruby/object:Gem::Requirement
212
+ requirements:
213
+ - - ">="
214
+ - !ruby/object:Gem::Version
215
+ version: '0'
216
+ required_rubygems_version: !ruby/object:Gem::Requirement
217
+ requirements:
218
+ - - ">="
219
+ - !ruby/object:Gem::Version
220
+ version: '0'
221
+ requirements: []
222
+ rubyforge_project:
223
+ rubygems_version: 2.6.14.3
224
+ signing_key:
225
+ specification_version: 4
226
+ summary: Data storage for `sample_data_dump` gem - PostgreSQL
227
+ test_files: []