summaryrefslogtreecommitdiff
path: root/Rakefile
blob: 14eb42ef3b6767d02c07e6dde979374d4e144380 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
require 'rubygems'
require 'bundler'
require 'logger'
Bundler.require(:default)

DATABASE_NAME = 'sql_bootcamp'
DATABASE = Sequel.connect("mysql2://root@localhost/#{DATABASE_NAME}")
DATABASE.loggers << Logger.new($stdout)

namespace :db do
  def pipe_to_mysql(command)
    `echo "#{command}" | mysql -u root`
  end

  desc "create database"
  task :create do
    pipe_to_mysql("CREATE DATABASE #{DATABASE_NAME}")
  end

  desc "drop database"
  task :drop do
    pipe_to_mysql("DROP DATABASE IF EXISTS #{DATABASE_NAME}")
  end

  desc "Run migrations"
  task :migrate, [:version] do |t, args|
    Sequel.extension :migration

    if args[:version]
      puts "Migrating to version #{args[:version]}"
      Sequel::Migrator.run(DATABASE, "db/migrations", target: args[:version].to_i)
    else
      puts "Migrating to latest"
      Sequel::Migrator.run(DATABASE, "db/migrations")
    end
  end

  task :seed do
    require_relative 'db/seeds.rb'
    Seeds.new(DATABASE).run
  end

  desc "drop, create and migrate the database"
  task :reset => [:drop, :create, :migrate, :seed]
end