Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #1313 from padrino/sequel-setup-rake-tasks
Browse files Browse the repository at this point in the history
Implemented create and drop tasks for Sequel
  • Loading branch information
DAddYE committed Jul 1, 2013
2 parents 97294d1 + f5ebf2d commit 8902260
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 51 deletions.
64 changes: 13 additions & 51 deletions padrino-gen/lib/padrino-gen/padrino-tasks/datamapper.rb
Expand Up @@ -55,68 +55,30 @@
database = config[:database] || config[:path].sub(/\//, "")
charset = config[:charset] || ENV['CHARSET'] || 'utf8'
collation = config[:collation] || ENV['COLLATION'] || 'utf8_unicode_ci'
puts "=> Creating database '#{database}'"
case config[:adapter]
when 'postgres'
arguments = []
arguments << "--encoding=#{charset}" if charset
arguments << "--host=#{host}" if host
arguments << "--username=#{user}" if user
arguments << database
system("createdb", *arguments)
puts "<= dm:create executed"
when 'mysql'
arguments = ["--user=#{user}"]
arguments << "--password=#{password}" unless password.blank?

unless %w[127.0.0.1 localhost].include?(host)
arguments << "--host=#{host}"
end

arguments << '-e'
arguments << "CREATE DATABASE #{database} DEFAULT CHARACTER SET #{charset} DEFAULT COLLATE #{collation}"

system('mysql',*arguments)
puts "<= dm:create executed"
when 'sqlite3'
DataMapper.setup(DataMapper.repository.name, config)
else
raise "Adapter #{config[:adapter]} not supported for creating databases yet."
puts "=> Creating database '#{database}'"
if config[:adapter] == 'sqlite3'
DataMapper.setup(DataMapper.repository.name, config)
else
# require 'padrino-gen/padrino-tasks/sql-helpers'
Padrino::Generators::SqlHelpers.create_db(config[:adapter], user, password, host, database, charset, collation)
end
puts "<= dm:create executed"
end

desc "Drop the database (postgres and mysql only)"
task :drop => :environment do
config = DataMapper.repository.adapter.options.symbolize_keys
user, password, host = config[:user], config[:password], config[:host]
database = config[:database] || config[:path].sub(/\//, "")
puts "=> Dropping database '#{database}'"
case config[:adapter]
when 'postgres'
arguments = []
arguments << "--host=#{host}" if host
arguments << "--username=#{user}" if user
arguments << database
system("dropdb", *arguments)
puts "<= dm:drop executed"
when 'mysql'
arguments = ["--user=#{user}"]
arguments << "--password=#{password}" unless password.blank?

unless %w[127.0.0.1 localhost].include?(host)
arguments << "--host=#{host}"
end

arguments << '-e'
arguments << "DROP DATABASE IF EXISTS #{database}"

system('mysql',*arguments)
puts "<= dm:drop executed"
when 'sqlite3'
File.delete(config[:path]) if File.exist?(config[:path])
else
raise "Adapter #{config[:adapter]} not supported for dropping databases yet."
puts "=> Dropping database '#{database}'"
if config[:adapter] == 'sqlite3'
File.delete(config[:path]) if File.exist?(config[:path])
else
Padrino::Generators::SqlHelpers.drop_db(config[:adapter], user, password, host, database)
end
puts "<= dm:drop executed"
end

desc "Drop the database, migrate from scratch and initialize with the seed data"
Expand Down
36 changes: 36 additions & 0 deletions padrino-gen/lib/padrino-gen/padrino-tasks/sequel.rb
Expand Up @@ -36,7 +36,43 @@

desc "Perform migration up to latest migration available"
task :migrate => 'sq:migrate:up'

desc "Create the database"
task :create => :environment do
config = Sequel::Model.db.opts
user, password, host = config[:user], config[:password], config[:host]
database = config[:database]
charset = config[:charset] || ENV['CHARSET'] || 'utf8'
collation = config[:collation] || ENV['COLLATION'] || 'utf8_unicode_ci'

puts "=> Creating database '#{database}'"
if config[:adapter] == 'sqlite3'
::Sequel.sqlite(database)
else
require 'padrino-gen/padrino-tasks/sql-helpers'
Padrino::Generators::SqlHelpers.create_db(config[:adapter], user, password, host, database, charset, collation)
end
puts "<= sq:create executed"
end

desc "Drop the database (postgres and mysql only)"
task :drop => :environment do
config = ::Sequel::Model.db.opts
user, password, host, database = config[:user], config[:password], config[:host], config[:database]

::Sequel::Model.db.disconnect

puts "=> Dropping database '#{database}'"
if config[:adapter] == 'sqlite3'
File.delete(database) if File.exist?(database)
else
Padrino::Generators::SqlHelpers.drop_db(config[:adapter], user, password, host, database)
end
puts "<= sq:drop executed"
end

end

task 'db:migrate' => 'sq:migrate'
task 'db:reset' => ['sq:drop', 'sq:create', 'sq:migrate', 'seed']
end
56 changes: 56 additions & 0 deletions padrino-gen/lib/padrino-gen/padrino-tasks/sql-helpers.rb
@@ -0,0 +1,56 @@
module Padrino
module Generators
module SqlHelpers
def self.create_db(adapter, user, password, host, database, charset, collation)
case adapter
when 'postgres'
arguments = []
arguments << "--encoding=#{charset}" if charset
arguments << "--host=#{host}" if host
arguments << "--username=#{user}" if user
arguments << database
system("createdb", *arguments)
when 'mysql'
arguments = ["--user=#{user}"]
arguments << "--password=#{password}" unless password.blank?

unless %w[127.0.0.1 localhost].include?(host)
arguments << "--host=#{host}"
end

arguments << '-e'
arguments << "CREATE DATABASE #{database} DEFAULT CHARACTER SET #{charset} DEFAULT COLLATE #{collation}"

system('mysql',*arguments)
else
raise "Adapter #{adapter} not supported for creating databases yet."
end
end

def self.drop_db(adapter, user, password, host, database)
case adapter
when 'postgres'
arguments = []
arguments << "--host=#{host}" if host
arguments << "--username=#{user}" if user
arguments << database
system("dropdb", *arguments)
when 'mysql'
arguments = ["--user=#{user}"]
arguments << "--password=#{password}" unless password.blank?

unless %w[127.0.0.1 localhost].include?(host)
arguments << "--host=#{host}"
end

arguments << '-e'
arguments << "DROP DATABASE IF EXISTS #{database}"

system('mysql',*arguments)
else
raise "Adapter #{adapter} not supported for dropping databases yet."
end
end
end
end
end

0 comments on commit 8902260

Please sign in to comment.