Skip to content

Commit

Permalink
Modernize gem.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Sep 21, 2024
1 parent 91be3eb commit 54ff576
Show file tree
Hide file tree
Showing 19 changed files with 75 additions and 66 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ jobs:
timeout-minutes: 5
run: bundle exec bake test

- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
with:
include-hidden-files: true
if-no-files-found: error
name: coverage-${{matrix.os}}-${{matrix.ruby}}
path: .covered.db

Expand All @@ -85,7 +87,7 @@ jobs:
ruby-version: "3.3"
bundler-cache: true

- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4

- name: Validate coverage
timeout-minutes: 5
Expand Down
7 changes: 7 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ Layout/IndentationConsistency:
Enabled: true
EnforcedStyle: normal

Layout/BlockAlignment:
Enabled: true

Layout/EndAlignment:
Enabled: true
EnforcedStyleAlignWith: start_of_line
Expand Down Expand Up @@ -44,3 +47,7 @@ Layout/EmptyLinesAroundModuleBody:

Style/FrozenStringLiteralComment:
Enabled: true

Style/StringLiterals:
Enabled: true
EnforcedStyle: double_quotes
32 changes: 16 additions & 16 deletions benchmark/benchmark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
# Released under the MIT License.
# Copyright, 2020-2024, by Samuel Williams.

require 'benchmark/ips'
require 'async'
require "benchmark/ips"
require "async"

require 'db/client'
require 'db/adapters'
require "db/client"
require "db/adapters"

require 'mysql2'
require 'pg'
require "mysql2"
require "pg"

describe DB::Client do
it "should be fast to insert data" do
Expand All @@ -29,7 +29,7 @@
x.report("db-#{name}") do |repeats|
Sync do
client.session do |session|
session.call('TRUNCATE benchmark')
session.call("TRUNCATE benchmark")

repeats.times do |index|
session.call("INSERT INTO benchmark (i) VALUES (#{index})")
Expand All @@ -39,19 +39,19 @@
end
end

x.report('mysql2') do |repeats|
x.report("mysql2") do |repeats|
client = Mysql2::Client.new(**CREDENTIALS)
client.query('TRUNCATE benchmark')
client.query("TRUNCATE benchmark")

repeats.times do |index|
client.query("INSERT INTO benchmark (i) VALUES (#{index})")
end
end

x.report('pg') do |repeats|
x.report("pg") do |repeats|
client = PG.connect(**PG_CREDENTIALS)

client.exec('TRUNCATE benchmark')
client.exec("TRUNCATE benchmark")

repeats.times do |index|
client.exec("INSERT INTO benchmark (i) VALUES (#{index})")
Expand Down Expand Up @@ -91,7 +91,7 @@
Sync do
client.session do |session|
repeats.times do |index|
session.call('SELECT * FROM benchmark') do |connection|
session.call("SELECT * FROM benchmark") do |connection|
result = connection.next_result
expect(result.to_a).to have_attributes(size: row_count)
end
Expand All @@ -101,20 +101,20 @@
end
end

x.report('mysql2') do |repeats|
x.report("mysql2") do |repeats|
client = Mysql2::Client.new(**CREDENTIALS)

repeats.times do |index|
result = client.query('SELECT * FROM benchmark')
result = client.query("SELECT * FROM benchmark")
expect(result.to_a).to have_attributes(size: row_count)
end
end

x.report('pg') do |repeats|
x.report("pg") do |repeats|
client = PG.connect(**PG_CREDENTIALS)

repeats.times do |index|
result = client.exec('SELECT * FROM benchmark')
result = client.exec("SELECT * FROM benchmark")
expect(result.to_a).to have_attributes(size: row_count)
end
end
Expand Down
12 changes: 6 additions & 6 deletions benchmark/compare/config.ru
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# frozen_string_literal: true

require 'active_support'
require "active_support"
ActiveSupport::IsolatedExecutionState.isolation_level = :fiber

require 'active_record'
require "active_record"
ActiveRecord::Base.establish_connection(adapter: "postgresql", database: "test", pool: 64)

require_relative '../../lib/db'
require 'db/postgres'
require_relative "../../lib/db"
require "db/postgres"

# TracePoint.new(:fiber_switch) do |trace_point|
# $stderr.puts "************* fiber switch (pid=#{Process.pid}) *************"
Expand All @@ -18,10 +18,10 @@ require 'db/postgres'
class Compare
def initialize(app)
@app = app
@db = DB::Client.new(DB::Postgres::Adapter.new(database: 'test'))
@db = DB::Client.new(DB::Postgres::Adapter.new(database: "test"))
end

PATH_INFO = 'PATH_INFO'.freeze
PATH_INFO = "PATH_INFO".freeze
OK = [200, [], ["OK"]]

def active_record_checkout(env)
Expand Down
10 changes: 5 additions & 5 deletions config/sus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
# Released under the MIT License.
# Copyright, 2020-2024, by Samuel Williams.

require 'covered/sus'
require "covered/sus"
include Covered::Sus

Bundler.require(:adapters)

::CREDENTIALS = {
username: 'test',
password: 'test',
database: 'test',
host: '127.0.0.1'
username: "test",
password: "test",
database: "test",
host: "127.0.0.1"
}

# Used for PG.connect:
Expand Down
6 changes: 3 additions & 3 deletions db.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Gem::Specification.new do |spec|
spec.authors = ["Samuel Williams"]
spec.license = "MIT"

spec.cert_chain = ['release.cert']
spec.signing_key = File.expand_path('~/.gem/release.pem')
spec.cert_chain = ["release.cert"]
spec.signing_key = File.expand_path("~/.gem/release.pem")

spec.homepage = "https://github.com/socketry/db"

Expand All @@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
"source_code_uri" => "https://github.com/socketry/db.git",
}

spec.files = Dir.glob(['{lib}/**/*', '*.md'], File::FNM_DOTMATCH, base: __dir__)
spec.files = Dir.glob(["{lib}/**/*", "*.md"], File::FNM_DOTMATCH, base: __dir__)

spec.required_ruby_version = ">= 3.1"

Expand Down
8 changes: 4 additions & 4 deletions examples/indexes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
# Released under the MIT License.
# Copyright, 2021-2024, by Samuel Williams.

require 'async'
require_relative '../lib/db/client'
require 'db/postgres'
require "async"
require_relative "../lib/db/client"
require "db/postgres"

# Create the client and connection pool:
client = DB::Client.new(DB::Postgres::Adapter.new(database: 'test'))
client = DB::Client.new(DB::Postgres::Adapter.new(database: "test"))

def create_schema(session)
session.clause("DROP TABLE IF EXISTS").identifier(:things).call
Expand Down
2 changes: 1 addition & 1 deletion examples/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module AppliationSchema
schema :todo => TodoSchema
end

client = DB::Client.new(DB::Postgres::Adapter.new(database: 'test'))
client = DB::Client.new(DB::Postgres::Adapter.new(database: "test"))
schema = ApplicationSchema.new(client)

schema.login.call(username, password)
Expand Down
6 changes: 3 additions & 3 deletions fixtures/db/client_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
# Released under the MIT License.
# Copyright, 2024, by Samuel Williams.

require 'db/client'
require 'db/adapters'
require 'sus/fixtures/async'
require "db/client"
require "db/adapters"
require "sus/fixtures/async"

module DB
ClientContext = Sus::Shared("client context") do |adapter|
Expand Down
2 changes: 1 addition & 1 deletion fixtures/db/datatype_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Released under the MIT License.
# Copyright, 2024, by Samuel Williams.

require_relative 'client_context'
require_relative "client_context"

module DB
DatatypeContext = Sus::Shared("datatype context") do |adapter, datatype|
Expand Down
6 changes: 3 additions & 3 deletions lib/db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
# Released under the MIT License.
# Copyright, 2020-2024, by Samuel Williams.

require_relative 'db/version'
require_relative 'db/adapters'
require_relative 'db/client'
require_relative "db/version"
require_relative "db/adapters"
require_relative "db/client"
6 changes: 3 additions & 3 deletions lib/db/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# Released under the MIT License.
# Copyright, 2020-2024, by Samuel Williams.

require 'async/pool/controller'
require "async/pool/controller"

require_relative 'context/session'
require_relative 'context/transaction'
require_relative "context/session"
require_relative "context/transaction"

module DB
# Binds a connection pool to the specified adapter.
Expand Down
4 changes: 2 additions & 2 deletions lib/db/context/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
# Released under the MIT License.
# Copyright, 2020-2024, by Samuel Williams.

require_relative '../query'
require_relative '../records'
require_relative "../query"
require_relative "../records"

module DB
module Context
Expand Down
2 changes: 1 addition & 1 deletion lib/db/context/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Released under the MIT License.
# Copyright, 2020-2024, by Samuel Williams.

require_relative 'session'
require_relative "session"

module DB
module Context
Expand Down
6 changes: 3 additions & 3 deletions lib/db/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def initialize(context, buffer = String.new)
# @parameter value [String] A raw SQL string, e.g. `WHERE x > 10`.
# @returns [Query] The mutable query itself.
def clause(value)
@buffer << ' ' unless @buffer.end_with?(' ') || @buffer.empty?
@buffer << " " unless @buffer.end_with?(" ") || @buffer.empty?

@buffer << value

Expand All @@ -50,7 +50,7 @@ def clause(value)
# @parameter value [Object] Any kind of object, passed to the underlying database connection for conversion to a string representation.
# @returns [Query] The mutable query itself.
def literal(value)
@buffer << ' ' unless @buffer.end_with?(' ')
@buffer << " " unless @buffer.end_with?(" ")

@connection.append_literal(value, @buffer)

Expand All @@ -62,7 +62,7 @@ def literal(value)
# @parameter value [String | Symbol | DB::Identifier] Passed to the underlying database connection for conversion to a string representation.
# @returns [Query] The mutable query itself.
def identifier(value)
@buffer << ' ' unless @buffer.end_with?(' ')
@buffer << " " unless @buffer.end_with?(" ")

@connection.append_identifier(value, @buffer)

Expand Down
10 changes: 5 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ Please see the [project documentation](https://socketry.github.io/db/) for more

## See Also

- [db-postgres](https://github.com/socketry/db-postgres) - Postgres adapter for the DB gem.
- [db-mariadb](https://github.com/socketry/db-mariadb) - MariaDB/MySQL adapter for the DB gem.
- [db-model](https://github.com/socketry/db-model) - A simple object relational mapper (ORM) for the DB gem.
- [db-migrate](https://github.com/socketry/db-migrate) - Database migration tooling for the DB gem.
- [db-active_record](https://github.com/socketry/db-active_record) - An ActiveRecord adapter for the DB gem.
- [db-postgres](https://github.com/socketry/db-postgres) - Postgres adapter for the DB gem.
- [db-mariadb](https://github.com/socketry/db-mariadb) - MariaDB/MySQL adapter for the DB gem.
- [db-model](https://github.com/socketry/db-model) - A simple object relational mapper (ORM) for the DB gem.
- [db-migrate](https://github.com/socketry/db-migrate) - Database migration tooling for the DB gem.
- [db-active\_record](https://github.com/socketry/db-active_record) - An ActiveRecord adapter for the DB gem.

## Contributing

Expand Down
2 changes: 1 addition & 1 deletion test/db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Released under the MIT License.
# Copyright, 2020-2024, by Samuel Williams.

require 'db'
require "db"

describe DB do
it "has a version number" do
Expand Down
12 changes: 6 additions & 6 deletions test/db/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Released under the MIT License.
# Copyright, 2020-2024, by Samuel Williams.

require 'db/client_context'
require "db/client_context"

describe DB::Client do
DB::Adapters.each do |name, klass|
Expand Down Expand Up @@ -62,7 +62,7 @@
end
end

with 'events table' do
with "events table" do
before do
client.transaction do |transaction|
transaction.call("DROP TABLE IF EXISTS events")
Expand All @@ -71,23 +71,23 @@
end
end

it 'can insert rows with timestamps' do
it "can insert rows with timestamps" do
client.session do |session|
session.call("INSERT INTO events (created_at, description) VALUES ('2020-05-04 03:02:01', 'Hello World')")

rows = session.call('SELECT * FROM events') do |connection|
rows = session.call("SELECT * FROM events") do |connection|
connection.next_result.to_a
end

expect(rows).to be == [[1, Time.parse("2020-05-04 03:02:01 UTC"), "Hello World"]]
end
end

it 'can insert null fields' do
it "can insert null fields" do
client.session do |session|
session.call("INSERT INTO events (created_at, description) VALUES ('2020-05-04 03:02:01', NULL)")

rows = session.call('SELECT * FROM events') do |connection|
rows = session.call("SELECT * FROM events") do |connection|
connection.next_result.to_a
end

Expand Down
2 changes: 1 addition & 1 deletion test/db/datatype/datetime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Released under the MIT License.
# Copyright, 2024, by Samuel Williams.

require 'db/datatype_context'
require "db/datatype_context"

describe "datetime datatype" do
DB::Adapters.each do |name, klass|
Expand Down

0 comments on commit 54ff576

Please sign in to comment.