Skip to content

Commit

Permalink
Ensure to create migrations artifacts for app arch
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Jun 18, 2015
1 parent 39e5510 commit af95d29
Show file tree
Hide file tree
Showing 8 changed files with 271 additions and 170 deletions.
2 changes: 1 addition & 1 deletion lib/lotus/commands/generate.rb
Expand Up @@ -81,7 +81,7 @@ def generator

def sanitize_input(app_name, name)
if options[:architecture] == APP_ARCHITECTURE
@app_name = ''
@app_name = nil
@name = app_name
else
@app_name = app_name
Expand Down
17 changes: 16 additions & 1 deletion lib/lotus/generators/application/app.rb
Expand Up @@ -52,13 +52,18 @@ def start
empty_directories = [
"app/controllers",
"app/views",
"db",
"lib/#{ app_name }/entities",
"lib/#{ app_name }/repositories",
"public/javascripts",
"public/stylesheets"
]

empty_directories << if sql_database?
"db/migrations"
else
"db"
end

# Add testing directories (spec/ is the default for both MiniTest and RSpec)
empty_directories << [
"spec/features",
Expand Down Expand Up @@ -89,6 +94,12 @@ def start
"spec/support"
]

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

templates.each do |src, dst|
cli.template(source.join(src), target.join(dst), opts)
end
Expand Down Expand Up @@ -129,6 +140,10 @@ def database_gem
}[@database]
end

def sql_database?
database_type == :sql
end

def database_type
case @database
when 'mysql', 'mysql2', 'postgresql', 'postgres', 'sqlite', 'sqlite3'
Expand Down
8 changes: 8 additions & 0 deletions lib/lotus/generators/application/app/lib/app_name.rb.tt
Expand Up @@ -16,6 +16,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.
11 changes: 8 additions & 3 deletions lib/lotus/generators/migration.rb
Expand Up @@ -25,9 +25,8 @@ class Migration < Abstract
def initialize(command)
super

timestamp = Time.now.utc.strftime(TIMESTAMP_FORMAT)
name = Utils::String.new(app_name).underscore
filename = FILENAME % { timestamp: timestamp, name: name }
timestamp = Time.now.utc.strftime(TIMESTAMP_FORMAT)
filename = FILENAME % { timestamp: timestamp, name: name }

env.require_application_environment
@destination = Lotus::Model.configuration.migrations.join(filename)
Expand All @@ -46,6 +45,12 @@ def start
cli.template(source.join(src), target.join(dst), {})
end
end

private

def name
Utils::String.new(app_name || super).underscore
end
end
end
end
33 changes: 32 additions & 1 deletion test/commands/new_test.rb
Expand Up @@ -1247,6 +1247,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 @@ -1255,6 +1257,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 @@ -1263,6 +1267,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 @@ -1272,6 +1278,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 @@ -1281,6 +1289,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 @@ -1289,6 +1299,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 Down Expand Up @@ -1316,7 +1328,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 af95d29

Please sign in to comment.