Skip to content

Commit

Permalink
rake: Add administrative tasks (#73)
Browse files Browse the repository at this point in the history
Co-authored-by: Paul Lemus <[email protected]>
  • Loading branch information
ethan-readyset and helpotters authored Jan 26, 2024
1 parent 9841c19 commit 065795e
Show file tree
Hide file tree
Showing 6 changed files with 329 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
# rspec failure tracking
.rspec_status
Gemfile.lock
spec/internal/db/readyset_caches.rb
spec/internal/tmp/
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ to your database. To create a cache for a specific query, you have a few options
Pick a query from the list that you'd like to cache, and pass the ID to the
`rails readyset:create_cache` command like so:
```sh
rails readyset:create_cache[your_query_id]
rails 'readyset:create_cache[your_query_id]'
```

Once a cache has been created for a particular query, it will persist on
Expand All @@ -268,7 +268,7 @@ rails readyset:caches
To drop a given cache in the list printed by the above command, you can pass the
name of the cache to the `readyset:caches:drop` Rake task like so:
```sh
rails readyset:caches:drop[my_cache]
rails 'readyset:caches:drop[my_cache]'
```
You can also view the list of existing caches in an interactive form via the
Rails console:
Expand Down
100 changes: 95 additions & 5 deletions lib/tasks/readyset.rake
Original file line number Diff line number Diff line change
@@ -1,13 +1,87 @@
require 'colorize'
require 'erb'
require 'progressbar'
require 'terminal-table'

namespace :readyset do
desc 'Creates a cache from the given query ID'
task :create_cache, [:id] => :environment do |_, args|
if args.key?(:id)
Readyset.create_cache!(id: args[:id])
else
Rails.logger.error 'A query ID must be passed to this task'
end
end

desc 'Creates a cache from the given query ID whose queries will never fall back to the ' \
'primary database'
task :create_cache_always, [:id] => :environment do |_, args|
if args.key?(:id)
Readyset.create_cache!(id: args[:id], always: true)
else
Rails.logger.error 'A query ID must be passed to this task'
end
end

desc 'Prints a list of all the queries that ReadySet has proxied'
task proxied_queries: :environment do
rows = Readyset::Query::ProxiedQuery.all.map do |q|
[q.id, q.text, q.supported, q.count]
end
table = Terminal::Table.new(headings: [:id, :text, :supported, :count], rows: rows)

Rails.logger.info table.to_s
end

namespace :proxied_queries do
desc 'Creates caches for all of the supported queries on ReadySet'
task cache_all_supported: :environment do
Readyset::Query::ProxiedQuery.cache_all_supported!
end

desc 'Clears the list of proxied queries on ReadySet'
task drop_all: :environment do
Readyset.raw_query('DROP ALL PROXIED QUERIES'.freeze)
end

desc 'Prints a list of all the queries that ReadySet has proxied that can be cached'
task supported: :environment do
rows = Readyset::Query::ProxiedQuery.all.
select { |query| query.supported == :yes }.
map { |q| [q.id, q.text, q.count] }
table = Terminal::Table.new(headings: [:id, :text, :count], rows: rows)

Rails.logger.info table.to_s
end
end

desc 'Prints a list of all the cached queries on ReadySet'
task caches: :environment do
rows = Readyset::Query::CachedQuery.all.map do |q|
[q.id, q.name, q.text, q.always, q.count]
end
table = Terminal::Table.new(headings: [:id, :name, :text, :always, :count], rows: rows)

Rails.logger.info table.to_s
end

namespace :caches do
desc 'Drops the cache with the given name'
task :drop, [:name] => :environment do |_, args|
if args.key?(:name)
Readyset.drop_cache!(args[:name])
else
Rails.logger.error 'A cache name must be passed to this task'
end
end

desc 'Drops all the caches on ReadySet'
task drop_all: :environment do
Readyset::Query::CachedQuery.drop_all!
end

desc 'Dumps the set of caches that currently exist on ReadySet to a file'
task dump: :environment do
Rails.application.eager_load!

template = File.read(File.join(File.dirname(__FILE__), '../templates/caches.rb.tt'))

queries = Readyset::Query::CachedQuery.all
Expand All @@ -20,8 +94,6 @@ namespace :readyset do
desc 'Synchronizes the caches on ReadySet such that the caches on ReadySet match those ' \
'listed in db/readyset_caches.rb'
task migrate: :environment do
Rails.application.eager_load!

file = Readyset.configuration.migration_path

# We load the definition of the `Readyset::Caches` subclass in the context of a
Expand Down Expand Up @@ -65,8 +137,26 @@ namespace :readyset do
end
end
else
puts 'Nothing to do'
Rails.logger.info 'Nothing to do'
end
end
end

desc 'Prints status information about ReadySet'
task status: :environment do
rows = Readyset.raw_query('SHOW READYSET STATUS'.freeze).
map { |result| [result['name'], result['value']] }
table = Terminal::Table.new(rows: rows)

Rails.logger.info table.to_s
end

desc 'Prints information about the tables known to ReadySet'
task tables: :environment do
rows = Readyset.raw_query('SHOW READYSET TABLES'.freeze).
map { |result| [result['table'], result['status'], result['description']] }
table = Terminal::Table.new(headings: [:table, :status, :description], rows: rows)

Rails.logger.info table.to_s
end
end
1 change: 1 addition & 0 deletions readyset.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Gem::Specification.new do |spec|
spec.add_dependency 'concurrent-ruby', '~> 1.2'
spec.add_dependency 'progressbar', '~> 1.13'
spec.add_dependency 'rake', '~> 13.0'
spec.add_dependency 'terminal-table', '~> 3.0'

spec.add_development_dependency 'combustion', '~> 1.3'
spec.add_development_dependency 'factory_bot', '~> 6.4'
Expand Down
Loading

0 comments on commit 065795e

Please sign in to comment.