Class: PG::Migrator

Inherits:
Object
  • Object
show all
Defined in:
lib/pg-migrator.rb,
lib/pg-migrator/version.rb

Constant Summary collapse

VERSION =
"1.2.2"

Instance Method Summary collapse

Constructor Details

#initialize(conn, migrations_dir = './db/migrations', logger = Logger.new(STDOUT)) ⇒ Migrator

Returns a new instance of Migrator.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/pg-migrator.rb', line 8

def initialize(conn, migrations_dir = './db/migrations', logger = Logger.new(STDOUT))
  @pg = 
    if conn.is_a? PGconn
      conn 
    else
      PGconn.connect conn
    end
  @pg.exec 'SET client_min_messages = warning'
  @migrations_dir = migrations_dir
  @log = logger
end

Instance Method Details

#closeObject



77
78
79
# File 'lib/pg-migrator.rb', line 77

def close
  @pg.finish
end

#migrate_downObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pg-migrator.rb', line 61

def migrate_down
  @pg.transaction do |conn|
    applied = conn.exec 'SELECT id, name FROM migration ORDER BY applied DESC LIMIT 1'
    id = applied.first['id']
    name = applied.first['name']
    down_sql_file = "#{@migrations_dir}/#{id}_#{name}_down.sql"
    unless File.exists? down_sql_file
      @log.error "No down migration found for #{id} #{name} at #{down_sql_file}"
      return
    end
    @log.info "Applying #{File.basename down_sql_file}"
    conn.exec File.read down_sql_file
    conn.exec "DELETE FROM migration WHERE id = $1", [id]
  end
end

#migrate_upObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/pg-migrator.rb', line 39

def migrate_up
  @pg.transaction do |conn|
    exists = conn.exec "SELECT true FROM pg_tables WHERE tablename = 'migration'"
    unless exists.any?
      conn.exec "CREATE TABLE migration (
        id bigint PRIMARY KEY,
        name varchar(255),
        applied timestamp DEFAULT current_timestamp)"
    end
    applied = conn.exec 'SELECT id, name FROM migration ORDER BY id'
    Dir["#{@migrations_dir}/*.sql"].sort.each do |m|
      next if m.end_with? '_down.sql'
      id = m.sub /.*\/(\d+)_.*/, '\1'
      name = m.sub /.*\/\d+_([^\.]*).sql$/, '\1'
      next if applied.any? { |a| a['id'] == id }
      @log.info "Applying #{File.basename m}"
      conn.exec File.read m
      conn.exec 'INSERT INTO migration (id, name) VALUES ($1, $2)', [id, name]
    end
  end
end

#resetObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/pg-migrator.rb', line 20

def reset
  @pg.transaction do |conn|
    search_path = conn.exec("SHOW search_path").values.first.first
    current_user = conn.exec("SELECT CURRENT_USER").values.first.first
    schemas_sql = "SELECT nspname FROM pg_namespace 
           WHERE NOT(nspname LIKE 'pg_%') 
           AND nspname != 'information_schema'
    "
    schemas = conn.exec(schemas_sql).values.flatten
    search_path.split(',').each do |p|
      p = p.strip.sub /"\$user"/, current_user
      next unless schemas.include? p
      conn.exec "DROP SCHEMA #{p} CASCADE; 
                 CREATE SCHEMA #{p};"
    end
  end
  migrate_up
end