Skip to content

Commit

Permalink
When generate a new application, if the database is SQL, generate mig…
Browse files Browse the repository at this point in the history
…ration files and code
  • Loading branch information
jodosha committed Jun 12, 2015
1 parent bb8d4e1 commit 1f0bc88
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ gem 'lotus-validations', '~> 0.3', require: false, github: 'lotus/validations',
gem 'lotus-router', '~> 0.4', require: false, github: 'lotus/router', branch: '0.4.x'
gem 'lotus-controller', '~> 0.4', require: false, github: 'lotus/controller', branch: '0.4.x'
gem 'lotus-view', '~> 0.4', require: false, github: 'lotus/view', branch: '0.4.x'
gem 'lotus-model', '~> 0.3', require: false, github: 'lotus/model', branch: '0.3.x'
gem 'lotus-model', '~> 0.3', require: false, github: 'lotus/model', branch: 'database-migrations'
gem 'lotus-helpers', '~> 0.2', require: false, github: 'lotus/helpers', branch: '0.2.x'

platforms :ruby do
Expand Down
13 changes: 12 additions & 1 deletion lib/lotus/generators/application/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,16 @@ def start
}

empty_directories = [
"db",
"lib/#{ app_name }/entities",
"lib/#{ app_name }/repositories"
]

empty_directories << if database_type == :sql
"db/migrations"
else
"db"
end

case options[:test]
when 'rspec'
templates.merge!(
Expand All @@ -64,6 +69,12 @@ def start
)
end

if database_type == :sql
templates.merge!(
'schema.sql.tt' => 'db/schema.sql'
)
end

empty_directories << [
"spec/#{ app_name }/entities",
"spec/#{ app_name }/repositories",
Expand Down
9 changes: 9 additions & 0 deletions lib/lotus/generators/application/container/lib/app_name.rb.tt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ require 'lotus/model'
Dir["#{ __dir__ }/<%= config[:app_name] %>/**/*.rb"].each { |file| require_relative file }

Lotus::Model.configure do
##
# Database adapter
#
# Available options:
Expand All @@ -16,6 +17,14 @@ Lotus::Model.configure do
#
adapter type: :<%= config[:database_config][:type] %>, uri: ENV['<%= config[:app_name].to_env_s %>_DATABASE_URL']

<%- if config[:database_config][:type] == :sql -%>
##
# Migrations
#
migrations 'db/migrations'
schema 'db/schema.sql'

<%- end -%>
##
# Database mapping
#
Expand Down
Empty file.
35 changes: 34 additions & 1 deletion test/commands/new_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,8 @@ def container_options
it 'generates adapter config for mysql' do
content = @root.join('lib/chirp.rb').read
content.must_match %(adapter type: :sql, uri: ENV['CHIRP_DATABASE_URL'])
content.must_match %(migrations 'db/migrations')
content.must_match %(schema 'db/schema.sql')
end
end

Expand All @@ -455,6 +457,8 @@ def container_options
it 'generates adapter config for mysql2' do
content = @root.join('lib/chirp.rb').read
content.must_match %(adapter type: :sql, uri: ENV['CHIRP_DATABASE_URL'])
content.must_match %(migrations 'db/migrations')
content.must_match %(schema 'db/schema.sql')
end
end

Expand All @@ -463,6 +467,8 @@ def container_options
it 'generates adapter config for postgresql' do
content = @root.join('lib/chirp.rb').read
content.must_match %(adapter type: :sql, uri: ENV['CHIRP_DATABASE_URL'])
content.must_match %(migrations 'db/migrations')
content.must_match %(schema 'db/schema.sql')
end
end

Expand All @@ -472,6 +478,8 @@ def container_options
it 'generates adapter config for postgres' do
content = @root.join('lib/chirp.rb').read
content.must_match %(adapter type: :sql, uri: ENV['CHIRP_DATABASE_URL'])
content.must_match %(migrations 'db/migrations')
content.must_match %(schema 'db/schema.sql')
end
end

Expand All @@ -481,6 +489,8 @@ def container_options
it 'generates adapter config for sqlite' do
content = @root.join('lib/chirp.rb').read
content.must_match %(adapter type: :sql, uri: ENV['CHIRP_DATABASE_URL'])
content.must_match %(migrations 'db/migrations')
content.must_match %(schema 'db/schema.sql')
end
end

Expand All @@ -489,6 +499,8 @@ def container_options
it 'generates adapter config for sqlite3' do
content = @root.join('lib/chirp.rb').read
content.must_match %(adapter type: :sql, uri: ENV['CHIRP_DATABASE_URL'])
content.must_match %(migrations 'db/migrations')
content.must_match %(schema 'db/schema.sql')
end
end

Expand All @@ -497,6 +509,8 @@ def container_options
it 'generates adapter config for memory' do
content = @root.join('lib/chirp.rb').read
content.must_match %(adapter type: :memory, uri: ENV['CHIRP_DATABASE_URL'])
content.wont_match %(migrations 'db/migrations')
content.wont_match %(schema 'db/schema.sql')
end
end
end
Expand All @@ -516,7 +530,26 @@ def container_options

describe 'db' do
it 'generates it' do
@root.join('db').must_be :directory?
@root.join('db').must_be :directory?
@root.join('db/migrations').wont_be :exist?
end

['postgres', 'postgresql', 'mysql', 'mysql2', 'sqlite', 'sqlite3'].each do |database|
describe "with #{ database }" do
let(:opts) { container_options.merge(database: database) }

it "generates 'db/migrations'" do
@root.join('db/migrations').must_be :directory?
@root.join('db/migrations/.gitkeep').must_be :exist?

@root.join('db/.gitkeep').wont_be :exist?
end

it "generates empty 'db/schema.sql'" do
@root.join('db/schema.sql').must_be :exist?
@root.join('db/schema.sql').read.must_be :empty?
end
end
end
end

Expand Down

0 comments on commit 1f0bc88

Please sign in to comment.