scripts/download_db.rb in standup-0.3.9 vs scripts/download_db.rb in standup-0.3.10
- old
+ new
@@ -1,31 +1,42 @@
Standup.script :node do
self.description = 'Download Rails application database from node to local server'
def run
in_temp_dir do |dir|
- su_exec 'postgres', "pg_dump -c #{scripts.webapp.db_name} > dump.sql"
+ exec "#{scripts.webapp.db.dump_command scripts.webapp.db_name} > dump.sql"
local_exec "mkdir -p tmp/db"
download "#{dir}/dump.sql",
:to => 'tmp/db/dump.sql',
:sudo => true
end
- bootstrap_db
+ create_user 'webapp', 'webapp'
+ create_database scripts.webapp.db_name, 'webapp'
- local_exec "psql #{scripts.webapp.db_name} < tmp/db/dump.sql"
+ local_exec "#{scripts.webapp.db.load_command scripts.webapp.db_name} < tmp/db/dump.sql"
end
- def bootstrap_db
- unless exec_sql("select * from pg_user where usename = 'webapp'") =~ /1 row/
- exec_sql "create user webapp with password 'webapp'"
+ def create_user name, password
+ if exec_sql("select user from mysql.user where user = '#{name}'").present?
+ false
+ else
+ exec_sql "create user '#{name}'@'localhost' identified by '#{password}'"
+ true
end
-
- unless exec_sql("select * from pg_database where datname = '#{scripts.webapp.db_name}'") =~ /1 row/
- exec_sql "create database #{scripts.webapp.db_name} with owner webapp"
+ end
+
+ def create_database name, owner
+ if exec_sql("show databases like '#{name}'").present?
+ false
+ else
+ exec_sql "create database #{name}"
+ exec_sql "grant all on #{name}.* to '#{owner}'@'localhost'"
+ true
end
end
- def exec_sql sql, db_name = 'postgres'
- local_exec "psql #{db_name} -c \"#{sql}\""
+
+ def exec_sql sql, db_name = 'mysql'
+ local_exec "mysql -uroot -proot #{db_name} -e \"#{sql}\""
end
end