Module: Mack::Database

Defined in:
lib/mack-data_mapper/database.rb,
lib/mack-data_mapper/paginator.rb,
lib/mack-data_mapper/generators.rb,
lib/mack-data_mapper/database_migrations.rb

Defined Under Namespace

Modules: Generators, Migrations Classes: Paginator

Class Method Summary collapse

Class Method Details

.create(env = Mack.env, repis = :default) ⇒ Object

Creates a database, if it doesn’t already exist for the specified environment



20
21
22
23
# File 'lib/mack-data_mapper/database.rb', line 20

def self.create(env = Mack.env, repis = :default)
  Mack::Database.establish_connection(env)
  create_database(repis)
end

.drop(env = Mack.env, repis = :default) ⇒ Object

Drops a database, if it exists for the specified environment



26
27
28
29
# File 'lib/mack-data_mapper/database.rb', line 26

def self.drop(env = Mack.env, repis = :default)
  Mack::Database.establish_connection(env)
  drop_database(repis)
end

.dump_structure(env = Mack.env, repis = :default) ⇒ Object

Dumps the structure of the database to a file.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mack-data_mapper/database.rb', line 48

def self.dump_structure(env = Mack.env, repis = :default)
  Mack::Database.establish_connection(env)
  adapter = repository(repis).adapter
  uri = adapter.uri
  structure = ""
  output_file = Mack::Paths.db("#{env}_schema_structure.sql")
  case adapter.class.name
  when /Mysql/
    sql = "SHOW TABLES"
    adapter.query(sql).each do |res|
      show = adapter.query("SHOW CREATE TABLE #{res}").first
      structure += show.attributes["create table".to_sym]
      structure += ";\n\n"
    end
    structure.gsub!('MyISAM', 'InnoDB')
    File.open(output_file, "w") {|f| f.puts structure}
  when /Postgres/
    `pg_dump -i -U "#{uri.user}" -s -x -O -n #{ENV["SCHEMA"] ||= "public"} -f #{output_file} #{uri.basename}`
  when /Sqlite3/
    puts `sqlite3 #{uri.path} .schema > #{output_file}`
  else
    raise "Task not supported for '#{repository(repis).adapter.class.name}'"
  end
end

.establish_connection(env = Mack.env) ⇒ Object

Sets up and establishes connections to the database based on the specified environment and the settings in the database.yml file.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/mack-data_mapper/database.rb', line 6

def self.establish_connection(env = Mack.env)
  dbs = YAML::load(ERB.new(IO.read(Mack::Paths.config("database.yml"))).result)
  settings = dbs[env]
  settings.symbolize_keys!
  if settings[:default]
    settings.each do |k,v|
      ::DataMapper.setup(k, v.symbolize_keys)
    end
  else
    ::DataMapper.setup(:default, settings)
  end
end

.load_structure(file, env = Mack.env, repis = :default) ⇒ Object

Loads the structure of the given file into the database



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mack-data_mapper/database.rb', line 32

def self.load_structure(file, env = Mack.env, repis = :default)
  Mack::Database.establish_connection(env)
  adapter = repository(repis).adapter
  sql = File.read(file)
  case adapter.class.name
  when /Mysql/
    sql.split(";").each do |s|
      s.strip! 
      adapter.execute(s) unless s.blank?
    end
  else
    adapter.execute(sql) unless sql.blank?
  end
end