Skip to content

Instantly share code, notes, and snippets.

@pranny
Created September 12, 2016 12:19
Show Gist options
  • Save pranny/3dbed97713c7f2db34fa09bbaaccd2a6 to your computer and use it in GitHub Desktop.
Save pranny/3dbed97713c7f2db34fa09bbaaccd2a6 to your computer and use it in GitHub Desktop.
massive_record table creations
# A workaround (hackish) way to perform migrations using ORM style models
# and massive_record. Ideally, you shouldn't be using this because
# in massive_record ORM, tables are created (with column families)
# whenever you perform an action toward the database. However if you are unlucky
# and get stuck with issues like
# https://github.com/CompanyBook/massive_record/issues/95#issuecomment-246317908
# this script provides a useful way to deal with it.
require 'activerecord'
def migrate
watch_stack = ActiveSupport::Dependencies::WatchStack.new
# If your classes exist inside a namespace like MyGem::Employee, you should
# add the top level namespace here. If there is none, and your class name
# is simply Employee, you can add top level namespace as "Object"
watch_namespaces = ['MyTopLevelNamespace']
watch_stack.watch_namespaces(watch_namespaces)
project_root = Dir.pwd
# This is the directory in which all your model files reside.
models_dir_path = "#{File.expand_path('lib/hbase/models', project_root)}/"
model_files = Dir.glob(File.join(models_dir_path, '**', '*.rb'))
# Data struct to store tables with their column families
tables = {}
model_files.sort.each do |file|
watch_stack.watch_namespaces(watch_namespaces)
require_dependency(file)
new_constants = watch_stack.new_constants
new_constants.each do |konst|
if MyTopLevelNamespace.const_get(konst).ancestors.include?(MassiveRecord::ORM::Table)
clazz = konst.constantize
if clazz.send(:table_exists?)
puts "Table Exists: #{ clazz.send(:table_name) }"
else
tables[clazz.table_name] = clazz.column_families.collect { |x| x.name }
end
end
end
end
# We are creating HBase shell commands and passing them to hbase shell
# These hbase shell commands are create commands.
tables.each do |name, col_families|
puts "Creating Table: #{ name }"
# This is important. The HBASE_PATH_BIN is the dir path of hbase's bin dir
# This is where the `hbase` resides.
hbase_path = ENV['HBASE_PATH_BIN']
cmd = "echo \"create '#{ name }', '#{ col_families.join(',') }' \" | #{ hbase_path }/hbase shell "
`#{ cmd }`
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment