Class: Pave::Database

Inherits:
Object
  • Object
show all
Includes:
Shell
Defined in:
lib/pave/database.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Shell

#file_insert, included, #sh, #shell

Constructor Details

#initialize(name) ⇒ Database

Returns a new instance of Database.



14
15
16
# File 'lib/pave/database.rb', line 14

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/pave/database.rb', line 5

def name
  @name
end

Class Method Details

.create(name) ⇒ Object



7
8
9
10
11
12
# File 'lib/pave/database.rb', line 7

def self.create(name)
  say ""
  return say "Options should be given after the database name. For details run: `pave help`" unless name && name.size > 0
  say "Creating mysql database: #{name}."
  new(name).setup
end

Instance Method Details

#download_remote_to_local(remote = "live") ⇒ Object



88
89
90
91
92
# File 'lib/pave/database.rb', line 88

def download_remote_to_local(remote="live")
  # Download the project's live database dump to local db directory.
  say "Downloading SQL dump from #{remote_url}/db/#{dump_file(:remote)} to ./db/#{dump_file(:remote)}"
  sh "scp #{remote_url}/db/#{dump_file(:remote)} ./db/#{dump_file(:remote)}"
end

#dropObject



22
23
24
25
# File 'lib/pave/database.rb', line 22

def drop
  destroy = agree("Are you sure you want to drop #{name}? All data will be lost.")
  sh "mysql -uroot -e 'DROP DATABASE #{name}'" if destroy
end

#dump_file(which_db) ⇒ Object



45
46
47
48
# File 'lib/pave/database.rb', line 45

def dump_file(which_db)
  @dump_file_name ||= {}
  @dump_file_name[which_db] ||= "#{Time.now.strftime("%Y-%m-%d_%H-%M-%S")}-#{name}-#{which_db}.sql.gz"
end

#dump_localObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/pave/database.rb', line 50

def dump_local
  if !File.directory?('db')
    sh "mkdir ./db"
    sh "echo '<?= die(); ?>' > ./db/index.php"
    sh "echo 'deny from all' > ./db/.htaccess"
    sh "sudo chmod -R 700 ./db/"
  end
  say "Creating dump of #{name} at #{Dir.pwd}/db/#{dump_file(:local)}"
  sh "mysqldump -uroot #{name} | gzip > ./db/#{dump_file(:local)}"
end

#dump_remote(remote = "live") ⇒ Object



61
62
63
64
65
66
67
# File 'lib/pave/database.rb', line 61

def dump_remote(remote="live")
  server = Pave::Remote.server(remote)
  directory = Pave::Remote.directory(remote)
  db = remote_db
  say "Remotely creating dump of #{db['name']} at #{server}:#{directory}/db/#{dump_file(:remote)}"
  sh "ssh #{server} \"cd #{directory}/db; mysqldump -u#{db['user']} -p#{db['pass']} #{db['name']} | gzip > #{dump_file(:remote)}\""
end

#execute_local_dump_on_remote_db(remote = "live") ⇒ Object



74
75
76
77
78
79
80
# File 'lib/pave/database.rb', line 74

def execute_local_dump_on_remote_db(remote="live")
  server = Pave::Remote.server(remote)
  directory = Pave::Remote.directory(remote)
  db = remote_db
  say "Remotely executing #{dump_file(:local)} on live #{db['name']}"
  sh "ssh #{server} \"cd #{directory}/db; gzip -dc #{dump_file(:local)} | mysql -u#{db['user']} -p#{db['pass']} #{db['name']}\""
end

#execute_remote_dump_on_local_dbObject



69
70
71
72
# File 'lib/pave/database.rb', line 69

def execute_remote_dump_on_local_db
  say "Executing #{dump_file(:remote)} on #{name}"
  sh "gzip -dc ./db/#{dump_file(:remote)} | mysql -uroot #{name}"
end

#pull(remote = "live") ⇒ Object



103
104
105
106
107
108
109
# File 'lib/pave/database.rb', line 103

def pull(remote="live")
  # Download the project's live database and replace local database.
  dump_local # for backup purposes
  dump_remote(remote)
  download_remote_to_local(remote)
  execute_remote_dump_on_local_db
end

#push(remote = "live") ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/pave/database.rb', line 94

def push(remote="live")
  # Upload the project's local database and replace the live database.
  dump_remote(remote) # for backup purposes
  download_remote_to_local(remote)
  dump_local
  upload_local_dump_to_remote(remote)
  execute_local_dump_on_remote_db(remote)
end

#remote_dbObject



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/pave/database.rb', line 27

def remote_db
  require 'json'
  live_domain = shell("php -r \"error_reporting(0);require('./config/site.php');echo LIVE_DOMAIN;\"").output
  db_json = shell("php -r \"error_reporting(0);"\
                  "\\$_SERVER = array('HTTP_HOST' => '#{live_domain}');"\
                  "require('./config/site.php');"\
                  "echo json_encode("\
                    "array('host' => DB_SERVER,"\
                          "'user' => DB_USERNAME,"\
                          "'pass' => DB_PASSWORD,"\
                          "'name' => DB_DATABASE));\"").output
  JSON.parse(db_json)
end

#remote_url(remote = "live") ⇒ Object



41
42
43
# File 'lib/pave/database.rb', line 41

def remote_url(remote="live")
  "#{Pave::Remote.server(remote)}:#{Pave::Remote.directory(remote)}"
end

#setupObject



18
19
20
# File 'lib/pave/database.rb', line 18

def setup
  sh "mysql -uroot -e 'CREATE DATABASE #{name}'"
end

#upload_local_dump_to_remote(remote = "live") ⇒ Object



82
83
84
85
86
# File 'lib/pave/database.rb', line 82

def upload_local_dump_to_remote(remote="live")
  # Upload the project's local database dump to remotes db directory.
  say "Uploading ./db/#{dump_file(:local)} SQL dump to #{remote_url}/db/#{dump_file(:local)}"
  sh "scp ./db/#{dump_file(:local)} #{remote_url}/db/#{dump_file(:local)}"
end