Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/ruby
- require 'rubygems'
- require 'mysql'
- DB1_USER = 'root'
- DB1_PASS = ''
- DB1_NAME = 'testapp1_development'
- DB2_USER = 'root'
- DB2_PASS = ''
- DB2_NAME = 'testapp2_development'
- begin
- dbh_1 = Mysql::real_connect("localhost", DB1_USER, DB1_PASS, DB1_NAME)
- dbh_1.query('SET NAMES `utf8`')
- dbh_2 = Mysql::real_connect("localhost", DB2_USER, DB2_PASS, DB2_NAME)
- dbh_2.query('SET NAMES `utf8`')
- # Posts, Images
- dbh_1.query('BEGIN')
- dbh_2.query('BEGIN')
- for i in 0...1000 do
- query_p = "INSERT INTO `posts` (title, description, created_at, updated_at) " +
- "VALUES ('Post number #{i+1}', 'This is descripton of post number #{i+1}', CURTIME(), CURTIME())"
- query_i = "INSERT INTO `images` (title, created_at, updated_at) " +
- "VALUES ('Image number #{i+1}', CURTIME(), CURTIME())"
- dbh_1.query query_p
- dbh_1.query query_i
- dbh_2.query query_p
- dbh_2.query query_i
- end
- dbh_1.query('COMMIT')
- dbh_2.query('COMMIT')
- # Comments
- COMMENTS = ['Oh! Pretty cool!',
- 'There is something wrong with my hands!',
- 'I recommend you write more.'].freeze
- dbh_1.query('BEGIN')
- dbh_2.query('BEGIN')
- for i in 0...1000000 do
- sql = "INSERT INTO `comments` (content, resource_id, resource_type, created_at, updated_at) " +
- "VALUES ('#{COMMENTS[i%3]}',
- #{rand(1000)},
- '#{rand(10) % 2 == 0 ? 'Post' : 'Image'}' ,
- CURTIME(), CURTIME())"
- print '.' if i % 10000 == 0
- dbh_1.query sql
- dbh_2.query sql
- end
- dbh_1.query('COMMIT')
- dbh_2.query('COMMIT')
- rescue Mysql::Error => e
- puts "Error code: #{e.errno}"
- puts "Error message: #{e.error}"
- puts "Error SQLSTATE: #{e.sqlstate}" if e.respond_to?("sqlstate")
- [dbh_1, dbh_2].each do |dbh|
- if dbh
- dbh.query('ROLLBACK')
- dbh.query('TRUNCATE TABLE `posts`')
- dbh.query('TRUNCATE TABLE `images`')
- end
- end
- ensure
- dbh_1.close if dbh_1
- dbh_2.close if dbh_2
- end
Add Comment
Please, Sign In to add comment