Class: PGI::SchemaMigrator

Inherits:
Object
  • Object
show all
Defined in:
lib/pgi/schema_migrator.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version) ⇒ SchemaMigrator

Returns a new instance of SchemaMigrator.



13
14
15
16
17
18
19
# File 'lib/pgi/schema_migrator.rb', line 13

def initialize(version)
  raise "FATAL: version must be an integer > 0" unless version.is_a?(Integer) && version.positive?
  raise "FATAL: Duplication migration version" if self.class.migrations.key?(version)
  raise "FATAL: Broken migration ID sequence" unless version == self.class.migrations.keys.max + 1

  @version = version
end

Class Attribute Details

.configObject (readonly)

Returns the value of attribute config.



30
31
32
# File 'lib/pgi/schema_migrator.rb', line 30

def config
  @config
end

.migrationsObject (readonly)

Returns the value of attribute migrations.



30
31
32
# File 'lib/pgi/schema_migrator.rb', line 30

def migrations
  @migrations
end

Class Method Details

.configure {|config| ... } ⇒ Object

Yields:



32
33
34
35
36
# File 'lib/pgi/schema_migrator.rb', line 32

def configure
  yield(config)

  self
end

.current_versionObject



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/pgi/schema_migrator.rb', line 72

def current_version
  current = config.pg_conn.exec(<<~SQL).first
    SELECT * FROM schema_migrations
    ORDER BY version DESC LIMIT 1
  SQL

  (current && current["version"].to_i) || 0
rescue PG::UndefinedTable => e
  raise unless e.message =~ /relation "schema_migrations" does not exist/

  -1
end

.destroy!Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/pgi/schema_migrator.rb', line 85

def destroy!
  config.pg_conn.exec(<<~SQL)
    DO $$ DECLARE
      r RECORD;
    BEGIN
      FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = current_schema()) LOOP
        EXECUTE 'DROP TABLE IF EXISTS ' || quote_ident(r.tablename) || ' CASCADE';
      END LOOP;
      FOR r IN (SELECT DISTINCT typname FROM pg_type INNER JOIN pg_enum ON pg_enum.enumtypid = pg_type.oid) LOOP
        EXECUTE 'DROP TYPE IF EXISTS ' || quote_ident(r.typname) || ' CASCADE';
      END LOOP;
    END $$;
  SQL
end

.migrate!(version = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/pgi/schema_migrator.rb', line 38

def migrate!(version = nil)
  raise "FATAL: version must be an integer >= 0" unless version.nil? || (version.is_a?(Integer) && version >= 0)

  config.migration_files.sort.each { |file| require file }

  raise "FATAL: Migration version does not exist" unless version.nil? || migrations.key?(version)

  to_version = version || latest_migration
  current    = current_version

  if current == to_version
    puts "No migrations detected..."
    return
  end

  config.pg_conn.transaction do
    if to_version > current
      ((current + 1)..to_version).each do |v|
        config.pg_conn.exec(migrations[v][1])
        add_version(v)
      end
    else
      current.downto(to_version + 1) do |v|
        delete_version(v)
        config.pg_conn.exec(migrations[v][-1])
      end
    end
  end
end

.version(version) {|new(version)| ... } ⇒ Object

Yields:



68
69
70
# File 'lib/pgi/schema_migrator.rb', line 68

def version(version)
  yield(new(version))
end

Instance Method Details

#downObject



25
26
27
# File 'lib/pgi/schema_migrator.rb', line 25

def down
  self.class.migrations[@version][-1] = yield
end

#upObject



21
22
23
# File 'lib/pgi/schema_migrator.rb', line 21

def up
  self.class.migrations[@version][1] = yield
end