diff --git a/Gemfile.lock b/Gemfile.lock index a72035c3..34afc729 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - dsu (3.0.0.rc.1) + dsu (3.0.0.beta.1) activemodel (>= 7.0.8, < 8.0) activesupport (>= 7.0.8, < 8.0) colorize (>= 1.1, < 2.0) diff --git a/lib/dsu.rb b/lib/dsu.rb index b898370e..8cc1ccaa 100644 --- a/lib/dsu.rb +++ b/lib/dsu.rb @@ -26,8 +26,8 @@ # NOTE: Add a new migration service to the array whenever a new migration is created. options = { pretend: false } migration_services = [ - V20230613121411::Service.new(options: options), - V20240210161248::Service.new(options: options) + Dsu::Migration::V20230613121411::Service.new(options: options), + Dsu::Migration::V20240210161248::Service.new(options: options) ] Dsu::Migration::Migrator.migrate_if!(migration_services: migration_services) end diff --git a/lib/dsu/migration/base_service.rb b/lib/dsu/migration/base_service.rb index 4c68cce7..b9828b08 100644 --- a/lib/dsu/migration/base_service.rb +++ b/lib/dsu/migration/base_service.rb @@ -91,7 +91,7 @@ def backup_exist? end def backup_folder - @backup_folder ||= File.join(root_folder, "dsu-#{from_migration_version}-backup") + @backup_folder ||= backup_folder_for(migration_version: from_migration_version) end def update_migration_version! diff --git a/lib/dsu/migration/v20240210161248/service.rb b/lib/dsu/migration/v20240210161248/service.rb index 7edfd95f..96565816 100644 --- a/lib/dsu/migration/v20240210161248/service.rb +++ b/lib/dsu/migration/v20240210161248/service.rb @@ -39,10 +39,6 @@ def run_migration! puts 'Migration completed successfully.' end - def config_file_from - File.join(root_folder, '.dsu') - end - def dsu_folder_from File.join(root_folder, 'dsu') end diff --git a/lib/dsu/support/fileable.rb b/lib/dsu/support/fileable.rb index a23751af..0d606d81 100644 --- a/lib/dsu/support/fileable.rb +++ b/lib/dsu/support/fileable.rb @@ -77,12 +77,6 @@ def gem_dir Gem.loaded_specs['dsu'].gem_dir end - # Back up folder - - def backup_folder(version:) - File.join(dsu_folder, 'backup', version.to_s) - end - # Seed data files and folders def seed_data_dsu_folder_for(migration_version:) @@ -130,6 +124,12 @@ def project_file_for(project_name:) end alias project_file project_file_for + # Backup folders + + def backup_folder_for(migration_version:) + File.join(root_folder, "dsu-#{migration_version}-backup") + end + extend self # rubocop:disable Style/ModuleFunction end end diff --git a/lib/dsu/version.rb b/lib/dsu/version.rb index c51c60b1..a5cbe7bf 100644 --- a/lib/dsu/version.rb +++ b/lib/dsu/version.rb @@ -2,5 +2,5 @@ module Dsu VERSION_REGEX = /\A\d+\.\d+\.\d+(\.(alpha|beta|rc)\.\d+)?\z/ - VERSION = '3.0.0.rc.1' + VERSION = '3.0.0.beta.1' end diff --git a/spec/dsu/migration/base_service_spec.rb b/spec/dsu/migration/base_service_spec.rb new file mode 100644 index 00000000..dfc66202 --- /dev/null +++ b/spec/dsu/migration/base_service_spec.rb @@ -0,0 +1,144 @@ +# frozen_string_literal: true + +# rubocop:disable RSpec/NestedGroups +RSpec.describe Dsu::Migration::BaseService, type: :migration do + subject(:base_service) { described_class.new(options: options) } + + let(:options) { { pretend: false } } + + describe '#initialize' do + it 'does not raise an error' do + expect { base_service }.to_not raise_error + end + end + + context 'when the required methods are not implemented' do + describe '#migrate_if!' do + subject(:base_service_error) { base_service.migrate_if! } + + let(:expected_error) { NotImplementedError } + + it_behaves_like 'an error is raised' + end + + describe '.migrates_to_latest_migration_version?' do + subject(:base_service_error) { described_class.migrates_to_latest_migration_version? } + + let(:expected_error) { NotImplementedError } + + it_behaves_like 'an error is raised' + end + + describe '.from_migration_version' do + subject(:base_service_error) { described_class.from_migration_version } + + let(:expected_error) { NotImplementedError } + + it_behaves_like 'an error is raised' + end + + describe '.to_migration_version' do + subject(:base_service_error) { described_class.to_migration_version } + + let(:expected_error) { NotImplementedError } + + it_behaves_like 'an error is raised' + end + end + + context 'when the required methods are implemented' do + subject(:base_service) do + service = Class.new(described_class) do + class << self + attr_accessor :from_migration_version, :to_migration_version + end + end.new(options: options) + service.class.from_migration_version = from_migration_version + service.class.to_migration_version = to_migration_version + service + end + + before do + create(:migration_version, version: to_migration_version, options: options) + end + + let(:from_migration_version) { 1 } + let(:to_migration_version) { Dsu::Migration::VERSION } + + describe 'class methods' do + describe '.from_migration_version' do + let(:from_migration_version) { 1 } + let(:to_migration_version) { 2 } + + it 'returns the from migration version' do + expect(base_service.class.from_migration_version).to eq(from_migration_version) + end + end + + describe '.to_migration_version' do + let(:from_migration_version) { 1 } + let(:to_migration_version) { 2 } + + it 'returns the to migration version' do + expect(base_service.class.to_migration_version).to eq(to_migration_version) + end + end + + describe '.migrates_to_latest_migration_version?' do + context 'when the to migration version is not the latest' do + let(:from_migration_version) { 1 } + let(:to_migration_version) { 1 + Dsu::Migration::VERSION } + + it 'returns false' do + expect(base_service.class.migrates_to_latest_migration_version?).to be false + end + end + + context 'when the to migration version is the latest' do + let(:from_migration_version) { 1 } + let(:to_migration_version) { Dsu::Migration::VERSION } + + it 'returns true' do + expect(base_service.class.migrates_to_latest_migration_version?).to be true + end + end + end + end + + describe 'instance methods' do + describe '#migrate_if!' do + before do + stub_const('Dsu::Migration::VERSION', to_migration_version) + mock_migration_version_for(version: from_migration_version) + create(:migration_version, version: migration_version, options: options) + base_service.migrate_if! + end + + context 'when the current migration version is equal to the from migration version' do + let(:migration_version) { from_migration_version } + let(:from_migration_version) { 20230613121411 } # rubocop:disable Style/NumericLiterals + let(:to_migration_version) { 20240210161248 } # rubocop:disable Style/NumericLiterals + + it 'creates a backup of the current dsu folder and updates the migration version' do + expected_backup_folder = Dsu::Support::Fileable.backup_folder_for(migration_version: migration_version) + expect(Dir.exist?(expected_backup_folder)).to be true + expect(Dsu::Models::MigrationVersion.new.version).to eq to_migration_version + end + end + + context 'when the current migration version is not equal to the from migration version' do + let(:migration_version) { to_migration_version } + let(:from_migration_version) { 20230613121411 } # rubocop:disable Style/NumericLiterals + let(:to_migration_version) { 20240210161248 } # rubocop:disable Style/NumericLiterals + + it 'does not create a backup of the current dsu folder and does not update the migration version' do + expected_backup_folder = Dsu::Support::Fileable.backup_folder_for(migration_version: from_migration_version) + expect(Dir.exist?(expected_backup_folder)).to be false + expect(Dsu::Models::MigrationVersion.new.version).to_not eq from_migration_version + end + end + end + end + end +end +# rubocop:enable RSpec/NestedGroups diff --git a/spec/dsu/migration/v20230613121411/service_spec.rb b/spec/dsu/migration/v20230613121411/service_spec.rb index 4c2742e0..1e4f4dcc 100644 --- a/spec/dsu/migration/v20230613121411/service_spec.rb +++ b/spec/dsu/migration/v20230613121411/service_spec.rb @@ -75,12 +75,6 @@ end def known_deleted_files - # %w[ - # entries/2023-12-28.json - # entries/2023-12-29.json - # entries/2024-01-01.json - # entries/2024-01-02.json - # ] [] end