0
-# Currently it supports 3 adapters: sqlite3, postgresql, and mysql
0
- ActiveRecord::Migrator.current_version
0
- ActiveRecord::Migrator.up(File.join(Mack.root, "db", "migrations"))
0
- def self.rollback(step = 1)
0
- step = (ENV["STEP"] || step).to_i
0
- cur_version = version.to_i
0
- target_version = cur_version - step
0
- target_version = 0 if target_version < 0
0
- ActiveRecord::Migrator.down(File.join(Mack.root, "db", "migrations"), target_version)
0
- def self.db_settings(env)
0
- dbs = YAML::load(ERB.new(IO.read(File.join(Mack.root, "config", "database.yml"))).result)
0
- def self.establish_connection(env)
0
+ # Sets up and establishes connections to the database based on the specified environment
0
+ # and the settings in the database.yml file.
0
+ def self.establish_connection(env = Mack.env)
0
ActiveRecord::Base.establish_connection(dbs)
0
+ end # establish_connection
0
+ # Creates a database, if it doesn't already exist for the specified environment
0
+ def self.create(env = Mack.env, repis = :default)
0
+ dbs = db_settings(env)
0
+ establish_mysql_connection
0
+ create_mysql_db(env, dbs)
0
+ ENV['PGHOST'] = dbs[:host] if dbs[:host]
0
+ ENV['PGPORT'] = dbs[:port].to_s if dbs[:port]
0
+ ENV['PGPASSWORD'] = dbs[:password].to_s if dbs[:password]
0
+ ActiveRecord::Base.clear_active_connections!
0
+ create_postgresql_db(env, dbs)
0
+ ActiveRecord::Base.clear_active_connections!
0
- # Perform db create or drop
0
- # By default the mode is drop then create, but caller will be able to
0
- # call this routine with a specific action (:drop, :create, or :drop_and_create)
0
- def self.drop_or_create_database(env, mode = :drop_and_create)
0
+ # Drops a database, if it exists for the specified environment
0
+ def self.drop(env = Mack.env, repis = :default)
0
- establish_mysql_connection
0
- drop_mysql_db(env, dbs) if mode == :drop or mode == :drop_and_create
0
- create_mysql_db(env, dbs) if mode == :create or mode == :drop_and_create
0
- ENV['PGHOST'] = dbs[:host] if dbs[:host]
0
- ENV['PGPORT'] = dbs[:port].to_s if dbs[:port]
0
- ENV['PGPASSWORD'] = dbs[:password].to_s if dbs[:password]
0
- ActiveRecord::Base.clear_active_connections!
0
- drop_postgresql_db(env, dbs) if mode == :drop or mode == :drop_and_create
0
- create_postgresql_db(env, dbs) if mode == :create or mode == :drop_and_create
0
- ActiveRecord::Base.clear_active_connections!
0
- FileUtils.rm_rf(dbs[:database]) if mode == :drop or mode == :drop_and_create
0
+ establish_mysql_connection
0
+ drop_mysql_db(env, dbs)
0
+ ENV['PGHOST'] = dbs[:host] if dbs[:host]
0
+ ENV['PGPORT'] = dbs[:port].to_s if dbs[:port]
0
+ ENV['PGPASSWORD'] = dbs[:password].to_s if dbs[:password]
0
+ ActiveRecord::Base.clear_active_connections!
0
+ drop_postgresql_db(env, dbs)
0
+ ActiveRecord::Base.clear_active_connections!
0
+ FileUtils.rm_rf(dbs[:database])
0
- def self.load_structure(file, env = Mack.env
)
0
+ def self.load_structure(file, env = Mack.env
, repis = :default)
0
Mack::Database.establish_connection(env)
0
@@ -83,7 +60,7 @@ module Mack
0
- def self.dump_structure(env = Mack.env
)
0
+ def self.dump_structure(env = Mack.env
, repis = :default)
0
Mack::Database.establish_connection(env)
0
@@ -99,8 +76,14 @@ module Mack
0
raise "Task not supported for '#{dbs[:adapter]}'"
0
+ def self.db_settings(env)
0
+ dbs = YAML::load(ERB.new(IO.read(File.join(Mack.root, "config", "database.yml"))).result)
0
def self.drop_postgresql_db(env, dbs)
0
@@ -146,5 +129,159 @@ module Mack
0
puts "Dropping (MySQL): #{dbs[:database]}"
0
ActiveRecord::Base.connection.execute "DROP DATABASE IF EXISTS `#{dbs[:database]}`"
0
\ No newline at end of file
0
+# # Currently it supports 3 adapters: sqlite3, postgresql, and mysql
0
+# ActiveRecord::Migrator.current_version
0
+# ActiveRecord::Migrator.up(File.join(Mack.root, "db", "migrations"))
0
+# def self.rollback(step = 1)
0
+# step = (ENV["STEP"] || step).to_i
0
+# cur_version = version.to_i
0
+# target_version = cur_version - step
0
+# target_version = 0 if target_version < 0
0
+# ActiveRecord::Migrator.down(File.join(Mack.root, "db", "migrations"), target_version)
0
+# def self.db_settings(env)
0
+# dbs = YAML::load(ERB.new(IO.read(File.join(Mack.root, "config", "database.yml"))).result)
0
+# def self.establish_connection(env)
0
+# dbs = db_settings(env)
0
+# ActiveRecord::Base.establish_connection(dbs)
0
+# # Perform db create or drop
0
+# # By default the mode is drop then create, but caller will be able to
0
+# # call this routine with a specific action (:drop, :create, or :drop_and_create)
0
+# def self.drop_or_create_database(env, mode = :drop_and_create)
0
+# dbs = db_settings(env)
0
+# establish_mysql_connection
0
+# drop_mysql_db(env, dbs) if mode == :drop or mode == :drop_and_create
0
+# create_mysql_db(env, dbs) if mode == :create or mode == :drop_and_create
0
+# ENV['PGHOST'] = dbs[:host] if dbs[:host]
0
+# ENV['PGPORT'] = dbs[:port].to_s if dbs[:port]
0
+# ENV['PGPASSWORD'] = dbs[:password].to_s if dbs[:password]
0
+# ActiveRecord::Base.clear_active_connections!
0
+# drop_postgresql_db(env, dbs) if mode == :drop or mode == :drop_and_create
0
+# create_postgresql_db(env, dbs) if mode == :create or mode == :drop_and_create
0
+# ActiveRecord::Base.clear_active_connections!
0
+# FileUtils.rm_rf(dbs[:database]) if mode == :drop or mode == :drop_and_create
0
+# def self.load_structure(file, env = Mack.env)
0
+# Mack::Database.establish_connection(env)
0
+# dbs = db_settings(env)
0
+# sql = File.read(file)
0
+# sql.split(";").each do |s|
0
+# ActiveRecord::Base.connection.execute(s) unless s.blank?
0
+# ActiveRecord::Base.connection.execute(sql) unless sql.blank?
0
+# def self.dump_structure(env = Mack.env)
0
+# Mack::Database.establish_connection(env)
0
+# dbs = db_settings(env)
0
+# output_file = File.join(Mack.root, "db", "#{env}_schema_structure.sql")
0
+# File.open(output_file, "w") {|f| f.puts ActiveRecord::Base.connection.structure_dump}
0
+# `pg_dump -i -U "#{dbs[:username]}" -s -x -O -n #{ENV["SCHEMA"] ||= "public"} -f #{output_file} #{dbs[:database]}`
0
+# `sqlite3 #{dbs[:database]} .schema > #{output_file}`
0
+# raise "Task not supported for '#{dbs[:adapter]}'"
0
+# def self.drop_postgresql_db(env, dbs)
0
+# puts "Dropping (PostgreSQL): #{dbs[:database]}"
0
+# `dropdb -U "#{dbs[:username]}" #{dbs[:database]}`
0
+# rescue Exception => e
0
+# def self.create_postgresql_db(env, dbs)
0
+# enc_option = "-E #{dbs[:encoding]}" if dbs[:encoding]
0
+# puts "Creating (PostgreSQL): #{dbs[:database]}"
0
+# `createdb #{enc_option} -U "#{dbs[:username]}" #{dbs[:database]}`
0
+# rescue Exception => e
0
+# def self.establish_mysql_connection
0
+# # connect to mysql meta database
0
+# ActiveRecord::Base.establish_connection(
0
+# :host => "localhost",
0
+# :database => "mysql",
0
+# :username => ENV["DB_USERNAME"] || "root",
0
+# :password => ENV["DB_PASSWORD"] || ""
0
+# def self.create_mysql_db(env, dbs)
0
+# puts "Dropping (MySQL): #{dbs[:database]}"
0
+# ActiveRecord::Base.connection.execute "CREATE DATABASE `#{dbs[:database]}` DEFAULT CHARACTER SET `#{dbs[:charset] || 'utf8'}` COLLATE `#{dbs[:collation]}`"
0
+# puts "Creating (MySQL): #{dbs[:database]}"
0
+# ActiveRecord::Base.connection.execute "CREATE DATABASE `#{dbs[:database]}` DEFAULT CHARACTER SET `#{dbs[:charset] || 'utf8'}`"
0
+# def self.drop_mysql_db(env, dbs)
0
+# puts "Dropping (MySQL): #{dbs[:database]}"
0
+# ActiveRecord::Base.connection.execute "DROP DATABASE IF EXISTS `#{dbs[:database]}`"
0
\ No newline at end of file