-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
72 lines (57 loc) · 1.76 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
require "bundler/gem_tasks"
require 'rake/testtask'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = false
end
task :default => :test
task :setup do
if File.exist?('.env')
puts 'This will overwrite your existing .env file'
end
print 'Enter your database name: [postgres_ext_postgis_test] '
db_name = STDIN.gets.chomp
print 'Enter your database user: [] '
db_user = STDIN.gets.chomp
print 'Enter your database password: [] '
db_password = STDIN.gets.chomp
print 'Enter your database server: [localhost] '
db_server = STDIN.gets.chomp
db_name = 'postgres_ext_postgis_test' if db_name.empty?
db_password = ":#{db_password}" unless db_password.empty?
db_server = 'localhost' if db_server.empty?
db_server = "@#{db_server}" unless db_user.empty?
env_path = File.expand_path('./.env')
File.open(env_path, 'w') do |file|
file.puts "DATABASE_NAME=#{db_name}"
file.puts "DATABASE_URL=\"postgres://#{db_user}#{db_password}#{db_server}/#{db_name}\""
end
puts '.env file saved'
end
namespace :db do
task :load_db_settings do
require 'active_record'
require 'postgres_ext/postgis'
unless ENV['DATABASE_URL']
require 'dotenv'
Dotenv.load
end
end
task :drop => :load_db_settings do
%x{ dropdb #{ENV['DATABASE_NAME']} }
end
task :create => :load_db_settings do
%x{ createdb #{ENV['DATABASE_NAME']} }
end
task :migrate => :load_db_settings do
ActiveRecord::Base.establish_connection
ActiveRecord::Base.connection.enable_extension 'postgis'
ActiveRecord::Base.connection.create_table :places, force: true do |t|
t.geometry :location
t.column :location_2, :geography
end
puts 'Database migrated'
end
end