-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.thor
66 lines (45 loc) · 1.76 KB
/
db.thor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class Db < Thor
desc "dump database", "Dumps the database for the current project to a file"
def export(database, file = 'db.sql')
puts "dumping database #{database}..."
mysqldump = '/opt/lampp/bin/mysqldump'
dest = File.join(Dir.pwd, file)
command = "sudo #{mysqldump} --opt --user=#{username} --password=#{password} #{database} > #{dest}"
system(command)
puts "dumped database to #{dest}"
end
desc "import database", "Import the specified sql dump into a database"
def import(file, database)
src = File.join(Dir.pwd, file)
exec("CREATE DATABASE IF NOT EXISTS #{database}")
command = "sudo mysql --user=#{username} --password=#{password} --verbose #{database} < #{src}"
system(command)
end
desc "connect to remote database", "launch the mysql terminal for the remote database"
def remote(user, password, host='127.0.0.1', port=3308)
start_tunnel_command = "pagoda -a allianceandalliance tunnel -c db1 &"
command = "mysql --host=#{host} --port=#{port} --user=#{user} --password=#{password}"
system(start_tunnel_command)
sleep 4
system(command)
end
desc "connect to remote database", "launch the mysql terminal for the remote database"
def rimport(user, password, host='127.0.0.1', port=3308)
start_tunnel_command = "pagoda -a allianceandalliance tunnel -c db1 &"
import_command = "mysql --host=#{host} --port=#{port} --user=#{user} --password=#{password} --verbose allianceandalliance < db.sql"
system(start_tunnel_command)
sleep 4
system(import_command)
end
private
def exec(sql)
command = "sudo mysql --user=#{username} --password=#{password} --verbose -e \"#{sql}\""
system(command)
end
def username
'root'
end
def password
'123'
end
end