module Postshift::Schema

Constants

ADMIN_UTILITIES
FILENAME

Public Class Methods

admin_uilities_exists?() click to toggle source
# File lib/postshift/schema.rb, line 24
def self.admin_uilities_exists?
  ensure_admin_schema
  ADMIN_UTILITIES.each do |table_name|
    Postshift.connection.exec("SELECT 'admin.#{table_name}'::regclass;")
  end
  true
rescue PG::UndefinedTable
  false
end
create_admin_utilities!() click to toggle source
# File lib/postshift/schema.rb, line 10
def self.create_admin_utilities!
  ensure_admin_schema
  ADMIN_UTILITIES.each do |table_name|
    Postshift.connection.exec(generate_ddl_sql(table_name))
  end
end
ddl_results(ddl_sql) click to toggle source
# File lib/postshift/schema.rb, line 64
def self.ddl_results(ddl_sql)
  Postshift.connection.exec(ddl_sql, schemas)
end
dump() click to toggle source
# File lib/postshift/schema.rb, line 39
def self.dump
  File.open(output_location, 'w+') do |file|
    ddl_results(tbl_ddl_sql).each_row do |row|
      file.puts(row)
    end
    ddl_results(view_ddl_sql).each_row do |row|
      file.puts(row)
    end
  end
end
dump_sql() click to toggle source
# File lib/postshift/schema.rb, line 50
def self.dump_sql
  if File.exist?(output_location)
    File.read(output_location)
  else
    puts 'Postshift Schema Dump file does not exist. Run task postshift:schema:dump'
    false
  end
end
ensure_admin_schema() click to toggle source
# File lib/postshift/schema.rb, line 6
def self.ensure_admin_schema
  Postshift.connection.exec('CREATE SCHEMA IF NOT EXISTS admin')
end
generate_ddl_sql(table_name) click to toggle source
# File lib/postshift/schema.rb, line 34
def self.generate_ddl_sql(table_name)
  path = File.join(Postshift.root, 'lib', 'tasks', "#{table_name}.sql")
  File.open(path).read
end
output_location() click to toggle source
# File lib/postshift/schema.rb, line 68
def self.output_location
  if defined?(Rails)
    File.join(Rails.root, 'db', FILENAME)
  else
    base_path = File.join(Postshift.root, 'tmp')
    Dir.mkdir(base_path) unless Dir.exist?(base_path)
    File.join(base_path, FILENAME)
  end
end
remove_admin_utilities!() click to toggle source
# File lib/postshift/schema.rb, line 17
def self.remove_admin_utilities!
  ensure_admin_schema
  ADMIN_UTILITIES.each do |table_name|
    Postshift.connection.exec("DROP VIEW IF EXISTS admin.#{table_name}")
  end
end
restore() click to toggle source
# File lib/postshift/schema.rb, line 59
def self.restore
  sql = dump_sql
  Postshift.connection.exec(sql) if sql.present?
end
schemas() click to toggle source
# File lib/postshift/schema.rb, line 78
def self.schemas
  %w(public)
end
tbl_ddl_sql() click to toggle source
# File lib/postshift/schema.rb, line 82
    def self.tbl_ddl_sql
      <<-SQL
        SELECT  ddl
        FROM    admin.v_generate_tbl_ddl
        WHERE   schemaname IN ($1)
        ORDER BY tablename ASC, seq ASC
      SQL
    end
view_ddl_sql() click to toggle source
# File lib/postshift/schema.rb, line 91
    def self.view_ddl_sql
      <<-SQL
        SELECT  ddl
        FROM    admin.v_generate_view_ddl
        WHERE   schemaname IN ($1)
        ORDER BY viewname ASC
      SQL
    end