forked from lighthouse-labs/sinatra-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
55 lines (45 loc) · 1.37 KB
/
Rakefile
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
46
47
48
49
50
51
52
53
54
55
require 'rake'
require "sinatra/activerecord/rake"
require ::File.expand_path('../config/environment', __FILE__)
Rake::Task["db:create"].clear
Rake::Task["db:drop"].clear
# NOTE: Assumes SQLite3 DB
desc "create the database"
task "db:create" do
touch 'db/db.sqlite3'
end
desc "drop the database"
task "db:drop" do
rm_f 'db/db.sqlite3'
end
task 'db:create_migration' do
unless ENV["NAME"]
puts "No NAME specified. Example usage: `rake db:create_migration NAME=create_users`"
exit
end
name = ENV["NAME"]
version = ENV["VERSION"] || Time.now.utc.strftime("%Y%m%d%H%M%S")
ActiveRecord::Migrator.migrations_paths.each do |directory|
next unless File.exist?(directory)
migration_files = Pathname(directory).children
if duplicate = migration_files.find { |path| path.basename.to_s.include?(name) }
puts "Another migration is already named \"#{name}\": #{duplicate}."
exit
end
end
filename = "#{version}_#{name}.rb"
dirname = ActiveRecord::Migrator.migrations_path
path = File.join(dirname, filename)
FileUtils.mkdir_p(dirname)
File.write path, <<-MIGRATION.strip_heredoc
class #{name.camelize} < ActiveRecord::Migration
def change
end
end
MIGRATION
puts path
end
desc 'Retrieves the current schema version number'
task "db:version" do
puts "Current version: #{ActiveRecord::Migrator.current_version}"
end