Skip to content

Commit

Permalink
Migrations for this version
Browse files Browse the repository at this point in the history
  • Loading branch information
gangelo committed Feb 10, 2024
1 parent 75efeb5 commit 3889703
Show file tree
Hide file tree
Showing 21 changed files with 233 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
dsu (2.4.4)
dsu (3.0.0.alpha.4)
activemodel (>= 7.0.8, < 8.0)
activesupport (>= 7.0.8, < 8.0)
colorize (>= 1.1, < 2.0)
Expand Down
6 changes: 6 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ require "rubocop/rake_task"
RuboCop::RakeTask.new

task default: %i[spec rubocop]

desc 'Generate a migration timestamp'
task :timestamp do
puts 'The below migration timestamp should be placed in the "lib/dsu/migration/version.rb" file.'
puts Time.now.strftime('%Y%m%d%H%M%S')
end
4 changes: 0 additions & 4 deletions current_project.bak

This file was deleted.

12 changes: 2 additions & 10 deletions lib/dsu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,6 @@
exit 1
end
end
# TODO: Hack. Integrate this into the migration service
# so that this runs only if the migration version changes.
%w[light.json christmas.json].each do |theme_file|
destination_theme_file_path = File.join(Dsu::Support::Fileable.themes_folder, theme_file)
next if File.exist?(destination_theme_file_path)

source_theme_file_path = File.join(Dsu::Support::Fileable.seed_data_folder, 'themes', theme_file)
FileUtils.cp(source_theme_file_path, destination_theme_file_path)
puts I18n.t('migrations.information.theme_copied', from: source_theme_file_path, to: destination_theme_file_path)
end

Dsu::Migration::Factory.migrate_if!
end
26 changes: 26 additions & 0 deletions lib/dsu/migration/factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require_relative '../models/migration_version'
require_relative 'service_20230613121411'
require_relative 'version'

module Dsu
module Migration
class Factory
class << self
def migrate_if!(options: {})
version = options.fetch(:version, migration_version)
if version == 20230613121411 # rubocop:disable Style/NumericLiterals
Service20230613121411.new(options: options).migrate!
end
end

private

def migration_version
@migration_version ||= Models::MigrationVersion.new.version
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/dsu/migration/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def call

class << self
def run_migrations?
Models::MigrationVersion.new.version < Dsu::Migration::VERSION
Models::MigrationVersion.new.version < 20230613121411 # rubocop:disable Style/NumericLiterals
end
end

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

require_relative '../support/fileable'
require_relative 'version'

# TODO: Read raw configuration .json file
# If default_project is not set...
# - Add default_project to configuration .json file and write it out.
# - Reload the configuration file.
# - Create a Models::Project object for the default project and initialize/save it.
# - Move the old entries folder into the default project folder.
# TODO: Add default_project to configuration .json file
module Dsu
module Migration
class Service20230613121411
include Support::Fileable

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

def migrate!
puts 'Running migrations...'
puts

puts "options[:pretend] is true\n" if pretend?

raise_wrong_migration_version_error_if!

puts "Migrating from: #{target_migration_version} to version: #{Migration::VERSION}"
puts

add_new_color_themes
backup
create_default_project
update_configuration
update_entry_groups
update_color_themes
delete_old_entry_folder
delete_old_theme_folder

puts 'Migration completed successfully.'
end

private

attr_reader :options

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

def add_new_color_themes
puts 'Copying new color themes...'
puts

%w[light.json christmas.json].each do |theme_file|
destination_theme_file_path = File.join(Dsu::Support::Fileable.themes_folder, theme_file)
next if File.exist?(destination_theme_file_path)

source_theme_file_path = File.join(Dsu::Support::Fileable.seed_data_folder, 'themes', theme_file)
FileUtils.cp(source_theme_file_path, destination_theme_file_path) unless pretend?
puts I18n.t('migrations.information.theme_copied', from: source_theme_file_path, to: destination_theme_file_path)
end
end

def backup
return if Dir.exist?(backup_folder)

puts 'Creating backup...'
puts

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

def create_default_project
default_project = Models::Configuration::DEFAULT_CONFIGURATION[:default_project]
return if Models::Project.project_initialized?(project_name: default_project)

puts "Creating default project \"#{default_project}\"..."
puts

Models::Project.create(project_name: default_project, options: options) unless pretend?
end

def update_configuration
puts 'Updating configuration...'
puts

Models::Configuration.new.write! unless pretend?
end

def update_entry_groups
puts 'Updating entry groups...'
puts

return if Dir.exist?(entries_folder) || pretend?

puts 'Copying entries to default project...'
puts

FileUtils.mkdir_p(entries_folder)
FileUtils.cp_r(File.join(backup_folder, 'entries', '.'), entries_folder)

puts 'Updating entry group version...'
puts

Models::EntryGroup.all.each do |entry_group|
puts "Updating entry group version: #{entry_group.time_yyyy_mm_dd}..."
entry_group.version = Dsu::Migration::VERSION
entry_group.save! unless pretend?
end
end

def update_color_themes
puts 'Updating color themes...'
puts

return if Dir.exist?(themes_folder) || pretend?

puts 'Copying color themes...'
puts

FileUtils.mkdir_p(themes_folder)
FileUtils.cp_r(File.join(backup_folder, 'themes', '.'), themes_folder)

puts 'Updating color theme version...'
puts

Models::ColorTheme.all.each do |color_theme|
puts "Updating color theme version: #{color_theme.theme_name}..."
color_theme.version = Dsu::Migration::VERSION
color_theme.save! unless pretend?
end
end

def delete_old_entry_folder
puts 'Cleaning up old entries...'
puts

FileUtils.rm_rf(File.join(dsu_folder, 'entries')) unless pretend?
end

def delete_old_theme_folder
puts 'Cleaning up old themes...'
puts

FileUtils.rm_rf(File.join(dsu_folder, 'themes')) unless pretend?
end

def backup_folder
@backup_folder ||= File.join(dsu_folder, target_migration_version.to_s)
end

def target_migration_version
20230613121411 # rubocop:disable Style/NumericLiterals
end

def raise_wrong_migration_version_error_if!
return if migration_version == target_migration_version

raise "Actual migration version #{migration_version} " \
"is not the expected migration version #{target_migration_version}."
end

def migration_version
@migration_version ||= Models::MigrationVersion.new.version
end
end
end
end
2 changes: 1 addition & 1 deletion lib/dsu/migration/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Dsu
module Migration
VERSION = 20230613121411 # rubocop:disable Style/NumericLiterals
VERSION = 20240210161248 # rubocop:disable Style/NumericLiterals
end
end
1 change: 0 additions & 1 deletion lib/dsu/models/migration_version.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# frozen_string_literal: true

require 'active_model'
require_relative '../crud/json_file'
require_relative '../services/migration_version/hydrator_service'
require_relative '../validators/version_validator'
Expand Down
2 changes: 1 addition & 1 deletion lib/dsu/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

module Dsu
VERSION_REGEX = /\A\d+\.\d+\.\d+(\.(alpha|rc)\.\d+)?\z/
VERSION = '2.4.4'
VERSION = '3.0.0.alpha.5'
end
4 changes: 2 additions & 2 deletions spec/dsu/migration/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@
end
end

context 'when the migration version less than the current version' do
context 'when the migration version less than 20230613121411' do
before do
create(:migration_version, version: Dsu::Migration::VERSION - 1)
create(:migration_version, version: 20230613121411 - 1) # rubocop:disable Style/NumericLiterals
end

it 'returns true' do
Expand Down
2 changes: 1 addition & 1 deletion spec/dsu/models/color_theme_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@

describe 'constants' do
it 'defines VERSION' do
expect(described_class::VERSION).to eq 20230613121411 # rubocop:disable Style/NumericLiterals
expect(described_class::VERSION).to eq 20240210161248 # rubocop:disable Style/NumericLiterals
end

it_behaves_like 'the version is a valid version'
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/files/current_project.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": 20230613121411,
"version": 20240210161248,
"project_name": "default"
}
2 changes: 1 addition & 1 deletion spec/fixtures/files/entries/2023-06-15.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"time": "2023-06-15 20:34:09 -0400",
"version": 20230613121411,
"version": 20240210161248,
"entries": [
{
"description": "20230615 description 0"
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/files/entries/2023-06-16.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"time": "2023-06-16 20:33:40 -0400",
"version": 20230613121411,
"version": 20240210161248,
"entries": [
{
"description": "20230616 description 0"
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/files/entries/2023-06-17.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"time": "2023-06-17 20:34:26 -0400",
"version": 20230613121411,
"version": 20240210161248,
"entries": [
{
"description": "20230617 description 0"
Expand Down
8 changes: 4 additions & 4 deletions spec/fixtures/files/import-with-duplicate-errors.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
project_name,version,entry_group,entry_no,total_entries,entry_group_entry
default,20230613121411,2023-12-31,1,2,Entry 2023-12-31 duplicate
default,20230613121411,2023-12-31,2,2,Entry 2023-12-31 duplicate
default,20230613121411,2024-01-01,1,1,Entry 2024-01-01 1 of 2
default,20230613121411,2024-01-02,1,1,Entry 2024-01-02 2 of 2
default,20240210161248,2023-12-31,1,2,Entry 2023-12-31 duplicate
default,20240210161248,2023-12-31,2,2,Entry 2023-12-31 duplicate
default,20240210161248,2024-01-01,1,1,Entry 2024-01-01 1 of 2
default,20240210161248,2024-01-02,1,1,Entry 2024-01-02 2 of 2
8 changes: 4 additions & 4 deletions spec/fixtures/files/import-with-errors.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
project_name,version,entry_group,entry_no,total_entries,entry_group_entry
default,20230613121411,2023-12-31,1,2,
default,20230613121411,2023-12-31,2,2,
default,20230613121411,2024-01-01,1,1,
default,20230613121411,2024-01-02,1,1,
default,20240210161248,2023-12-31,1,2,
default,20240210161248,2023-12-31,2,2,
default,20240210161248,2024-01-01,1,1,
default,20240210161248,2024-01-02,1,1,
8 changes: 4 additions & 4 deletions spec/fixtures/files/import.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
project_name,version,entry_group,entry_no,total_entries,entry_group_entry
default,20230613121411,2023-12-31,1,2,Entry 2023-12-31 1 of 2
default,20230613121411,2023-12-31,2,2,Entry 2023-12-31 2 of 2
default,20230613121411,2024-01-01,1,1,Entry 2024-01-01 1 of 1
default,20230613121411,2024-01-02,1,1,Entry 2024-01-02 1 of 1
default,20240210161248,2023-12-31,1,2,Entry 2023-12-31 1 of 2
default,20240210161248,2023-12-31,2,2,Entry 2023-12-31 2 of 2
default,20240210161248,2024-01-01,1,1,Entry 2024-01-01 1 of 1
default,20240210161248,2024-01-02,1,1,Entry 2024-01-02 1 of 1
8 changes: 4 additions & 4 deletions spec/fixtures/files/nothing-to-import.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
project_name,version,entry_group,entry_no,total_entries,entry_group_entry
bad-project,20230613121411,2023-12-31,1,2,Entry 2023-12-31 1 of 2
bad-project,20230613121411,2023-12-31,2,2,Entry 2023-12-31 2 of 2
bad-project,20230613121411,2024-01-01,1,1,Entry 2024-01-01 1 of 1
bad-project,20230613121411,2024-01-02,1,1,Entry 2024-01-02 1 of 1
bad-project,20240210161248,2023-12-31,1,2,Entry 2023-12-31 1 of 2
bad-project,20240210161248,2023-12-31,2,2,Entry 2023-12-31 2 of 2
bad-project,20240210161248,2024-01-01,1,1,Entry 2024-01-01 1 of 1
bad-project,20240210161248,2024-01-02,1,1,Entry 2024-01-02 1 of 1
2 changes: 1 addition & 1 deletion spec/fixtures/files/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": 20230613121411,
"version": 20240210161248,
"project_name": "default",
"description": "Default project"
}

0 comments on commit 3889703

Please sign in to comment.