themejuice/tj

View on GitHub
lib/theme-juice/tasks/database.rb

Summary

Maintainability
A
0 mins
Test Coverage
# encoding: UTF-8

module ThemeJuice
  module Tasks
    class Database < Entry

      def initialize(opts = {})
        super

        @entry = {
          :project => @project.name,
          :file    => "#{@env.vm_path}/database/init-custom.sql",
          :name    => "database",
          :id      => "DB"
        }
      end

      def execute
        if @project.db_host && @project.db_name && @project.db_user && @project.db_pass
          create_entry_file
          create_entry do
%Q{CREATE DATABASE IF NOT EXISTS `#{@project.db_name}`;
GRANT ALL PRIVILEGES ON `#{@project.db_name}`.* TO '#{@project.db_user}'@'localhost' IDENTIFIED BY '#{@project.db_pass}';}
          end
        end
      end

      def unexecute
        remove_entry
        drop_database
      end

      private

      def drop_database
        return unless @project.db_drop

        # Double check that the database should be dropped
        if @io.agree? "Are you sure you want to drop the database for '#{@project.name}'?"
          @io.log "Dropping database"
          @util.run_inside_vm [], { :verbose => @env.verbose,
            :capture => @env.quiet } do |cmds|
            cmds << "cd #{@project.vm_srv}"
            cmds << "wp db drop --yes"
          end
        end
      end
    end
  end
end