From cd8d63ad14eb884aba8d4bc6e65a388ceb63b3ac Mon Sep 17 00:00:00 2001 From: "Patrick J. Cherry" Date: Wed, 10 Nov 2021 09:28:41 +0000 Subject: [PATCH 01/27] Remove load rake task --- lib/contentful_migrations.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/contentful_migrations.rb b/lib/contentful_migrations.rb index f83186e..ae95c33 100644 --- a/lib/contentful_migrations.rb +++ b/lib/contentful_migrations.rb @@ -6,4 +6,4 @@ require 'contentful_migrations/migration' require 'contentful_migrations/migrator' -load 'tasks/contentful_migrations.rake' if defined?(Rails) +# load 'tasks/contentful_migrations.rake' if defined?(Rails) From f53fd00e8f223f532dc70c1361d2514797b6d88f Mon Sep 17 00:00:00 2001 From: "Patrick J. Cherry" Date: Wed, 10 Nov 2021 12:04:05 +0000 Subject: [PATCH 02/27] Replace rake loading with railtie --- lib/contentful_migrations.rb | 2 +- lib/contentful_migrations/railtie.rb | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 lib/contentful_migrations/railtie.rb diff --git a/lib/contentful_migrations.rb b/lib/contentful_migrations.rb index ae95c33..e298774 100644 --- a/lib/contentful_migrations.rb +++ b/lib/contentful_migrations.rb @@ -6,4 +6,4 @@ require 'contentful_migrations/migration' require 'contentful_migrations/migrator' -# load 'tasks/contentful_migrations.rake' if defined?(Rails) +require 'contentful_migrations/railtie' if defined?(Rails::Railtie) diff --git a/lib/contentful_migrations/railtie.rb b/lib/contentful_migrations/railtie.rb new file mode 100644 index 0000000..704bc43 --- /dev/null +++ b/lib/contentful_migrations/railtie.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module ContentfulMiagrations + class Railtie < Rails::Railtie + rake_tasks do + load File.join(File.dirname(__FILE__), '..', 'tasks', 'contentful_migrations.rake') + end + + generators do + require_relative "../generators/contentful_migration_generator" + end + end +end From 6e36bcdecf7ffb875480ac1e1952b3d915924c86 Mon Sep 17 00:00:00 2001 From: "Patrick J. Cherry" Date: Wed, 10 Nov 2021 12:10:07 +0000 Subject: [PATCH 03/27] Use rails env --- lib/tasks/contentful_migrations.rake | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/tasks/contentful_migrations.rake b/lib/tasks/contentful_migrations.rake index 57f653c..c03518d 100644 --- a/lib/tasks/contentful_migrations.rake +++ b/lib/tasks/contentful_migrations.rake @@ -1,17 +1,17 @@ require 'contentful_migrations' namespace :contentful_migrations do desc 'Migrate the contentful space, runs all pending migrations' - task :migrate, [:contentful_space] do |_t, _args| + task migrate: :environment do |_t, _args| ContentfulMigrations::Migrator.migrate end desc 'Rollback previous contentful migration' - task :rollback, [:contentful_space] do |_t, _args| + task rollback: :environment do |_t, _args| ContentfulMigrations::Migrator.rollback end desc 'List any pending contentful migrations' - task :pending, [:contentful_space] do |_t, _args| + task pending: :environment do |_t, _args| ContentfulMigrations::Migrator.pending end end From b9cd6a1e818569635bc4b8b53ba64942ae6f45de Mon Sep 17 00:00:00 2001 From: "Patrick J. Cherry" Date: Wed, 10 Nov 2021 12:50:37 +0000 Subject: [PATCH 04/27] Fix path for generator --- lib/contentful_migrations/railtie.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/contentful_migrations/railtie.rb b/lib/contentful_migrations/railtie.rb index 704bc43..d2f22cf 100644 --- a/lib/contentful_migrations/railtie.rb +++ b/lib/contentful_migrations/railtie.rb @@ -7,7 +7,7 @@ class Railtie < Rails::Railtie end generators do - require_relative "../generators/contentful_migration_generator" + require_relative '../generators/contentful_migration/contentful_migration_generator' end end end From e1adab6cd27b30e3ba33201a0925c3d46fe6502b Mon Sep 17 00:00:00 2001 From: "Patrick J. Cherry" Date: Wed, 10 Nov 2021 14:35:28 +0000 Subject: [PATCH 05/27] In Ruby 3, named params need to be splatted --- lib/contentful_migrations/migrator.rb | 6 +++--- .../contentful_migrations/migration_content_type_spec.rb | 4 ++-- spec/lib/contentful_migrations/migrator_spec.rb | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/contentful_migrations/migrator.rb b/lib/contentful_migrations/migrator.rb index 08f2f7e..a7ef906 100644 --- a/lib/contentful_migrations/migrator.rb +++ b/lib/contentful_migrations/migrator.rb @@ -11,15 +11,15 @@ def initialize(migrations_path) DEFAULT_MIGRATION_PATH = 'db/contentful_migrations'.freeze def self.migrate(args = {}) - new(parse_options(args)).migrate + new(**parse_options(args)).migrate end def self.rollback(args = {}) - new(parse_options(args)).rollback + new(**parse_options(args)).rollback end def self.pending(args = {}) - new(parse_options(args)).pending + new(**parse_options(args)).pending end attr_reader :migrations_path, :access_token, :space_id, :client, :space, :env_id, diff --git a/spec/lib/contentful_migrations/migration_content_type_spec.rb b/spec/lib/contentful_migrations/migration_content_type_spec.rb index 154f4f6..273f18c 100644 --- a/spec/lib/contentful_migrations/migration_content_type_spec.rb +++ b/spec/lib/contentful_migrations/migration_content_type_spec.rb @@ -13,7 +13,7 @@ describe '#initialize' do it 'sets name and version' do - migrator = described_class.new(defaults) + migrator = described_class.new(**defaults) expect(migrator.client).to eq(client) expect(migrator.space).to eq(space) @@ -22,7 +22,7 @@ end describe '#resolve' do - subject { described_class.new(defaults) } + subject { described_class.new(**defaults) } let(:content_types) { double(:content_types) } let(:migration_content_type) { double(:migration_content_type) } diff --git a/spec/lib/contentful_migrations/migrator_spec.rb b/spec/lib/contentful_migrations/migrator_spec.rb index a7d51f1..9814406 100644 --- a/spec/lib/contentful_migrations/migrator_spec.rb +++ b/spec/lib/contentful_migrations/migrator_spec.rb @@ -55,7 +55,7 @@ describe '#initialize' do it 'sets name and version' do - migrator = described_class.new(defaults) + migrator = described_class.new(**defaults) expect(migrator.migrations_path).to eq('spec/db/contentful_migrations') expect(migrator.access_token).to eq('access_token') @@ -65,12 +65,12 @@ end it 'raises error when invalid path' do expect do - described_class.new(defaults.merge(migrations_path: 'bad/path')) + described_class.new(**defaults.merge(migrations_path: 'bad/path')) end.to raise_error(ContentfulMigrations::Migrator::InvalidMigrationPath) end end describe '#migrate' do - subject { described_class.new(defaults) } + subject { described_class.new(**defaults) } context 'when no migrations' do before do allow(subject).to receive(:migrations).and_return([]) From 1436e83adfae82c1ad6ba2df90c87281726fe0bb Mon Sep 17 00:00:00 2001 From: "Patrick J. Cherry" Date: Wed, 10 Nov 2021 14:55:45 +0000 Subject: [PATCH 06/27] Upgrade contentful-management gem to v3 Also remove rubocop config --- .rubocop.yml | 179 ---------------------------------- Gemfile.lock | 86 +++++++++------- contentful-migrations.gemspec | 21 ++-- spec/spec_helper.rb | 8 +- 4 files changed, 65 insertions(+), 229 deletions(-) delete mode 100644 .rubocop.yml diff --git a/.rubocop.yml b/.rubocop.yml deleted file mode 100644 index d8891b8..0000000 --- a/.rubocop.yml +++ /dev/null @@ -1,179 +0,0 @@ -AllCops: - TargetRubyVersion: 2.2 - - -# Indent private/protected/public as deep as method definitions -AccessModifierIndentation: - EnforcedStyle: outdent - SupportedStyles: - - outdent - - indent - -# Align the elements of a hash literal if they span more than one line. -AlignHash: - # Alignment of entries using hash rocket as separator. Valid values are: - # - # key - left alignment of keys - # 'a' => 2 - # 'bb' => 3 - # separator - alignment of hash rockets, keys are right aligned - # 'a' => 2 - # 'bb' => 3 - # table - left alignment of keys, hash rockets, and values - # 'a' => 2 - # 'bb' => 3 - EnforcedHashRocketStyle: key - # Alignment of entries using colon as separator. Valid values are: - # - # key - left alignment of keys - # a: 0 - # bb: 1 - # separator - alignment of colons, keys are right aligned - # a: 0 - # bb: 1 - # table - left alignment of keys and values - # a: 0 - # bb: 1 - EnforcedColonStyle: key - -# Allow safe assignment in conditions. -AssignmentInCondition: - AllowSafeAssignment: true - -BlockNesting: - Max: 3 - -BracesAroundHashParameters: - EnforcedStyle: no_braces - SupportedStyles: - - braces - - no_braces - -# Indentation of `when`. -CaseIndentation: - EnforcedStyle: case - SupportedStyles: - - case - - end - IndentOneStep: false - -# Checks formatting of special comments -CommentAnnotation: - Keywords: - - TODO - - FIXME - - OPTIMIZE - - HACK - - REVIEW - -# Use empty lines between defs. -EmptyLineBetweenDefs: - # If true, this parameter means that single line method definitions don't - # need an empty line between them. - AllowAdjacentOneLineDefs: false - -Encoding: - Enabled: true - -# Align ends correctly. -Lint/EndAlignment: - # The value `keyword` means that `end` should be aligned with the matching - # keyword (if, while, etc.). - # The value `variable` means that in assignments, `end` should be aligned - # with the start of the variable on the left hand side of `=`. In all other - # situations, `end` should still be aligned with the keyword. - EnforcedStyleAlignWith: variable - SupportedStylesAlignWith: - - keyword - - variable - -# Checks use of for or each in multiline loops. -For: - EnforcedStyle: each - SupportedStyles: - - for - - each - -HashSyntax: - EnforcedStyle: ruby19 - SupportedStyles: - - ruby19 - - hash_rockets - -LambdaCall: - EnforcedStyle: call - SupportedStyles: - - call - - braces - -MethodDefParentheses: - EnforcedStyle: require_parentheses - SupportedStyles: - - require_parentheses - - require_no_parentheses - -MethodName: - EnforcedStyle: snake_case - SupportedStyles: - - snake_case - - camelCase - -NumericLiterals: - MinDigits: 10 - -# Allow safe assignment in conditions. -ParenthesesAroundCondition: - AllowSafeAssignment: true - -RedundantReturn: - # When true allows code like `return x, y`. - AllowMultipleReturnValues: false - -Semicolon: - # Allow ; to separate several expressions on the same line. - AllowAsExpressionSeparator: false - -TrailingCommaInLiteral: - EnforcedStyleForMultiline: no_comma - SupportedStylesForMultiline: - - comma - - no_comma - -Style/TrailingCommaInArguments: - EnforcedStyleForMultiline: no_comma - SupportedStylesForMultiline: - - comma - - no_comma - -# TrivialAccessors doesn't require exact name matches and doesn't allow -# predicated methods by default. -TrivialAccessors: - ExactNameMatch: false - AllowPredicates: false - Whitelist: - - to_ary - - to_a - - to_c - - to_enum - - to_h - - to_hash - - to_i - - to_int - - to_io - - to_open - - to_path - - to_proc - - to_r - - to_regexp - - to_str - - to_s - - to_sym - -VariableName: - EnforcedStyle: snake_case - SupportedStyles: - - snake_case - - camelCase - -WordArray: - Enabled: False diff --git a/Gemfile.lock b/Gemfile.lock index 13805c5..343fb27 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,67 +2,79 @@ PATH remote: . specs: contentful-migrations (0.1.4) - contentful-management (~> 2.6) - http (~> 4.1.1) + contentful-management (~> 3.0) GEM remote: https://rubygems.org/ specs: - addressable (2.7.0) + addressable (2.8.0) public_suffix (>= 2.0.2, < 5.0) - byebug (10.0.2) - coderay (1.1.2) - contentful-management (2.12.0) + byebug (11.1.3) + coderay (1.1.3) + contentful-management (3.3.0) http (> 1.0, < 5.0) json (>= 1.8, < 3.0) multi_json (~> 1) - diff-lcs (1.3) + diff-lcs (1.4.4) + docile (1.4.0) domain_name (0.5.20190701) unf (>= 0.0.5, < 1.0.0) - http (4.1.1) + ffi (1.15.4) + ffi-compiler (1.0.1) + ffi (>= 1.0.0) + rake + http (4.4.1) addressable (~> 2.3) http-cookie (~> 1.0) - http-form_data (~> 2.0) - http_parser.rb (~> 0.6.0) - http-cookie (1.0.3) + http-form_data (~> 2.2) + http-parser (~> 1.2.0) + http-cookie (1.0.4) domain_name (~> 0.5) http-form_data (2.3.0) - http_parser.rb (0.6.0) - json (2.3.0) - method_source (0.9.2) - multi_json (1.14.1) - pry (0.12.2) - coderay (~> 1.1.0) - method_source (~> 0.9.0) - public_suffix (4.0.3) - rake (12.3.2) - rspec (3.8.0) - rspec-core (~> 3.8.0) - rspec-expectations (~> 3.8.0) - rspec-mocks (~> 3.8.0) - rspec-core (3.8.0) - rspec-support (~> 3.8.0) - rspec-expectations (3.8.2) + http-parser (1.2.3) + ffi-compiler (>= 1.0, < 2.0) + json (2.6.1) + method_source (1.0.0) + multi_json (1.15.0) + pry (0.14.1) + coderay (~> 1.1) + method_source (~> 1.0) + public_suffix (4.0.6) + rake (13.0.6) + rspec (3.10.0) + rspec-core (~> 3.10.0) + rspec-expectations (~> 3.10.0) + rspec-mocks (~> 3.10.0) + rspec-core (3.10.1) + rspec-support (~> 3.10.0) + rspec-expectations (3.10.1) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.8.0) - rspec-mocks (3.8.0) + rspec-support (~> 3.10.0) + rspec-mocks (3.10.2) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.8.0) - rspec-support (3.8.0) + rspec-support (~> 3.10.0) + rspec-support (3.10.3) + simplecov (0.21.2) + docile (~> 1.1) + simplecov-html (~> 0.11) + simplecov_json_formatter (~> 0.1) + simplecov-html (0.12.3) + simplecov_json_formatter (0.1.3) unf (0.1.4) unf_ext - unf_ext (0.0.7.6) + unf_ext (0.0.8) PLATFORMS ruby DEPENDENCIES - bundler (~> 1.16) - byebug (~> 10.0.0) + bundler + byebug contentful-migrations! pry - rake (~> 12.3.0) - rspec (~> 3.6) + rake + rspec + simplecov BUNDLED WITH - 1.17.3 + 2.2.3 diff --git a/contentful-migrations.gemspec b/contentful-migrations.gemspec index 3e44cb6..fd3dc16 100644 --- a/contentful-migrations.gemspec +++ b/contentful-migrations.gemspec @@ -1,5 +1,4 @@ - -lib = File.expand_path('../lib', __FILE__) +lib = File.expand_path('lib', __dir__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'contentful_migrations/version' @@ -12,29 +11,29 @@ Gem::Specification.new do |spec| spec.summary = 'Contentful Migrations in Ruby' spec.description = 'Migration library system for Contentful API dependent on contentful-management gem and plagarized from activerecord.' - spec.homepage = "https://github.com/monkseal/contentful-migrations.rb" + spec.homepage = 'https://github.com/monkseal/contentful-migrations.rb' spec.license = 'MIT' # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host' # to allow pushing to a single host or delete this section to allow pushing to any host. if spec.respond_to?(:metadata) - spec.metadata['allowed_push_host'] = "https://rubygems.org" + spec.metadata['allowed_push_host'] = 'https://rubygems.org' else raise 'RubyGems 2.0 or newer is required to protect against ' \ 'public gem pushes.' end - spec.files = Dir["{lib,vendor}/**/*"] + spec.files = Dir['{lib,vendor}/**/*'] spec.bindir = 'exe' spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ['lib'] - spec.add_dependency 'contentful-management', '~> 2.6' - spec.add_dependency 'http', '~> 4.1.1' + spec.add_dependency 'contentful-management', '~> 3.0' + spec.add_development_dependency 'bundler' + spec.add_development_dependency 'byebug' spec.add_development_dependency 'pry' - spec.add_development_dependency 'bundler', '~> 1.16' - spec.add_development_dependency 'rake', '~> 12.3.0' - spec.add_development_dependency 'rspec', '~> 3.6' - spec.add_development_dependency 'byebug', '~> 10.0.0' + spec.add_development_dependency 'rake' + spec.add_development_dependency 'rspec' + spec.add_development_dependency 'simplecov' end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 09e1fb8..ff11645 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,10 @@ -require 'bundler/setup' require 'contentful_migrations' -require 'pry' + +require 'simplecov' +SimpleCov.start do + enable_coverage :branch + add_filter %r{^/spec/} +end RSpec.configure do |config| # Enable flags like --only-failures and --next-failure From c7bf4c1e8396728ee8f32d87f09965898d882f61 Mon Sep 17 00:00:00 2001 From: "Patrick J. Cherry" Date: Wed, 10 Nov 2021 16:25:11 +0000 Subject: [PATCH 07/27] Mega refactor --- .tool-versions | 1 + Gemfile | 6 ++- Gemfile.lock | 26 ++++++++- Rakefile | 10 ++-- bin/console | 7 +-- contentful-migrations.gemspec | 14 +++-- lib/contentful/migrations.rb | 4 +- lib/contentful_migrations.rb | 3 +- lib/contentful_migrations/migration.rb | 3 ++ .../migration_content_type.rb | 6 ++- lib/contentful_migrations/migration_proxy.rb | 10 ++-- lib/contentful_migrations/migrator.rb | 21 ++++---- .../string_refinements.rb | 54 +++++++++++++++++++ lib/contentful_migrations/utils.rb | 51 ------------------ lib/contentful_migrations/version.rb | 4 +- .../contentful_migration_generator.rb | 4 +- lib/tasks/contentful_migrations.rake | 4 +- .../20180216021826_build_test_content.rb | 2 + .../migration_content_type_spec.rb | 4 ++ .../migration_proxy_spec.rb | 4 ++ .../contentful_migrations/migration_spec.rb | 4 ++ .../contentful_migrations/migrator_spec.rb | 5 ++ spec/lib/contentful_migrations_spec.rb | 9 ---- spec/spec_helper.rb | 3 +- 24 files changed, 162 insertions(+), 97 deletions(-) create mode 100644 .tool-versions create mode 100644 lib/contentful_migrations/string_refinements.rb delete mode 100644 lib/contentful_migrations/utils.rb delete mode 100644 spec/lib/contentful_migrations_spec.rb diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..2c0c270 --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +ruby 3.0.0 diff --git a/Gemfile b/Gemfile index 7ceced1..e8b53d5 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,8 @@ -source "https://rubygems.org" +# frozen_string_literal: true -git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } +source 'https://rubygems.org' + +git_source(:github) { |repo_name| "https://github.com/#{repo_name}" } # Specify your gem's dependencies in contentful-migrations.gemspec gemspec diff --git a/Gemfile.lock b/Gemfile.lock index 343fb27..7e7d36e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -9,7 +9,9 @@ GEM specs: addressable (2.8.0) public_suffix (>= 2.0.2, < 5.0) + ast (2.4.2) byebug (11.1.3) + climate_control (1.0.1) coderay (1.1.3) contentful-management (3.3.0) http (> 1.0, < 5.0) @@ -36,11 +38,17 @@ GEM json (2.6.1) method_source (1.0.0) multi_json (1.15.0) + parallel (1.21.0) + parser (3.0.2.0) + ast (~> 2.4.1) pry (0.14.1) coderay (~> 1.1) method_source (~> 1.0) public_suffix (4.0.6) + rainbow (3.0.0) rake (13.0.6) + regexp_parser (2.1.1) + rexml (3.2.5) rspec (3.10.0) rspec-core (~> 3.10.0) rspec-expectations (~> 3.10.0) @@ -54,6 +62,20 @@ GEM diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.10.0) rspec-support (3.10.3) + rubocop (1.22.3) + parallel (~> 1.10) + parser (>= 3.0.0.0) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 1.8, < 3.0) + rexml + rubocop-ast (>= 1.12.0, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 1.4.0, < 3.0) + rubocop-ast (1.13.0) + parser (>= 3.0.1.1) + rubocop-rspec (2.6.0) + rubocop (~> 1.19) + ruby-progressbar (1.11.0) simplecov (0.21.2) docile (~> 1.1) simplecov-html (~> 0.11) @@ -63,6 +85,7 @@ GEM unf (0.1.4) unf_ext unf_ext (0.0.8) + unicode-display_width (2.1.0) PLATFORMS ruby @@ -70,10 +93,11 @@ PLATFORMS DEPENDENCIES bundler byebug + climate_control contentful-migrations! pry - rake rspec + rubocop-rspec simplecov BUNDLED WITH diff --git a/Rakefile b/Rakefile index 535c7bd..82bb534 100644 --- a/Rakefile +++ b/Rakefile @@ -1,8 +1,8 @@ -require "bundler/gem_tasks" -require "rspec/core/rake_task" +# frozen_string_literal: true + +require 'bundler/gem_tasks' +require 'rspec/core/rake_task' RSpec::Core::RakeTask.new(:spec) -task :default => :spec - # "./lib/tasks/downloader.rake" -import './lib/tasks/contentful_migrations.rake' +task default: :spec diff --git a/bin/console b/bin/console index b797d0c..5941b53 100755 --- a/bin/console +++ b/bin/console @@ -1,7 +1,8 @@ #!/usr/bin/env ruby +# frozen_string_literal: true -require "bundler/setup" -require "contentful_migrations" +require 'bundler/setup' +require 'contentful_migrations' # You can add fixtures and/or initialization code here to make experimenting # with your gem easier. You can also use a different console, if you like. @@ -10,5 +11,5 @@ require "contentful_migrations" # require "pry" # Pry.start -require "irb" +require 'irb' IRB.start(__FILE__) diff --git a/contentful-migrations.gemspec b/contentful-migrations.gemspec index fd3dc16..9f269dc 100644 --- a/contentful-migrations.gemspec +++ b/contentful-migrations.gemspec @@ -1,3 +1,5 @@ +# frozen_string_literal: true + lib = File.expand_path('lib', __dir__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'contentful_migrations/version' @@ -14,6 +16,8 @@ Gem::Specification.new do |spec| spec.homepage = 'https://github.com/monkseal/contentful-migrations.rb' spec.license = 'MIT' + spec.required_ruby_version = '>= 2.7.0' + # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host' # to allow pushing to a single host or delete this section to allow pushing to any host. if spec.respond_to?(:metadata) @@ -23,17 +27,17 @@ Gem::Specification.new do |spec| 'public gem pushes.' end - spec.files = Dir['{lib,vendor}/**/*'] - spec.bindir = 'exe' - spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } + spec.files = Dir['lib/**/*'] + spec.bindir = 'bin' + spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.require_paths = ['lib'] - spec.add_dependency 'contentful-management', '~> 3.0' spec.add_development_dependency 'bundler' spec.add_development_dependency 'byebug' + spec.add_development_dependency 'climate_control' spec.add_development_dependency 'pry' - spec.add_development_dependency 'rake' spec.add_development_dependency 'rspec' + spec.add_development_dependency 'rubocop-rspec' spec.add_development_dependency 'simplecov' end diff --git a/lib/contentful/migrations.rb b/lib/contentful/migrations.rb index 0be713c..d3948e8 100644 --- a/lib/contentful/migrations.rb +++ b/lib/contentful/migrations.rb @@ -1 +1,3 @@ -require File.expand_path('../../contentful_migrations', __FILE__) +# frozen_string_literal: true + +require File.expand_path('../contentful_migrations', __dir__) diff --git a/lib/contentful_migrations.rb b/lib/contentful_migrations.rb index e298774..65a7b90 100644 --- a/lib/contentful_migrations.rb +++ b/lib/contentful_migrations.rb @@ -1,5 +1,6 @@ +# frozen_string_literal: true + require 'contentful/management' -require 'contentful_migrations/utils' require 'contentful_migrations/version' require 'contentful_migrations/migration_content_type' require 'contentful_migrations/migration_proxy' diff --git a/lib/contentful_migrations/migration.rb b/lib/contentful_migrations/migration.rb index 2ae6445..31d60da 100644 --- a/lib/contentful_migrations/migration.rb +++ b/lib/contentful_migrations/migration.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module ContentfulMigrations class Migration attr_reader :name, :version, :contentful_client, :contentful_space @@ -34,6 +36,7 @@ def record_migration(migration_content_type) def erase_migration(migration_content_type) entry = migration_content_type.entries.all.find { |m| m.version.to_i == version.to_i } return unless entry + entry.unpublish entry.destroy entry diff --git a/lib/contentful_migrations/migration_content_type.rb b/lib/contentful_migrations/migration_content_type.rb index d9bfd19..02a3eba 100644 --- a/lib/contentful_migrations/migration_content_type.rb +++ b/lib/contentful_migrations/migration_content_type.rb @@ -1,6 +1,8 @@ +# frozen_string_literal: true + module ContentfulMigrations class MigrationContentType - DEFAULT_MIGRATION_CONTENT_TYPE = 'migrations'.freeze + DEFAULT_MIGRATION_CONTENT_TYPE = 'migrations' attr_reader :access_token, :space_id, :client, :space, :migration_content_type_name, :logger @@ -19,7 +21,7 @@ def resolve @migration_content_type ||= find_or_create_migration_content_type end - private + private def find_or_create_migration_content_type content_type = space.content_types.find(migration_content_type_name) diff --git a/lib/contentful_migrations/migration_proxy.rb b/lib/contentful_migrations/migration_proxy.rb index a9d4e69..f2bdfdd 100644 --- a/lib/contentful_migrations/migration_proxy.rb +++ b/lib/contentful_migrations/migration_proxy.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + require 'forwardable' -require 'contentful_migrations/utils' +require 'contentful_migrations/string_refinements' module ContentfulMigrations # MigrationProxy is used to defer loading of the actual migration classes @@ -8,7 +10,7 @@ module ContentfulMigrations MigrationProxy = Struct.new(:name, :version, :filename, :scope) do extend Forwardable - include Utils + using StringRefinements def initialize(name, version, filename, scope) super @@ -29,7 +31,7 @@ def migration def load_migration require(File.expand_path(filename)) - constantize(name).new(name, version) - end + name.constantize.new(name, version) + end end end diff --git a/lib/contentful_migrations/migrator.rb b/lib/contentful_migrations/migrator.rb index a7ef906..0040c81 100644 --- a/lib/contentful_migrations/migrator.rb +++ b/lib/contentful_migrations/migrator.rb @@ -1,14 +1,19 @@ +# frozen_string_literal: true + +require 'contentful/management/client' +require 'contentful_migrations/string_refinements' + module ContentfulMigrations class Migrator - include Utils + using StringRefinements - class InvalidMigrationPath < StandardError #:nodoc: + class InvalidMigrationPath < StandardError # :nodoc: def initialize(migrations_path) super("#{migrations_path} is not a valid directory.") end end - DEFAULT_MIGRATION_PATH = 'db/contentful_migrations'.freeze + DEFAULT_MIGRATION_PATH = 'db/contentful_migrations' def self.migrate(args = {}) new(**parse_options(args)).migrate @@ -45,9 +50,7 @@ def initialize(migrations_path:, def migrate runnable = migrations(migrations_path).reject { |m| ran?(m) } - if runnable.empty? - logger.info('No migrations to run, everything up to date!') - end + logger.info('No migrations to run, everything up to date!') if runnable.empty? runnable.each do |migration| logger.info("running migration #{migration.version} #{migration.name} ") @@ -73,7 +76,7 @@ def pending end end - private + private def self.parse_options(args) { @@ -81,7 +84,7 @@ def self.parse_options(args) access_token: ENV['CONTENTFUL_MANAGEMENT_ACCESS_TOKEN'], space_id: ENV['CONTENTFUL_SPACE_ID'], migration_content_type_name: MigrationContentType::DEFAULT_MIGRATION_CONTENT_TYPE, - logger: Logger.new(STDOUT) + logger: Logger.new($stdout) }.merge(args) end @@ -120,7 +123,7 @@ def migrations(paths) paths = Array(paths) migrations = migration_files(paths).map do |file| version, name, scope = parse_migration_filename(file) - ContentfulMigrations::MigrationProxy.new(camelize(name), version.to_i, file, scope) + ContentfulMigrations::MigrationProxy.new(name.camelize, version.to_i, file, scope) end migrations.sort_by(&:version) diff --git a/lib/contentful_migrations/string_refinements.rb b/lib/contentful_migrations/string_refinements.rb new file mode 100644 index 0000000..1e5dd29 --- /dev/null +++ b/lib/contentful_migrations/string_refinements.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +module ContentfulMigrations + module StringRefinements + refine String do + # This method was taken from ActiveSupport::Inflector to avoid having + # a dependency on ActiveSupport in this project. + # http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-constantize + def camelize(uppercase_first_letter: true) + string = if uppercase_first_letter + sub(/^[a-z\d]*/, &:capitalize) + else + sub(/^((?=\b|[A-Z_])|\w)/, &:downcase) + end + string.gsub!(%r{(?:_|(/))([a-z\d]*)}i) { "#{Regexp.last_match(1)}#{Regexp.last_match(2).capitalize}" } + string.gsub!('/', '::') + string + end + + # This method was taken from ActiveSupport::Inflector to avoid having + # a dependency on ActiveSupport in this project. + # http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-constantize + def constantize + names = split('::') + + # Trigger a built-in NameError exception including the ill-formed constant in the message. + Object.const_get(self) if names.empty? + + # Remove the first blank element in case of '::ClassName' notation. + names.shift if names.size > 1 && names.first.empty? + + names.inject(Object) do |constant, name| + if constant == Object + constant.const_get(name) + else + candidate = constant.const_get(name) + next candidate if constant.const_defined?(name, false) + next candidate unless Object.const_defined?(name) + + # Go down the ancestors to check if it is owned directly. The check + # stops when we reach Object or the end of ancestors tree. + constant = constant.ancestors.each_with_object(constant) do |ancestor, const| + break const if ancestor == Object + break ancestor if ancestor.const_defined?(name, false) + end + + # owner is in Object, so raise + constant.const_get(name, false) + end + end + end + end + end +end diff --git a/lib/contentful_migrations/utils.rb b/lib/contentful_migrations/utils.rb deleted file mode 100644 index ddc521f..0000000 --- a/lib/contentful_migrations/utils.rb +++ /dev/null @@ -1,51 +0,0 @@ -module ContentfulMigrations - module Utils - # This method was taken from ActiveSupport::Inflector to avoid having - # a dependency on ActiveSupport in this project. - # http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-constantize - def camelize(term, uppercase_first_letter = true) - string = term.to_s - string = if uppercase_first_letter - string.sub(/^[a-z\d]*/, &:capitalize) - else - string.sub(/^((?=\b|[A-Z_])|\w)/, &:downcase) - end - string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{Regexp.last_match(1)}#{Regexp.last_match(2).capitalize}" } - string.gsub!('/'.freeze, '::'.freeze) - string - end - # This method was taken from ActiveSupport::Inflector to avoid having - # a dependency on ActiveSupport in this project. - # http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-constantize - - def constantize(camel_cased_word) - names = camel_cased_word.split('::'.freeze) - - # Trigger a built-in NameError exception including the ill-formed constant in the message. - Object.const_get(camel_cased_word) if names.empty? - - # Remove the first blank element in case of '::ClassName' notation. - names.shift if names.size > 1 && names.first.empty? - - names.inject(Object) do |constant, name| - if constant == Object - constant.const_get(name) - else - candidate = constant.const_get(name) - next candidate if constant.const_defined?(name, false) - next candidate unless Object.const_defined?(name) - - # Go down the ancestors to check if it is owned directly. The check - # stops when we reach Object or the end of ancestors tree. - constant = constant.ancestors.each_with_object(constant) do |ancestor, const| - break const if ancestor == Object - break ancestor if ancestor.const_defined?(name, false) - end - - # owner is in Object, so raise - constant.const_get(name, false) - end - end - end - end -end diff --git a/lib/contentful_migrations/version.rb b/lib/contentful_migrations/version.rb index 39175c9..22acd50 100644 --- a/lib/contentful_migrations/version.rb +++ b/lib/contentful_migrations/version.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module ContentfulMigrations - VERSION = '0.1.4'.freeze + VERSION = '0.1.4' end diff --git a/lib/generators/contentful_migration/contentful_migration_generator.rb b/lib/generators/contentful_migration/contentful_migration_generator.rb index 579955f..283cb67 100644 --- a/lib/generators/contentful_migration/contentful_migration_generator.rb +++ b/lib/generators/contentful_migration/contentful_migration_generator.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'rails/generators' class ContentfulMigrationGenerator < Rails::Generators::NamedBase @@ -22,7 +24,7 @@ def down end end - FILE + FILE end def next_migration_number diff --git a/lib/tasks/contentful_migrations.rake b/lib/tasks/contentful_migrations.rake index c03518d..0211137 100644 --- a/lib/tasks/contentful_migrations.rake +++ b/lib/tasks/contentful_migrations.rake @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'contentful_migrations' namespace :contentful_migrations do desc 'Migrate the contentful space, runs all pending migrations' @@ -11,7 +13,7 @@ namespace :contentful_migrations do end desc 'List any pending contentful migrations' - task pending: :environment do |_t, _args| + task pending: :environment do |_t, _args| ContentfulMigrations::Migrator.pending end end diff --git a/spec/db/contentful_migrations/20180216021826_build_test_content.rb b/spec/db/contentful_migrations/20180216021826_build_test_content.rb index 89770a4..b42831b 100644 --- a/spec/db/contentful_migrations/20180216021826_build_test_content.rb +++ b/spec/db/contentful_migrations/20180216021826_build_test_content.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class BuildTestContent < ContentfulMigrations::Migration def up with_space do |space| diff --git a/spec/lib/contentful_migrations/migration_content_type_spec.rb b/spec/lib/contentful_migrations/migration_content_type_spec.rb index 273f18c..ec26f13 100644 --- a/spec/lib/contentful_migrations/migration_content_type_spec.rb +++ b/spec/lib/contentful_migrations/migration_content_type_spec.rb @@ -1,3 +1,7 @@ +# frozen_string_literal: true + +require 'contentful_migrations/migration_content_type' + RSpec.describe ContentfulMigrations::MigrationContentType do let(:client) { double(:client) } let(:space) { double(:space) } diff --git a/spec/lib/contentful_migrations/migration_proxy_spec.rb b/spec/lib/contentful_migrations/migration_proxy_spec.rb index 05ee319..2bd3d76 100644 --- a/spec/lib/contentful_migrations/migration_proxy_spec.rb +++ b/spec/lib/contentful_migrations/migration_proxy_spec.rb @@ -1,3 +1,7 @@ +# frozen_string_literal: true + +require 'contentful_migrations/migration_proxy' + RSpec.describe ContentfulMigrations::MigrationProxy do let(:version) { '20180216021826' } let(:filename) { 'spec/db/contentful_migrations/20180216021826_build_test_content.rb' } diff --git a/spec/lib/contentful_migrations/migration_spec.rb b/spec/lib/contentful_migrations/migration_spec.rb index 8d3f702..a876ac3 100644 --- a/spec/lib/contentful_migrations/migration_spec.rb +++ b/spec/lib/contentful_migrations/migration_spec.rb @@ -1,3 +1,7 @@ +# frozen_string_literal: true + +require 'contentful_migrations/migration' + RSpec.describe ContentfulMigrations::Migration do let(:version) { '20180216021826' } let(:name) { 'BuildTestContent' } diff --git a/spec/lib/contentful_migrations/migrator_spec.rb b/spec/lib/contentful_migrations/migrator_spec.rb index 9814406..e153dfa 100644 --- a/spec/lib/contentful_migrations/migrator_spec.rb +++ b/spec/lib/contentful_migrations/migrator_spec.rb @@ -1,3 +1,7 @@ +# frozen_string_literal: true + +require 'contentful_migrations/migrator' + RSpec.describe ContentfulMigrations::Migrator do ######## ## Class methods @@ -69,6 +73,7 @@ end.to raise_error(ContentfulMigrations::Migrator::InvalidMigrationPath) end end + describe '#migrate' do subject { described_class.new(**defaults) } context 'when no migrations' do diff --git a/spec/lib/contentful_migrations_spec.rb b/spec/lib/contentful_migrations_spec.rb deleted file mode 100644 index 7ec143c..0000000 --- a/spec/lib/contentful_migrations_spec.rb +++ /dev/null @@ -1,9 +0,0 @@ -RSpec.describe ContentfulMigrations do - it 'has a version number' do - expect(ContentfulMigrations::VERSION).not_to be nil - end - - it 'does something useful' do - expect(true).to eq(true) - end -end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ff11645..9bb93a4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,7 @@ -require 'contentful_migrations' +# frozen_string_literal: true require 'simplecov' + SimpleCov.start do enable_coverage :branch add_filter %r{^/spec/} From 3b7aff3b3cfc27f1b9a71ca90a0ed36ea4912305 Mon Sep 17 00:00:00 2001 From: "Patrick J. Cherry" Date: Wed, 10 Nov 2021 16:31:45 +0000 Subject: [PATCH 08/27] Version bump to v0.2.0 --- .gitignore | 1 + lib/contentful_migrations/version.rb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index d1b9172..7156e74 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ /pkg/ /spec/reports/ /tmp/ +/vendor/ # rspec failure tracking .rspec_status diff --git a/lib/contentful_migrations/version.rb b/lib/contentful_migrations/version.rb index 22acd50..218ed38 100644 --- a/lib/contentful_migrations/version.rb +++ b/lib/contentful_migrations/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module ContentfulMigrations - VERSION = '0.1.4' + VERSION = '0.2.0' end From a91cefcddad5adcd3d9a5597b045973009e32831 Mon Sep 17 00:00:00 2001 From: "Patrick J. Cherry" Date: Wed, 10 Nov 2021 16:37:32 +0000 Subject: [PATCH 09/27] Add circle ci --- .circleci/config.yml | 56 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 .circleci/config.yml diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..9ae23cf --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,56 @@ +--- +version: 2.1 + +jobs: + rubocop: + docker: + - image: 'cimg/base:2021.10' + parameters: + ruby-version: + type: string + steps: + - checkout + - ruby/install: + version: << parameters.ruby-version >> + - ruby/install-deps + - ruby/rubocop-check: + format: progress + label: Inspecting with Rubocop + + test: + docker: + - image: 'cimg/base:2021.10' + parameters: + ruby-version: + type: string + steps: + - checkout + - ruby/install: + version: << parameters.ruby-version >> + - ruby/install-deps + - ruby/rspec-test + - store_artifacts: + path: coverage + +orbs: + ruby: circleci/ruby@1 + +workflows: + code_quality: + jobs: + - rubocop: + matrix: + parameters: + ruby-version: ["2.7", "3.0"] + filters: + branches: + ignore: + - master + - main + test: + jobs: + - test: + matrix: + parameters: + ruby-version: ["2.7", "3.0"] + From 589a40c0842af2ac4685abe4b28fcb27ce7879a7 Mon Sep 17 00:00:00 2001 From: "Patrick J. Cherry" Date: Wed, 10 Nov 2021 16:44:16 +0000 Subject: [PATCH 10/27] Fix version --- Gemfile.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile.lock b/Gemfile.lock index 7e7d36e..971cc47 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - contentful-migrations (0.1.4) + contentful-migrations (0.2.0) contentful-management (~> 3.0) GEM From 31c34f96011be5ddfb39bdb70939f1360576a0b6 Mon Sep 17 00:00:00 2001 From: "Patrick J. Cherry" Date: Wed, 10 Nov 2021 16:46:15 +0000 Subject: [PATCH 11/27] Add junit formatter for circleci --- Gemfile.lock | 3 +++ contentful-migrations.gemspec | 1 + 2 files changed, 4 insertions(+) diff --git a/Gemfile.lock b/Gemfile.lock index 971cc47..03bcc29 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -62,6 +62,8 @@ GEM diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.10.0) rspec-support (3.10.3) + rspec_junit_formatter (0.4.1) + rspec-core (>= 2, < 4, != 2.12.0) rubocop (1.22.3) parallel (~> 1.10) parser (>= 3.0.0.0) @@ -97,6 +99,7 @@ DEPENDENCIES contentful-migrations! pry rspec + rspec_junit_formatter rubocop-rspec simplecov diff --git a/contentful-migrations.gemspec b/contentful-migrations.gemspec index 9f269dc..534a844 100644 --- a/contentful-migrations.gemspec +++ b/contentful-migrations.gemspec @@ -38,6 +38,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'climate_control' spec.add_development_dependency 'pry' spec.add_development_dependency 'rspec' + spec.add_development_dependency 'rspec_junit_formatter' spec.add_development_dependency 'rubocop-rspec' spec.add_development_dependency 'simplecov' end From 22193deb89a19fab7e78c50542468480a5e5d2b4 Mon Sep 17 00:00:00 2001 From: "Patrick J. Cherry" Date: Wed, 10 Nov 2021 16:55:16 +0000 Subject: [PATCH 12/27] Add a standard rubocop config --- .rubocop.yml | 24 ++++++++++++++++++++++++ .tool-versions | 1 - contentful-migrations.gemspec | 2 +- lib/contentful_migrations/migrator.rb | 2 +- 4 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 .rubocop.yml delete mode 100644 .tool-versions diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..8702167 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,24 @@ +--- +AllCops: + TargetRubyVersion: "2.7" + NewCops: enable + Exclude: + - "Gemfile" + - "Guardfile" + - "bin/**/*" + - "db/**/*" + - "node_modules/**/*" + - "public/**/*" + - "test/**/*" + - "tmp/**/*" + - "vendor/**/*" + +Style/Documentation: + Enabled: false + +Metrics/BlockLength: + Exclude: + - '*.gemspec' + - config/routes.rb + - spec/**/*_spec.rb + diff --git a/.tool-versions b/.tool-versions deleted file mode 100644 index 2c0c270..0000000 --- a/.tool-versions +++ /dev/null @@ -1 +0,0 @@ -ruby 3.0.0 diff --git a/contentful-migrations.gemspec b/contentful-migrations.gemspec index 534a844..ac3cdaf 100644 --- a/contentful-migrations.gemspec +++ b/contentful-migrations.gemspec @@ -24,7 +24,7 @@ Gem::Specification.new do |spec| spec.metadata['allowed_push_host'] = 'https://rubygems.org' else raise 'RubyGems 2.0 or newer is required to protect against ' \ - 'public gem pushes.' + 'public gem pushes.' end spec.files = Dir['lib/**/*'] diff --git a/lib/contentful_migrations/migrator.rb b/lib/contentful_migrations/migrator.rb index 0040c81..bd60770 100644 --- a/lib/contentful_migrations/migrator.rb +++ b/lib/contentful_migrations/migrator.rb @@ -139,7 +139,7 @@ def migration_content_type ).resolve end - MIGRATION_FILENAME_REGEX = /\A([0-9]+)_([_a-z0-9]*)\.?([_a-z0-9]*)?\.rb\z/ + MIGRATION_FILENAME_REGEX = /\A([0-9]+)_([_a-z0-9]*)\.?([_a-z0-9]*)?\.rb\z/.freeze def parse_migration_filename(filename) File.basename(filename).scan(MIGRATION_FILENAME_REGEX).first From 06be75b1905ab11e311773a1fc6179c2834b271a Mon Sep 17 00:00:00 2001 From: "Patrick J. Cherry" Date: Wed, 10 Nov 2021 16:58:27 +0000 Subject: [PATCH 13/27] Tidy rubocop config a little --- .rubocop.yml | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 8702167..df8a386 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -3,15 +3,9 @@ AllCops: TargetRubyVersion: "2.7" NewCops: enable Exclude: - - "Gemfile" - - "Guardfile" - "bin/**/*" - - "db/**/*" - - "node_modules/**/*" - - "public/**/*" - - "test/**/*" - - "tmp/**/*" - "vendor/**/*" + - "spec/db/contentful_migrations/*.rb" Style/Documentation: Enabled: false @@ -19,6 +13,5 @@ Style/Documentation: Metrics/BlockLength: Exclude: - '*.gemspec' - - config/routes.rb - - spec/**/*_spec.rb + - "spec/**/*_spec.rb" From 826e1b34c0e4488505a87d3f99ce85f295bef72b Mon Sep 17 00:00:00 2001 From: "Patrick J. Cherry" Date: Wed, 10 Nov 2021 17:01:26 +0000 Subject: [PATCH 14/27] Add rubocop todo file --- .rubocop.yml | 2 ++ .rubocop_todo.yml | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 .rubocop_todo.yml diff --git a/.rubocop.yml b/.rubocop.yml index df8a386..db10d69 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,4 +1,6 @@ --- +inherit_from: .rubocop_todo.yml + AllCops: TargetRubyVersion: "2.7" NewCops: enable diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 0000000..45b2950 --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,50 @@ +# This configuration was generated by +# `rubocop --auto-gen-config` +# on 2021-11-10 17:01:10 UTC using RuboCop version 1.22.3. +# The point is for the user to remove these configuration records +# one by one as the offenses are removed from the code base. +# Note that changes in the inspected code, or installation of new +# versions of RuboCop, may require this file to be generated again. + +# Offense count: 1 +Lint/IneffectiveAccessModifier: + Exclude: + - 'lib/contentful_migrations/migrator.rb' + +# Offense count: 2 +# Configuration parameters: IgnoredMethods, CountRepeatedAttributes. +Metrics/AbcSize: + Max: 23 + +# Offense count: 1 +# Configuration parameters: CountComments, CountAsOne. +Metrics/ClassLength: + Max: 110 + +# Offense count: 1 +# Configuration parameters: IgnoredMethods. +Metrics/CyclomaticComplexity: + Max: 11 + +# Offense count: 2 +# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods. +Metrics/MethodLength: + Max: 17 + +# Offense count: 2 +# Configuration parameters: CountKeywordArgs. +Metrics/ParameterLists: + MaxOptionalParameters: 4 + Max: 6 + +# Offense count: 1 +# Configuration parameters: IgnoredMethods. +Metrics/PerceivedComplexity: + Max: 12 + +# Offense count: 1 +# Configuration parameters: EnforcedStyleForLeadingUnderscores. +# SupportedStylesForLeadingUnderscores: disallowed, required, optional +Naming/MemoizedInstanceVariableName: + Exclude: + - 'lib/contentful_migrations/migration_content_type.rb' From 5fe97c07882e986cb144f437bb219554dbbf37b2 Mon Sep 17 00:00:00 2001 From: "Patrick J. Cherry" Date: Thu, 11 Nov 2021 16:45:40 +0000 Subject: [PATCH 15/27] Raise errors by default in the Contentful::Management::Client This ensures that migrations bail out when errors are returned, lessening the chance of failed migrations --- README.md | 2 +- lib/contentful_migrations/migrator.rb | 33 +++++++++++-------- .../contentful_migrations/migrator_spec.rb | 6 ++-- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 76ae994..df295b5 100644 --- a/README.md +++ b/README.md @@ -145,7 +145,7 @@ end ## Development -After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. +After checking out the repo, run `bundle install` to install dependencies. Then, run `bundle exec spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). diff --git a/lib/contentful_migrations/migrator.rb b/lib/contentful_migrations/migrator.rb index bd60770..f5ada53 100644 --- a/lib/contentful_migrations/migrator.rb +++ b/lib/contentful_migrations/migrator.rb @@ -14,6 +14,13 @@ def initialize(migrations_path) end DEFAULT_MIGRATION_PATH = 'db/contentful_migrations' + DEFAULT_MIGRATOR_OPTIONS = { + migrations_path: ENV.fetch('MIGRATION_PATH', DEFAULT_MIGRATION_PATH), + access_token: ENV['CONTENTFUL_MANAGEMENT_ACCESS_TOKEN'], + space_id: ENV['CONTENTFUL_SPACE_ID'], + migration_content_type_name: MigrationContentType::DEFAULT_MIGRATION_CONTENT_TYPE, + logger: Logger.new($stdout) + }.freeze def self.migrate(args = {}) new(**parse_options(args)).migrate @@ -27,7 +34,11 @@ def self.pending(args = {}) new(**parse_options(args)).pending end - attr_reader :migrations_path, :access_token, :space_id, :client, :space, :env_id, + def self.parse_options(args) + DEFAULT_MIGRATOR_OPTIONS.merge(args) + end + + attr_reader :migrations_path, :access_token, :space_id, :env_id, :migration_content_type_name, :logger, :page_size def initialize(migrations_path:, @@ -41,13 +52,19 @@ def initialize(migrations_path:, @logger = logger @space_id = space_id @migration_content_type_name = migration_content_type_name - @client = Contentful::Management::Client.new(access_token) @env_id = env_id || ENV['CONTENTFUL_ENV'] || 'master' - @space = @client.environments(space_id).find(@env_id) @page_size = 1000 validate_options end + def client + @client ||= Contentful::Management::Client.new(access_token, raise_errors: true) + end + + def space + @space ||= client.environments(space_id).find(env_id) + end + def migrate runnable = migrations(migrations_path).reject { |m| ran?(m) } logger.info('No migrations to run, everything up to date!') if runnable.empty? @@ -78,16 +95,6 @@ def pending private - def self.parse_options(args) - { - migrations_path: ENV.fetch('MIGRATION_PATH', DEFAULT_MIGRATION_PATH), - access_token: ENV['CONTENTFUL_MANAGEMENT_ACCESS_TOKEN'], - space_id: ENV['CONTENTFUL_SPACE_ID'], - migration_content_type_name: MigrationContentType::DEFAULT_MIGRATION_CONTENT_TYPE, - logger: Logger.new($stdout) - }.merge(args) - end - def validate_options raise InvalidMigrationPath, migrations_path unless File.directory?(migrations_path) end diff --git a/spec/lib/contentful_migrations/migrator_spec.rb b/spec/lib/contentful_migrations/migrator_spec.rb index e153dfa..67fd256 100644 --- a/spec/lib/contentful_migrations/migrator_spec.rb +++ b/spec/lib/contentful_migrations/migrator_spec.rb @@ -98,14 +98,14 @@ let(:migration) { double(:migration, version: 20_180_216_021_826, name: 'BuildTestContent') } before do - expect(Contentful::Management::Client).to receive(:new).and_return(client) - expect(client).to receive(:environments).with('space_id').and_return(space) - expect(space).to receive(:find).with('master').and_return(space) allow(subject).to receive(:migration_content_type).and_return(migration_content_type) allow(logger).to receive(:info) end it 'sets name and version' do + expect(Contentful::Management::Client).to receive(:new).and_return(client) + expect(client).to receive(:environments).with('space_id').and_return(space) + expect(space).to receive(:find).with('master').and_return(space) expect(migration_content_type).to receive(:entries).and_return(entries) expect(ContentfulMigrations::MigrationProxy).to receive(:new).with( 'BuildTestContent', From 178769c9043bac254857e1cd81435a0470a80148 Mon Sep 17 00:00:00 2001 From: "Patrick J. Cherry" Date: Thu, 11 Nov 2021 17:02:19 +0000 Subject: [PATCH 16/27] Move env vars back; ensure they're there and use ClimateControl in tests --- lib/contentful_migrations/migrator.rb | 15 +++++++-------- spec/lib/contentful_migrations/migrator_spec.rb | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/lib/contentful_migrations/migrator.rb b/lib/contentful_migrations/migrator.rb index f5ada53..407ee97 100644 --- a/lib/contentful_migrations/migrator.rb +++ b/lib/contentful_migrations/migrator.rb @@ -14,13 +14,6 @@ def initialize(migrations_path) end DEFAULT_MIGRATION_PATH = 'db/contentful_migrations' - DEFAULT_MIGRATOR_OPTIONS = { - migrations_path: ENV.fetch('MIGRATION_PATH', DEFAULT_MIGRATION_PATH), - access_token: ENV['CONTENTFUL_MANAGEMENT_ACCESS_TOKEN'], - space_id: ENV['CONTENTFUL_SPACE_ID'], - migration_content_type_name: MigrationContentType::DEFAULT_MIGRATION_CONTENT_TYPE, - logger: Logger.new($stdout) - }.freeze def self.migrate(args = {}) new(**parse_options(args)).migrate @@ -35,7 +28,13 @@ def self.pending(args = {}) end def self.parse_options(args) - DEFAULT_MIGRATOR_OPTIONS.merge(args) + { + migrations_path: ENV.fetch('MIGRATION_PATH', DEFAULT_MIGRATION_PATH), + access_token: ENV.fetch('CONTENTFUL_MANAGEMENT_ACCESS_TOKEN'), + space_id: ENV.fetch('CONTENTFUL_SPACE_ID'), + migration_content_type_name: MigrationContentType::DEFAULT_MIGRATION_CONTENT_TYPE, + logger: Logger.new($stdout) + }.merge(args) end attr_reader :migrations_path, :access_token, :space_id, :env_id, diff --git a/spec/lib/contentful_migrations/migrator_spec.rb b/spec/lib/contentful_migrations/migrator_spec.rb index 67fd256..21444a6 100644 --- a/spec/lib/contentful_migrations/migrator_spec.rb +++ b/spec/lib/contentful_migrations/migrator_spec.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +require 'climate_control' require 'contentful_migrations/migrator' RSpec.describe ContentfulMigrations::Migrator do @@ -7,6 +8,21 @@ ## Class methods ######## + let(:env) do + { + CONTENTFUL_MANAGEMENT_ACCESS_TOKEN: management_access_token, + CONTENTFUL_SPACE_ID: space_id + } + end + let(:management_access_token) { 'management_access_token' } + let(:space_id) { 'space_id' } + + around do |example| + ClimateControl.modify(env) do + example.run + end + end + describe '.migrate' do let(:migrated) { double(:migrated) } From 9a2dc5c5824058fe06fe0cf4184da55f24cc9186 Mon Sep 17 00:00:00 2001 From: "Patrick J. Cherry" Date: Fri, 12 Nov 2021 09:13:28 +0000 Subject: [PATCH 17/27] Refactor MigrationContentType so that errors raised by contentful are caught --- .../migration_content_type.rb | 58 ++++++++-------- lib/contentful_migrations/migrator.rb | 6 +- .../migration_content_type_spec.rb | 67 +++++++------------ 3 files changed, 59 insertions(+), 72 deletions(-) diff --git a/lib/contentful_migrations/migration_content_type.rb b/lib/contentful_migrations/migration_content_type.rb index 02a3eba..afe9eca 100644 --- a/lib/contentful_migrations/migration_content_type.rb +++ b/lib/contentful_migrations/migration_content_type.rb @@ -1,47 +1,51 @@ # frozen_string_literal: true +require 'contentful/management' + module ContentfulMigrations class MigrationContentType - DEFAULT_MIGRATION_CONTENT_TYPE = 'migrations' + DEFAULT_CONTENT_TYPE_NAME = 'migrations' + + attr_reader :space, :content_type_name - attr_reader :access_token, :space_id, :client, :space, - :migration_content_type_name, :logger + def initialize(space:, + content_type_name: DEFAULT_CONTENT_TYPE_NAME) - def initialize(client:, - space:, - logger:, - migration_content_type_name: DEFAULT_MIGRATION_CONTENT_TYPE) - @client = client @space = space - @logger = logger - @migration_content_type_name = migration_content_type_name + @content_type_name = content_type_name end - def resolve - @migration_content_type ||= find_or_create_migration_content_type + def content_type + @content_type ||= find_or_create_content_type end private - def find_or_create_migration_content_type - content_type = space.content_types.find(migration_content_type_name) - if content_type.nil? || content_type.is_a?(Contentful::Management::Error) - build_migration_content_type - else - content_type - end + def content_types + @content_types ||= space.content_types + end + + def find_or_create_content_type + content_types.find(content_type_name) + + # This would be better if it were the proper + # Contentful::Management::NotFound error, but they're difficult to + # recreate in testing. + rescue StandardError + create_content_type end - def build_migration_content_type - content_type = space.content_types.create( - name: migration_content_type_name, - id: migration_content_type_name, + def create_content_type + ct = content_types.create( + name: content_type_name, + id: content_type_name, description: 'Migration Table for interal use only, do not delete' ) - content_type.fields.create(id: 'version', name: 'version', type: 'Integer') - content_type.save - content_type.publish - content_type + ct.fields.create(id: 'version', name: 'version', type: 'Integer') + ct.save + ct.publish + + ct end end end diff --git a/lib/contentful_migrations/migrator.rb b/lib/contentful_migrations/migrator.rb index 407ee97..dc18533 100644 --- a/lib/contentful_migrations/migrator.rb +++ b/lib/contentful_migrations/migrator.rb @@ -32,7 +32,7 @@ def self.parse_options(args) migrations_path: ENV.fetch('MIGRATION_PATH', DEFAULT_MIGRATION_PATH), access_token: ENV.fetch('CONTENTFUL_MANAGEMENT_ACCESS_TOKEN'), space_id: ENV.fetch('CONTENTFUL_SPACE_ID'), - migration_content_type_name: MigrationContentType::DEFAULT_MIGRATION_CONTENT_TYPE, + migration_content_type_name: ENV.fetch('CONTENTFUL_MIGRATION_CONTENT_TYPE', nil), logger: Logger.new($stdout) }.merge(args) end @@ -141,8 +141,8 @@ def migration_files(paths) def migration_content_type @migration_content_type ||= MigrationContentType.new( - space: space, client: client, logger: logger - ).resolve + space: space, content_type_name: migration_content_type_name + ).content_type end MIGRATION_FILENAME_REGEX = /\A([0-9]+)_([_a-z0-9]*)\.?([_a-z0-9]*)?\.rb\z/.freeze diff --git a/spec/lib/contentful_migrations/migration_content_type_spec.rb b/spec/lib/contentful_migrations/migration_content_type_spec.rb index ec26f13..b832f24 100644 --- a/spec/lib/contentful_migrations/migration_content_type_spec.rb +++ b/spec/lib/contentful_migrations/migration_content_type_spec.rb @@ -1,67 +1,50 @@ # frozen_string_literal: true +require 'byebug' require 'contentful_migrations/migration_content_type' RSpec.describe ContentfulMigrations::MigrationContentType do - let(:client) { double(:client) } - let(:space) { double(:space) } + subject { described_class.new(**defaults) } - let(:logger) { double(:logger) } - let(:defaults) do - { - client: client, - space: space, - logger: logger - } - end + let(:space) { double(Contentful::Management::Space) } + let(:content_type_name) { 'foos' } + let(:defaults) { { space: space, content_type_name: content_type_name } } - describe '#initialize' do - it 'sets name and version' do - migrator = described_class.new(**defaults) + describe '#content_type' do + subject { described_class.new(**defaults).content_type } - expect(migrator.client).to eq(client) - expect(migrator.space).to eq(space) - expect(migrator.migration_content_type_name).to eq('migrations') - end - end + let(:content_types) { double(Contentful::Management::ContentType) } + let(:content_type) { instance_double(Contentful::Management::ContentType) } - describe '#resolve' do - subject { described_class.new(**defaults) } + before do + expect(space).to receive(:content_types).and_return(content_types) + end - let(:content_types) { double(:content_types) } - let(:migration_content_type) { double(:migration_content_type) } context 'when content type exists' do before do - expect(space).to receive(:content_types).and_return(content_types) - expect(content_types).to receive(:find).with('migrations').and_return(migration_content_type) + expect(content_types).to receive(:find).with(content_type_name).and_return(content_type) end - it 'calls contentful to retrive content type' do - expect(subject.resolve).to eq(migration_content_type) - end + it { is_expected.to eq content_type } end context 'when content type not exist' do - let(:fields) { double(:fields) } + let(:fields) { double(Contentful::Management::Field) } + before do - allow(space).to receive(:content_types).and_return(content_types) - expect(content_types).to receive(:find).with('migrations').and_return(nil) + expect(content_types).to receive(:find).with(content_type_name).and_raise(StandardError) expect(content_types).to receive(:create).with( - name: 'migrations', - id: 'migrations', + name: content_type_name, + id: content_type_name, description: 'Migration Table for interal use only, do not delete' - ).and_return(migration_content_type) - expect(migration_content_type).to receive(:fields).and_return(fields) - expect(fields).to receive(:create).with( - id: 'version', name: 'version', type: 'Integer' - ) - expect(migration_content_type).to receive(:save) - expect(migration_content_type).to receive(:publish) + ).and_return(content_type) + expect(content_type).to receive(:fields).and_return(fields) + expect(fields).to receive(:create).with(id: 'version', name: 'version', type: 'Integer') + expect(content_type).to receive(:save) + expect(content_type).to receive(:publish) end - it 'calls contentful to retrive content type' do - expect(subject.resolve).to eq(migration_content_type) - end + it { is_expected.to eq content_type } end end end From dfdeba936feccb595ec99e67042de86182e5eacf Mon Sep 17 00:00:00 2001 From: "Patrick J. Cherry" Date: Fri, 12 Nov 2021 09:13:50 +0000 Subject: [PATCH 18/27] Refactor tests for ContentfulMigrations::Migrator --- .../contentful_migrations/migrator_spec.rb | 241 +++++++++--------- 1 file changed, 123 insertions(+), 118 deletions(-) diff --git a/spec/lib/contentful_migrations/migrator_spec.rb b/spec/lib/contentful_migrations/migrator_spec.rb index 21444a6..8ab3515 100644 --- a/spec/lib/contentful_migrations/migrator_spec.rb +++ b/spec/lib/contentful_migrations/migrator_spec.rb @@ -2,153 +2,158 @@ require 'climate_control' require 'contentful_migrations/migrator' +require 'contentful_migrations/migration_proxy' +require 'contentful_migrations/migration_content_type' RSpec.describe ContentfulMigrations::Migrator do - ######## - ## Class methods - ######## - - let(:env) do - { - CONTENTFUL_MANAGEMENT_ACCESS_TOKEN: management_access_token, - CONTENTFUL_SPACE_ID: space_id - } - end - let(:management_access_token) { 'management_access_token' } - let(:space_id) { 'space_id' } - - around do |example| - ClimateControl.modify(env) do - example.run + describe 'class methods' do + let(:env) do + { + CONTENTFUL_MANAGEMENT_ACCESS_TOKEN: management_access_token, + CONTENTFUL_SPACE_ID: space_id + } end - end + let(:management_access_token) { 'management_access_token' } + let(:space_id) { 'space_id' } - describe '.migrate' do - let(:migrated) { double(:migrated) } - - before do - expect(described_class).to receive(:new).and_return(double(:m, migrate: migrated)) + around do |example| + ClimateControl.modify(env) do + example.run + end end - it 'calls migrate' do - expect(described_class.migrate).to eq(migrated) - end - end + describe '.migrate' do + let(:migrated) { double(:migrated) } - describe '.rollback' do - let(:rolledback) { double(:rolledback) } + before do + expect(described_class).to receive(:new).and_return(double(:m, migrate: migrated)) + end - before do - expect(described_class).to receive(:new).and_return(double(:m, rollback: rolledback)) + it 'calls migrate' do + expect(described_class.migrate).to eq(migrated) + end end - it 'calls migrate' do - expect(described_class.rollback).to eq(rolledback) - end - end + describe '.rollback' do + let(:rolledback) { double(:rolledback) } - describe '.pending' do - let(:pending_result) { double(:pending_result) } + before do + expect(described_class).to receive(:new).and_return(double(:m, rollback: rolledback)) + end - before do - expect(described_class).to receive(:new).and_return(double(:m, pending: pending_result)) + it 'calls migrate' do + expect(described_class.rollback).to eq(rolledback) + end end - it 'calls migrate' do - expect(described_class.pending).to eq(pending_result) - end - end + describe '.pending' do + let(:pending_result) { double(:pending_result) } - ######## - ## Instance methods - ######## - - let(:logger) { double(:logger) } - let(:defaults) do - { migrations_path: 'spec/db/contentful_migrations', - access_token: 'access_token', - space_id: 'space_id', - migration_content_type_name: ContentfulMigrations::MigrationContentType::DEFAULT_MIGRATION_CONTENT_TYPE, - logger: logger, - env_id: 'master' } - end - - describe '#initialize' do - it 'sets name and version' do - migrator = described_class.new(**defaults) - - expect(migrator.migrations_path).to eq('spec/db/contentful_migrations') - expect(migrator.access_token).to eq('access_token') - expect(migrator.space_id).to eq('space_id') - expect(migrator.migration_content_type_name).to eq('migrations') - expect(migrator.env_id).to eq('master') - end - it 'raises error when invalid path' do - expect do - described_class.new(**defaults.merge(migrations_path: 'bad/path')) - end.to raise_error(ContentfulMigrations::Migrator::InvalidMigrationPath) - end - end - - describe '#migrate' do - subject { described_class.new(**defaults) } - context 'when no migrations' do before do - allow(subject).to receive(:migrations).and_return([]) - expect(logger).to receive(:info) + expect(described_class).to receive(:new).and_return(double(:m, pending: pending_result)) end - it 'sets name and version' do - expect(subject.migrate).to eq(subject) + it 'calls migrate' do + expect(described_class.pending).to eq(pending_result) end end + end - context 'when migrations' do - let(:client) { double(:client) } - let(:spaces) { double(:spaces) } - let(:space) { double(:space) } - let(:content_types) { double(:content_types) } - let(:migration_content_type) { double(:migration_content_type) } - let(:entries) { double(:entries, all: all) } - let(:all) { [] } - let(:migration) { double(:migration, version: 20_180_216_021_826, name: 'BuildTestContent') } - - before do - allow(subject).to receive(:migration_content_type).and_return(migration_content_type) - allow(logger).to receive(:info) - end + describe 'instance methods' do + subject(:migrator) { described_class.new(**defaults) } + + let(:logger) { double(:logger) } + let(:migrations_path) { 'spec/db/contentful_migrations' } + let(:access_token) { 'my_access_token' } + let(:space_id) { 'my_space_id' } + let(:migration_content_type_name) { 'my_migrations' } + let(:env_id) { 'my_env_id' } + + let(:defaults) do + { migrations_path: migrations_path, + access_token: access_token, + space_id: space_id, + migration_content_type_name: migration_content_type_name, + logger: logger, + env_id: env_id } + end + describe '#initialize' do it 'sets name and version' do - expect(Contentful::Management::Client).to receive(:new).and_return(client) - expect(client).to receive(:environments).with('space_id').and_return(space) - expect(space).to receive(:find).with('master').and_return(space) - expect(migration_content_type).to receive(:entries).and_return(entries) - expect(ContentfulMigrations::MigrationProxy).to receive(:new).with( - 'BuildTestContent', - 20_180_216_021_826, - 'spec/db/contentful_migrations/20180216021826_build_test_content.rb', - '' - ).and_return(migration) - expect(migration).to receive(:migrate).with(:up, client, space) - expect(migration).to receive(:record_migration).with(migration_content_type) - expect(subject.migrate).to eq(subject) + + expect(migrator.migrations_path).to eq migrations_path + expect(migrator.access_token).to eq access_token + expect(migrator.space_id).to eq space_id + expect(migrator.migration_content_type_name).to eq migration_content_type_name + expect(migrator.env_id).to eq env_id end - it 'sets @page_size during construction' do - expect(subject.instance_variable_get('@page_size')).to eq(1000) + it 'raises error when invalid path' do + expect do + described_class.new(**defaults.merge(migrations_path: 'bad/path')) + end.to raise_error(ContentfulMigrations::Migrator::InvalidMigrationPath) end + end + + describe '#migrate' do + context 'when no migrations' do + before do + allow(migrator).to receive(:migrations).and_return([]) + expect(logger).to receive(:info) + end - it 'calls fetch_page when loading migrated records' do - allow(subject).to receive(:fetch_page).and_return([]) - expect(subject).to receive(:fetch_page).once - subject.send(:migrated) + it 'sets name and version' do + expect(migrator.migrate).to eq(migrator) + end end - it 'pages through contentful records' do - subject.instance_variable_set('@page_size', 10) - allow(subject).to receive(:fetch_page).and_return((1..10).to_a, (1..9).to_a) - expect(subject).to receive(:fetch_page).twice - subject.send(:load_migrated) + context 'when migrations' do + let(:client) { double(:client) } + let(:spaces) { double(:spaces) } + let(:space) { double(:space) } + let(:content_types) { double(:content_types) } + let(:migration_content_type) { double(:migration_content_type) } + let(:entries) { double(:entries, all: all) } + let(:all) { [] } + let(:migration) { double(:migration, version: 20_180_216_021_826, name: 'BuildTestContent') } + + before do + allow(migrator).to receive(:migration_content_type).and_return(migration_content_type) + allow(logger).to receive(:info) + end + + it 'sets name and version' do + expect(Contentful::Management::Client).to receive(:new).and_return(client) + expect(client).to receive(:environments).with(space_id).and_return(space) + expect(space).to receive(:find).with(env_id).and_return(space) + expect(migration_content_type).to receive(:entries).and_return(entries) + expect(ContentfulMigrations::MigrationProxy).to receive(:new).with( + 'BuildTestContent', + 20_180_216_021_826, + 'spec/db/contentful_migrations/20180216021826_build_test_content.rb', + '' + ).and_return(migration) + expect(migration).to receive(:migrate).with(:up, client, space) + expect(migration).to receive(:record_migration).with(migration_content_type) + expect(migrator.migrate).to eq(migrator) + end + + it 'sets @page_size during construction' do + expect(migrator.instance_variable_get('@page_size')).to eq(1000) + end + + it 'calls fetch_page when loading migrated records' do + allow(migrator).to receive(:fetch_page).and_return([]) + expect(migrator).to receive(:fetch_page).once + migrator.send(:migrated) + end + + it 'pages through contentful records' do + migrator.instance_variable_set('@page_size', 10) + allow(migrator).to receive(:fetch_page).and_return((1..10).to_a, (1..9).to_a) + expect(migrator).to receive(:fetch_page).twice + migrator.send(:load_migrated) + end end end end From 1db76a64015caeff62102a36c1039daddd23799d Mon Sep 17 00:00:00 2001 From: "Patrick J. Cherry" Date: Fri, 12 Nov 2021 09:35:57 +0000 Subject: [PATCH 19/27] Use environment where we mean it; move default content type name We're really operating on environments, so lets call them that. --- lib/contentful_migrations/migration_content_type.rb | 12 ++++-------- lib/contentful_migrations/migrator.rb | 13 +++++++------ .../migration_content_type_spec.rb | 6 +++--- 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/lib/contentful_migrations/migration_content_type.rb b/lib/contentful_migrations/migration_content_type.rb index afe9eca..f31bb43 100644 --- a/lib/contentful_migrations/migration_content_type.rb +++ b/lib/contentful_migrations/migration_content_type.rb @@ -4,14 +4,10 @@ module ContentfulMigrations class MigrationContentType - DEFAULT_CONTENT_TYPE_NAME = 'migrations' + attr_reader :environment, :content_type_name - attr_reader :space, :content_type_name - - def initialize(space:, - content_type_name: DEFAULT_CONTENT_TYPE_NAME) - - @space = space + def initialize(environment:, content_type_name:) + @environment = environment @content_type_name = content_type_name end @@ -22,7 +18,7 @@ def content_type private def content_types - @content_types ||= space.content_types + @content_types ||= environment.content_types end def find_or_create_content_type diff --git a/lib/contentful_migrations/migrator.rb b/lib/contentful_migrations/migrator.rb index dc18533..35d1462 100644 --- a/lib/contentful_migrations/migrator.rb +++ b/lib/contentful_migrations/migrator.rb @@ -14,6 +14,7 @@ def initialize(migrations_path) end DEFAULT_MIGRATION_PATH = 'db/contentful_migrations' + DEFAULT_MIGRATION_CONTENT_TYPE_NAME = 'migrations' def self.migrate(args = {}) new(**parse_options(args)).migrate @@ -32,7 +33,7 @@ def self.parse_options(args) migrations_path: ENV.fetch('MIGRATION_PATH', DEFAULT_MIGRATION_PATH), access_token: ENV.fetch('CONTENTFUL_MANAGEMENT_ACCESS_TOKEN'), space_id: ENV.fetch('CONTENTFUL_SPACE_ID'), - migration_content_type_name: ENV.fetch('CONTENTFUL_MIGRATION_CONTENT_TYPE', nil), + migration_content_type_name: ENV.fetch('CONTENTFUL_MIGRATION_CONTENT_TYPE', DEFAULT_MIGRATION_CONTENT_TYPE_NAME), logger: Logger.new($stdout) }.merge(args) end @@ -60,8 +61,8 @@ def client @client ||= Contentful::Management::Client.new(access_token, raise_errors: true) end - def space - @space ||= client.environments(space_id).find(env_id) + def environment + @environment ||= client.environments(space_id).find(env_id) end def migrate @@ -70,7 +71,7 @@ def migrate runnable.each do |migration| logger.info("running migration #{migration.version} #{migration.name} ") - migration.migrate(:up, client, space) + migration.migrate(:up, client, environment) migration.record_migration(migration_content_type) end self @@ -80,7 +81,7 @@ def rollback already_migrated = migrations(migrations_path).select { |m| ran?(m) } migration = already_migrated.pop logger.info("Rolling back migration #{migration.version} #{migration.name} ") - migration.migrate(:down, client, space) + migration.migrate(:down, client, environment) migration.erase_migration(migration_content_type) end @@ -141,7 +142,7 @@ def migration_files(paths) def migration_content_type @migration_content_type ||= MigrationContentType.new( - space: space, content_type_name: migration_content_type_name + environment: environment, content_type_name: migration_content_type_name ).content_type end diff --git a/spec/lib/contentful_migrations/migration_content_type_spec.rb b/spec/lib/contentful_migrations/migration_content_type_spec.rb index b832f24..95aadd7 100644 --- a/spec/lib/contentful_migrations/migration_content_type_spec.rb +++ b/spec/lib/contentful_migrations/migration_content_type_spec.rb @@ -6,9 +6,9 @@ RSpec.describe ContentfulMigrations::MigrationContentType do subject { described_class.new(**defaults) } - let(:space) { double(Contentful::Management::Space) } + let(:environment) { instance_double(Contentful::Management::Environment) } let(:content_type_name) { 'foos' } - let(:defaults) { { space: space, content_type_name: content_type_name } } + let(:defaults) { { environment: environment, content_type_name: content_type_name } } describe '#content_type' do subject { described_class.new(**defaults).content_type } @@ -17,7 +17,7 @@ let(:content_type) { instance_double(Contentful::Management::ContentType) } before do - expect(space).to receive(:content_types).and_return(content_types) + expect(environment).to receive(:content_types).and_return(content_types) end context 'when content type exists' do From 42fd001c14556f4e9e5d58c893efb1c0c0660cd1 Mon Sep 17 00:00:00 2001 From: "Patrick J. Cherry" Date: Tue, 8 Mar 2022 12:40:43 +0000 Subject: [PATCH 20/27] Set the default locale on the client for the migration content type --- lib/contentful_migrations/migrator.rb | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/contentful_migrations/migrator.rb b/lib/contentful_migrations/migrator.rb index 35d1462..97b9843 100644 --- a/lib/contentful_migrations/migrator.rb +++ b/lib/contentful_migrations/migrator.rb @@ -141,9 +141,24 @@ def migration_files(paths) end def migration_content_type - @migration_content_type ||= MigrationContentType.new( + return @migration_content_type if @migration_content_type.is_a?(MigrationContentType) + + content_type ||= MigrationContentType.new( environment: environment, content_type_name: migration_content_type_name ).content_type + + # Set the default locale on the content type client (ugh) + content_type.client.default_locale = default_locale + + @migration_content_type = content_type + end + + def available_locales + @available_locales ||= environment.locales.all + end + + def default_locale + @default_locale ||= available_locales.find(&:default) end MIGRATION_FILENAME_REGEX = /\A([0-9]+)_([_a-z0-9]*)\.?([_a-z0-9]*)?\.rb\z/.freeze From 22918e0809977c4a2efb9defddc5a6cc54ec7459 Mon Sep 17 00:00:00 2001 From: "Patrick J. Cherry" Date: Tue, 8 Mar 2022 12:43:50 +0000 Subject: [PATCH 21/27] Poke the config directly --- lib/contentful_migrations/migrator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/contentful_migrations/migrator.rb b/lib/contentful_migrations/migrator.rb index 97b9843..f1b989b 100644 --- a/lib/contentful_migrations/migrator.rb +++ b/lib/contentful_migrations/migrator.rb @@ -148,7 +148,7 @@ def migration_content_type ).content_type # Set the default locale on the content type client (ugh) - content_type.client.default_locale = default_locale + content_type.client.configuration[:default_locale] = default_locale @migration_content_type = content_type end From 4e060443c242f2ef9a772bfd9458b68168312723 Mon Sep 17 00:00:00 2001 From: "Patrick J. Cherry" Date: Tue, 8 Mar 2022 12:56:54 +0000 Subject: [PATCH 22/27] Try setting the default locale on the environment instead --- lib/contentful_migrations/migrator.rb | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/lib/contentful_migrations/migrator.rb b/lib/contentful_migrations/migrator.rb index f1b989b..560f1c0 100644 --- a/lib/contentful_migrations/migrator.rb +++ b/lib/contentful_migrations/migrator.rb @@ -62,7 +62,15 @@ def client end def environment - @environment ||= client.environments(space_id).find(env_id) + return @environment if @environment.is_a?(Contentful::Management::Environment) + + env = client.environments(space_id).find(env_id) + + # Set the default locale on the environment's client (ugh) + default_locale = env.locales.all.find(&:default) + env.client.configuration[:default_locale] = default_locale + + @environment = env end def migrate @@ -141,24 +149,9 @@ def migration_files(paths) end def migration_content_type - return @migration_content_type if @migration_content_type.is_a?(MigrationContentType) - - content_type ||= MigrationContentType.new( + @migration_content_type ||= MigrationContentType.new( environment: environment, content_type_name: migration_content_type_name ).content_type - - # Set the default locale on the content type client (ugh) - content_type.client.configuration[:default_locale] = default_locale - - @migration_content_type = content_type - end - - def available_locales - @available_locales ||= environment.locales.all - end - - def default_locale - @default_locale ||= available_locales.find(&:default) end MIGRATION_FILENAME_REGEX = /\A([0-9]+)_([_a-z0-9]*)\.?([_a-z0-9]*)?\.rb\z/.freeze From 891d4b01da17bb581ae78e6174c14fb810ce6f96 Mon Sep 17 00:00:00 2001 From: "Patrick J. Cherry" Date: Tue, 8 Mar 2022 13:02:12 +0000 Subject: [PATCH 23/27] Use the locale's code. --- lib/contentful_migrations/migrator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/contentful_migrations/migrator.rb b/lib/contentful_migrations/migrator.rb index 560f1c0..c2ac392 100644 --- a/lib/contentful_migrations/migrator.rb +++ b/lib/contentful_migrations/migrator.rb @@ -68,7 +68,7 @@ def environment # Set the default locale on the environment's client (ugh) default_locale = env.locales.all.find(&:default) - env.client.configuration[:default_locale] = default_locale + env.client.configuration[:default_locale] = default_locale.code @environment = env end From 98e80332d6d381138eaa2a0958c66d66788f934b Mon Sep 17 00:00:00 2001 From: "Patrick J. Cherry" Date: Tue, 8 Mar 2022 13:04:46 +0000 Subject: [PATCH 24/27] Silence our friend, Rubocop --- contentful-migrations.gemspec | 1 + lib/contentful_migrations/migrator.rb | 5 +++-- spec/lib/contentful_migrations/migrator_spec.rb | 1 - 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/contentful-migrations.gemspec b/contentful-migrations.gemspec index ac3cdaf..6a4f00f 100644 --- a/contentful-migrations.gemspec +++ b/contentful-migrations.gemspec @@ -41,4 +41,5 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'rspec_junit_formatter' spec.add_development_dependency 'rubocop-rspec' spec.add_development_dependency 'simplecov' + spec.metadata['rubygems_mfa_required'] = 'true' end diff --git a/lib/contentful_migrations/migrator.rb b/lib/contentful_migrations/migrator.rb index c2ac392..ae3440e 100644 --- a/lib/contentful_migrations/migrator.rb +++ b/lib/contentful_migrations/migrator.rb @@ -4,7 +4,7 @@ require 'contentful_migrations/string_refinements' module ContentfulMigrations - class Migrator + class Migrator # rubocop:disable Metrics/ClassLength using StringRefinements class InvalidMigrationPath < StandardError # :nodoc: @@ -33,7 +33,8 @@ def self.parse_options(args) migrations_path: ENV.fetch('MIGRATION_PATH', DEFAULT_MIGRATION_PATH), access_token: ENV.fetch('CONTENTFUL_MANAGEMENT_ACCESS_TOKEN'), space_id: ENV.fetch('CONTENTFUL_SPACE_ID'), - migration_content_type_name: ENV.fetch('CONTENTFUL_MIGRATION_CONTENT_TYPE', DEFAULT_MIGRATION_CONTENT_TYPE_NAME), + migration_content_type_name: ENV.fetch('CONTENTFUL_MIGRATION_CONTENT_TYPE', + DEFAULT_MIGRATION_CONTENT_TYPE_NAME), logger: Logger.new($stdout) }.merge(args) end diff --git a/spec/lib/contentful_migrations/migrator_spec.rb b/spec/lib/contentful_migrations/migrator_spec.rb index 8ab3515..0a4b07d 100644 --- a/spec/lib/contentful_migrations/migrator_spec.rb +++ b/spec/lib/contentful_migrations/migrator_spec.rb @@ -80,7 +80,6 @@ describe '#initialize' do it 'sets name and version' do - expect(migrator.migrations_path).to eq migrations_path expect(migrator.access_token).to eq access_token expect(migrator.space_id).to eq space_id From 568a58cf1eebf9bb8fe4941daa0d08d14e39f3aa Mon Sep 17 00:00:00 2001 From: "Patrick J. Cherry" Date: Tue, 8 Mar 2022 13:15:19 +0000 Subject: [PATCH 25/27] Add in a mass of mocks to allow tests to pass --- spec/lib/contentful_migrations/migrator_spec.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/spec/lib/contentful_migrations/migrator_spec.rb b/spec/lib/contentful_migrations/migrator_spec.rb index 0a4b07d..bb08a29 100644 --- a/spec/lib/contentful_migrations/migrator_spec.rb +++ b/spec/lib/contentful_migrations/migrator_spec.rb @@ -110,6 +110,12 @@ let(:client) { double(:client) } let(:spaces) { double(:spaces) } let(:space) { double(:space) } + let(:locales) { double(:locales) } + let(:all_locales) { [default_locale] } + let(:default_locale) { Struct.new(:code, :default).new('en', true) } + let(:space_client) { double(:space_client) } + let(:space_client_config) { {} } + let(:content_types) { double(:content_types) } let(:migration_content_type) { double(:migration_content_type) } let(:entries) { double(:entries, all: all) } @@ -125,6 +131,11 @@ expect(Contentful::Management::Client).to receive(:new).and_return(client) expect(client).to receive(:environments).with(space_id).and_return(space) expect(space).to receive(:find).with(env_id).and_return(space) + expect(space).to receive(:locales).and_return(locales) + expect(locales).to receive(:all).and_return(all_locales) + expect(space).to receive(:client).and_return(space_client) + expect(space_client).to receive(:configuration).and_return(space_client_config) + expect(migration_content_type).to receive(:entries).and_return(entries) expect(ContentfulMigrations::MigrationProxy).to receive(:new).with( 'BuildTestContent', @@ -135,6 +146,7 @@ expect(migration).to receive(:migrate).with(:up, client, space) expect(migration).to receive(:record_migration).with(migration_content_type) expect(migrator.migrate).to eq(migrator) + expect(space_client_config[:default_locale]).to eq default_locale.code end it 'sets @page_size during construction' do From 5051ed5f11e950333c75e067d07fdf445b5fb118 Mon Sep 17 00:00:00 2001 From: "Patrick J. Cherry" Date: Tue, 8 Mar 2022 13:16:55 +0000 Subject: [PATCH 26/27] Bump version number --- lib/contentful_migrations/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/contentful_migrations/version.rb b/lib/contentful_migrations/version.rb index 218ed38..e78e662 100644 --- a/lib/contentful_migrations/version.rb +++ b/lib/contentful_migrations/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module ContentfulMigrations - VERSION = '0.2.0' + VERSION = '0.2.1' end From aa43f97a113accba037e04aa3acfb38baa0da0e0 Mon Sep 17 00:00:00 2001 From: "Patrick J. Cherry" Date: Tue, 8 Mar 2022 13:19:19 +0000 Subject: [PATCH 27/27] Also bump the Gemfile --- Gemfile.lock | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 03bcc29..4e50a98 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - contentful-migrations (0.2.0) + contentful-migrations (0.2.1) contentful-management (~> 3.0) GEM @@ -21,7 +21,7 @@ GEM docile (1.4.0) domain_name (0.5.20190701) unf (>= 0.0.5, < 1.0.0) - ffi (1.15.4) + ffi (1.15.5) ffi-compiler (1.0.1) ffi (>= 1.0.0) rake