Module: DbAgile::Core::IO::Robustness

Included in:
DbAgile::Command::Robust, DSL
Defined in:
lib/dbagile/core/io/robustness.rb

Instance Method Summary collapse

Instance Method Details

#has_database!(repository, db_name = nil) ⇒ DbAgile::Core::Database

Asserts that a database exists inside a Repository instance. When db_name is nil, asserts that a default database is set.

Parameters:

Returns:

Raises:

  • ArgumentError if repository is not a Repository instance

  • DbAgile::NoSuchDatabaseError if the database cannot be found.

  • DbAgile::NoDefaultDatabaseError if db_name is nil and no current database is set on the repository.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dbagile/core/io/robustness.rb', line 51

def has_database!(repository, db_name = nil)
  raise ArgumentError, "Repository expected, got #{repository}"\
    unless repository.kind_of?(DbAgile::Core::Repository)
  db = if db_name.nil?
    repository.current_database
  else 
    repository.database(db_name)
  end
  if db.nil?
    raise DbAgile::NoSuchDatabaseError, "Unknown database #{db_name}" if db_name
    raise DbAgile::NoDefaultDatabaseError, "No default database set (try 'dba use ...' first)"
  else
    db
  end
end

#valid_database_name!(name) ⇒ Object

Asserts that a database name is valid or raises a InvalidDatabaseName error. A valid database name is a Symbol that matches /[a-z]*/.

Raises:

  • DbAgile::InvalidDatabaseName if assertion fails



13
14
15
16
17
# File 'lib/dbagile/core/io/robustness.rb', line 13

def valid_database_name!(name)
  raise DbAgile::InvalidDatabaseName, "Invalid database name #{name}"\
    unless name.kind_of?(Symbol) and /[a-z][a-z0-9_]*/ =~ name.to_s
  name
end

#valid_database_uri!(uri) ⇒ String

Asserts that a database uri is valid or raises a InvalidDatabaseUri error.

A valid database uri is any valid absolute URI (for now, will be restricted to known adapters in the future).

Returns:

  • (String)

    uri

Raises:

  • DbAgile::InvalidDatabaseUri if assertion fails



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dbagile/core/io/robustness.rb', line 28

def valid_database_uri!(uri)
  require 'uri'
  got = URI::parse(uri)
  if got.scheme or (uri =~ /\.db$/)
    uri
  else
    raise DbAgile::InvalidDatabaseUri, "Invalid database uri: #{uri}"
  end
rescue URI::InvalidURIError
  raise DbAgile::InvalidDatabaseUri, "Invalid database uri: #{uri}"
end

#valid_schema_files!(*files) ⇒ Object

Coerces and asserts that schema files arguments are correct. Returns normalized version.



71
72
73
74
75
76
77
# File 'lib/dbagile/core/io/robustness.rb', line 71

def valid_schema_files!(*files)
  files = files.flatten
  unless files.all?{|f| f.kind_of?(String)}
    raise DbAgile::CorruptedRepositoryError, "Invalid schema files #{files.inspect}"
  end
  files
end