Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for SQLite #1248

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ This approach uses an [application template][] to generate a new Rails
application with Suspenders.

We skip the [default test framework][] in favor of [RSpec][], and [prefer
PostgreSQL][] as our database.
PostgreSQL][] as our database, although we do support SQLite.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm being a bit … careful (?) with my README changes here. I want to suggest it's possible, but we're just trying it out, and it's worth evaluating and not that you should necessarily switch away from Postgres.

That said, maybe we need an explicit example?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is tricky, and I'm not sure the best approach to take. Suspenders is about limiting the choices we need to make, but I recognize we're in an exploratory phase.

I don't know if we have an explicit example (yet), but I imagine it would relate to avoiding a Platform as a service.


We skip [RuboCop rules by default][] in favor of our [holistic linting rules][].

Expand Down
4 changes: 4 additions & 0 deletions lib/generators/suspenders/ci_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def using_node?
File.exist? "package.json"
end

def using_postgres?
database_adapter == "postgresql"
end

def has_gem?(name)
Bundler.rubygems.find_name(name).any?
end
Expand Down
4 changes: 4 additions & 0 deletions lib/generators/templates/ci/ci.yml.tt
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to borrow from the Rails CI template.

Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ jobs:
runs-on: ubuntu-latest

services:
<%- if using_postgres? -%>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this block be tested that it is present for Postgres and absent for SQLite?

postgres:
image: postgres
env:
Expand All @@ -94,6 +95,7 @@ jobs:
ports:
- 5432:5432
options: --health-cmd="pg_isready" --health-interval=10s --health-timeout=5s --health-retries=3
<%- end %>

# redis:
# image: redis
Expand Down Expand Up @@ -127,7 +129,9 @@ jobs:
- name: Run tests
env:
RAILS_ENV: test
<%- if using_postgres? -%>
DATABASE_URL: postgres://postgres:postgres@localhost:5432
<%- end -%>
# REDIS_URL: redis://localhost:6379/0
<%- if using_rspec? -%>
run: bin/rails db:setup spec
Expand Down
6 changes: 5 additions & 1 deletion lib/install/web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ def node_version_unsupported?
node_version < "20.0.0"
end

def database_supported?
["postgresql", "sqlite"].include?(options[:database])
end

def apply_template!
if node_not_installed? || node_version_unsupported?
message = <<~ERROR
Expand All @@ -22,7 +26,7 @@ def apply_template!

fail Rails::Generators::Error, message
end
if options[:database] == "postgresql" && options[:skip_test] && options[:skip_rubocop]
if database_supported? && options[:skip_test] && options[:skip_rubocop]
after_bundle do
gem_group :development, :test do
if ARGV.include?("--suspenders-main")
Expand Down
14 changes: 8 additions & 6 deletions lib/suspenders/generators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
module Suspenders
module Generators
module Helpers
def database_adapter
configuration = File.read(Rails.root.join("config/database.yml"))
configuration = YAML.safe_load(configuration, aliases: true)
configuration["default"]["adapter"]
end

def default_test_suite?
File.exist? Rails.root.join("test")
end
Expand Down Expand Up @@ -62,7 +68,7 @@ module DatabaseUnsupported

class Error < StandardError
def message
"This generator requires PostgreSQL"
"This generator requires either PostgreSQL or SQLite"
end
end

Expand All @@ -78,11 +84,7 @@ def raise_if_database_unsupported
private

def database_unsupported?
configuration = File.read(Rails.root.join("config/database.yml"))
configuration = YAML.safe_load(configuration, aliases: true)
adapter = configuration["default"]["adapter"]

adapter != "postgresql"
database_adapter != ("postgresql" || "sqlite")
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/generators/suspenders/ci_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CiGeneratorTest < Rails::Generators::TestCase

test "raises if PostgreSQL is not the adapter" do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Should this test description reflect "PostgreSQL or SQLite"?

  2. Should this or another test assert that with_database "sqlite" does not raise?

with_database "unsupported" do
assert_raises Suspenders::Generators::DatabaseUnsupported::Error, match: "This generator requires PostgreSQL" do
assert_raises Suspenders::Generators::DatabaseUnsupported::Error, match: "This generator requires either PostgreSQL or SQLite" do
run_generator

assert_no_file app_root(".github/workflows/ci.yaml")
Expand Down
2 changes: 1 addition & 1 deletion test/generators/suspenders/install/web_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class WebGeneratorTest < Rails::Generators::TestCase
end
end

test "raises if PostgreSQL is not the adapter" do
test "raises if the provided database is not supported" do
with_database "unsupported" do
assert_raises Suspenders::Generators::DatabaseUnsupported::Error do
run_generator
Expand Down
13 changes: 12 additions & 1 deletion test/suspenders/generators_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
require "test_helper"

class Suspenders::GeneratorsTest < ActiveSupport::TestCase
class HelpersTest < ActiveSupport::TestCase
include Suspenders::TestHelpers
include Suspenders::Generators::Helpers

test "database_adapter returns the current database" do
with_database "postgresql" do
assert_equal database_adapter, "postgresql"
end
end
Comment on lines +8 to +12
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this also cover the SQLite case?

end

class APIAppUnsupportedTest < ActiveSupport::TestCase
test "message returns a custom message" do
expected = "This generator cannot be used on API only applications."
Expand All @@ -11,7 +22,7 @@ class APIAppUnsupportedTest < ActiveSupport::TestCase

class DatabaseUnsupportedTest < ActiveSupport::TestCase
test "message returns a custom message" do
expected = "This generator requires PostgreSQL"
expected = "This generator requires either PostgreSQL or SQLite"

assert_equal expected, Suspenders::Generators::DatabaseUnsupported::Error.new.message
end
Expand Down
Loading