Module Mack::Database
In: lib/mack-data_mapper/database.rb
lib/mack-data_mapper/database_migrations.rb
lib/mack-data_mapper/generators.rb
lib/mack-data_mapper/paginator.rb

Methods

Classes and Modules

Module Mack::Database::Generators
Module Mack::Database::Migrations
Class Mack::Database::Paginator

Public Class methods

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

[Source]

    # File lib/mack-data_mapper/database.rb, line 20
20:     def self.create(env = Mack.env, repis = :default)
21:       Mack::Database.establish_connection(env)
22:       create_database(repis)
23:     end

Drops a database, if it exists for the specified environment

[Source]

    # File lib/mack-data_mapper/database.rb, line 26
26:     def self.drop(env = Mack.env, repis = :default)
27:       Mack::Database.establish_connection(env)
28:       drop_database(repis)
29:     end

Dumps the structure of the database to a file.

[Source]

    # File lib/mack-data_mapper/database.rb, line 48
48:     def self.dump_structure(env = Mack.env, repis = :default)
49:       Mack::Database.establish_connection(env)
50:       adapter = repository(repis).adapter
51:       uri = adapter.uri
52:       structure = ""
53:       output_file = Mack::Paths.db("#{env}_schema_structure.sql")
54:       case adapter.class.name
55:       when /Mysql/
56:         sql = "SHOW TABLES"
57:         adapter.query(sql).each do |res|
58:           show = adapter.query("SHOW CREATE TABLE #{res}").first
59:           structure += show.attributes["create table".to_sym]
60:           structure += ";\n\n"
61:         end
62:         structure.gsub!('MyISAM', 'InnoDB')
63:         File.open(output_file, "w") {|f| f.puts structure}
64:       when /Postgres/
65:         `pg_dump -i -U "#{uri.user}" -s -x -O -n #{ENV["SCHEMA"] ||= "public"} -f #{output_file} #{uri.basename}`
66:       when /Sqlite3/
67:         puts `sqlite3 #{uri.path} .schema > #{output_file}`
68:       else
69:         raise "Task not supported for '#{repository(repis).adapter.class.name}'"
70:       end
71:     end

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

[Source]

    # File lib/mack-data_mapper/database.rb, line 6
 6:     def self.establish_connection(env = Mack.env)
 7:       dbs = YAML::load(ERB.new(IO.read(Mack::Paths.config("database.yml"))).result)
 8:       settings = dbs[env]
 9:       settings.symbolize_keys!
10:       if settings[:default]
11:         settings.each do |k,v|
12:           ::DataMapper.setup(k, v.symbolize_keys)
13:         end
14:       else
15:         ::DataMapper.setup(:default, settings)
16:       end
17:     end

Loads the structure of the given file into the database

[Source]

    # File lib/mack-data_mapper/database.rb, line 32
32:     def self.load_structure(file, env = Mack.env, repis = :default)
33:       Mack::Database.establish_connection(env)
34:       adapter = repository(repis).adapter
35:       sql = File.read(file)
36:       case adapter.class.name
37:       when /Mysql/
38:         sql.split(";").each do |s|
39:           s.strip! 
40:           adapter.execute(s) unless s.blank?
41:         end
42:       else
43:         adapter.execute(sql) unless sql.blank?
44:       end
45:     end

[Validate]