Class: Shaf::Tasks::DbTask

Inherits:
Object
  • Object
show all
Includes:
Rake::DSL
Defined in:
lib/shaf/tasks/db_task.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, description:, args: [], &block) ⇒ DbTask

Returns a new instance of DbTask.



6
7
8
9
10
11
12
# File 'lib/shaf/tasks/db_task.rb', line 6

def initialize(name, description:, args: [], &block)
  @name = name
  @desc = description
  @args = args
  @block = block
  define_task
end

Instance Method Details

#define_taskObject



14
15
16
17
18
19
20
21
22
23
# File 'lib/shaf/tasks/db_task.rb', line 14

def define_task
  namespace :db do
    desc @desc
    task @name, @args do |t, args|
      require 'config/database'
      Sequel.extension :migration
      instance_exec(t, args, &@block)
    end
  end
end

#extract_version_and_filename(filename) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/shaf/tasks/db_task.rb', line 33

def extract_version_and_filename(filename)
  return [] unless filename
  filename = filename[:filename] if filename.is_a? Hash
  match = /(\d*)_(.*).rb/.match(filename)
  return [] unless match
  match[1..2]
end

#last_migrationObject



29
30
31
# File 'lib/shaf/tasks/db_task.rb', line 29

def last_migration
  DB[:schema_migrations].order(:filename).last
end

#migrationsObject



25
26
27
# File 'lib/shaf/tasks/db_task.rb', line 25

def migrations
  @migrations ||= DB[:schema_migrations].all
end