Class: DbMemoize::Migrations

Inherits:
Object
  • Object
show all
Defined in:
lib/db_memoize/migrations.rb

Class Method Summary collapse

Class Method Details

.create_tables(migration) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/db_memoize/migrations.rb', line 4

def create_tables(migration)
  migration.execute <<~SQL
    CREATE TABLE IF NOT EXISTS memoized_values (
      entity_table_name varchar NOT NULL,
      entity_id integer NOT NULL,
      method_name varchar NOT NULL,
      created_at timestamp without time zone NOT NULL
    );

    -- entity_id/entity_table_name should have a better chance to be useful, since
    -- there is more variance in entity_ids than there is in entity_table_names.
    DROP INDEX IF EXISTS index_memoized_values_on_entity_id_and_entity_table_name;
    DROP INDEX IF EXISTS index_memoized_values_on_entity_table_name_and_entity_id;
    CREATE UNIQUE INDEX IF NOT EXISTS memoized_attributes_idx2
      ON memoized_values(entity_id, entity_table_name, method_name);

    ALTER TABLE memoized_values DROP COLUMN IF EXISTS arguments_hash;
    ALTER TABLE memoized_values DROP COLUMN IF EXISTS value;

    ALTER TABLE memoized_values ADD COLUMN IF NOT EXISTS val_string varchar;
    ALTER TABLE memoized_values ADD COLUMN IF NOT EXISTS val_integer bigint;
    ALTER TABLE memoized_values ADD COLUMN IF NOT EXISTS val_float double precision;
    ALTER TABLE memoized_values ADD COLUMN IF NOT EXISTS val_time timestamp without time zone;
    ALTER TABLE memoized_values ADD COLUMN IF NOT EXISTS val_object jsonb;
    ALTER TABLE memoized_values ADD COLUMN IF NOT EXISTS val_boolean boolean;
    ALTER TABLE memoized_values ADD COLUMN IF NOT EXISTS val_nil boolean;
  SQL
end