-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
753 additions
and
571 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.