Class: Pave::Database
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
- #download(remote = "live") ⇒ Object
- #drop ⇒ Object
- #dump ⇒ Object
- #dump_file(which_db) ⇒ Object
- #dump_remote(remote = "live") ⇒ Object
- #execute ⇒ Object
- #execute_remote(remote = "live") ⇒ Object
-
#initialize(name) ⇒ Database
constructor
A new instance of Database.
- #pull(remote = "live") ⇒ Object
- #push(remote = "live") ⇒ Object
- #remote_db ⇒ Object
- #remote_url(remote = "live") ⇒ Object
- #setup ⇒ Object
- #upload(remote = "live") ⇒ Object
Methods included from 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
#name ⇒ Object
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 = "live") ⇒ Object
88 89 90 91 92 |
# File 'lib/pave/database.rb', line 88 def download(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 |
#drop ⇒ Object
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 ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/pave/database.rb', line 50 def dump 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_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_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 ⇒ Object
69 70 71 72 |
# File 'lib/pave/database.rb', line 69 def execute say "Executing #{dump_file(:local)} on #{name}" sh "gzip -dc ./db/#{dump_file(:local)} | mysql -uroot #{name}" end |
#execute_remote(remote = "live") ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/pave/database.rb', line 74 def execute_remote(remote="live") server = Pave::Remote.server(remote) directory = Pave::Remote.directory(remote) db = remote_db say "Remotely executing #{dump_file(:remote)} on live #{db['name']}" sh "ssh #{server} \"cd #{directory}/db; gzip -dc #{dump_file(:remote)} | mysql -u#{db['user']} -p#{db['pass']} #{db['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 # for backup purposes dump_remote(remote) download(remote) execute 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) dump upload(remote) execute_remote(remote) end |
#remote_db ⇒ Object
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 |
#setup ⇒ Object
18 19 20 |
# File 'lib/pave/database.rb', line 18 def setup sh "mysql -uroot -e 'CREATE DATABASE #{name}'" end |
#upload(remote = "live") ⇒ Object
82 83 84 85 86 |
# File 'lib/pave/database.rb', line 82 def upload(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 |