Module: MysqlToPgDump::TasksHelper

Defined in:
lib/mysql_to_pg_dump/tasks_helper.rb

Instance Method Summary collapse

Instance Method Details

#clean_databaseObject



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

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



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

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

#devObject



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

def dev
  show_db_info 'development'
end

#file_to_save(table_name, location) ⇒ Object



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

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

#login_to_mysqlObject



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

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

#productionObject



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

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

#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



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

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

#sql_select(table_name) ⇒ Object



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

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

#uniq_dir_locationObject



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

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

#uniq_stringObject



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

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