Class: WordpressDeploy::Database::MySql
- Inherits:
-
Object
- Object
- WordpressDeploy::Database::MySql
- Includes:
- Cli::Helpers
- Defined in:
- lib/wordpress_deploy/database/mysql.rb
Constant Summary
Constants included from Cli::Helpers
Instance Method Summary collapse
- #base_url(new_url = nil) ⇒ Object
- #charset(new_charset = nil) ⇒ Object
- #collate(new_collate = nil) ⇒ Object
-
#file ⇒ Object
The file that the instance would save to if save is called.
-
#host(new_host = nil) ⇒ Object
Get just the hostname from DB_HOST.
-
#initialize ⇒ MySql
constructor
A new instance of MySql.
- #migrate!(to_env) ⇒ Object
-
#name(new_name = nil) ⇒ Object
Name of the database (DB_NAME).
-
#password(new_password = nil) ⇒ Object
Password credentials for the specified database.
-
#port ⇒ Object
Extract just the port number from the DB_HOST configuration file.
-
#port? ⇒ Boolean
Does DB_HOST contain a port number? (it does if one was specified and it does not equal 3306; the default MySQL port number..
-
#save! ⇒ Object
Save the database to a file locally.
- #send!(to_env) ⇒ Object
-
#socket ⇒ Object
Extract just the socket part from the DB_HOST configuration file.
-
#socket? ⇒ Boolean
Does DB_HOST contain a socket path?.
- #table_prefix(new_prefix = nil) ⇒ Object
-
#user(new_user = nil) ⇒ Object
User credentials for the specified database.
-
#wp_host ⇒ Object
This value should be able to be plugged directly into DB_HOST int he wp-config.php file.
Methods included from Cli::Helpers
Constructor Details
#initialize ⇒ MySql
Returns a new instance of MySql.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/wordpress_deploy/database/mysql.rb', line 12 def initialize @user ||= 'root' @password ||= '' @host ||= 'localhost' @port ||= 3306 @socket ||= '' @name ||= 'wordpress' @has_port ||= true @has_socket ||= false @base_url ||= @host end |
Instance Method Details
#base_url(new_url = nil) ⇒ Object
130 131 132 133 |
# File 'lib/wordpress_deploy/database/mysql.rb', line 130 def base_url(new_url = nil) @base_url = new_url.to_s unless new_url.nil? @base_url end |
#charset(new_charset = nil) ⇒ Object
113 114 115 116 |
# File 'lib/wordpress_deploy/database/mysql.rb', line 113 def charset(new_charset = nil) @charset = new_charset.to_s unless new_charset.nil? @charset end |
#collate(new_collate = nil) ⇒ Object
118 119 120 121 |
# File 'lib/wordpress_deploy/database/mysql.rb', line 118 def collate(new_collate = nil) @collate = new_collate.to_s unless new_collate.nil? @collate end |
#file ⇒ Object
The file that the instance would save to if save is called.
138 139 140 |
# File 'lib/wordpress_deploy/database/mysql.rb', line 138 def file File.join(Config.sql_dir, "#{name}.sql") end |
#host(new_host = nil) ⇒ Object
Get just the hostname from DB_HOST. Only different from DB_HOST if DB_HOST has a socket or a port number in it.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/wordpress_deploy/database/mysql.rb', line 51 def host(new_host = nil) unless new_host.nil? match = /(?<host>.*?)(?=:|\z)(:(?<socket>\/.*)|:(?<port>\d+))?/.match(new_host.to_s) @host = match[:host].to_s unless match[:host].nil? # Set the socket information if @has_socket = !match[:socket].nil? @has_socket = true @socket = match[:socket] end # Set the port information unless match[:port].nil? @port = match[:port].to_i end # Has port is true; unless a socket was set @has_port = !@has_socket end # return the host @host end |
#migrate!(to_env) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/wordpress_deploy/database/mysql.rb', line 195 def migrate!(to_env) # Get the MySql instance to_db = to_env.database client = Mysql2::Client.new( :host => to_db.host, :username => to_db.user, :password => to_db.password, :port => to_db.port, :database => to_db.name, #:socket = '/path/to/mysql.sock', :encoding => to_db.charset ) # MySQL escape value_to_find = base_url value_to_replace = to_env.base_url escaped_value_to_find = client.escape(value_to_find) # wp_options option_value sql = <<-EOS SELECT `option_id`, `option_value` FROM `#{table_prefix}options` WHERE `option_value` REGEXP '#{escaped_value_to_find}'; EOS = client.query(sql) .each do |row| row.each do |key, value| if PHP.serialized?(value) ruby_php = PHP.unserialize(value) ruby_php.find_and_replace!(value_to_find, value_to_replace) value.replace PHP.serialize(ruby_php) else value.gsub!(/#{value_to_find}/, value_to_replace) if value.instance_of? String end end # Update the database sql = <<-EOD UPDATE `#{table_prefix}options` SET `option_value`='#{client.escape(row['option_value'])}' WHERE `option_id` = #{row['option_id']}; EOD Logger.debug sql client.query(sql) end # wp_posts post_content, guid sql = <<-EOS SELECT `ID`, `post_content`, `guid` FROM `#{table_prefix}posts` WHERE `post_content` REGEXP '#{escaped_value_to_find}' AND `guid` REGEXP '#{escaped_value_to_find}'; EOS wp_posts = client.query(sql) wp_posts.each do |row| row.each do |key, value| if PHP.serialized?(value) ruby_php = PHP.unserialize(value) ruby_php.find_and_replace!(value_to_find, value_to_replace) value.replace PHP.serialize(ruby_php) else value.gsub!(/#{value_to_find}/, value_to_replace) if value.instance_of? String end end sql = <<-EOD UPDATE `#{table_prefix}posts` SET `post_content` = '#{client.escape(row['post_content'])}', `guid` = '#{client.escape(row['guid'])}' WHERE `ID` = #{row['ID']}; EOD Logger.debug sql client.query(sql) end # wp_postmeta sql = <<-EOS SELECT `meta_id`, `meta_value` FROM `#{table_prefix}postmeta` WHERE `meta_value` REGEXP '#{escaped_value_to_find}'; EOS = client.query(sql) .each do |row| row.each do |key, value| if PHP.serialized?(value) ruby_php = PHP.unserialize(value) ruby_php.find_and_replace!(value_to_find, value_to_replace) value.replace PHP.serialize(ruby_php) else value.gsub!(/#{value_to_find}/, value_to_replace) if value.instance_of? String end end sql = <<-EOD UPDATE `wp_postmeta` SET `meta_value` = '#{client.escape(row['meta_value'])}' WHERE `meta_id` = #{row['meta_id']}; EOD Logger.debug sql client.query(sql) end end |
#name(new_name = nil) ⇒ Object
Name of the database (DB_NAME)
29 30 31 32 |
# File 'lib/wordpress_deploy/database/mysql.rb', line 29 def name(new_name = nil) @name = new_name.to_s unless new_name.nil? @name end |
#password(new_password = nil) ⇒ Object
Password credentials for the specified database
43 44 45 46 |
# File 'lib/wordpress_deploy/database/mysql.rb', line 43 def password(new_password = nil) @password = new_password.to_s unless new_password.nil? @password end |
#port ⇒ Object
Extract just the port number from the DB_HOST configuration file. Or return the default port of 3306.
88 89 90 |
# File 'lib/wordpress_deploy/database/mysql.rb', line 88 def port @port end |
#port? ⇒ Boolean
Does DB_HOST contain a port number? (it does if one was specified and it does not equal 3306; the default MySQL port number.
96 97 98 |
# File 'lib/wordpress_deploy/database/mysql.rb', line 96 def port? @has_port && port != 3306 end |
#save! ⇒ Object
Save the database to a file locally.
The database will be output into #file.
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/wordpress_deploy/database/mysql.rb', line 146 def save! # Get the output from MySQL Dump cmd = mysqldump dump_str = run cmd # Open the supplied file; or create a temporary one file_io = File.new(file, 'w') # Start writing to file file_io.write(dump_str) true ensure file_io.close unless file_io.nil? end |
#send!(to_env) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/wordpress_deploy/database/mysql.rb', line 164 def send!(to_env) # Get the MySql instance to_db = to_env.database # Check to see if there is a SQL file if File.exists? file # Open the source sql file for reading tmp_file = Tempfile.new(["#{to_db.name}", '.sql']) # Write sql to tmpfile while changing the # the CREATE DATABASE and USE commands to make sense for # the 'to' configuration sql_dump = File.read(file) sql_dump.gsub!(/^CREATE\ DATABASE.*$/i, '') sql_dump.gsub!(/^USE\ `#{name}`/, "USE `#{to_db.name}`") tmp_file.puts sql_dump # Get the MySQL load command cmd = mysqlload to_db, tmp_file.path # Run the mysql command to load the mysqldump into # the destination mysql instance run cmd end ensure # Delete the temp file unless it was never made tmp_file.unlink unless tmp_file.nil? end |
#socket ⇒ Object
Extract just the socket part from the DB_HOST configuration file. Or return an empty string if none.
103 104 105 |
# File 'lib/wordpress_deploy/database/mysql.rb', line 103 def socket @socket end |
#socket? ⇒ Boolean
Does DB_HOST contain a socket path?
109 110 111 |
# File 'lib/wordpress_deploy/database/mysql.rb', line 109 def socket? @has_socket end |
#table_prefix(new_prefix = nil) ⇒ Object
123 124 125 126 |
# File 'lib/wordpress_deploy/database/mysql.rb', line 123 def table_prefix(new_prefix = nil) @prefix = new_prefix.to_s unless new_prefix.nil? @prefix end |
#user(new_user = nil) ⇒ Object
User credentials for the specified database
36 37 38 39 |
# File 'lib/wordpress_deploy/database/mysql.rb', line 36 def user(new_user = nil) @user = new_user.to_s unless new_user.nil? @user end |
#wp_host ⇒ Object
This value should be able to be plugged directly into DB_HOST int he wp-config.php file.
78 79 80 81 82 83 |
# File 'lib/wordpress_deploy/database/mysql.rb', line 78 def wp_host extra = nil extra = ":#{socket}" if socket? extra = ":#{port}" if port? "#{host}#{extra}" end |