SHARE
TWEET

Untitled

a guest Apr 26th, 2018 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/ruby
  2. require 'rubygems'
  3. require 'mysql'
  4.  
  5. DB1_USER = 'root'
  6. DB1_PASS = ''
  7. DB1_NAME = 'testapp1_development'
  8.  
  9. DB2_USER = 'root'
  10. DB2_PASS = ''
  11. DB2_NAME = 'testapp2_development'
  12.  
  13.  
  14. begin
  15.   dbh_1 = Mysql::real_connect("localhost", DB1_USER, DB1_PASS, DB1_NAME)
  16.   dbh_1.query('SET NAMES `utf8`')
  17.  
  18.   dbh_2 = Mysql::real_connect("localhost", DB2_USER, DB2_PASS, DB2_NAME)
  19.   dbh_2.query('SET NAMES `utf8`')
  20.  
  21.   # Posts, Images
  22.  
  23.   dbh_1.query('BEGIN')
  24.   dbh_2.query('BEGIN')
  25.  
  26.   for i in 0...1000 do
  27.  
  28.     query_p = "INSERT INTO `posts` (title, description, created_at, updated_at) " +
  29.               "VALUES ('Post number #{i+1}', 'This is descripton of post number #{i+1}', CURTIME(), CURTIME())"
  30.  
  31.     query_i = "INSERT INTO `images` (title, created_at, updated_at) " +
  32.               "VALUES ('Image number #{i+1}', CURTIME(), CURTIME())"
  33.  
  34.     dbh_1.query query_p
  35.     dbh_1.query query_i
  36.  
  37.     dbh_2.query query_p
  38.     dbh_2.query query_i
  39.   end
  40.  
  41.   dbh_1.query('COMMIT')
  42.   dbh_2.query('COMMIT')
  43.  
  44.  
  45.   # Comments
  46.  
  47.   COMMENTS = ['Oh! Pretty cool!',
  48.               'There is something wrong with my hands!',
  49.               'I recommend you write more.'].freeze
  50.  
  51.   dbh_1.query('BEGIN')
  52.   dbh_2.query('BEGIN')
  53.  
  54.   for i in 0...1000000 do
  55.  
  56.     sql = "INSERT INTO `comments` (content, resource_id, resource_type, created_at, updated_at) " +
  57.           "VALUES ('#{COMMENTS[i%3]}',
  58.                    #{rand(1000)},
  59.                   '#{rand(10) % 2 == 0 ? 'Post' : 'Image'}' ,
  60.                   CURTIME(), CURTIME())"
  61.  
  62.     print '.' if i % 10000 == 0
  63.  
  64.     dbh_1.query sql
  65.     dbh_2.query sql
  66.   end
  67.  
  68.   dbh_1.query('COMMIT')
  69.   dbh_2.query('COMMIT')
  70.  
  71.  
  72.  
  73. rescue Mysql::Error => e
  74.   puts "Error code: #{e.errno}"
  75.   puts "Error message: #{e.error}"
  76.   puts "Error SQLSTATE: #{e.sqlstate}" if e.respond_to?("sqlstate")
  77.  
  78.   [dbh_1, dbh_2].each do |dbh|
  79.     if dbh
  80.       dbh.query('ROLLBACK')
  81.       dbh.query('TRUNCATE TABLE `posts`')
  82.       dbh.query('TRUNCATE TABLE `images`')
  83.     end
  84.   end
  85.  
  86. ensure
  87.   dbh_1.close if dbh_1
  88.   dbh_2.close if dbh_2
  89. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top