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

Methods

Classes and Modules

Module Mack::Database::Generators
Module Mack::Database::Migrations

Public Class methods

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

[Source]

    # File lib/mack-active_record/database.rb, line 12
12:     def self.create(env = Mack.env, repis = :default)
13:       dbs = db_settings(env)
14:       case dbs[:adapter]
15:       when "mysql"
16:         establish_mysql_connection
17:         create_mysql_db(env, dbs)
18:       when "postgresql"
19:         ENV['PGHOST']     = dbs[:host] if dbs[:host]
20:         ENV['PGPORT']     = dbs[:port].to_s if dbs[:port]
21:         ENV['PGPASSWORD'] = dbs[:password].to_s if dbs[:password]
22:         ActiveRecord::Base.clear_all_connections!
23:         create_postgresql_db(env, dbs)
24:       when "sqlite3"
25:         ActiveRecord::Base.clear_all_connections!
26:       end
27:     end

Drops a database, if it exists for the specified environment

[Source]

    # File lib/mack-active_record/database.rb, line 30
30:     def self.drop(env = Mack.env, repis = :default)
31:       dbs = db_settings(env)
32:       case dbs[:adapter]
33:       when "mysql"
34:         establish_mysql_connection
35:         drop_mysql_db(env, dbs)
36:         # ActiveRecord::Base.connection.drop_database dbs[:database]
37:       when "postgresql"
38:         ENV['PGHOST']     = dbs[:host] if dbs[:host]
39:         ENV['PGPORT']     = dbs[:port].to_s if dbs[:port]
40:         ENV['PGPASSWORD'] = dbs[:password].to_s if dbs[:password]
41:         ActiveRecord::Base.clear_all_connections!
42:         drop_postgresql_db(env, dbs)
43:       when "sqlite3"
44:         ActiveRecord::Base.clear_all_connections!
45:         FileUtils.rm_rf(dbs[:database])
46:       end
47:     end

Dumps the structure of the database to a file.

[Source]

    # File lib/mack-active_record/database.rb, line 66
66:     def self.dump_structure(env = Mack.env, repis = :default)
67:       Mack::Database.establish_connection(env)
68:       dbs = db_settings(env)
69:       structure = ""
70:       output_file = Mack::Paths.db("#{env}_schema_structure.sql")
71:       case dbs[:adapter]
72:       when "mysql"
73:         File.open(output_file, "w") {|f| f.puts ActiveRecord::Base.connection.structure_dump}
74:       when "postgresql"
75:         `pg_dump -i -U "#{dbs[:username]}" -s -x -O -n #{ENV["SCHEMA"] ||= "public"} -f #{output_file} #{dbs[:database]}`
76:       when "sqlite3"
77:         `sqlite3 #{dbs[:database]} .schema > #{output_file}`
78:       else
79:         raise "Task not supported for '#{dbs[:adapter]}'"
80:       end
81:     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-active_record/database.rb, line 6
6:     def self.establish_connection(env = Mack.env)
7:       dbs = db_settings(env)
8:       ActiveRecord::Base.establish_connection(dbs)
9:     end

Loads the structure of the given file into the database

[Source]

    # File lib/mack-active_record/database.rb, line 50
50:     def self.load_structure(file, env = Mack.env, repis = :default)
51:       Mack::Database.establish_connection(env)
52:       dbs = db_settings(env)
53:       sql = File.read(file)
54:       case dbs[:adapter]
55:       when "mysql", "sqlite3"
56:         sql.split(";").each do |s|
57:           s.strip! 
58:           ActiveRecord::Base.connection.execute(s) unless s.blank?
59:         end
60:       else
61:         ActiveRecord::Base.connection.execute(sql) unless sql.blank?
62:       end
63:     end

[Validate]