Class: PGTrunk::Registry

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/pg_trunk/core/registry.rb

Overview

The internal model to represent the gem-specific registry where we store information about objects added by migrations.

Every time when an object is created, we should record it in the table, setting its oid along with the reference to the system table (classid::oid).

The third column version::text keeps the current version where the object has been added.

rubocop: disable Metrics/ClassLength

Class Method Summary collapse

Class Method Details

._internal?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/pg_trunk/core/registry.rb', line 18

def _internal?
  true
end

.create_tableObject

rubocop: disable Metrics/MethodLength



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/pg_trunk/core/registry.rb', line 31

def create_table
  return if connection.table_exists?(table_name)

  connection.create_table(
    table_name,
    id: false,
    if_not_exists: true,
    comment: "Objects added by migrations",
  ) do |t|
    t.column :oid, :oid, primary_key: true, comment: "Object identifier"
    t.column :classid, :oid, null: false, comment: \
             "ID of the systems catalog in pg_class"
    t.column :version, :string, index: true, comment: \
             "Version of the migration that added the object"
    t.foreign_key ActiveRecord::Base.schema_migrations_table_name,
                  column: :version, primary_key: :version,
                  on_update: :cascade, on_delete: :cascade
  end
end

.drop_tableObject



63
64
65
# File 'lib/pg_trunk/core/registry.rb', line 63

def drop_table
  connection.drop_table table_name, if_exists: true
end

.finalizeObject

This method is called by a migrator after applying all migrations in whatever direction.



54
55
56
57
58
59
60
61
# File 'lib/pg_trunk/core/registry.rb', line 54

def finalize
  connection.execute [
    *create_table,
    *forget_dropped_objects,
    *remember_tables,
    *fill_missed_version,
  ].join(";")
end

.primary_keyObject



22
23
24
# File 'lib/pg_trunk/core/registry.rb', line 22

def primary_key
  "oid"
end

.table_nameObject



26
27
28
# File 'lib/pg_trunk/core/registry.rb', line 26

def table_name
  "pg_trunk"
end