-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 584efb7
Showing
22 changed files
with
680 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
name: Tests | ||
|
||
on: | ||
pull_request: | ||
branches: | ||
- "*" | ||
push: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
ruby_version: [3.1.3, 3.2.1, 3.3.0] | ||
bridgetown_version: [1.3.4] | ||
continue-on-error: ${{ endsWith(matrix.ruby, 'head') || matrix.ruby == 'debug' }} | ||
# Has to be top level to cache properly | ||
env: | ||
BUNDLE_JOBS: 3 | ||
BUNDLE_PATH: "vendor/bundle" | ||
BRIDGETOWN_VERSION: ${{ matrix.bridgetown_version }} | ||
steps: | ||
- uses: actions/checkout@master | ||
- name: Setup Ruby | ||
uses: ruby/setup-ruby@v1 | ||
with: | ||
ruby-version: ${{ matrix.ruby_version }} | ||
bundler-cache: true | ||
- name: Test with Rake | ||
run: script/cibuild |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/vendor | ||
/.bundle/ | ||
/.yardoc | ||
/Gemfile.lock | ||
/_yardoc/ | ||
/coverage/ | ||
/doc/ | ||
/pkg/ | ||
/spec/reports/ | ||
/tmp/ | ||
*.bundle | ||
*.so | ||
*.o | ||
*.a | ||
mkmf.log | ||
*.gem | ||
Gemfile.lock | ||
.bundle | ||
.ruby-version | ||
|
||
# Node | ||
node_modules | ||
.npm | ||
.node_repl_history | ||
|
||
# Yarn | ||
yarn-error.log | ||
yarn-debug.log* | ||
.pnp/ | ||
.pnp.js | ||
|
||
# Yarn Integrity file | ||
.yarn-integrity | ||
|
||
test/dest | ||
.bridgetown-metadata | ||
.bridgetown-cache | ||
.bridgetown-webpack | ||
|
||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require: rubocop-bridgetown | ||
|
||
inherit_gem: | ||
rubocop-bridgetown: .rubocop.yml | ||
|
||
AllCops: | ||
TargetRubyVersion: 3.1 | ||
|
||
Exclude: | ||
- .gitignore | ||
- .rubocop.yml | ||
- "*.gemspec" | ||
|
||
- Gemfile.lock | ||
- CHANGELOG.md | ||
- LICENSE.txt | ||
- README.md | ||
- Rakefile | ||
- bridgetown.automation.rb | ||
|
||
- script/**/* | ||
- test/fixtures/**/* | ||
- vendor/**/* | ||
|
||
Metrics/BlockLength: | ||
Exclude: | ||
- lib/tasks/*.rake | ||
|
||
Metrics/ParameterLists: | ||
Enabled: false | ||
|
||
Style/FrozenStringLiteralComment: | ||
Enabled: false | ||
|
||
Style/HashSyntax: | ||
Exclude: | ||
- lib/tasks/*.rake |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Changelog | ||
|
||
All notable changes to this project will be documented in this file. | ||
|
||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), | ||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). | ||
|
||
## [Unreleased] | ||
|
||
- ... | ||
|
||
## [1.0.0] - 2024-04-03 | ||
|
||
- First version of the Bridgetown Sequel gem. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
source "https://rubygems.org" | ||
gemspec | ||
|
||
gem "bridgetown", ENV["BRIDGETOWN_VERSION"] if ENV["BRIDGETOWN_VERSION"] | ||
|
||
group :test do | ||
gem "minitest" | ||
gem "minitest-profile" | ||
gem "minitest-reporters" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2023-present Jared White & Bridgetown maintainers | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
# Bridgetown Sequel | ||
|
||
A Bridgetown plugin to make it easy to integrate and use [Sequel](https://sequel.jeremyevans.net), a popular database toolkit for Ruby. | ||
|
||
It's been tested only with PostgreSQL, but it should support any of the databases supported by Sequel. | ||
|
||
## Installation | ||
|
||
Run this command to add this plugin to your site's Gemfile: | ||
|
||
```shell | ||
bundle add bridgetown_sequel | ||
``` | ||
|
||
Then add the database URI and initializer to your configuration in `config/initializers.rb` (note that the initializer _must_ be excepted from the `sequel_tasks` context): | ||
|
||
```ruby | ||
database_uri ENV.fetch("DATABASE_URL", "postgres://localhost/your_database_name_here_#{Bridgetown.env}") | ||
|
||
except :sequel_tasks do | ||
init :bridgetown_sequel | ||
end | ||
``` | ||
|
||
You'll also want to add this plugin's Rake tasks to your `Rakefile`: | ||
|
||
```rb | ||
# This is at the top of your Rakefile already: | ||
Bridgetown.load_tasks | ||
|
||
# Now add this: | ||
require "bridgetown_sequel" | ||
Bridgetown::Sequel.load_tasks | ||
``` | ||
|
||
Finally, you'll want to create a `models` folder at the top-level of your site repo, as well as a `migrations` folder. | ||
|
||
## Usage | ||
|
||
To add your first database table & model, first you'll want to add a model file to your new `models` folder. It can look as simple as this: | ||
|
||
```rb | ||
# models/project.rb | ||
|
||
class Project < Sequel::Model | ||
# you can add optional model configuration along with your own Ruby code here later... | ||
end | ||
``` | ||
|
||
Next, you'll want to create a migration. Run the following command: | ||
|
||
```shell | ||
bin/bridgetown db::migrations:new name=create_projects | ||
``` | ||
|
||
And modify the new `migrations/001_create_projects.rb` file to look something like this: | ||
|
||
```rb | ||
Sequel.migration do | ||
change do | ||
create_table(:projects) do | ||
primary_key :id | ||
String :name, null: false | ||
String :category | ||
Integer :order, default: 0 | ||
|
||
DateTime :created_at | ||
DateTime :updated_at | ||
end | ||
end | ||
end | ||
``` | ||
|
||
Now let's set up the database and run migrations. First, run this command (you only need to do this once for your repo): | ||
|
||
```shell | ||
bin/bridgetown db:setup | ||
``` | ||
|
||
Then run migrations: | ||
|
||
```shell | ||
bin/bridgetown db:migrate | ||
``` | ||
|
||
This will create the `projects` table and annotate your `models/project.rb` file with comments showing the table schema. | ||
|
||
Now let's test your model. Run `bin/bridgetown console` (or `bin/bt c` for short): | ||
|
||
```rb | ||
> Project.create(name: "My new project") | ||
|
||
> project = Project[1] | ||
``` | ||
|
||
You should now see that you can save and load project records in your database. | ||
|
||
If you ever need to drop your database and start over, run `bin/bridgetown db:drop`. | ||
|
||
### Optional Configuration | ||
|
||
You can pass various options to the `bridgetown_sequel` initializer to customize the behavior of Sequel: | ||
|
||
```rb | ||
init :bridgetown_sequel do | ||
connection_options do # pass options to Sequel's `connect` method | ||
# This adds a nice console debugging feature, aka `Project.dataset.print` | ||
extensions [:pretty_table] | ||
end | ||
skip_autoload true # only set to `true` if you're manually configuring your autoload settings | ||
models_dir "another_folder" # change the default `models` to something else | ||
model_setup ->(model) do # here you can add `Sequel::Model` plugins to apply to all your models | ||
model.plugin :update_or_create | ||
end | ||
end | ||
``` | ||
|
||
At any time after the initializer is run, you can access `Bridgetown.database` (aliased `db`) anywhere in your Ruby code to access the Sequel connection object. (This is equivalent to the `DB` constant you see in a lot of Sequel documentation.) For example, in your console: | ||
|
||
```rb | ||
> db = Bridgetown.db | ||
> db.fetch("SELECT * FROM projects ORDER BY name desc LIMIT 1").first | ||
> db["SELECT COUNT(*) FROM projects"].first[:count] | ||
``` | ||
|
||
Raw SQL statements won't be logged out-of-the-box, but you can attach Bridgetown's logger to Sequel. Just add this statement right after your initializer: | ||
|
||
```rb | ||
Bridgetown.db.loggers << Bridgetown.logger | ||
``` | ||
|
||
For a quick reference on what you can do with the Sequel DSL, check out this [handy cheat sheet](https://sequel.jeremyevans.net/rdoc/files/doc/cheat_sheet_rdoc.html). | ||
|
||
## Testing | ||
|
||
* Run `bundle exec rake test` to run the test suite | ||
* Or run `script/cibuild` to validate with Rubocop and Minitest together. | ||
|
||
## Contributing | ||
|
||
1. Fork it (https://github.com/bridgetownrb/bridgetown_sequel/fork) | ||
2. Clone the fork using `git clone` to your local development machine. | ||
3. Create your feature branch (`git checkout -b my-new-feature`) | ||
4. Commit your changes (`git commit -am 'Add some feature'`) | ||
5. Push to the branch (`git push origin my-new-feature`) | ||
6. Create a new Pull Request |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require "bundler/gem_tasks" | ||
require "rake/testtask" | ||
|
||
Rake::TestTask.new(:test) do |t| | ||
t.libs << "test" | ||
t.libs << "lib" | ||
t.test_files = FileList["test/**/test_*.rb"] | ||
t.warning = false | ||
end | ||
|
||
task :default => :test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "lib/bridgetown_sequel/version" | ||
|
||
Gem::Specification.new do |spec| | ||
spec.name = "bridgetown_sequel" | ||
spec.version = BridgetownSequel::VERSION | ||
spec.author = "Bridgetown Team" | ||
spec.email = "[email protected]" | ||
spec.summary = "Bridgetown plugin for integrating the Sequel database gem" | ||
spec.homepage = "https://github.com/bridgetownrb/bridgetown_sequel" | ||
spec.license = "MIT" | ||
|
||
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r!^(test|script|spec|features|frontend)/!) } | ||
spec.test_files = spec.files.grep(%r!^test/!) | ||
spec.require_paths = ["lib"] | ||
|
||
spec.required_ruby_version = ">= 3.1.0" | ||
|
||
spec.add_dependency "bridgetown", ">= 1.3.0" | ||
spec.add_dependency "sequel", ">= 5.76" | ||
spec.add_dependency "sequel-annotate", ">= 1.7" | ||
|
||
spec.add_development_dependency "bundler" | ||
spec.add_development_dependency "rake", ">= 13.0" | ||
spec.add_development_dependency "rubocop-bridgetown", "~> 0.3" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
require "bridgetown" | ||
|
||
# Needs export OBJC_DISABLE_INITIALIZE_FORK_SAFETY="yes" on macOS | ||
# See: https://stackoverflow.com/questions/52671926/rails-may-have-been-in-progress-in-another-thread-when-fork-was-called | ||
require "sequel" | ||
|
||
module Bridgetown | ||
module Sequel | ||
def self.load_tasks(models_dir: "models") | ||
ENV["BRIDGETOWN_SEQUEL_MODELS_DIR"] ||= models_dir | ||
load File.expand_path("tasks/sequel_database.rake", __dir__) | ||
end | ||
end | ||
end | ||
|
||
Bridgetown.initializer :bridgetown_sequel do | | ||
config, | ||
models_dir: ENV.fetch("BRIDGETOWN_SEQUEL_MODELS_DIR", "models"), | ||
skip_autoload: false, | ||
model_setup: -> {}, | ||
connection_options: {} | ||
| | ||
unless skip_autoload | ||
config.autoload_paths << { | ||
path: models_dir, | ||
eager: true, | ||
} | ||
end | ||
|
||
config.database_models_dir = models_dir | ||
|
||
# Add a `Bridgetown.database` convenience method | ||
Bridgetown.define_singleton_method :database do | ||
Sequel::DATABASES.first | ||
end | ||
Bridgetown.singleton_class.alias_method :db, :database | ||
|
||
# Connect to the Database | ||
config.database_connection_options = connection_options | ||
# Example URI: postgres://user:password@localhost/blog | ||
Sequel.connect(config.database_uri, **connection_options) | ||
|
||
# Set up model plugins | ||
Sequel::Model.plugin :timestamps | ||
model_setup.(Sequel::Model) | ||
end |
Oops, something went wrong.