Class: OpenHood::Rake::SequelTask

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/openhood/rake/sequel.rb

Instance Method Summary collapse

Constructor Details

#initializeSequelTask

Returns a new instance of SequelTask.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/openhood/rake/sequel.rb', line 30

def initialize
  desc "Sequel migration"
  namespace :db do
    namespace :migrate do
      desc "Rollbacks the database one migration and re migrate up. If you want to rollback more than one step, define STEP=x"
      task :redo => [ "db:rollback", "db:migrate" ]
    end

    desc "Migrate the database through scripts in db/migrate. Target specific version with VERSION=x"
    task :migrate do
      connection
      options = default_options
      options[:target] = ENV["VERSION"].to_i if ENV["VERSION"]
      Sequel::Migrator.run(Sequel::Model.db, path, options)
    end

    desc "Rolls the schema back to the previous version. Specify the number of steps with STEP=n"
    task :rollback do
      connection
      step = ENV["STEP"] ? ENV["STEP"].to_i : 1
      target = Sequel::Migrator.get_current_migration_version(Sequel::Model.db) - step
      Sequel::Migrator.run(Sequel::Model.db, path, default_options.merge({:target => target}))
    end

    desc "Retrieves the current schema version number"
    task :version do
      connection
      puts "Current version: #{Sequel::Migrator.get_current_migration_version(Sequel::Model.db, default_options)}"
    end
  end
end

Instance Method Details

#base_pathObject



9
10
11
# File 'lib/openhood/rake/sequel.rb', line 9

def base_path
  File.expand_path('.')
end

#configObject



17
18
19
20
# File 'lib/openhood/rake/sequel.rb', line 17

def config
  environment = ENV["RACK_ENV"] || "development"
  @config ||= YAML.load_file(File.join(base_path, "config", "#{environment}.yml"))
end

#connectionObject



22
23
24
# File 'lib/openhood/rake/sequel.rb', line 22

def connection
  Sequel.connect(config[:db][:uri])
end

#default_optionsObject



26
27
28
# File 'lib/openhood/rake/sequel.rb', line 26

def default_options
  {:table => :"#{config[:app_name]}_schema_info", :column => :version}
end

#pathObject



13
14
15
# File 'lib/openhood/rake/sequel.rb', line 13

def path
  File.join(base_path, "migrations")
end