Module: MysqlToPgDump::TasksHelper

Defined in:
lib/mysql_to_pg_dump/tasks_helper.rb

Instance Method Summary collapse

Instance Method Details

#clean_databaseObject



29
30
31
32
# File 'lib/mysql_to_pg_dump/tasks_helper.rb', line 29

def clean_database
  task_names = %w(db:drop db:create db:migrate)
  task_names.each { |t| Rake::Task[t].invoke }
end

#data_already_pulled?Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
# File 'lib/mysql_to_pg_dump/tasks_helper.rb', line 10

def data_already_pulled?
  if %x{ls tmp}.split("\n").include? 'db_server_data'
    %x(ls tmp/db_server_data).split("\n").size == db_tables.size
  else
    false
  end
end

#db_tablesObject



49
50
51
# File 'lib/mysql_to_pg_dump/tasks_helper.rb', line 49

def db_tables
  ActiveRecord::Base.connection.tables - ['schema_migrations']
end

#devObject



67
68
69
# File 'lib/mysql_to_pg_dump/tasks_helper.rb', line 67

def dev
  show_db_info 'development'
end

#file_to_save(table_name, location) ⇒ Object



41
42
43
# File 'lib/mysql_to_pg_dump/tasks_helper.rb', line 41

def file_to_save table_name, location
  "#{location}/#{production['database']}_#{table_name}.csv"
end

#login_to_mysqlObject



34
35
36
37
38
39
# File 'lib/mysql_to_pg_dump/tasks_helper.rb', line 34

def 
  "mysql " \
  "--user=#{production['username']} " \
  "--password=#{production['password']} " \
  "#{production['database']}"
end

#productionObject



71
72
73
# File 'lib/mysql_to_pg_dump/tasks_helper.rb', line 71

def production
  show_db_info 'production'
end

#psql_import_query(table_name) ⇒ Object



18
19
20
21
22
# File 'lib/mysql_to_pg_dump/tasks_helper.rb', line 18

def psql_import_query table_name
  "\\copy #{table_name} from " \
  "'tmp/db_server_data/#{production['database']}_#{table_name}.csv' " \
  "delimiter E'\\t' null as 'NULL' csv header;"
end

#psql_set_sequence(table_name) ⇒ Object



24
25
26
27
# File 'lib/mysql_to_pg_dump/tasks_helper.rb', line 24

def psql_set_sequence table_name
  "SELECT setval(pg_get_serial_sequence('#{table_name}', 'id'), " \
  "coalesce(max(id),0) + 1, false) FROM #{table_name};"
end

#server_addr_inputObject



5
6
7
8
# File 'lib/mysql_to_pg_dump/tasks_helper.rb', line 5

def server_addr_input
  printf "Enter server address like '[email protected]': "
  STDIN.gets.strip
end

#show_db_info(env) ⇒ Object



63
64
65
# File 'lib/mysql_to_pg_dump/tasks_helper.rb', line 63

def show_db_info env
  Rails.application.config.database_configuration[env]
end

#sql_select(table_name) ⇒ Object



45
46
47
# File 'lib/mysql_to_pg_dump/tasks_helper.rb', line 45

def sql_select table_name
  "SELECT * FROM #{table_name};"
end

#uniq_dir_locationObject



53
54
55
# File 'lib/mysql_to_pg_dump/tasks_helper.rb', line 53

def uniq_dir_location
  "app/current/tmp/db_server_data/#{uniq_string}"
end

#uniq_stringObject



57
58
59
60
61
# File 'lib/mysql_to_pg_dump/tasks_helper.rb', line 57

def uniq_string
  s = ""
  20.times { s << ('0'..'9').to_a.sample }
  s
end