Class: ROM::Cassandra::Migrations::Migrator

Inherits:
Object
  • Object
show all
Defined in:
lib/rom/cassandra/migrations/migrator.rb

Overview

Class Migrator finds the migration files and migrates Cassandra cluster to the required version using method [#apply].

The class is responcible for searching files and deciding, which of them should be runned up and down. Every single migration is applied or rolled back using ‘RunnerUp` and `RunnerDown` classes.

Examples:

migrator = Migrator.new(hosts: ["127.0.0.1"], port: 9042)

# Applies all migrations
migrator.apply

# Rolls back all migrations
migrator.apply version: 0

# Moves to the required version (before the year 2016)
migrator.apply version: 20151231235959

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session, options = {}) ⇒ Migrator

Initializes a migrator with Cassandra uri settings.

Can specify logger and path as well.

See [ROM::Cassandra::Session] for other avaliable options for URI

Parameters:

Options Hash (options):

  • :logger (::Logger)
  • :path (String)


62
63
64
65
66
67
# File 'lib/rom/cassandra/migrations/migrator.rb', line 62

def initialize(session, options = {})
  @session = session
  @logger  = options.fetch(:logger) { Logger.new }
  @root    = options.fetch(:path) { DEFAULT_PATH }
  @paths   = Dir[File.join(root, "*.rb")].sort
end

Instance Attribute Details

#logger::Logger (readonly)

Returns The logger used by the migrator.

Returns:

  • (::Logger)

    The logger used by the migrator



38
39
40
# File 'lib/rom/cassandra/migrations/migrator.rb', line 38

def logger
  @logger
end

#pathsArray<String> (readonly)

Returns The list of paths to migration files.

Returns:

  • (Array<String>)

    The list of paths to migration files



50
51
52
# File 'lib/rom/cassandra/migrations/migrator.rb', line 50

def paths
  @paths
end

#rootArray<String> (readonly)

Returns The root path for migrations.

Returns:

  • (Array<String>)

    The root path for migrations



44
45
46
# File 'lib/rom/cassandra/migrations/migrator.rb', line 44

def root
  @root
end

#sessionROM::Cassandra::Session (readonly)

Returns The session to the Cassandra cluster.

Returns:



32
33
34
# File 'lib/rom/cassandra/migrations/migrator.rb', line 32

def session
  @session
end

Instance Method Details

#apply(options = {}) ⇒ undefined

Migrates the Cassandra cluster to selected version

Applies all migrations if a version is skipped. Rolls all migrations back if a version is set to 0.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :version (Integer, nil)

Returns:

  • (undefined)


78
79
80
81
82
83
# File 'lib/rom/cassandra/migrations/migrator.rb', line 78

def apply(options = {})
  version = options.fetch(:version) { ALL_VERSIONS }

  migrate_to version
  rollback_to version
end