master
1require 'rubygems'
2require 'bundler'
3require 'logger'
4Bundler.require(:default)
5
6DATABASE_NAME = 'sql_bootcamp'
7DATABASE = Sequel.connect("mysql2://root@localhost/#{DATABASE_NAME}")
8DATABASE.loggers << Logger.new($stdout)
9
10namespace :db do
11 def pipe_to_mysql(command)
12 `echo "#{command}" | mysql -u root`
13 end
14
15 desc "create database"
16 task :create do
17 pipe_to_mysql("CREATE DATABASE #{DATABASE_NAME}")
18 end
19
20 desc "drop database"
21 task :drop do
22 pipe_to_mysql("DROP DATABASE IF EXISTS #{DATABASE_NAME}")
23 end
24
25 desc "Run migrations"
26 task :migrate, [:version] do |t, args|
27 Sequel.extension :migration
28
29 if args[:version]
30 puts "Migrating to version #{args[:version]}"
31 Sequel::Migrator.run(DATABASE, "db/migrations", target: args[:version].to_i)
32 else
33 puts "Migrating to latest"
34 Sequel::Migrator.run(DATABASE, "db/migrations")
35 end
36 end
37
38 task :seed do
39 require_relative 'db/seeds.rb'
40 Seeds.new(DATABASE).run
41 end
42
43 desc "drop, create and migrate the database"
44 task :reset => [:drop, :create, :migrate, :seed]
45end