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 17, 2024
1 parent 2e4191e commit 0a39793
Show file tree
Hide file tree
Showing 24 changed files with 753 additions and 571 deletions.
14 changes: 10 additions & 4 deletions lib/dsu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@
require_relative 'dsu/env'
require 'pry-byebug' if Dsu.env.development?

Dir.glob("#{__dir__}/dsu/**/*.rb").each do |file|
require file
end
Dir.glob("#{__dir__}/dsu/**/*.rb").each { |file| require file }

Dsu::Migration::Factory.migrate_if!(options: { pretend: false }) unless Dsu.env.test? || Dsu.env.development?
unless Dsu.env.test? || Dsu.env.development?
# 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::Migrator.migrate_if!(migration_services: migration_services)
end
118 changes: 118 additions & 0 deletions lib/dsu/migration/base_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# frozen_string_literal: true

require_relative '../models/migration_version'
require_relative '../support/fileable'
require_relative 'version'

module Dsu
module Migration
class BaseService
include Support::Fileable

def initialize(options: {})
@options = options || {}
end

class << self
def migrates_to_latest_migration_version?
to_migration_version == Migration::VERSION
end

# The migration version that this migration is upgrading from.
def from_migration_version
raise NotImplementedError, 'You must implement the #from_migration_version method in your subclass'
end

# The migration version that this migration is upgrading to.
def to_migration_version
raise NotImplementedError, 'You must implement the #to_migration_version method in your subclass'
end
end

def migrate_if!
return unless run_migration?

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

run_migration!
update_migration_version!

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

private

attr_accessor :options

# You must implement your own migration logic in your subclass and call this method.
def run_migration!
create_backup
end

def pretend?
options.fetch(:pretend, true)
end

def run_migration?
migration_version == from_migration_version
end

# The migration version that this migration is upgrading from.
def from_migration_version
self.class.from_migration_version
end

# The migration version that this migration is upgrading to.
def to_migration_version
self.class.to_migration_version
end

# 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.
Models::MigrationVersion.new.version
end

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

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

FileUtils.cp_r(dsu_folder, backup_folder)
FileUtils.cp(config_path, backup_folder)
end

def backup_exist?
Dir.exist?(backup_folder)
end

def backup_folder
@backup_folder ||= File.join(root_folder, "dsu-#{from_migration_version}-backup")
end

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

return if pretend? || migration_version == to_migration_version

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 seed_data_configuration
seed_data_dsu_configuration_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
end
20 changes: 0 additions & 20 deletions lib/dsu/migration/factory.rb

This file was deleted.

24 changes: 24 additions & 0 deletions lib/dsu/migration/migrator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

require_relative '../env'
require_relative 'version'

module Dsu
module Migration
class Migrator
class << self
def migrate_if!(migration_services: [])
return if migration_services.any? do |migration_service|
migration_service.migrate_if!
migration_service.class.migrates_to_latest_migration_version?
end

raise I18n.t('migrations.error.missing_current_migration_service', migration_version: Migration::VERSION)
rescue StandardError => e
puts I18n.t('migrations.error.failed', message: e.message)
exit 1 unless Dsu.env.test?
end
end
end
end
end
13 changes: 13 additions & 0 deletions lib/dsu/migration/raw_helpers/color_theme_hash.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Dsu
module Migration
module RawHelpers
module ColorThemeHash
def to_h
read.merge(version: version)
end
end
end
end
end
15 changes: 15 additions & 0 deletions lib/dsu/migration/raw_helpers/configuration_hash.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module Dsu
module Migration
module RawHelpers
module ConfigurationHash
attr_accessor :default_project

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

module Dsu
module Migration
module RawHelpers
module EntryGroupHash
def to_h
read.merge(version: version)
end
end
end
end
end
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
87 changes: 0 additions & 87 deletions lib/dsu/migration/service.rb

This file was deleted.

Loading

0 comments on commit 0a39793

Please sign in to comment.