contentful-database-importer 0.1.0 → 0.2.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
2
  SHA1:
3
- metadata.gz: 98d3aacd7022ac369a2b9b81cd21048d52d1da11
4
- data.tar.gz: 063b897d2875b9ac709da9f930035f108b41c135
3
+ metadata.gz: 452224245e7a1c58fdb80466f7beef6911546e4d
4
+ data.tar.gz: 7f860c92ea18230ecc8a7a50b7a39fc6be06fe32
5
5
  SHA512:
6
- metadata.gz: 9b3a9767b6a65e6e5fc9ad2efb26e68190411b358f35e9fa25e8075cbfbae1943e6851f759447d76a819dd9f1baa91e610e86af3277c970e274e914c2309f585
7
- data.tar.gz: 99db145e30c2adf4bfb8b3e07c49d5626f6576f331aaccd7ed41a142eacbe0a336ecd074671667cea63ffb5cb25ee7407d4908275b9ab26ebad12fc6ebedacca
6
+ metadata.gz: faa9477146f720378be62b5299ea633e4bec319ac930c181138ff47bf7f9d10e2837736bbd2f0bd4895350cf424ee0c7709a5dfad9613df8ab0db314e5698d62
7
+ data.tar.gz: 30589e531b05f151f1b6e66a74f527cdcab762579fd72ef6af03818e307fce78c1aae941fdef7874bd43ef7d6eed2d5f2b931fd43cc698e0e7d78b0542beef6a
data/CHANGELOG.md ADDED
@@ -0,0 +1,20 @@
1
+ # CHANGELOG
2
+
3
+ ## Unreleased
4
+
5
+ ## 0.2.0
6
+
7
+ ### Added
8
+ * Added `::update_space!` command - [#2](https://github.com/contentful/contentful-database-importer.rb/issues/2)
9
+ * Added `:skip_content_types` configuration option for `::update_space!`
10
+ * Added `::query` support on `Resource` for selecting specific content for a table
11
+
12
+ ## 0.1.0
13
+
14
+ Initial version which includes all the basic features including:
15
+
16
+ * Major Databases Support
17
+ * Database to Content Type Mapping
18
+ * Field-level transformations
19
+ * Contentful Bootstrap JSON Generation
20
+ * Space creation
data/Guardfile CHANGED
@@ -1,14 +1,19 @@
1
- guard :rspec, cmd: "bundle exec rspec" do
2
- require "guard/rspec/dsl"
3
- dsl = Guard::RSpec::Dsl.new(self)
1
+ group :green_red_refactor, halt_on_fail: true do
2
+ guard :rspec, cmd: "bundle exec rspec" do
3
+ require "guard/rspec/dsl"
4
+ dsl = Guard::RSpec::Dsl.new(self)
4
5
 
5
- # RSpec files
6
- rspec = dsl.rspec
7
- watch(rspec.spec_helper) { rspec.spec_dir }
8
- watch(rspec.spec_support) { rspec.spec_dir }
9
- watch(rspec.spec_files)
6
+ # RSpec files
7
+ rspec = dsl.rspec
8
+ watch(rspec.spec_helper) { rspec.spec_dir }
9
+ watch(rspec.spec_support) { rspec.spec_dir }
10
+ watch(rspec.spec_files)
10
11
 
11
- # Ruby files
12
- ruby = dsl.ruby
13
- dsl.watch_spec_files_for(ruby.lib_files)
12
+ # Ruby files
13
+ ruby = dsl.ruby
14
+ dsl.watch_spec_files_for(ruby.lib_files)
15
+ end
16
+
17
+ guard :rubocop, cmd: "rubocop", all_on_start: false, cli: ['--format', 'clang', 'lib'] do
18
+ end
14
19
  end
data/README.md CHANGED
@@ -211,12 +211,33 @@ In the case of circular references, you will have to create 2 or more classes po
211
211
 
212
212
  **Note**: Merge classes require at least 1 field declared, even if it's excluded from output.
213
213
 
214
+ ### Querying
215
+
216
+ You might want to reduce your datasets to specific subsets, in that case, you can use Querying to specify your subsets of data.
217
+
218
+ A query is an `SQL String`. E.g: `foo = 'bar' AND baz > 2`.
219
+
220
+ This is optional and can be specified in the Resource like follows:
221
+
222
+ ```ruby
223
+ class MyResource
224
+ include Contentful::DatabaseImporter::Resource
225
+
226
+ self.query = "foo = 'bar' AND baz > 2"
227
+
228
+ field :foo, type: :string
229
+ field :baz, type: :integer
230
+ end
231
+ ```
232
+
214
233
  ### Configuration
215
234
 
216
235
  ```ruby
217
236
  Contentful::DatabaseImporter.setup do |config|
218
- config.space_name = 'My Cool New Space' # Required - the destination space name
237
+ config.space_name = 'My Cool New Space' # Required only for `::run!` - the destination space name
238
+ config.space_id = 'aAbBcC123foo' # Required only for `::update_space!` - the destination space ID
219
239
  config.database_connection = 'postgres://user:pass@host:port' # Required - the DB Connection string
240
+ config.skip_content_types = true # Optional (only for `::update_space!`) - defaults to `true` - Skips Content Type creation upon updating a space
220
241
  end
221
242
  ```
222
243
 
@@ -248,6 +269,12 @@ Contentful::DatabaseImporter.generate_json!
248
269
  Contentful::DatabaseImporter.run!
249
270
  ```
250
271
 
272
+ * Generate the JSON and Import it to Contentful (updates a Space with all the content):
273
+
274
+ ```ruby
275
+ Contentful::DatabaseImporter.update_space!
276
+ ```
277
+
251
278
  ## Contributing
252
279
 
253
280
  Feel free to improve this tool by submitting a Pull Request. For more information, please read [CONTRIBUTING.md](./CONTRIBUTING.md)
@@ -32,5 +32,6 @@ Gem::Specification.new do |spec|
32
32
  spec.add_development_dependency 'simplecov'
33
33
  spec.add_development_dependency 'guard'
34
34
  spec.add_development_dependency 'guard-rspec'
35
+ spec.add_development_dependency 'guard-rubocop'
35
36
  spec.add_development_dependency 'pry'
36
37
  end
@@ -2,11 +2,26 @@ module Contentful
2
2
  module DatabaseImporter
3
3
  # Configuration for Importer
4
4
  class Config
5
- attr_accessor :space_name, :database_connection
5
+ attr_accessor :space_name,
6
+ :space_id,
7
+ :database_connection,
8
+ :skip_content_types
6
9
 
7
- def complete?
10
+ def initialize
11
+ @skip_content_types = true
12
+ end
13
+
14
+ def complete_for_run?
8
15
  !space_name.nil? && !database_connection.nil?
9
16
  end
17
+
18
+ def complete_for_update?
19
+ !space_id.nil? && !database_connection.nil?
20
+ end
21
+
22
+ def complete?
23
+ complete_for_run? || complete_for_update?
24
+ end
10
25
  end
11
26
  end
12
27
  end
@@ -1,8 +1,9 @@
1
1
  require 'contentful/database_importer/support'
2
- require 'contentful/database_importer/resource_class_methods'
3
2
  require 'contentful/database_importer/resource_coercions'
4
3
  require 'contentful/database_importer/resource_relationships'
5
4
  require 'contentful/database_importer/resource_bootstrap_methods'
5
+ require 'contentful/database_importer/resource_class_methods'
6
+ require 'contentful/database_importer/resource_field_class_methods'
6
7
  require 'contentful/database_importer/resource_bootstrap_class_methods'
7
8
  require 'mimemagic'
8
9
 
@@ -17,6 +18,7 @@ module Contentful
17
18
 
18
19
  def self.included(base)
19
20
  base.extend(ResourceClassMethods)
21
+ base.extend(ResourceFieldClassMethods)
20
22
  base.extend(ResourceBootstrapClassMethods)
21
23
  end
22
24
 
@@ -29,6 +29,14 @@ module Contentful
29
29
  @content_type_name = name
30
30
  end
31
31
 
32
+ def query
33
+ @query
34
+ end
35
+
36
+ def query=(query)
37
+ @query = query
38
+ end
39
+
32
40
  def default_generator_options
33
41
  {
34
42
  table_name: table_name,
@@ -54,68 +62,13 @@ module Contentful
54
62
  @display_field = field_name
55
63
  end
56
64
 
57
- def fields
58
- @fields || []
59
- end
60
-
61
- def relationship_fields
62
- @fields.select { |f| resource?(f[:type]) }
63
- end
64
-
65
- def prepare_standard_field_options(database_name, options)
66
- {
67
- db_name: database_name,
68
- maps_to: options.fetch(:maps_to, database_name),
69
- name: options.fetch(:name, database_name),
70
- type: options.fetch(:type),
71
- pre_process: options.fetch(:pre_process, nil),
72
- exclude_from_output: options.fetch(:exclude_from_output, false)
73
- }
74
- end
75
-
76
- def prepare_field(database_name, options)
77
- field = prepare_standard_field_options(database_name, options)
78
- field[:item_type] = options.fetch(:item_type) if field[:type] == :array
79
- fetch_relationship_options(
80
- field,
81
- options
82
- ) if options.fetch(:relationship, false)
83
-
84
- field
85
- end
86
-
87
- def field(database_name, options = {})
88
- @fields ||= []
89
- @fields << prepare_field(database_name, options)
90
- end
91
-
92
- def fetch_many_relationship_options(field, options)
93
- field[:id_field] = options.fetch(:id_field)
94
- field[:key] = options.fetch(:key)
95
- end
96
- alias fetch_one_relationship_options fetch_many_relationship_options
97
-
98
- def fetch_through_relationship_options(field, options)
99
- field[:through] = options.fetch(:through)
100
- field[:primary_id_field] = options.fetch(:primary_id_field)
101
- field[:foreign_id_field] = options.fetch(:foreign_id_field)
102
- field[:primary_key] = options.fetch(:primary_key)
103
- field[:foreign_key] = options.fetch(:foreign_key)
104
- end
105
-
106
- def fetch_relationship_options(field, options)
107
- field[:relationship] = options.fetch(:relationship)
108
-
109
- send(
110
- "fetch_#{options.fetch(:relationship)}_relationship_options",
111
- field,
112
- options
113
- )
65
+ def table
66
+ Contentful::DatabaseImporter.database[table_name]
114
67
  end
115
68
 
116
69
  def all
117
70
  entries = []
118
- rows = Contentful::DatabaseImporter.database[table_name].all
71
+ rows = table.where(query).all
119
72
  rows.each_with_index do |row, index|
120
73
  entries << new(row, index)
121
74
  end
@@ -0,0 +1,65 @@
1
+ module Contentful
2
+ module DatabaseImporter
3
+ # Field related Class Methods for Resource
4
+ module ResourceFieldClassMethods
5
+ def field(database_name, options = {})
6
+ @fields ||= []
7
+ @fields << prepare_field(database_name, options)
8
+ end
9
+
10
+ def fields
11
+ @fields || []
12
+ end
13
+
14
+ def relationship_fields
15
+ @fields.select { |f| resource?(f[:type]) }
16
+ end
17
+
18
+ def prepare_standard_field_options(database_name, options)
19
+ {
20
+ db_name: database_name,
21
+ maps_to: options.fetch(:maps_to, database_name),
22
+ name: options.fetch(:name, database_name),
23
+ type: options.fetch(:type),
24
+ pre_process: options.fetch(:pre_process, nil),
25
+ exclude_from_output: options.fetch(:exclude_from_output, false)
26
+ }
27
+ end
28
+
29
+ def prepare_field(database_name, options)
30
+ field = prepare_standard_field_options(database_name, options)
31
+ field[:item_type] = options.fetch(:item_type) if field[:type] == :array
32
+ fetch_relationship_options(
33
+ field,
34
+ options
35
+ ) if options.fetch(:relationship, false)
36
+
37
+ field
38
+ end
39
+
40
+ def fetch_many_relationship_options(field, options)
41
+ field[:id_field] = options.fetch(:id_field)
42
+ field[:key] = options.fetch(:key)
43
+ end
44
+ alias fetch_one_relationship_options fetch_many_relationship_options
45
+
46
+ def fetch_through_relationship_options(field, options)
47
+ field[:through] = options.fetch(:through)
48
+ field[:primary_id_field] = options.fetch(:primary_id_field)
49
+ field[:foreign_id_field] = options.fetch(:foreign_id_field)
50
+ field[:primary_key] = options.fetch(:primary_key)
51
+ field[:foreign_key] = options.fetch(:foreign_key)
52
+ end
53
+
54
+ def fetch_relationship_options(field, options)
55
+ field[:relationship] = options.fetch(:relationship)
56
+
57
+ send(
58
+ "fetch_#{options.fetch(:relationship)}_relationship_options",
59
+ field,
60
+ options
61
+ )
62
+ end
63
+ end
64
+ end
65
+ end
@@ -1,5 +1,5 @@
1
1
  module Contentful
2
2
  module DatabaseImporter
3
- VERSION = '0.1.0'.freeze
3
+ VERSION = '0.2.0'.freeze
4
4
  end
5
5
  end
@@ -37,10 +37,14 @@ module Contentful
37
37
  JsonGenerator.generate_json!
38
38
  end
39
39
 
40
- def self.run_bootstrap!(file, json)
41
- file.write(json)
40
+ def self.generate_json_file!
41
+ file = Tempfile.new("import_#{config.space_name}")
42
+ file.write(generate_json!)
42
43
  file.close
44
+ file
45
+ end
43
46
 
47
+ def self.bootstrap_create_space!(file)
44
48
  Contentful::Bootstrap::CommandRunner.new.create_space(
45
49
  config.space_name,
46
50
  json_template: file.path
@@ -49,11 +53,24 @@ module Contentful
49
53
  file.unlink
50
54
  end
51
55
 
56
+ def self.bootstrap_update_space!(file)
57
+ Contentful::Bootstrap::CommandRunner.new.update_space(
58
+ config.space_id,
59
+ json_template: file.path,
60
+ skip_content_types: config.skip_content_types
61
+ )
62
+ ensure
63
+ file.unlink
64
+ end
65
+
52
66
  def self.run!
53
- json = generate_json!
67
+ raise 'Configuration is incomplete' unless config.complete_for_run?
68
+ bootstrap_create_space!(generate_json_file!)
69
+ end
54
70
 
55
- file = Tempfile.new("import_#{config.space_name}")
56
- run_bootstrap!(file, json)
71
+ def self.update_space!
72
+ raise 'Configuration is incomplete' unless config.complete_for_update?
73
+ bootstrap_update_space!(generate_json_file!)
57
74
  end
58
75
  end
59
76
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: contentful-database-importer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Contentful GmbH (David Litvak Bruno)
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-11-03 00:00:00.000000000 Z
11
+ date: 2016-11-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: contentful_bootstrap
@@ -178,6 +178,20 @@ dependencies:
178
178
  - - ">="
179
179
  - !ruby/object:Gem::Version
180
180
  version: '0'
181
+ - !ruby/object:Gem::Dependency
182
+ name: guard-rubocop
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
181
195
  - !ruby/object:Gem::Dependency
182
196
  name: pry
183
197
  requirement: !ruby/object:Gem::Requirement
@@ -202,6 +216,7 @@ files:
202
216
  - ".gitignore"
203
217
  - ".rspec"
204
218
  - ".travis.yml"
219
+ - CHANGELOG.md
205
220
  - CODE_OF_CONDUCT.md
206
221
  - CONTRIBUTING.md
207
222
  - Gemfile
@@ -221,6 +236,7 @@ files:
221
236
  - lib/contentful/database_importer/resource_bootstrap_methods.rb
222
237
  - lib/contentful/database_importer/resource_class_methods.rb
223
238
  - lib/contentful/database_importer/resource_coercions.rb
239
+ - lib/contentful/database_importer/resource_field_class_methods.rb
224
240
  - lib/contentful/database_importer/resource_relationships.rb
225
241
  - lib/contentful/database_importer/support.rb
226
242
  - lib/contentful/database_importer/version.rb