class Cassie::Schema::StructureDumper

@deprecated Use {Cassie::Schema::SchemaDumper} instead

Attributes

destination_path[R]

Public Class Methods

new(opts={}) click to toggle source
# File lib/cassie/schema/structure_dumper.rb, line 8
def initialize(opts={})
  @destination_path = opts[:destination_path] || default_destination_path
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/structure_dumper.rb, line 59
def dump
  stream << keyspace_structure
  stream << schema_meta_structure
  stream << "\n\n"
  stream << versions_insert_cql
  stream << "\n"

  close_stream
end
keyspace_structure() click to toggle source

Fetch the CQL that can be used to recreate the current environment's keyspace @return [String] CQL commands @raise [RuntimeError] if the {Cassie.configuration} keyspace could not be described.

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

    runner.output
  end
end
schema_meta_structure() click to toggle source

Fetch the CQL that can be used to recreat the schema metadata keyspace, if it has been defined. If it could not be fetched (likely because it doesn't exist), an empty string is returned. @return [String] CQL commands

# File lib/cassie/schema/structure_dumper.rb, line 36
def schema_meta_structure
  CreateVersionsTableQuery.new.to_cql
end
stream() click to toggle source
# File lib/cassie/schema/structure_dumper.rb, line 12
def stream
  @stream ||= begin
    prepare_stream
    File.open(destination_path, "w+")
  end
end
versions() click to toggle source
# File lib/cassie/schema/structure_dumper.rb, line 40
def versions
  @versions ||= begin
    versions_query.fetch
  rescue Cassandra::Errors::InvalidError => e
    log_versions_not_found(e)
    []
  end
end
versions_insert_cql() click to toggle source
# File lib/cassie/schema/structure_dumper.rb, line 49
def versions_insert_cql
  inserts = versions.map do |v|
    InsertVersionQuery.new(version: v).to_cql
  end
  inserts.join("\n")
end
versions_table_name() click to toggle source
# File lib/cassie/schema/structure_dumper.rb, line 69
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/structure_dumper.rb, line 92
def close_stream
  stream.close
  @stream = nil
end
default_destination_path() click to toggle source
# File lib/cassie/schema/structure_dumper.rb, line 83
def default_destination_path
  Cassie::Schema.paths[:schema_file]
end
log_versions_not_found(error) click to toggle source
# File lib/cassie/schema/structure_dumper.rb, line 97
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/structure_dumper.rb, line 104
def logger
  Cassie.logger
end
prepare_stream() click to toggle source
# File lib/cassie/schema/structure_dumper.rb, line 87
def prepare_stream
  dir = File.dirname(destination_path)
  Dir.mkdir(dir) unless File.directory?(dir)
end
versions_query() click to toggle source
# File lib/cassie/schema/structure_dumper.rb, line 79
def versions_query
  SelectVersionsQuery.new
end