Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- ## lib/tasks/git.thor
- #
- # Thor task to fully copy your master database into a new database for current branch.
- # Sample usage:
- #
- # thor git:db:clone
- #
- # What gets run:
- #
- # mysqladmin -u #{user} create #{target}
- # mysqldump -u #{user} #{from} | mysql -u #{user} #{target}
- #
- # Limitation: only works if password authentication isn't required.
- #
- class Git < Thor
- module Command
- protected
- def command(com, capture = false)
- unless capture
- system(com)
- else
- @output = %x(#{com}).chomp
- end
- end
- def last_command_was_success?
- $?.success?
- end
- end
- class Db < Thor
- include Command
- desc "clone", "Branch your development database"
- method_options :force => :boolean
- def clone
- require 'config/boot'
- require 'git_conf'
- config = GitConf.new
- dbconfig = config.database_configuration["development"]
- if config.branched_database?
- user = dbconfig["username"]
- from = dbconfig["master-database"]
- to = dbconfig["database"]
- # create the new database
- command "mysqladmin -u #{user} create #{to} 2>&1", true
- unless last_command_was_success?
- if @output.index('database exists')
- warn %(Database "#{to}" already exists)
- return unless options[:force]
- else
- warn @output
- return
- end
- end
- # clone development database
- command "mysqldump -u #{user} #{from} | mysql -u #{user} #{to}"
- puts %(Finished cloning "#{from}" to "#{to}".) if last_command_was_success?
- else
- warn "branch isn't configured for a separate database"
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment