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

treewide: Rename ReadySet to Readyset #39

Merged
merged 1 commit into from
Nov 30, 2023
Merged
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 @@ -2,7 +2,7 @@

TODO: Delete this and the text below, and describe your gem

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ready_set`. To experiment with that code, run `bin/console` for an interactive prompt.
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/readyset`. To experiment with that code, run `bin/console` for an interactive prompt.

## Installation

Expand Down
2 changes: 1 addition & 1 deletion bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# frozen_string_literal: true

require 'bundler/setup'
require 'ready_set'
require 'readyset'

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
Expand Down
16 changes: 8 additions & 8 deletions lib/ready_set.rb → lib/readyset.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# lib/ready_set.rb
# lib/readyset.rb

require 'ready_set/configuration'
require 'ready_set/default_resolver'
require 'ready_set/middleware'
require 'ready_set/query'
require 'ready_set/railtie' if defined?(Rails::Railtie)
require 'readyset/configuration'
require 'readyset/default_resolver'
require 'readyset/middleware'
require 'readyset/query'
require 'readyset/railtie' if defined?(Rails::Railtie)

require 'active_record'

module ReadySet
module Readyset
attr_writer :configuration

def self.configuration
Expand All @@ -28,7 +28,7 @@ def self.current_config
# @param [Array<Object>] *sql_array the SQL array to be executed against ReadySet
# @return [PG::Result]
def self.raw_query(*sql_array)
ActiveRecord::Base.establish_connection(ReadySet.configuration.connection_url)
ActiveRecord::Base.establish_connection(Readyset.configuration.connection_url)
ActiveRecord::Base.connection.execute(ActiveRecord::Base.sanitize_sql_array(sql_array))
end
end
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# lib/ready_set/configuration.rb
# lib/readyset/configuration.rb

module ReadySet
module Readyset
class Configuration
attr_accessor :connection_url, :database_selector, :database_resolver,
:database_resolver_context

def initialize
@connection_url = ENV['READYSET_URL'] || default_connection_url
@database_selector = { delay: 2.seconds }
@database_resolver = ReadySet::DefaultResolver
@database_resolver = Readyset::DefaultResolver
@database_resolver_context = nil
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# lib/ready_set/default_resolver.rb
# lib/readyset/default_resolver.rb

require 'active_record'

module ReadySet
module Readyset
class DefaultResolver < ActiveRecord::Middleware::DatabaseSelector::Resolver
def read_from_replica?(session, &block)
# TODO: Implement good defaults for resolving requests
Expand Down
8 changes: 4 additions & 4 deletions lib/ready_set/middleware.rb → lib/readyset/middleware.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# lib/ready_set/middleware.rb
# lib/readyset/middleware.rb

# Mentioned in the docs:
# The core time extension is necessary for the default 2-second delay
require 'active_support/core_ext/integer/time'
require 'action_dispatch'

module ReadySet
module Readyset
class Middleware
def initialize(app)
@app = app
@resolver_klass = ReadySet.configuration.database_resolver ||
@resolver_klass = Readyset.configuration.database_resolver ||
ActiveRecord::Middleware::DatabaseSelector::Resolver
@context_klass = ReadySet.configuration.database_resolver_context ||
@context_klass = Readyset.configuration.database_resolver_context ||
ActiveRecord::Middleware::DatabaseSelector::Resolver::Session
end

Expand Down
38 changes: 19 additions & 19 deletions lib/ready_set/query.rb → lib/readyset/query.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# lib/ready_set/query.rb
# lib/readyset/query.rb

require 'active_model'

module ReadySet
module Readyset
# Represents a query that has been seen by ReadySet. This query may be cached or uncached.
class Query
include ActiveModel::AttributeMethods

# An error raised when a `ReadySet::Query` is expected to be cached but isn't.
# An error raised when a `Readyset::Query` is expected to be cached but isn't.
class NotCachedError < StandardError
attr_reader :id

Expand All @@ -20,7 +20,7 @@ def to_s
end
end

# An error raised when a `ReadySet::Query` with the given ID can't be found on the ReadySet
# An error raised when a `Readyset::Query` with the given ID can't be found on the ReadySet
# instance.
class NotFoundError < StandardError
attr_reader :id
Expand All @@ -39,59 +39,59 @@ def to_s
# Returns all of the queries currently cached on ReadySet by invoking the `SHOW CACHES` SQL
# extension on ReadySet.
#
# @return [Array<ReadySet::Query>]
# @return [Array<Readyset::Query>]
def self.all_cached
ReadySet.raw_query('SHOW CACHES').map { |result| new(result) }
Readyset.raw_query('SHOW CACHES').map { |result| new(result) }
end

# Returns all of the queries seen by ReadySet that are not currently cached. This list is
# retrieved by invoking the `SHOW PROXIED QUERIES` SQL extension on ReadySet.
#
# @return [Array<ReadySet::Query>]
# @return [Array<Readyset::Query>]
def self.all_seen_but_not_cached
ReadySet.raw_query('SHOW PROXIED QUERIES').map { |result| new(result) }
Readyset.raw_query('SHOW PROXIED QUERIES').map { |result| new(result) }
end

# Finds the query with the given query ID by directly querying ReadySet. If a query with the
# given ID doesn't exist, this method raises a `ReadySet::Query::NotFoundError`.
# given ID doesn't exist, this method raises a `Readyset::Query::NotFoundError`.
#
# @param [String] id the ID of the query to be searched for
# @return [Query]
# @raise [ReadySet::Query::NotFoundError] raised if a query with the given ID cannot be found
# @raise [Readyset::Query::NotFoundError] raised if a query with the given ID cannot be found
def self.find(id)
find_seen_but_not_cached(id)
rescue NotFoundError
find_cached(id)
end

# Returns the cached query with the given query ID by directly querying ReadySet. If a cached
# query with the given ID doesn't exist, this method raises a `ReadySet::Query::NotFoundError`.
# query with the given ID doesn't exist, this method raises a `Readyset::Query::NotFoundError`.
#
# @param [String] id the ID of the query to be searched for
# @return [Query]
# @raise [ReadySet::Query::NotFoundError] raised if a cached query with the given ID cannot be
# @raise [Readyset::Query::NotFoundError] raised if a cached query with the given ID cannot be
# found
def self.find_cached(id)
find_inner('SHOW CACHES WHERE query_id = ?', id)
end

# Returns the query with the given query ID that has been seen by ReadySet but is not cached.
# The query is searched for by directly querying ReadySet. If a seen-but-not-cached query with
# the given ID doesn't exist, this method raises a `ReadySet::Query::NotFoundError`.
# the given ID doesn't exist, this method raises a `Readyset::Query::NotFoundError`.
#
# @param [String] id the ID of the query to be searched for
# @return [Query]
# @raise [ReadySet::Query::NotFoundError] raised if a seen-but-not-cached query with the given
# @raise [Readyset::Query::NotFoundError] raised if a seen-but-not-cached query with the given
# ID cannot be found
def self.find_seen_but_not_cached(id)
find_inner('SHOW PROXIED QUERIES WHERE query_id = ?', id)
end

# Constructs a new `ReadySet::Query` from the given hash. The keys of this hash should
# Constructs a new `Readyset::Query` from the given hash. The keys of this hash should
# correspond to the columns in the results returned by the `SHOW PROXIED QUERIES` and
# `SHOW CACHES` ReadySet SQL extensions.
#
# @param [Hash] attributes the attributes from which the `ReadySet::Query` should be
# @param [Hash] attributes the attributes from which the `Readyset::Query` should be
# constructed
# @return [Query]
def initialize(attributes)
Expand All @@ -112,10 +112,10 @@ def cached?

# Returns true if the cached query supports falling back to the upstream database and false
# otherwise. If the query is not cached, this method raises a
# `ReadySet::Query::NotCachedError`.
# `Readyset::Query::NotCachedError`.
#
# @return [Boolean]
# @raise [ReadySet::Query::NotCachedError]
# @raise [Readyset::Query::NotCachedError]
def fallback_allowed?
if cached?
@fallback_behavior == 'fallback allowed'.to_sym
Expand Down Expand Up @@ -147,7 +147,7 @@ def reload
private

def self.find_inner(query, id)
result = ReadySet.raw_query(query, id).first
result = Readyset.raw_query(query, id).first

if result.nil?
raise NotFoundError, id
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

module ReadySet
module Readyset
module Rails
VERSION = '0.1.0'
end
Expand Down
6 changes: 3 additions & 3 deletions lib/ready_set/railtie.rb → lib/readyset/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# lib/ready_set/railtie.rb
# lib/readyset/railtie.rb

module ReadySet
module Readyset
class Railtie < Rails::Railtie
initializer 'readyset.configure_rails_initialization' do |app|
app.middleware.use ReadySet::Middleware
app.middleware.use Readyset::Middleware
end
end
end
2 changes: 1 addition & 1 deletion lib/ready_set/version.rb → lib/readyset/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module ReadySet
module Readyset
VERSION = '0.1.0'
end
4 changes: 2 additions & 2 deletions readyset.gemspec
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# frozen_string_literal: true

require_relative 'lib/ready_set/version'
require_relative 'lib/readyset/version'

Gem::Specification.new do |spec|
spec.name = 'readyset'
spec.version = ReadySet::VERSION
spec.version = Readyset::VERSION
spec.authors = ['ReadySet Technology, Inc.']
spec.email = ['info@readyset.io']

Expand Down
2 changes: 1 addition & 1 deletion sig/readyset.rbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module ReadySet
module Readyset
VERSION: String
# See the writing guide of rbs: https://github.com/ruby/rbs#guides
end
8 changes: 4 additions & 4 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
# spec/readyset-rails/configuration_spec.rb

require 'spec_helper'
require_relative './../lib/ready_set/configuration.rb'
require_relative './../lib/readyset/configuration.rb'

RSpec.describe ReadySet::Configuration do
RSpec.describe Readyset::Configuration do
let(:config) { described_class.new }

describe '#initialize' do
it 'sets default values' do
config = ReadySet::Configuration.new
config = Readyset::Configuration.new

expect(config.connection_url).
to eq(ENV['READYSET_URL'] || 'postgres://user:password@localhost:5432/readyset')
expect(config.database_selector).to eq({ delay: 2.seconds })
expect(config.database_resolver).to eq(ReadySet::DefaultResolver)
expect(config.database_resolver).to eq(Readyset::DefaultResolver)
expect(config.database_resolver_context).to be_nil
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/default_resolver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
# spec/readyset/default_resolver_spec.rb

require 'spec_helper'
require_relative './../lib/ready_set/default_resolver'
require_relative './../lib/readyset/default_resolver'

RSpec.describe ReadySet::DefaultResolver do
RSpec.describe Readyset::DefaultResolver do
describe '#read_from_replica?' do
let(:resolver) { ReadySet::DefaultResolver.new({ delay: 2.seconds }) }
let(:resolver) { Readyset::DefaultResolver.new({ delay: 2.seconds }) }

it 'returns true by default' do
expect(resolver.read_from_replica?(nil)).to be true
Expand Down
6 changes: 3 additions & 3 deletions spec/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
# spec/readyset/middleware_spec.rb

require 'spec_helper'
require_relative './../lib/ready_set/middleware'
require_relative './../lib/readyset/middleware'

RSpec.describe ReadySet::Middleware do
RSpec.describe Readyset::Middleware do
let(:app) { double('App', call: true) }
let(:env) { {} }
let(:middleware) { ReadySet::Middleware.new(app) }
let(:middleware) { Readyset::Middleware.new(app) }

it 'initializes with an app' do
expect(middleware.instance_variable_get(:@app)).to eq(app)
Expand Down
Loading