Class: MigrationUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/sequel_tools/migration_utils.rb

Class Method Summary collapse

Class Method Details

.apply_migration(context, version, direction) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/sequel_tools/migration_utils.rb', line 7

def self.apply_migration(context, version, direction)
  ( puts 'migration version is missing - aborting.'; exit 1 ) if version.nil?
  filename = "#{File.basename version, '.rb'}.rb"
  migrator = find_migrator context, direction
  migrator.migration_tuples.delete_if{|(m, fn, dir)| fn != filename }
  unless (size = migrator.migration_tuples.size) == 1
    puts "Expected a single unapplied migration for #{filename} but found #{size}. Aborting."
    exit 1
  end
  migrator.run
end

.current_version(context) ⇒ Object



29
30
31
32
# File 'lib/sequel_tools/migration_utils.rb', line 29

def self.current_version(context)
  migrator = find_migrator(context)
  migrator.ds.order(Sequel.desc(migrator.column)).get migrator.column
end

.find_migrator(context, direction = :up) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/sequel_tools/migration_utils.rb', line 19

def self.find_migrator(context, direction = :up)
  Sequel.extension :migration unless Sequel.respond_to? :migration
  SequelTools::ActionsManager::Action[:connect_db].run({}, context) unless context[:db]
  options = { allow_missing_migration_files: true }
  options[:target] = 0 if direction == :down
  config = context[:config]
  Sequel::Migrator.migrator_class(config[:migrations_location]).
    new(context[:db], config[:migrations_location], options)
end

.last_found_migration(context) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/sequel_tools/migration_utils.rb', line 34

def self.last_found_migration(context)
  migrations_path = context[:config][:migrations_location]
  migrator = find_migrator(context)
  migrator.ds.order(Sequel.desc(migrator.column)).select_map(migrator.column).find do |fn|
    File.exist?("#{migrations_path}/#{fn}")
  end
end

.migrations_differences(context) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/sequel_tools/migration_utils.rb', line 42

def self.migrations_differences(context)
  config = context[:config]
  migrations_path = config[:migrations_location]
  existing = Dir["#{migrations_path}/*.rb"].map{|fn| File.basename fn }.sort
  migrator = find_migrator context
  migrated = migrator.ds.select_order_map(migrator.column)
  unapplied = existing - migrated
  files_missing = migrated - existing
  [ unapplied, files_missing ]
end