Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env ruby
- require 'tempfile'
- def print_help
- puts %{
- Runs database migrations
- Usage:
- ./script/run_db_migrations
- }
- exit
- end
- def exec_sql(s)
- tf = Tempfile.open('cohepub_migration')
- tf.write(s)
- tf.close
- out = `mysql -u #{DB_USER} --password="#{DB_PASS}" -h #{DB_HOST} #{DB_NAME} < #{tf.path}`
- tf.unlink
- out
- end
- SCHEMA_SETUP = %{
- CREATE TABLE IF NOT EXISTS schema_migrations (
- id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
- name VARCHAR(255) NOT NULL,
- direction TINYINT NOT NULL DEFAULT 1,
- performed_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
- );
- }
- DB_HOST = "localhost"
- DB_NAME = ARGV.first || "cohepub_dev"
- DB_USER = "root"
- DB_PASS = ""
- puts "\nMigrating #{DB_NAME}...\n"
- exec_sql(SCHEMA_SETUP)
- out = exec_sql("SELECT name FROM schema_migrations WHERE direction = 1 ORDER BY performed_at DESC LIMIT 1;")
- migrations = Dir[File.dirname(__FILE__) + "/../db/migrations/*.sql"].select {|m| !m.include?("migrations/reverse_")}
- unless out.empty?
- end
- migrations.each do |migration|
- print ">>>=================== #{File.basename(migration)}"
- `mysql -u #{DB_USER} --password="#{DB_PASS}" -h #{DB_HOST} #{DB_NAME} < #{migration}`
- exec_sql("INSERT INTO schema_migrations (name, direction) VALUES (\"#{File.basename(migration)}\",1)")
- end
Add Comment
Please, Sign In to add comment