Class: SchemaEvolutionManager::Db

Inherits:
Object
  • Object
show all
Defined in:
lib/schema-evolution-manager/db.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, opts = {}) ⇒ Db



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/schema-evolution-manager/db.rb', line 7

def initialize(url, opts={})
  @url = Preconditions.check_not_blank(url, "url cannot be blank")
  password = opts.delete(:password)
  Preconditions.assert_empty_opts(opts)
  connection_data = ConnectionData.parse_url(@url)

  if password
    ENV['PGPASSFILE'] = Library.write_to_temp_file(connection_data.pgpass(password))
    puts "Created PGPASSFILE=%s" % ENV['PGPASSFILE']
  end
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



5
6
7
# File 'lib/schema-evolution-manager/db.rb', line 5

def url
  @url
end

Class Method Details

.attribute_values(path) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/schema-evolution-manager/db.rb', line 36

def Db.attribute_values(path)
  Preconditions.assert_class(path, String)

  options = []

  ['quiet', 'no-align', 'tuples-only'].each do |v|
    options << "--#{v}"
  end

  SchemaEvolutionManager::MigrationFile.new(path).attribute_values.map do |value|
    if value.attribute.name == "transaction"
      if value.value == "single"
        options << "--single-transaction"
      elsif value.value == "none"
        # No-op
      else
        raise "File[%s] - attribute[%s] unsupported value[%s]" % [path, value.attribute.name, value.value]
      end
    else
      raise "File[%s] - unsupported attribute named[%s]" % [path, value.attribute.name]
    end
  end

  options
end

.from_args(args, opts = {}) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/schema-evolution-manager/db.rb', line 95

def Db.from_args(args, opts={})
  Preconditions.assert_class(args, Args)
  password = opts.delete(:password)
  Preconditions.assert_empty_opts(opts)

  if args.url
    Db.new(args.url, :password => password)
  else
    base = "%s:%s/%s" % [args.host || "localhost", args.port || ConnectionData::DEFAULT_PORT, args.name]
    url = args.user ? "%s@%s" % [args.user, base] : base
    Db.new("postgres://" + url, :password => password)
  end
end

.parse_command_line_config(arg_string) ⇒ Object

Parses command line arguments returning an instance of Db. Exists if invalid config.



88
89
90
91
92
# File 'lib/schema-evolution-manager/db.rb', line 88

def Db.parse_command_line_config(arg_string)
  Preconditions.assert_class(arg_string, String)
  args = Args.new(arg_string, :optional => ['url', 'host', 'user', 'name', 'port'])
  Db.from_args(args)
end

.schema_nameObject

Returns the name of the schema_evolution_manager schema



110
111
112
# File 'lib/schema-evolution-manager/db.rb', line 110

def Db.schema_name
  "schema_evolution_manager"
end

Instance Method Details

#bootstrap!Object

Installs schema_evolution_manager. Automatically upgrades schema_evolution_manager.



20
21
22
23
24
25
26
27
# File 'lib/schema-evolution-manager/db.rb', line 20

def bootstrap!
  scripts = Scripts.new(self, Scripts::BOOTSTRAP_SCRIPTS)
  dir = File.join(Library.base_dir, "scripts")
  scripts.each_pending(dir) do |filename, path|
    psql_file(path)
    scripts.record_as_run!(filename)
  end
end

#psql_command(sql_command) ⇒ Object

executes a simple sql command.



30
31
32
33
34
# File 'lib/schema-evolution-manager/db.rb', line 30

def psql_command(sql_command)
  Preconditions.assert_class(sql_command, String)
  command = "psql --no-align --tuples-only --command \"%s\" %s" % [sql_command, @url]
  Library.system_or_error(command)
end

#psql_file(path) ⇒ Object

executes sql commands from a file in a single transaction



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/schema-evolution-manager/db.rb', line 63

def psql_file(path)
  Preconditions.assert_class(path, String)
  Preconditions.check_state(File.exists?(path), "File[%s] not found" % path)

  options = Db.attribute_values(path).join(" ")

  Library.with_temp_file(:prefix => File.basename(path)) do |tmp|
    File.open(tmp, "w") do |out|
      out << "\\set ON_ERROR_STOP true\n\n"
      out << IO.read(path)
    end

    command = "psql --file \"%s\" #{options} %s" % [tmp, @url]
    Library.system_or_error(command)
  end
end

#schema_schema_evolution_manager_exists?Boolean

True if the specific schema exists; false otherwise



81
82
83
84
# File 'lib/schema-evolution-manager/db.rb', line 81

def schema_schema_evolution_manager_exists?
  sql = "select count(*) from pg_namespace where nspname='%s'" % Db.schema_name
  psql_command(sql).to_i > 0
end