diff --git a/lib/tasks/readyset.rake b/lib/tasks/readyset.rake index 93f9aab..df21c27 100644 --- a/lib/tasks/readyset.rake +++ b/lib/tasks/readyset.rake @@ -4,6 +4,29 @@ require 'progressbar' require 'terminal-table' namespace :readyset do + desc 'Creates a cache from the given query ID' + task :create_cache, [:id] => :environment do |_, args| + Rails.application.eager_load! + + if args.first + Readyset.create_cache!(id: args.first) + else + puts '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| + Rails.application.eager_load! + + if args.first + Readyset.create_cache!(id: args.first, always: true) + else + puts '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 Rails.application.eager_load! diff --git a/spec/rake_spec.rb b/spec/rake_spec.rb index c63e62a..c53bef3 100644 --- a/spec/rake_spec.rb +++ b/spec/rake_spec.rb @@ -10,6 +10,48 @@ end describe 'readyset' do + describe 'create_cache' do + context 'when given a query ID as an argument' do + it 'creates a cache from the given query' do + query = build_and_execute_proxied_query(:proxied_query) + build_and_execute_proxied_query(:proxied_query_2) + + Rake::Task['readyset:create_cache'].execute([query.id]) + + caches = Readyset::Query::CachedQuery.all + expect(caches).to eq([build(:cached_query)]) + end + end + + context 'when given no arguments' do + it 'prints an error message' do + expect { Rake::Task['readyset:create_cache'].execute }. + to output("A query ID must be passed to this task\n").to_stdout + end + end + end + + describe 'create_cache_always' do + context 'when given a query ID as an argument' do + it 'creates a cache from the given query with `always` set to true' do + query = build_and_execute_proxied_query(:proxied_query) + build_and_execute_proxied_query(:proxied_query_2) + + Rake::Task['readyset:create_cache_always'].execute([query.id]) + + caches = Readyset::Query::CachedQuery.all + expect(caches).to eq([build(:cached_query, always: true)]) + end + end + + context 'when given no arguments' do + it 'prints an error message' do + expect { Rake::Task['readyset:create_cache_always'].execute }. + to output("A query ID must be passed to this task\n").to_stdout + end + end + end + describe 'caches' do it 'prints a table with the caches that currently exist on ReadySet' do build_and_create_cache(:cached_query)