lib/padrino-gen/generators/components/orms/sequel.rb in padrino-gen-0.14.1.1 vs lib/padrino-gen/generators/components/orms/sequel.rb in padrino-gen-0.14.2

- old
+ new

@@ -1,7 +1,6 @@ SEQUEL = (<<-SEQUEL) unless defined?(SEQUEL) -Sequel::Model.plugin(:schema) Sequel::Model.raise_on_save_failure = false # Do not throw exceptions on failure Sequel::Model.db = case Padrino.env when :development then Sequel.connect(!DB_DEVELOPMENT!, :loggers => [logger]) when :production then Sequel.connect(!DB_PRODUCTION!, :loggers => [logger]) when :test then Sequel.connect(!DB_TEST!, :loggers => [logger]) @@ -10,35 +9,41 @@ def setup_orm sequel = SEQUEL db = @project_name.underscore require_dependencies 'sequel' - case options[:adapter] - when 'mysql-gem' - sequel.gsub!(/!DB_DEVELOPMENT!/, "\"mysql://localhost/#{db}_development\"") - sequel.gsub!(/!DB_PRODUCTION!/, "\"mysql://localhost/#{db}_production\"") - sequel.gsub!(/!DB_TEST!/,"\"mysql://localhost/#{db}_test\"") - require_dependencies 'mysql', :version => "~> 2.8.1" - 'mysql' - when 'mysql', 'mysql2' - sequel.gsub!(/!DB_DEVELOPMENT!/, "\"mysql2://localhost/#{db}_development\"") - sequel.gsub!(/!DB_PRODUCTION!/, "\"mysql2://localhost/#{db}_production\"") - sequel.gsub!(/!DB_TEST!/,"\"mysql2://localhost/#{db}_test\"") - require_dependencies 'mysql2' - 'mysql2' - when 'postgres' - sequel.gsub!(/!DB_DEVELOPMENT!/, "\"postgres://localhost/#{db}_development\"") - sequel.gsub!(/!DB_PRODUCTION!/, "\"postgres://localhost/#{db}_production\"") - sequel.gsub!(/!DB_TEST!/,"\"postgres://localhost/#{db}_test\"") - require_dependencies 'pg' - 'pg' - else - sequel.gsub!(/!DB_DEVELOPMENT!/,"\"sqlite://db/#{db}_development.db\"") - sequel.gsub!(/!DB_PRODUCTION!/, "\"sqlite://db/#{db}_production.db\"") - sequel.gsub!(/!DB_TEST!/, "\"sqlite://db/#{db}_test.db\"") - require_dependencies 'sqlite3' - 'sqlite3' + + begin + case adapter ||= options[:adapter] + when 'mysql-gem' + sequel.gsub!(/!DB_DEVELOPMENT!/, "\"mysql://localhost/#{db}_development\"") + sequel.gsub!(/!DB_PRODUCTION!/, "\"mysql://localhost/#{db}_production\"") + sequel.gsub!(/!DB_TEST!/,"\"mysql://localhost/#{db}_test\"") + require_dependencies 'mysql', :version => "~> 2.8.1" + when 'mysql', 'mysql2' + sequel.gsub!(/!DB_DEVELOPMENT!/, "\"mysql2://localhost/#{db}_development\"") + sequel.gsub!(/!DB_PRODUCTION!/, "\"mysql2://localhost/#{db}_production\"") + sequel.gsub!(/!DB_TEST!/,"\"mysql2://localhost/#{db}_test\"") + require_dependencies 'mysql2' + when 'postgres' + sequel.gsub!(/!DB_DEVELOPMENT!/, "\"postgres://localhost/#{db}_development\"") + sequel.gsub!(/!DB_PRODUCTION!/, "\"postgres://localhost/#{db}_production\"") + sequel.gsub!(/!DB_TEST!/,"\"postgres://localhost/#{db}_test\"") + require_dependencies 'pg' + when 'sqlite' + sequel.gsub!(/!DB_DEVELOPMENT!/,"\"sqlite://db/#{db}_development.db\"") + sequel.gsub!(/!DB_PRODUCTION!/, "\"sqlite://db/#{db}_production.db\"") + sequel.gsub!(/!DB_TEST!/, "\"sqlite://db/#{db}_test.db\"") + require_dependencies 'sqlite3' + else + say "Failed to generate `config/database.rb` for ORM adapter `#{options[:adapter]}`", :red + fail ArgumentError + end + rescue ArgumentError + adapter = ask("Please, choose a proper adapter:", :limited_to => %w[mysql mysql2 mysql-gem postgres sqlite]) + retry end + create_file("config/database.rb", sequel) empty_directory('db/migrate') end SQ_MODEL = (<<-MODEL) unless defined?(SQ_MODEL)