class TableMigrator

Constants

DATABASE_PASS
DATABASE_USER
FEED_PATH

Public Class Methods

create(db_name) click to toggle source
# File lib/ruby_feed/table_migrator.rb, line 92
def self.create(db_name)
  db = connect
  create_string = SqlGenerator.generate_db_create(db_name)
  db.run(create_string)
end
create_current(db_name) click to toggle source

def self.update

drop_and_recreate(DATABASES[:two_days_ago], DATABASES[:yesterday])
drop_and_recreate(DATABASES[:yesterday], DATABASES[:current])
create_current(DATABASES[:current])

end

# File lib/ruby_feed/table_migrator.rb, line 24
def self.create_current(db_name)
  drop(db_name)
  create(db_name)
  create_tables(db_name)
  insert_into(db_name)
end
create_tables(db_name) click to toggle source
# File lib/ruby_feed/table_migrator.rb, line 49
def self.create_tables(db_name)
  db = connect(db_name)
  table_names = get_table_names

  tables = table_names.map {|f| File.basename(f, ".*")}
  non_existant_tables = tables.reject { |file| db.table_exists?(file) }

  non_existant_tables.each do |table|
    create_string = SqlGenerator.generate_table_create_string(table)
    # puts create_string
    db.run(create_string)
  end
end
drop(db_name) click to toggle source
# File lib/ruby_feed/table_migrator.rb, line 79
def self.drop(db_name)
  db = connect
  drop_string = SqlGenerator.generate_drop(db_name)
  db.run(drop_string)
end
drop_and_recreate(to, from) click to toggle source
# File lib/ruby_feed/table_migrator.rb, line 69
def self.drop_and_recreate(to, from)
  drop(to)
  create(to)
  dump_old_to_new(to, from)
end
dump_old_to_new(to, from) click to toggle source
# File lib/ruby_feed/table_migrator.rb, line 75
def self.dump_old_to_new(to, from)
  `mysqldump -u #{DATABASE_USER} -p#{DATABASE_PASS} #{from} | mysql -u #{DATABASE_USER} -p#{DATABASE_PASS} #{to}`
end
insert_into(db_name) click to toggle source
# File lib/ruby_feed/table_migrator.rb, line 31
def self.insert_into(db_name)
  db = connect(db_name)
  table_names = get_table_names

  table_names.each do |table|
    table_name = File.basename(table)
    p "#{FEED_PATH}#{table}"
    table_file =  "#{FEED_PATH}#{table}"
    insert_string = Parser.parse_file(File.readlines(table_file).drop(1), table_name)
    next if insert_string.nil?
    insert_string.split(";").each do |s|
      next if s == " "
      # p s
      db.run(s)
    end
  end
end
migrate_info(to, from) click to toggle source
# File lib/ruby_feed/table_migrator.rb, line 63
def self.migrate_info(to, from)
  drop_and_recreate(to)
  to = connect(to)
  from = connect(from)
end
rename(to, from) click to toggle source
# File lib/ruby_feed/table_migrator.rb, line 85
def self.rename(to, from)
  db = connect
  rename_string = SqlGenerator.generate_rename(to, from)
  p rename_string
  db.run(rename_string)
end

Private Class Methods

connect(db_name="mysql") click to toggle source
# File lib/ruby_feed/table_migrator.rb, line 99
def self.connect(db_name="mysql")
  Sequel.connect("mysql2://#{DATABASE_USER}:#{DATABASE_PASS}@localhost/#{db_name}")
end
entries_to_be_excluded_from_db() click to toggle source
# File lib/ruby_feed/table_migrator.rb, line 108
def self.entries_to_be_excluded_from_db
  [".", "..", "list"]
end
get_table_names() click to toggle source
# File lib/ruby_feed/table_migrator.rb, line 103
def self.get_table_names
  files = Dir.entries(FEED_PATH) - entries_to_be_excluded_from_db
  files
end