From 00424007741ae0b75a69be6de52e3400e8d0e4f6 Mon Sep 17 00:00:00 2001 From: Edgar Giovanni Lepe Date: Thu, 26 Jul 2018 00:20:44 -0700 Subject: [PATCH] Fix mysql and sqlite3 database option bug --- lib/suspenders/app_builder.rb | 10 +++++++ lib/suspenders/generators/app_generator.rb | 6 ++-- spec/features/database_spec.rb | 34 ++++++++++++++++++++++ templates/Gemfile.erb | 1 - 4 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 spec/features/database_spec.rb diff --git a/lib/suspenders/app_builder.rb b/lib/suspenders/app_builder.rb index 6ac34d8ac..e18f9bfa1 100644 --- a/lib/suspenders/app_builder.rb +++ b/lib/suspenders/app_builder.rb @@ -148,6 +148,16 @@ def use_postgres_config_template force: true end + def add_database_to_gemfile(database) + if database == "sqlite3" + @generator.gem "sqlite3", "~> 1.3" + elsif database == "mysql" + @generator.gem "mysql2", "~> 0.5" + else + @generator.gem "pg", "~> 0.18" + end + end + def create_database bundle_command "exec rails db:create db:migrate" end diff --git a/lib/suspenders/generators/app_generator.rb b/lib/suspenders/generators/app_generator.rb index a756cf293..0737f1ac3 100644 --- a/lib/suspenders/generators/app_generator.rb +++ b/lib/suspenders/generators/app_generator.rb @@ -71,10 +71,8 @@ def customize_gemfile def setup_database say 'Setting up database' - if 'postgresql' == options[:database] - build :use_postgres_config_template - end - + build :add_database_to_gemfile, options[:database] + build :use_postgres_config_template if options[:database] == "postgresql" build :create_database end diff --git a/spec/features/database_spec.rb b/spec/features/database_spec.rb new file mode 100644 index 000000000..e52ff3d4a --- /dev/null +++ b/spec/features/database_spec.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.shared_context "Project Setup Hook" do |option| + before :context do + drop_dummy_database + remove_project_directory + run_suspenders option + setup_app_dependencies + end +end + +RSpec.describe "Suspend a new project with custom database option" do + context "--database=sqlite3" do + include_context "Project Setup Hook", "--database=sqlite3" + + it "adds sqlite3 to Gemfile" do + gemfile = IO.read "#{project_path}/Gemfile" + + expect(gemfile).to match(/sqlite3/) + end + end + + context "--database=mysql" do + include_context "Project Setup Hook", "--database=mysql" + + it "adds mysql2 to Gemfile" do + gemfile = IO.read "#{project_path}/Gemfile" + + expect(gemfile).to match(/mysql2/) + end + end +end diff --git a/templates/Gemfile.erb b/templates/Gemfile.erb index aa7d98282..2c464fccb 100644 --- a/templates/Gemfile.erb +++ b/templates/Gemfile.erb @@ -11,7 +11,6 @@ gem "autoprefixer-rails" gem "flutie" gem "honeybadger" gem "jquery-rails" -gem "pg", "~> 0.18" gem "puma" gem "rack-canonical-host" gem "rails", "<%= Suspenders::RAILS_VERSION %>"