class DatastaxRails::Schema::Migrator

DatastaxRails reads the attributes from the individual models. This class migrates both Cassandra and Solr to the point where they reflect what is specified in the models.

Attributes

errors[RW]

Public Class Methods

new(keyspace) click to toggle source
# File lib/datastax_rails/schema/migrator.rb, line 13
def initialize(keyspace)
  @keyspace = keyspace
  check_schema_migrations unless keyspace == 'system'
  @errors = []
end

Public Instance Methods

connection() click to toggle source
# File lib/datastax_rails/schema/migrator.rb, line 75
def connection
  DatastaxRails::Base.connection
end
migrate_all(force = false) click to toggle source
# File lib/datastax_rails/schema/migrator.rb, line 44
def migrate_all(force = false)
  say_with_time('Migrating all models') do
    FileList[rails_models].each do |model|
      require model
    end

    count = 0
    DatastaxRails::Base.models.each do |m|
      count += migrate_one(m, force) unless m.abstract_class?
    end
    count
  end
end
migrate_one(model, force = false) click to toggle source
# File lib/datastax_rails/schema/migrator.rb, line 58
def migrate_one(model, force = false)
  count = 0
  say_with_time("Migrating #{model.name} to latest version") do
    unless column_family_exists?(model.column_family.to_s)
      create_cql3_column_family(model)
      count += 1
    end

    count += check_missing_schema(model)

    unless model <= DatastaxRails::CassandraOnlyModel
      count += upload_solr_configuration(model, force)
    end
  end
  count
end
reindex_all() click to toggle source
# File lib/datastax_rails/schema/migrator.rb, line 19
def reindex_all
  say_with_time('Reindexing all models') do
    FileList[rails_models].each do |model|
      require model
    end

    count = 0
    DatastaxRails::Base.models.each do |m|
      count += reindex_one(m) unless m.abstract_class?
    end
    count
  end
end
reindex_one(model) click to toggle source
# File lib/datastax_rails/schema/migrator.rb, line 33
def reindex_one(model)
  count = 0
  unless model <= DatastaxRails::CassandraOnlyModel
    say_with_time("Reindexing #{model.name}") do
      reindex_solr(model, false)
      count += 1
    end
  end
  count
end

Private Instance Methods

check_schema_migrations() click to toggle source

Checks to ensure that the schema_migrations column family exists and creates it if not

# File lib/datastax_rails/schema/migrator.rb, line 94
def check_schema_migrations
  return if column_family_exists?('schema_migrations')
  say 'Creating schema_migrations column family'
  DatastaxRails::Cql::CreateColumnFamily.new('schema_migrations').primary_key('cf')
    .columns(cf: :text, digest: :text, solrconfig: :text, stopwords: :text).execute
end
rails_models() click to toggle source

Determine all models to be included within the migration using Rails config paths instead of absolute paths. This enables Rails Engines to monkey patch their own models in, to be automatically included within migrations.

@see pivotallabs.com/leave-your-migrations-in-your-rails-engines/

@return [Array] list of configured application models

# File lib/datastax_rails/schema/migrator.rb, line 89
def rails_models
  Rails.configuration.paths['app/models'].expanded.map { |p| p + '/*.rb' }
end
say(message, subitem = false) click to toggle source
# File lib/datastax_rails/schema/migrator.rb, line 105
def say(message, subitem = false)
  write "#{subitem ? '   ->' : '--'} #{message}"
end
say_with_time(message) { || ... } click to toggle source
# File lib/datastax_rails/schema/migrator.rb, line 109
def say_with_time(message)
  say(message)
  result = nil
  time = Benchmark.measure { result = yield }
  say format('%.4fs', time.real), :subitem
  say("#{result} changes", :subitem) if result.is_a?(Integer)
  result
end
suppress_messages() { || ... } click to toggle source
# File lib/datastax_rails/schema/migrator.rb, line 118
def suppress_messages
  save, self.verbose = verbose, false
  yield
ensure
  self.verbose = save
end
write(text = '') click to toggle source
# File lib/datastax_rails/schema/migrator.rb, line 101
def write(text = '')
  puts(text) if verbose
end