Skip to content

Instantly share code, notes, and snippets.

@Ultrabenosaurus
Last active August 29, 2015 14:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ultrabenosaurus/761f2430109354a4d470 to your computer and use it in GitHub Desktop.
Save Ultrabenosaurus/761f2430109354a4d470 to your computer and use it in GitHub Desktop.
VagrantMySQL - transfer and run SQL files in a single line, make a queue of SQL files/commands to process at once and in multiple boxes!
# simple usage
# based on Vagrantfile for the laravel/homestead box
VAGRANTFILE_API_VERSION = "2"
path = "#{File.dirname(__FILE__)}"
require path + '/scripts/VagrantMySQL.rb'
require 'yaml'
require path + '/scripts/homestead.rb'
Vagrant.configure( VAGRANTFILE_API_VERSION ) do |config|
config.vm.define "homestead" do |homestead|
Homestead.configure( homestead, YAML::load( File.read( path + '/Homestead.yaml' ) ) )
VagrantMySQL.runFile( "scripts/db-schema.sql", homestead, "homestead", "secret" )
end
end
# queue usage
# based on Vagrantfile for the laravel/homestead box
VAGRANTFILE_API_VERSION = "2"
path = "#{File.dirname(__FILE__)}"
require path + '/scripts/VagrantMySQL.rb'
# storing VagrantMySQL.init() to a variable is required to make queueing persistant!
# parameters are optional, though, but make subsequent calls simpler when using the same box
vgsql = VagrantMySQL.init( false, "homestead", "secret" )
vgsql.queueFiles( ["scripts/db-user.sql", "scripts/db-schema.sql"] )
require 'yaml'
require path + '/scripts/homestead.rb'
Vagrant.configure( VAGRANTFILE_API_VERSION ) do |config|
config.vm.define "mysql_sync_test_one" do |mysql_sync_test_one|
Homestead.configure( mysql_sync_test_one, YAML::load( File.read( path + '/Homestead.yaml' ) ) )
vgsql.processFiles( mysql_sync_test_one )
end
config.vm.define "mysql_sync_test_two" do |mysql_sync_test_two|
Homestead.configure (mysql_sync_test_two, YAML::load( File.read( path + '/Homestead.yaml' ) ) )
# NOTE: it's not an array! VagrantMySQL wraps non-arrays as arrays!
vgsql.queueFiles( "scripts/db-data.sql" )
vgsql.processFiles( mysql_sync_test_two )
end
end
#
# VagrantMySQL
#
# A helper for interfacing with MySQL from your Vagrantfile
#
# @author Dan Bennett <http://about.me/d.bennett>
# @package Vagrant\Helpers
# @version 1.0.0
# @license BSD 3-Clause <http://opensource.org/licenses/BSD-3-Clause>
# @todo function DocBlocks!!!!
# @todo type-checking on parameters
# @todo support DBs other than MySQL?
#
class VagrantMySQL
# setup stuff
def VagrantMySQL.init( _box=false, _user="root", _pass="" )
@box = _box
@user = _user
@pass = _pass
@files = Array.new
@commands = Array.new
return self
end # VagrantMySQL.init
# run a single SQL file on the box
def VagrantMySQL.runFile( _src, _box=@box, _user=@user, _pass=@pass )
@box = @box == _box ? @box : _box
@user = @user == _user ? @user : _user
@pass = @pass == _pass ? @pass : _pass
if @box
file = _src.split( '/' ).last
@box.vm.provision "file", source: _src, destination: "~/" + file
@box.vm.provision "shell", inline: "mysql -u" + @user + " -p" + @pass + " < '/home/vagrant/" + file + "';"
end
end # VagrantMySQL.runFile
# add SQL files to the queue to run all at once
def VagrantMySQL.queueFiles( _files )
unless _files.kind_of?( Array )
_files = [_files]
end
@files.concat( _files )
end # VagrantMySQL.queueFiles
# process the file queue
def VagrantMySQL.processFiles( _box=@box, _user=@user, _pass=@pass )
@box = @box == _box ? @box : _box
@user = @user == _user ? @user : _user
@pass = @pass == _pass ? @pass : _pass
if @box
unless @files.empty?
@files.each do |src|
file = src.split( '/' ).last
@box.vm.provision "file", source: src, destination: "~/" + file
@box.vm.provision "shell", inline: "mysql -u" + @user + " -p" + @pass + " < '/home/vagrant/" + file + "';"
end
end
end
end # VagrantMySQL.processFiles
# run a single SQL command
def VagrantMySQL.runCommand( _command, _box=@box, _user=@user, _pass=@pass )
@box = @box == _box ? @box : _box
@user = @user == _user ? @user : _user
@pass = @pass == _pass ? @pass : _pass
if @box
@box.vm.provision "shell", inline: "mysql -u" + @user + " -p" + @pass + " -e '" + _command + "';"
end
end # VagrantMySQL.runCommand
# add SQL commands to the queue to run all at once
def VagrantMySQL.queueCommands( _commands )
unless _commands.kind_of?( Array )
_commands = [_commands]
end
@commands.concat( _commands )
end # VagrantMySQL.queueCommands
# process the command queue
def VagrantMySQL.processCommands( _box=@box, _user="root", _pass=@pass )
@box = @box == _box ? @box : _box
@user = @user == _user ? @user : _user
@pass = @pass == _pass ? @pass : _pass
if @box
unless @commands.empty?
@commands.each do |command|
@box.vm.provision "shell", inline: "mysql -u" + @user + " -p" + @pass + " -e '" + command + "';"
end
end
end
end # VagrantMySQL.processCommands
end # class VagrantMySQL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment