class Cassie::Schema::SchemaDumper

Attributes

destination_path[R]

The location to dump the ruby file

keyspace[R]

The keyspace that will be extracted and replaced with +#{default_keyspace}+ upon dumping

Public Class Methods

new(opts={}) click to toggle source
# File lib/cassie/schema/schema_dumper.rb, line 12
def initialize(opts={})
  @destination_path = opts[:destination_path] || default_destination_path
  @keyspace = Cassie.configuration[:keyspace]
end

Public Instance Methods

dump() click to toggle source

Dump the CQL for the current environment's keyspace, the schema metadata keyspace, and the versions rows that are currently in the schema versions table.

# File lib/cassie/schema/schema_dumper.rb, line 28
    def dump
      stream << <<-EOS
# This file describes the keyspace-agnostic schema
# for this application's environments.
#
# It is the definitive source of the current state
# of the schema and should not be modified directly.
#
# It is strongly recommened that this schema file be checked into source control.
#
# Use `cassie` commands to apply this schema to a particular environment:
# * Load this schema with `cassie schema:load`
# * Reset the schema to this definition with `cassie schema:reset`
Cassie::Schema.define do

EOS
      stream << "  create_schema <<-EOS\n"
      stream << "#{keyspace_agnostic_cql}\n"
      stream << "EOS\n"
      stream << "\n\n"
      versions.each do |v|
        stream << "  record_version #{version_arg_str(v)}\n"
      end
      stream << "end\n"

      close_stream
    end
stream() click to toggle source

The stream to dump the source to

# File lib/cassie/schema/schema_dumper.rb, line 18
def stream
  @stream ||= begin
    prepare_stream
    File.open(destination_path, "w+")
  end
end
versions_table_name() click to toggle source
# File lib/cassie/schema/schema_dumper.rb, line 56
def versions_table_name
  "#{Cassie::Schema.schema_keyspace}.#{Cassie::Schema.versions_table}"
end

Protected Instance Methods

close_stream() click to toggle source
# File lib/cassie/schema/schema_dumper.rb, line 103
def close_stream
  stream.close
  @stream = nil
end
default_destination_path() click to toggle source
# File lib/cassie/schema/schema_dumper.rb, line 94
def default_destination_path
  Cassie::Schema.paths[:schema_file]
end
keyspace_agnostic_cql() click to toggle source

Fetch the keyspace agnostic CQL ruby string that can be used to recreate the keyspace. The keyspace value is replaced with +#{default_keyspace}+ @return [String] A ruby string with keyspace interpolated CQL commands @raise [RuntimeError] if the {Cassie.configuration} keyspace could not be described.

# File lib/cassie/schema/schema_dumper.rb, line 71
def keyspace_agnostic_cql
  @keyspace_structure ||= begin
    args = ["-e", "'DESCRIBE KEYSPACE #{keyspace}'"]
    runner = Cassie::Support::SystemCommand.new("cqlsh", args)
    runner.succeed

    runner.output.gsub(keyspace, '#{default_keyspace}').strip
  end
end
log_versions_not_found(error) click to toggle source
# File lib/cassie/schema/schema_dumper.rb, line 108
def log_versions_not_found(error)
  msg = "WARNING: Cassie Schema Versions table not found at '#{versions_table_name}'. Initialize your schema with `cassie schema:init` or `cassie:migrations:import` for versioned migration support."
  msg << "\n\t- "
  msg << error.message.split("\n").join("\n\t- ")
  logger.warn(msg)
end
logger() click to toggle source
# File lib/cassie/schema/schema_dumper.rb, line 115
def logger
  Cassie.logger
end
prepare_stream() click to toggle source
# File lib/cassie/schema/schema_dumper.rb, line 98
def prepare_stream
  dir = File.dirname(destination_path)
  Dir.mkdir(dir) unless File.directory?(dir)
end
version_arg_str(version) click to toggle source
# File lib/cassie/schema/schema_dumper.rb, line 119
    def version_arg_str(version)
      <<-EOS.strip
"#{version}", "#{version.description}", "#{version.id}", "#{version.executor}", "#{version.executed_at.try(:iso8601, 6)}"
EOS
    end
versions() click to toggle source
# File lib/cassie/schema/schema_dumper.rb, line 81
def versions
  @versions ||= begin
    versions_query.fetch
  rescue Cassandra::Errors::InvalidError => e
    log_versions_not_found(e)
    []
  end
end
versions_query() click to toggle source
# File lib/cassie/schema/schema_dumper.rb, line 90
def versions_query
  SelectVersionsQuery.new
end