about summary refs log tree commit diff stats
path: root/db
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2018-01-14 13:06:18 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2018-01-14 13:06:18 -0500
commit30cd66e1521bc760df45908adb2f7d3ba6683900 (patch)
treeb730fbfc687516be2e4df95250d02598e9e7fa73 /db
parent91eeaa33a436ba2233f1fe7509f7c104db1d4533 (diff)
downloadpokeviewer-30cd66e1521bc760df45908adb2f7d3ba6683900.tar.gz
pokeviewer-30cd66e1521bc760df45908adb2f7d3ba6683900.tar.bz2
pokeviewer-30cd66e1521bc760df45908adb2f7d3ba6683900.zip
Cached a Pokémon's latest revision
This removes the need to use a GROUP or DISTINCT in eager-loading Pokémon on the front page. It duplicates data, but provisions are in place to keep the cache up-to-date. When a Pokémon record is updated, it is required that its cache points to the current revision. When a revision is created, it updates the Pokémon's cache to point to it, because a new revision will always be the most recent one, and it is impossible to reorder revisions.

This does not affect the number of queries used on the front page (see #1).

refs #4
Diffstat (limited to 'db')
-rw-r--r--db/migrate/20180114170238_cache_current_pokemon_revision.rb19
1 files changed, 19 insertions, 0 deletions
diff --git a/db/migrate/20180114170238_cache_current_pokemon_revision.rb b/db/migrate/20180114170238_cache_current_pokemon_revision.rb new file mode 100644 index 0000000..a0e1747 --- /dev/null +++ b/db/migrate/20180114170238_cache_current_pokemon_revision.rb
@@ -0,0 +1,19 @@
1class CacheCurrentPokemonRevision < ActiveRecord::Migration[5.1]
2 def up
3 change_table :pokeviewer_pokemon do |t|
4 t.references :current, null: true
5 end
6
7 add_foreign_key :pokeviewer_pokemon, :pokeviewer_revisions,
8 column: :current_id
9
10 Pokeviewer::Pokemon.all.each do |p|
11 p.current_id = p.revisions.last.id
12 p.save!
13 end
14 end
15
16 def down
17 remove_column :pokeviewer_pokemon, :current_id
18 end
19end