From 54ff576f253e07d04c2bfa009ce4862076335127 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Sun, 22 Sep 2024 10:55:47 +1200 Subject: [PATCH] Modernize gem. --- .github/workflows/test-coverage.yaml | 6 ++++-- .rubocop.yml | 7 ++++++ benchmark/benchmark.rb | 32 ++++++++++++++-------------- benchmark/compare/config.ru | 12 +++++------ config/sus.rb | 10 ++++----- db.gemspec | 6 +++--- examples/indexes.rb | 8 +++---- examples/model.rb | 2 +- fixtures/db/client_context.rb | 6 +++--- fixtures/db/datatype_context.rb | 2 +- lib/db.rb | 6 +++--- lib/db/client.rb | 6 +++--- lib/db/context/session.rb | 4 ++-- lib/db/context/transaction.rb | 2 +- lib/db/query.rb | 6 +++--- readme.md | 10 ++++----- test/db.rb | 2 +- test/db/client.rb | 12 +++++------ test/db/datatype/datetime.rb | 2 +- 19 files changed, 75 insertions(+), 66 deletions(-) diff --git a/.github/workflows/test-coverage.yaml b/.github/workflows/test-coverage.yaml index f3f52c4..5d57aaa 100644 --- a/.github/workflows/test-coverage.yaml +++ b/.github/workflows/test-coverage.yaml @@ -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 @@ -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 diff --git a/.rubocop.yml b/.rubocop.yml index 442c667..3b8d476 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -16,6 +16,9 @@ Layout/IndentationConsistency: Enabled: true EnforcedStyle: normal +Layout/BlockAlignment: + Enabled: true + Layout/EndAlignment: Enabled: true EnforcedStyleAlignWith: start_of_line @@ -44,3 +47,7 @@ Layout/EmptyLinesAroundModuleBody: Style/FrozenStringLiteralComment: Enabled: true + +Style/StringLiterals: + Enabled: true + EnforcedStyle: double_quotes diff --git a/benchmark/benchmark.rb b/benchmark/benchmark.rb index 90ab0a5..ef9ef40 100644 --- a/benchmark/benchmark.rb +++ b/benchmark/benchmark.rb @@ -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 @@ -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})") @@ -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})") @@ -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 @@ -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 diff --git a/benchmark/compare/config.ru b/benchmark/compare/config.ru index 754ec33..20a28b9 100644 --- a/benchmark/compare/config.ru +++ b/benchmark/compare/config.ru @@ -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}) *************" @@ -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) diff --git a/config/sus.rb b/config/sus.rb index af0ab98..695fe29 100644 --- a/config/sus.rb +++ b/config/sus.rb @@ -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: diff --git a/db.gemspec b/db.gemspec index f496b3e..0697633 100644 --- a/db.gemspec +++ b/db.gemspec @@ -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" @@ -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" diff --git a/examples/indexes.rb b/examples/indexes.rb index a18ee6b..d40aaeb 100755 --- a/examples/indexes.rb +++ b/examples/indexes.rb @@ -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 diff --git a/examples/model.rb b/examples/model.rb index 9db48ce..67bc156 100644 --- a/examples/model.rb +++ b/examples/model.rb @@ -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) diff --git a/fixtures/db/client_context.rb b/fixtures/db/client_context.rb index 211c29c..2d7cf85 100644 --- a/fixtures/db/client_context.rb +++ b/fixtures/db/client_context.rb @@ -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| diff --git a/fixtures/db/datatype_context.rb b/fixtures/db/datatype_context.rb index c59fbaa..2977a05 100644 --- a/fixtures/db/datatype_context.rb +++ b/fixtures/db/datatype_context.rb @@ -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| diff --git a/lib/db.rb b/lib/db.rb index 5cdeaf0..596bf22 100644 --- a/lib/db.rb +++ b/lib/db.rb @@ -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" diff --git a/lib/db/client.rb b/lib/db/client.rb index a7a18b2..c276840 100644 --- a/lib/db/client.rb +++ b/lib/db/client.rb @@ -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. diff --git a/lib/db/context/session.rb b/lib/db/context/session.rb index 2753e01..4670858 100644 --- a/lib/db/context/session.rb +++ b/lib/db/context/session.rb @@ -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 diff --git a/lib/db/context/transaction.rb b/lib/db/context/transaction.rb index 0a1a439..5098f0b 100644 --- a/lib/db/context/transaction.rb +++ b/lib/db/context/transaction.rb @@ -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 diff --git a/lib/db/query.rb b/lib/db/query.rb index 6f83470..c0b07c7 100644 --- a/lib/db/query.rb +++ b/lib/db/query.rb @@ -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 @@ -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) @@ -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) diff --git a/readme.md b/readme.md index ce4b81d..f7bf521 100644 --- a/readme.md +++ b/readme.md @@ -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 diff --git a/test/db.rb b/test/db.rb index 8a09e4d..21bb6e0 100644 --- a/test/db.rb +++ b/test/db.rb @@ -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 diff --git a/test/db/client.rb b/test/db/client.rb index 57eb9cc..dbdfe5c 100644 --- a/test/db/client.rb +++ b/test/db/client.rb @@ -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| @@ -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") @@ -71,11 +71,11 @@ 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 @@ -83,11 +83,11 @@ 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 diff --git a/test/db/datatype/datetime.rb b/test/db/datatype/datetime.rb index de27401..3903ac1 100644 --- a/test/db/datatype/datetime.rb +++ b/test/db/datatype/datetime.rb @@ -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|