Class: CassSchema::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/cass_schema/runner.rb

Defined Under Namespace

Classes: DropCommandsNotAllowed

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Runner

Create a new Runner drop commands will raise an exception instead of executing the command.

Parameters:

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

    a customizable set of options

Options Hash (options):

  • :datastores (Array<CassSchema::Datastore>)
    • The list of datastore objects for which schemas

    will be managed.

  • :schema_bath_path (String)
    • The directory where schema definitions live. In a rails env,

    this defaults to <rails root>/cass_schema.

  • :logger (#info|#error)

    optional logger to use when creating schemas

  • :disallow_drops (Boolean)

    Defaults to false. If set to true,



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cass_schema/runner.rb', line 20

def initialize(options = {})
  options[:schema_base_path] ||= defined?(::Rails) ? File.join(::Rails.root, 'cass_schema') : nil

  @datastores = options[:datastores]
  @schema_base_path = options[:schema_base_path]
  @logger = options[:logger]
  @disallow_drops = options[:disallow_drops]

  raise ":datastores is a required argument!" unless @datastores

  @datastores.each { |ds| ds._setup(options) }
end

Instance Attribute Details

#cluster_builderObject (readonly)

Returns the value of attribute cluster_builder.



10
11
12
# File 'lib/cass_schema/runner.rb', line 10

def cluster_builder
  @cluster_builder
end

#datastoresObject (readonly)

Returns the value of attribute datastores.



10
11
12
# File 'lib/cass_schema/runner.rb', line 10

def datastores
  @datastores
end

#disallow_dropsObject (readonly)

Returns the value of attribute disallow_drops.



10
11
12
# File 'lib/cass_schema/runner.rb', line 10

def disallow_drops
  @disallow_drops
end

#loggerObject (readonly)

Returns the value of attribute logger.



10
11
12
# File 'lib/cass_schema/runner.rb', line 10

def logger
  @logger
end

#schema_base_pathObject (readonly)

Returns the value of attribute schema_base_path.



10
11
12
# File 'lib/cass_schema/runner.rb', line 10

def schema_base_path
  @schema_base_path
end

Class Method Details

.setup(options = {}) ⇒ Object

Create a new Runner drop commands will raise an exception instead of executing the command.

Parameters:

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

    a customizable set of options

Options Hash (options):

  • :datastores (Array<CassSchema::Datastore>)
    • The list of datastore objects for which schemas

    will be managed.

  • :schema_bath_path (String)
    • The directory where schema definitions live. In a rails env,

    this defaults to <rails root>/cass_schema.

  • :logger (#info|#error)

    optional logger to use when creating schemas

  • :disallow_drops (Boolean)

    Defaults to false. If set to true,



76
77
78
# File 'lib/cass_schema/runner.rb', line 76

def setup(options = {})
  @runner = Runner.new(options)
end

Instance Method Details

#create(datastore_name) ⇒ Object

Create the schema for a particular datastore

Parameters:

  • datastore_name (String)


46
47
48
# File 'lib/cass_schema/runner.rb', line 46

def create(datastore_name)
  datastore_lookup(datastore_name).create
end

#create_allObject

Create all schemas for all datastores



34
35
36
# File 'lib/cass_schema/runner.rb', line 34

def create_all
  datastores.each { |d| d.create }
end

#datastore_lookup(datastore_name) ⇒ CassSchema::Datastore

Find a datastore based on the datastore name

Parameters:

  • datastore_name (String|Symbol)

    The datastore name

Returns:

  • (CassSchema::Datastore)


67
68
69
70
# File 'lib/cass_schema/runner.rb', line 67

def datastore_lookup(datastore_name)
  @datastore_lookup ||= Hash[datastores.map { |ds| [ds.name, ds] }]
  @datastore_lookup[datastore_name.to_s] || (raise ArgumentError.new("CassSchema datastore #{datastore_name} not found"))
end

#drop(datastore_name) ⇒ Object

Drop the schema for a particular datastore

Parameters:

  • datastore_name (String)

Raises:



52
53
54
55
# File 'lib/cass_schema/runner.rb', line 52

def drop(datastore_name)
  raise DropCommandsNotAllowed if disallow_drops
  datastore_lookup(datastore_name).drop
end

#drop_allObject

Drop all schemas for all datastores



39
40
41
42
# File 'lib/cass_schema/runner.rb', line 39

def drop_all
  raise DropCommandsNotAllowed if disallow_drops
  datastores.each { |d| d.drop }
end

#migrate(datastore_name, migration_name) ⇒ Object

Run a particular named migration for a datastore

Parameters:

  • datastore_name (String)
  • migration_name (String)


60
61
62
# File 'lib/cass_schema/runner.rb', line 60

def migrate(datastore_name, migration_name)
  datastore_lookup(datastore_name).migrate(migration_name)
end