Skip to content

Commit

Permalink
Migration and migration specs
Browse files Browse the repository at this point in the history
  • Loading branch information
gangelo committed Feb 16, 2024
1 parent 2e4191e commit 0ef89f6
Show file tree
Hide file tree
Showing 19 changed files with 489 additions and 426 deletions.
30 changes: 20 additions & 10 deletions lib/dsu/migration/service.rb → lib/dsu/migration/base_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

module Dsu
module Migration
class Service
class BaseService
include Support::Fileable

def initialize(options: {})
Expand All @@ -17,12 +17,12 @@ def migrate_if!
return unless run_migration?

puts "Running migrations #{from_migration_version} -> #{to_migration_version}..."
puts "pretend?: #{pretend?}" if pretend?
puts "\tpretend?: #{pretend?}" if pretend?

run_migration!
update_migration_version!

puts "Migration #{from_migration_version} -> #{to_migration_version} complete."
puts "\tMigration #{from_migration_version} -> #{to_migration_version} complete."
end

private
Expand Down Expand Up @@ -54,16 +54,19 @@ def to_migration_version

# The migration version before running this migration.
def migration_version
# Typically we should not be using models in any of the migration services
# because if these change, the migrations will break. However, using
# the MigrationVersion model is an exception because it is a very simple
# model and is unlikely to change.
@migration_version ||= Models::MigrationVersion.new.version
end

def create_backup
puts "Creating backup #{backup_folder}..."

return puts 'Skipping: backup already exists.' if backup_exist?
return puts "\tSkipping: backup already exists." if backup_exist?

FileUtils.cp_r(dsu_folder, backup_folder) unless pretend?
puts 'Done.'
FileUtils.cp_r(dsu_folder, backup_folder)
end

def backup_exist?
Expand All @@ -74,13 +77,20 @@ def backup_folder
@backup_folder ||= File.join(root_folder, "dsu-#{from_migration_version}-backup")
end

def update_migration_version
def update_migration_version!
puts 'Updating migration version...'
puts

return if pretend? || migration_version == Migration::VERSION
return if pretend? || migration_version == to_migration_version

Models::MigrationVersion.new(version: Migration::VERSION).save!
Models::MigrationVersion.new(version: to_migration_version).save!
end

def seed_data_folder
seed_data_dsu_folder_for(migration_version: to_migration_version)
end

def raise_backup_folder_does_not_exist_error_if!
raise "Backup folder #{backup_folder} does not exist, cannot continue" unless backup_exist?
end
end
end
Expand Down
9 changes: 5 additions & 4 deletions lib/dsu/migration/factory.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# frozen_string_literal: true

require_relative 'service_20230613121411'
require_relative 'service_20240210161248'
require_relative 'v20230613121411/service'
require_relative 'v20240210161248/service'

module Dsu
module Migration
class Factory
class << self
def migrate_if!(options: {})
Service20230613121411.new(options: options).migrate_if!
Service20240210161248.new(options: options).migrate_if!
V20230613121411::Service.new(options: options).migrate_if!
binding.pry
V20240210161248::Service.new(options: options).migrate_if!
rescue StandardError => e
puts I18n.t('migrations.error.failed', message: e.message)
exit 1
Expand Down
15 changes: 15 additions & 0 deletions lib/dsu/migration/raw_json_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require_relative '../crud/json_file'

module Dsu
module Migration
class RawJsonFile < Crud::JsonFile
public :read, :read!, :version=

def to_h
read.merge(version: version)
end
end
end
end
56 changes: 56 additions & 0 deletions lib/dsu/migration/raw_json_files.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# frozen_string_literal: true

require 'fileutils'
require 'pathname'

require_relative 'raw_json_file'

module Dsu
module Migration
class RawJsonFiles
attr_reader :folder

def initialize(folder)
@folder = folder
end

def each_file(regex: //)
return unless folder_exist?

Pathname.new(folder).children.each do |child|
next unless child.file? && child.to_s.match?(regex)

yield RawJsonFile.new(child)
end
end

def folder_exist?
self.class.folder_exist?(folder: folder)
end

class << self
def folder_exist?(folder:)
Dir.exist?(folder)
end
end

private

attr_writer :folder

def safe_cp_r(source, destination)
Pathname.new(source).find do |source_path|
next if source_path.directory?

relative_path = source_path.relative_path_from(Pathname.new(source))
target_path = Pathname.new(destination).join(relative_path)

next if target_path.exist?

FileUtils.mkdir_p(target_path.dirname)
FileUtils.cp(source_path, target_path)
end
end
end
end
end
107 changes: 0 additions & 107 deletions lib/dsu/migration/service_20230613121411.rb

This file was deleted.

121 changes: 0 additions & 121 deletions lib/dsu/migration/service_20240210161248.rb

This file was deleted.

Loading

0 comments on commit 0ef89f6

Please sign in to comment.