Class: PGWarehouse

Inherits:
Object show all
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pg-1.4.5/sample/warehouse_partitions.rb

Overview

A tablespace migration class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ PGWarehouse



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pg-1.4.5/sample/warehouse_partitions.rb', line 51

def initialize( opts )
  @opts = opts
  @db = PG.connect(
    :dbname   => opts.database,
    :host     => opts.host,
    :port     => opts.port,
    :user     => opts.user,
    :password => opts.pass,
    :sslmode  => 'prefer'
  )
  @db.exec "SET search_path TO %s" % [ opts.schema ] if opts.schema

  @relations = self.relations
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



66
67
68
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pg-1.4.5/sample/warehouse_partitions.rb', line 66

def db
  @db
end

Instance Method Details

#migrateObject

Perform the tablespace moves.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pg-1.4.5/sample/warehouse_partitions.rb', line 74

def migrate
  if @relations.empty?
    $stderr.puts 'No tables were found for warehousing.'
    return
  end

  $stderr.puts "Found %d relation%s to move." % [ relations.length, relations.length == 1 ? '' : 's' ]
  @relations.sort_by{|_,v| v[:name] }.each do |_, val|
    $stderr.print "  - Moving table '%s' to '%s'... "  % [
      val[:name], @opts.tablespace
    ]

    if @opts.dryrun
      $stderr.puts '(not really)'

    else
      age = self.timer do
        db.exec "ALTER TABLE %s SET TABLESPACE %s;" % [
          val[:name], @opts.tablespace
        ]
      end
      puts age
    end

    val[ :indexes ].each do |idx|
      $stderr.print "      - Moving index '%s' to '%s'... "  % [
        idx, @opts.tablespace
      ]
      if @opts.dryrun
        $stderr.puts '(not really)'

      else
        age = self.timer do
          db.exec "ALTER INDEX %s SET TABLESPACE %s;" % [
            idx, @opts.tablespace
          ]
        end
        puts age
      end
    end
  end
end