Class: Torque::PostgreSQL::VersionedCommands::SchemaTable
- Inherits:
-
Object
- Object
- Torque::PostgreSQL::VersionedCommands::SchemaTable
- Defined in:
- lib/torque/postgresql/versioned_commands/schema_table.rb
Instance Attribute Summary collapse
-
#arel_table ⇒ Object
readonly
Returns the value of attribute arel_table.
Instance Method Summary collapse
- #count ⇒ Object
- #create_table ⇒ Object
- #create_version(command) ⇒ Object
- #delete_version(command) ⇒ Object
- #drop_table ⇒ Object
-
#initialize(pool) ⇒ SchemaTable
constructor
A new instance of SchemaTable.
- #name ⇒ Object
- #primary_key ⇒ Object
- #table_exists? ⇒ Boolean
- #table_name ⇒ Object
- #versions_of(type) ⇒ Object
Constructor Details
#initialize(pool) ⇒ SchemaTable
Returns a new instance of SchemaTable.
9 10 11 12 |
# File 'lib/torque/postgresql/versioned_commands/schema_table.rb', line 9 def initialize(pool) @pool = pool @arel_table = ::Arel::Table.new(table_name) end |
Instance Attribute Details
#arel_table ⇒ Object (readonly)
Returns the value of attribute arel_table.
7 8 9 |
# File 'lib/torque/postgresql/versioned_commands/schema_table.rb', line 7 def arel_table @arel_table end |
Instance Method Details
#count ⇒ Object
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/torque/postgresql/versioned_commands/schema_table.rb', line 70 def count return 0 unless table_exists? sm = ::Arel::SelectManager.new(arel_table) sm.project(*FN.count(::Arel.star)) @pool.with_connection do |connection| connection.select_value(sm, "#{self.class} Count") end end |
#create_table ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/torque/postgresql/versioned_commands/schema_table.rb', line 52 def create_table @pool.with_connection do |connection| return if connection.table_exists?(table_name) parent = @pool.schema_migration.table_name connection.create_table(table_name, inherits: parent) do |t| t.string :type, null: false, index: true t.string :object_name, null: false, index: true end end end |
#create_version(command) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/torque/postgresql/versioned_commands/schema_table.rb', line 14 def create_version(command) im = ::Arel::InsertManager.new(arel_table) im.insert( arel_table[primary_key] => command.version, arel_table['type'] => command.type, arel_table['object_name'] => command.object_name, ) @pool.with_connection do |connection| connection.insert(im, "#{name} Create", primary_key, command.version) end end |
#delete_version(command) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/torque/postgresql/versioned_commands/schema_table.rb', line 27 def delete_version(command) dm = ::Arel::DeleteManager.new(arel_table) dm.wheres = [arel_table[primary_key].eq(command.version.to_s)] @pool.with_connection do |connection| connection.delete(dm, "#{name} Destroy") end end |
#drop_table ⇒ Object
64 65 66 67 68 |
# File 'lib/torque/postgresql/versioned_commands/schema_table.rb', line 64 def drop_table @pool.with_connection do |connection| connection.drop_table table_name, if_exists: true end end |
#name ⇒ Object
40 41 42 |
# File 'lib/torque/postgresql/versioned_commands/schema_table.rb', line 40 def name 'Torque::PostgreSQL::VersionedCommand' end |
#primary_key ⇒ Object
36 37 38 |
# File 'lib/torque/postgresql/versioned_commands/schema_table.rb', line 36 def primary_key 'version' end |
#table_exists? ⇒ Boolean
81 82 83 |
# File 'lib/torque/postgresql/versioned_commands/schema_table.rb', line 81 def table_exists? @pool.with_connection { |connection| connection.data_source_exists?(table_name) } end |
#table_name ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/torque/postgresql/versioned_commands/schema_table.rb', line 44 def table_name [ ActiveRecord::Base.table_name_prefix, PostgreSQL.config.versioned_commands.table_name, ActiveRecord::Base.table_name_suffix, ].join end |
#versions_of(type) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/torque/postgresql/versioned_commands/schema_table.rb', line 85 def versions_of(type) return [] unless table_exists? sm = ::Arel::SelectManager.new(arel_table) sm.project(arel_table['object_name'], FN.count(::Arel.star).as('version')) sm.where(arel_table['type'].eq(type.to_s)) sm.group(arel_table['object_name']) sm.order(arel_table['object_name'].asc) @pool.with_connection do |connection| connection.select_rows(sm, "#{name} Load") end end |