Class: PactBroker::DB::MigrationTask

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/pact_broker/tasks/migration_task.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ MigrationTask

Returns a new instance of MigrationTask.



21
22
23
24
# File 'lib/pact_broker/tasks/migration_task.rb', line 21

def initialize &block
  @options = {}
  rake_task &block
end

Instance Attribute Details

#database_connectionObject

Returns the value of attribute database_connection.



18
19
20
# File 'lib/pact_broker/tasks/migration_task.rb', line 18

def database_connection
  @database_connection
end

#optionsObject

Returns the value of attribute options.



19
20
21
# File 'lib/pact_broker/tasks/migration_task.rb', line 19

def options
  @options
end

Instance Method Details

#rake_task(&block) ⇒ Object



26
27
28
29
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
# File 'lib/pact_broker/tasks/migration_task.rb', line 26

def rake_task &block
  namespace :pact_broker do
    namespace :db do
      desc "Run sequel migrations for pact broker database"
      task :migrate, [:target] do | t, args |
        require 'pact_broker/db/migrate'
        require 'pact_broker/db/version'

        instance_eval(&block)

        if args[:target]
          options[:target] = args[:target].to_i
        end

        if (logger = database_connection.loggers.first)
          current_version = PactBroker::DB::Version.call(database_connection)
          if options[:target]
            logger.info "Migrating from schema version #{current_version} to #{options[:target]}"
          else
            logger.info "Migrating from schema version #{current_version} to latest"
          end
        end

        PactBroker::DB::Migrate.call(database_connection, options)

        if logger
          current_version = PactBroker::DB::Version.call(database_connection)
          logger.info "Current schema version is now #{current_version}"
        end
      end
    end
  end
end