class Migrate::Storage::Postgres

Public Class Methods

new(*args) click to toggle source
Calls superclass method Migrate::Storage::DB::new
# File lib/migrate/storage/postgres.rb, line 7
def initialize(*args)
  super
  @conn = PG.connect({
    dbname: @config.database,
    host: @config.host,
    user: @config.user,
    password: @config.password,
  })
end

Public Instance Methods

create_tables() click to toggle source
# File lib/migrate/storage/postgres.rb, line 37
      def create_tables
        Log.info("Creating version table")
        self.exec_sql <<-eos
        CREATE TABLE #{@config.version_info}
        (
          version SERIAL PRIMARY KEY NOT NULL,
          description TEXT,
          created_date TIMESTAMP WITH TIME ZONE NOT NULL,
          last_up TIMESTAMP WITH TIME ZONE,
          last_down TIMESTAMP WITH TIME ZONE
        );
        CREATE UNIQUE INDEX #{@config.version_info}_version_uindex ON #{@config.version_info} (version);

        CREATE TABLE #{@config.version_number}
        (
          version INT PRIMARY KEY NOT NULL
        );

        INSERT INTO #{@config.version_number} VALUES(0);
        eos
        Log.success("Version table created")
      end
exec_sql(sql) click to toggle source
# File lib/migrate/storage/postgres.rb, line 69
def exec_sql(sql)
  @tx.exec sql
end
extract_version(results) click to toggle source
# File lib/migrate/storage/postgres.rb, line 61
def extract_version(results)
  if results && results.count > 0
    results[0]["version"]
  else
    raise VersionNotFound
  end
end
has_tx() click to toggle source
# File lib/migrate/storage/postgres.rb, line 73
def has_tx
  @tx != nil
end
tables_exists?() click to toggle source
# File lib/migrate/storage/postgres.rb, line 17
      def tables_exists?
        vi = self.exec_sql <<-eos
          SELECT EXISTS (
            SELECT 1
            FROM information_schema.tables
            WHERE table_name = '#{@config.version_info}'
          );
        eos

        vn = self.exec_sql <<-eos
          SELECT EXISTS (
            SELECT 1
            FROM information_schema.tables
            WHERE table_name = '#{@config.version_number}'
          );
        eos

        vi[0]["exists"].to_b && vn[0]["exists"].to_b
      end
tx() { || ... } click to toggle source
# File lib/migrate/storage/postgres.rb, line 77
def tx
  if has_tx
    yield
  else
    begin
      @conn.transaction do |tx|
        @tx = tx
        yield
      end
    ensure
      @tx = nil
    end
  end
end