Class: BigRecord::Migration

Inherits:
Object show all
Defined in:
lib/big_record/migration.rb

Overview

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

Constant Summary

@@verbose =
true

Class Method Summary

Class Method Details

+ (Object) announce(message)



131
132
133
134
135
# 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

+ (Object) connection



157
158
159
# File 'lib/big_record/migration.rb', line 157

def connection
  BigRecord::Base.connection
end

+ (Object) down_with_benchmarks

:nodoc:



85
86
87
# File 'lib/big_record/migration.rb', line 85

def down_with_benchmarks 
  migrate(:down)
end

+ (Object) method_missing(method, *arguments, &block)



161
162
163
164
165
166
167
168
169
170
# 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

+ (Object) migrate(direction)

Execute this migration in the named direction



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# 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

+ (Object) say(message, subitem = false)



137
138
139
# File 'lib/big_record/migration.rb', line 137

def say(message, subitem=false)
  write "#{subitem ? "   ->" : "--"} #{message}"
end

+ (Object) say_with_time(message)



141
142
143
144
145
146
147
148
# 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

+ (Object) singleton_method_added(sym)

Because the method added may do an alias_method, it can be invoked recursively. We use @ignore_new_methods as a guard to indicate whether it is safe for the call to proceed.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/big_record/migration.rb', line 112

def singleton_method_added(sym) #:nodoc:
  return if defined?(@ignore_new_methods) && @ignore_new_methods

  begin
    @ignore_new_methods = true

    case sym
      when :up, :down
        metaclass.send(:alias_method_chain, sym, "benchmarks")
    end
  ensure
    @ignore_new_methods = false
  end
end

+ (Object) suppress_messages



150
151
152
153
154
155
# File 'lib/big_record/migration.rb', line 150

def suppress_messages
  save, self.verbose = verbose, false
  yield
ensure
  self.verbose = save
end

+ (Object) up_with_benchmarks

:nodoc:



81
82
83
# File 'lib/big_record/migration.rb', line 81

def up_with_benchmarks 
  migrate(:up)
end

+ (Object) write(text = "")



127
128
129
# File 'lib/big_record/migration.rb', line 127

def write(text="")
  puts(text) if verbose
end