Class: Aro::Db
- Inherits:
-
Object
- Object
- Aro::Db
- Defined in:
- lib/aro/db.rb
Constant Summary collapse
- CONFIG_FILE =
"database.yml"- SQL_FILE =
"database.sql"- SCHEMA_FILE =
"schema.rb"- MIGRATIONS_DIR =
"db/migrate"
Class Method Summary collapse
Instance Method Summary collapse
- #config(name = nil) ⇒ Object
- #connect(name) ⇒ Object
- #db_config_filepath(name) ⇒ Object
- #db_filepath(name) ⇒ Object
-
#initialize(name = nil) ⇒ Db
constructor
A new instance of Db.
- #setup(name) ⇒ Object
- #setup_local_aro(name = nil, force = false) ⇒ Object
Constructor Details
#initialize(name = nil) ⇒ Db
Returns a new instance of Db.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/aro/db.rb', line 13 def initialize(name = nil) # show queries in stout ActiveRecord::Base.logger = Logger.new(STDOUT) if ENV[:ARO_ENV.to_s] == :development.to_s # generate .name file if name.nil? && Aro::Mancy.is_aro_dir? # pwd is in aro directory, use name file name = get_name_from_namefile elsif !name.nil? && !Aro::Mancy.is_aro_dir? # first use, pwd is not in aro directory yet echo_cmd = "echo #{name} >> #{name}/#{Aro::Mancy::NAME_FILE}" Aro::P.say("#{echo_cmd} (result: #{system(echo_cmd)})") end if name.nil? # if name is still nil, need to use create to generate aro directory raise "invalid aro directory. use `aro create` to generate one" end setup_local_aro(name) end |
Class Method Details
.base_aro_dir(name) ⇒ Object
43 44 45 |
# File 'lib/aro/db.rb', line 43 def self.base_aro_dir(name) "#{Aro::Mancy.is_aro_dir? ? "." : name}/#{Aro::DIRS[:ARO].call}" end |
Instance Method Details
#config(name = nil) ⇒ Object
39 40 41 |
# File 'lib/aro/db.rb', line 39 def config(name = nil) @config ||= YAML.load_file(db_config_filepath(name)) end |
#connect(name) ⇒ Object
35 36 37 |
# File 'lib/aro/db.rb', line 35 def connect(name) ActiveRecord::Base.establish_connection(config(name)) end |
#db_config_filepath(name) ⇒ Object
47 48 49 |
# File 'lib/aro/db.rb', line 47 def db_config_filepath(name) "#{Aro::Db.base_aro_dir(name)}/#{CONFIG_FILE}" end |
#db_filepath(name) ⇒ Object
51 52 53 |
# File 'lib/aro/db.rb', line 51 def db_filepath(name) "#{Aro::Db.base_aro_dir(name)}/#{SQL_FILE}" end |
#setup(name) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/aro/db.rb', line 87 def setup(name) local_migrate_dir = "#{Aro::Db.base_aro_dir(name)}/#{MIGRATIONS_DIR}" unless Dir.exist?(local_migrate_dir) gem_dir = Dir[Gem.loaded_specs[:aro.to_s]&.full_gem_path || '.'].first cp_cmd = "cp -R #{gem_dir}/db #{Aro::Db.base_aro_dir(name)}" Aro::P.say(cp_cmd) system(cp_cmd) end migration_version = Dir["#{local_migrate_dir}/*.rb"].map{|n| Pathname.new(n).basename.to_s.split("_")[0].to_i }.max ActiveRecord::MigrationContext.new(local_migrate_dir).migrate(migration_version) require 'active_record/schema_dumper' filename = "#{Aro::Db.base_aro_dir(name)}/#{SCHEMA_FILE}" File.open(filename, "w+") do |f| ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection_pool, f) end end |
#setup_local_aro(name = nil, force = false) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/aro/db.rb', line 59 def setup_local_aro(name = nil, force = false) # create local .aro/ directory unless File.exist?(Aro::Db.base_aro_dir(name)) || force if File.exist?(Aro::Db.base_aro_dir(name)) && force rm_cmd = "rm -rf #{Aro::Db.base_aro_dir(name)}" Aro::P.say(rm_cmd) system(rm_cmd) end mk_cmd = "mkdir #{Aro::Db.base_aro_dir(name)}" Aro::P.say("#{mk_cmd} (result: #{system(mk_cmd)})") end # create database config yaml file c = { adapter: :sqlite3.to_s, database: "#{Aro::Db.base_aro_dir(name)}/#{SQL_FILE}", username: name, password: name }.to_yaml File.open(db_config_filepath(name), "w") do |file| file.write(c) end connect(name) setup(name) end |