forked from LeFnord/grape-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorms.rb
117 lines (98 loc) · 3.15 KB
/
orms.rb
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# frozen_string_literal: true
module Starter
class Orms
class << self
def build(dest, orm)
load_orm(orm: orm)
return if @orm.nil?
if @orm == 'ar' || @orm == 'activerecord'
# Fixes pooling
add_middleware(dest, 'ActiveRecord::Rack::ConnectionManagement')
build_standalone_migrations(File.join(dest, '.standalone_migrations'))
end
build_initializer(File.join(dest, 'config', 'initializers'))
build_config(File.join(dest, 'config'))
append_to_file(File.join(dest, 'Rakefile'), rakefile)
append_to_file(File.join(dest, 'Gemfile'), gemfile)
prepare_for_migrations(File.join(dest, 'db'))
end
def standalone_migrations
<<-FILE.strip_heredoc
config:
database: config/database.yml
FILE
end
def config
<<-FILE.strip_heredoc
# ActiveRecord Database Configuration
development:
adapter: #{adapter}
encoding: unicode
timeout: 5000
user: postgres
database: name_development
test:
adapter: #{adapter}
encoding: unicode
timeout: 5000
user: postgres
database: name_test
production:
adapter: #{adapter}
encoding: unicode
timeout: 5000
user: postgres
database: name_production
FILE
end
def add_migration(klass_name, resource)
load_orm
return if @orm.nil?
file_name = "#{Time.now.strftime('%Y%m%d%H%m%S')}_Create#{klass_name}.rb"
migration_dest = File.join(Dir.pwd, 'db', 'migrate', file_name)
FileFoo.write_file(migration_dest, migration(klass_name, resource))
end
def load_orm(orm: ::Starter::Config.read[:orm])
@orm = orm
case @orm
when 'sequel'
require 'starter/builder/templates/sequel'
extend(::Starter::Templates::Sequel)
when 'activerecord', 'ar'
require 'starter/builder/templates/activerecord'
extend(::Starter::Templates::ActiveRecord)
else
@orm = nil
end
end
private
def adapter
'postgresql'
end
def build_initializer(dest)
FileUtils.mkdir_p(dest)
FileFoo.write_file(File.join(dest, 'database.rb'), initializer)
end
def build_config(dest)
FileFoo.write_file(File.join(dest, 'database.yml'), config)
end
def build_standalone_migrations(dest)
FileFoo.write_file(dest, standalone_migrations)
end
# adds a middleware to config.ru
def add_middleware(dest, middleware)
replacement = "use #{middleware}\n\n\\1"
FileFoo.call!(File.join(dest, 'config.ru')) { |content| content.sub!(/^(run.+)$/, replacement) }
end
def append_to_file(file_name, content)
original = FileFoo.read_file(file_name)
FileFoo.write_file(file_name, "#{original}\n\n#{content}")
end
def prepare_for_migrations(dest)
migrate = File.join(dest, 'migrate')
FileUtils.mkdir_p(migrate)
`touch #{migrate}/.keep`
end
end
end
end