class BigRecord::Migration
Migrations¶ ↑
Although column-oriented databases are generally schema-less, certain ones (like HBase) require the creation of tables and column families ahead of time. The individual columns, however, are defined in the model itself and can be modified dynamically without the need for migrations.
Unless you're familiar with column families, the majority of use cases work perfectly fine within one column family. When you generate a bigrecord_model, it will default to creating the :attribute column family.
The following is a standard migration file that creates a table called “Books” with the default column family :attribute that has the following option of 100 versions and uses the 'lzo' compression scheme. Leave any options blank for the default value.
class CreateBooks < BigRecord::Migration def self.up create_table :books, :force => true do |t| t.family :attribute, :versions => 100, :compression => 'lzo' end end def self.down drop_table :books end end
HBase column family options (HBase specific)¶ ↑
-
versions: integer. By default, Hbase will store 3 versions of changes for
any column family. Changing this value on the creation will change this behavior.
-
compression: 'none', 'gz', 'lzo'. Defaults to 'none'. Since Hbase 0.20,
column families can be stored using compression. The compression scheme you define here must be installed on the Hbase servers!
Migrating¶ ↑
Run the following rake task to migrate your tables and column families up to the latest version:
rake bigrecord:migrate
Public Class Methods
# File lib/big_record/migration.rb, line 131 def announce(message) text = "#{@version} #{name}: #{message}" length = [0, 75 - text.length].max write "== %s %s" % [text, "=" * length] end
# File lib/big_record/migration.rb, line 157 def connection BigRecord::Base.connection end
# File lib/big_record/migration.rb, line 161 def method_missing(method, *arguments, &block) arg_list = arguments.map(&:inspect) * ', ' say_with_time "#{method}(#{arg_list})" do unless arguments.empty? || method == :execute arguments[0] = Migrator.proper_table_name(arguments.first) end connection.send(method, *arguments, &block) end end
Execute this migration in the named direction
# File lib/big_record/migration.rb, line 90 def migrate(direction) return unless respond_to?(direction) case direction when :up then announce "migrating" when :down then announce "reverting" end result = nil time = Benchmark.measure { result = send("#{direction}_without_benchmarks") } case direction when :up then announce "migrated (%.4fs)" % time.real; write when :down then announce "reverted (%.4fs)" % time.real; write end result end
# File lib/big_record/migration.rb, line 137 def say(message, subitem=false) write "#{subitem ? " ->" : "--"} #{message}" end
# File lib/big_record/migration.rb, line 141 def say_with_time(message) say(message) result = nil time = Benchmark.measure { result = yield } say "%.4fs" % time.real, :subitem say("#{result} rows", :subitem) if result.is_a?(Integer) result end
# File lib/big_record/migration.rb, line 150 def suppress_messages save, self.verbose = verbose, false yield ensure self.verbose = save end
# File lib/big_record/migration.rb, line 127 def write(text="") puts(text) if verbose end