Module: Mack::Database
- Defined in:
- lib/mack-active_record/database.rb,
lib/mack-active_record/generators.rb,
lib/mack-active_record/database_migrations.rb
Defined Under Namespace
Modules: Generators, Migrations
Class Method Summary collapse
-
.create(env = Mack.env, repis = :default) ⇒ Object
Creates a database, if it doesn’t already exist for the specified environment.
-
.drop(env = Mack.env, repis = :default) ⇒ Object
Drops a database, if it exists for the specified environment.
-
.dump_structure(env = Mack.env, repis = :default) ⇒ Object
Dumps the structure of the database to a file.
-
.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.
-
.load_structure(file, env = Mack.env, repis = :default) ⇒ Object
Loads the structure of the given file into the database.
Class Method Details
.create(env = Mack.env, repis = :default) ⇒ Object
Creates a database, if it doesn’t already exist for the specified environment
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/mack-active_record/database.rb', line 12 def self.create(env = Mack.env, repis = :default) dbs = db_settings(env) case dbs[:adapter] when "mysql" establish_mysql_connection create_mysql_db(env, dbs) when "postgresql" ENV['PGHOST'] = dbs[:host] if dbs[:host] ENV['PGPORT'] = dbs[:port].to_s if dbs[:port] ENV['PGPASSWORD'] = dbs[:password].to_s if dbs[:password] ActiveRecord::Base.clear_all_connections! create_postgresql_db(env, dbs) when "sqlite3" ActiveRecord::Base.clear_all_connections! end end |
.drop(env = Mack.env, repis = :default) ⇒ Object
Drops a database, if it exists for the specified environment
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/mack-active_record/database.rb', line 30 def self.drop(env = Mack.env, repis = :default) dbs = db_settings(env) case dbs[:adapter] when "mysql" establish_mysql_connection drop_mysql_db(env, dbs) # ActiveRecord::Base.connection.drop_database dbs[:database] when "postgresql" ENV['PGHOST'] = dbs[:host] if dbs[:host] ENV['PGPORT'] = dbs[:port].to_s if dbs[:port] ENV['PGPASSWORD'] = dbs[:password].to_s if dbs[:password] ActiveRecord::Base.clear_all_connections! drop_postgresql_db(env, dbs) when "sqlite3" ActiveRecord::Base.clear_all_connections! FileUtils.rm_rf(dbs[:database]) end end |
.dump_structure(env = Mack.env, repis = :default) ⇒ Object
Dumps the structure of the database to a file.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/mack-active_record/database.rb', line 66 def self.dump_structure(env = Mack.env, repis = :default) Mack::Database.establish_connection(env) dbs = db_settings(env) structure = "" output_file = Mack::Paths.db("#{env}_schema_structure.sql") case dbs[:adapter] when "mysql" File.open(output_file, "w") {|f| f.puts ActiveRecord::Base.connection.structure_dump} when "postgresql" `pg_dump -i -U "#{dbs[:username]}" -s -x -O -n #{ENV["SCHEMA"] ||= "public"} -f #{output_file} #{dbs[:database]}` when "sqlite3" `sqlite3 #{dbs[:database]} .schema > #{output_file}` else raise "Task not supported for '#{dbs[:adapter]}'" 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 |
# File 'lib/mack-active_record/database.rb', line 6 def self.establish_connection(env = Mack.env) dbs = db_settings(env) ActiveRecord::Base.establish_connection(dbs) end |
.load_structure(file, env = Mack.env, repis = :default) ⇒ Object
Loads the structure of the given file into the database
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/mack-active_record/database.rb', line 50 def self.load_structure(file, env = Mack.env, repis = :default) Mack::Database.establish_connection(env) dbs = db_settings(env) sql = File.read(file) case dbs[:adapter] when "mysql", "sqlite3" sql.split(";").each do |s| s.strip! ActiveRecord::Base.connection.execute(s) unless s.blank? end else ActiveRecord::Base.connection.execute(sql) unless sql.blank? end end |