Module: PgShrink

Defined in:
lib/pg_shrink.rb,
lib/pg_shrink/table.rb,
lib/pg_shrink/version.rb,
lib/pg_shrink/database.rb,
lib/pg_shrink/table_filter.rb,
lib/pg_shrink/table_sanitizer.rb,
lib/pg_shrink/sub_table_filter.rb,
lib/pg_shrink/database/postgres.rb,
lib/pg_shrink/sub_table_operator.rb,
lib/pg_shrink/sub_table_sanitizer.rb

Defined Under Namespace

Classes: Database, SubTableFilter, SubTableOperator, SubTableSanitizer, Table, TableFilter, TableSanitizer

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.blank_optionsObject



18
19
20
21
22
23
24
25
26
# File 'lib/pg_shrink.rb', line 18

def self.blank_options
  {
    url: nil,
    config: 'Shrinkfile',
    force: false,
    batch_size: 10000,
    log: true,
  }
end

.run(options) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/pg_shrink.rb', line 47

def self.run(options)
  unless File.exists?(options[:config])
    if options[:config] == 'Shrinkfile'
      abort("Error loading Shrinkfile: " +
            "Please specify location using -c <path/to/Shrinkfile>")
    else
      abort("Error loading Shrinkfile: " +
            "Could not find file at: #{options[:config]}")
    end
  end

  validate_pg_url!(options[:url])
  batch_size = options[:batch_size].to_i
  unless batch_size >= 1
    abort("Batch size must be at least 1.  #{options[:batch_size]} is invalid!")
  end

  database = PgShrink::Database::Postgres.new(postgres_url: options[:url],
                                              batch_size: batch_size,
                                              log: options[:log])

  database.instance_eval(File.read(options[:config]), options[:config], 1)

  # TODO: Figure out how to write a spec for this.
  unless options[:force] == true
    puts 'WARNING:  pg_shrink is destructive!  It will change this database in place.'
    puts 'Are you sure you want to continue? (y/N)'
    cont = gets
    cont = cont.strip
    unless cont == 'y' || cont == 'Y'
      abort('Aborting!')
    end
  end

  database.shrink!
end

.validate_pg_url!(url) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pg_shrink.rb', line 28

def self.validate_pg_url!(url)
  if url.blank?
    abort("Error loading postgres: " +
          "Please specify postgres url using -u <postgres_url>")
  end
  uri = URI.parse(url)
  if uri.scheme == 'postgres' && uri.path != '/'
    return true
  else
    abort("Error loading postgres: " +
          "#{url} is not a valid postgres url")
  end
rescue => ex
  abort(
    "Error loading postgres: " +
    "#{url} is not a valid postgres url"
  )
end