-
-
Save saulius/54e0f522d0b54d96d2aa to your computer and use it in GitHub Desktop.
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
require 'singleton' | |
require 'forwardable' | |
module TradeTariffBackend | |
class DataMigration | |
include Singleton | |
extend SingleForwardable | |
module BlockAccessor | |
extend ActiveSupport::Concern | |
module ClassMethods | |
def block_accessor(*names) | |
names.each do |name| | |
define_method(name) do |&block| | |
if block | |
instance_variable_set(:"@#{name}", block) | |
else | |
# lazy call | |
instance_variable_get(:"@#{name}") #.call | |
end | |
end | |
end | |
end | |
end | |
end | |
class Runner | |
include BlockAccessor | |
block_accessor :met, :meet | |
def initialize(&block) | |
instance_eval &block | |
end | |
end | |
def_delegators :instance, :applied? | |
delegate :up, :down, to: self.class | |
def up_runner | |
@up_runner ||= Runner.new(&up) | |
end | |
def down_runner | |
@down_runner ||= Runner.new(&down) | |
end | |
def applied? | |
up_runner.met #.call | |
end | |
def can_rollback? | |
down_runner.met #.call | |
end | |
def created_at | |
# parse from filename or created_at | |
# or default to NullDate? | |
end | |
class << self | |
include BlockAccessor | |
block_accessor :up, :down | |
end | |
def self.[](migration_name) | |
# DataMigrator.migrations...? | |
end | |
end | |
class Dependency | |
attr_reader :migrations | |
def self.[](*migrations) | |
new(migrations) | |
end | |
def new(migrations) | |
@migrations = migrations | |
end | |
def met? | |
migrations.all?(&:met?) | |
end | |
end | |
class ExampleMigration < DataMigration | |
up do | |
met { | |
Dependency[AnotherMigration, SomeOtherMigration].met? && Chief::Mfcm.where(cmdty_code: '00000000').none? | |
} | |
meet {} | |
end | |
down do | |
met { false } | |
meet {} | |
end | |
end | |
class DataMigrator | |
class Presenter | |
def self.present(migrations, presenter = ConsolePresenter) | |
migrations.sort_by(&:created_at).each {|migration| | |
presenter.present(migration) | |
} | |
end | |
end | |
class ConsolePreseter < Presenter | |
def self.present(migration) | |
puts "[ #{migration.state} ] #{migration.name}" | |
end | |
end | |
class << self | |
def migrations | |
DataMigration.descendants.map(&:instance) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment