Module: Thredded::DbTools
- Defined in:
- lib/thredded/db_tools.rb
Constant Summary collapse
- MIGRATION_SPEC_SOURCE_VERSION =
'v0.8'
Class Method Summary collapse
- .adapter ⇒ Object
- .config ⇒ Object
- .database ⇒ Object
- .dump(to = dump_file) ⇒ Object
- .dump_file ⇒ Object
- .host ⇒ Object
-
.migrate(paths:, quiet:, &filter) ⇒ Object
Runs the migrations in the given paths.
- .password ⇒ Object
- .restore(from = dump_file) ⇒ Object
- .silence_active_record ⇒ Object
- .username ⇒ Object
Class Method Details
permalink .adapter ⇒ Object
[View source]
77 78 79 |
# File 'lib/thredded/db_tools.rb', line 77 def adapter config['adapter'] end |
permalink .config ⇒ Object
[View source]
73 74 75 |
# File 'lib/thredded/db_tools.rb', line 73 def config @config ||= Rails.configuration.database_configuration[Rails.env] end |
permalink .database ⇒ Object
[View source]
81 82 83 |
# File 'lib/thredded/db_tools.rb', line 81 def database config['database'] end |
permalink .dump(to = dump_file) ⇒ Object
[View source]
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/thredded/db_tools.rb', line 27 def dump(to = dump_file) case adapter when /sqlite/i system ['sqlite3', Rails.root.join(database), '.dump', '>', to].join(' ') when /postgres/i cmd = "pg_dump --dbname=postgresql://#{username}:#{password}@#{host}:5432/#{database}" \ "--verbose --clean --no-owner --no-acl --format=c > #{to}" system cmd when /mysql/i system("mysqldump --user #{username} -p#{password} #{database} > #{to}") end end |
permalink .dump_file ⇒ Object
[View source]
23 24 25 |
# File 'lib/thredded/db_tools.rb', line 23 def dump_file File.("../../../spec/migration/#{MIGRATION_SPEC_SOURCE_VERSION}.#{adapter}.dump", __FILE__) end |
permalink .host ⇒ Object
[View source]
93 94 95 |
# File 'lib/thredded/db_tools.rb', line 93 def host config['host'] end |
permalink .migrate(paths:, quiet:, &filter) ⇒ Object
Runs the migrations in the given paths.
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/thredded/db_tools.rb', line 9 def migrate(paths:, quiet:, &filter) verbose_was = ActiveRecord::Migration.verbose ActiveRecord::Migration.verbose = !quiet migrate = -> { ActiveRecord::MigrationContext.new(paths, ActiveRecord::SchemaMigration).migrate(nil, &filter) } if quiet silence_active_record(&migrate) else migrate.call end ensure ActiveRecord::Migration.verbose = verbose_was end |
permalink .password ⇒ Object
[View source]
89 90 91 |
# File 'lib/thredded/db_tools.rb', line 89 def password config['password'] end |
permalink .restore(from = dump_file) ⇒ Object
[View source]
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/thredded/db_tools.rb', line 40 def restore(from = dump_file) case adapter when /postgres/i cmd = [ 'pg_restore --verbose --clean --no-owner --no-acl', "--dbname=postgresql://#{username}:#{password}@#{host}:5432/#{database}", from, '>', Rails.root.join('log', 'restore.log') ].join(' ') system cmd when /mysql/i, /sqlite/i connection = ActiveRecord::Base.connection statements = File.read(from).split(/;$/) statements.pop silence_active_record do ActiveRecord::Base.transaction do statements.each do |statement| connection.execute(statement) unless /(BEGIN TRANSACTION|COMMIT)/.match?(statement) end end end end end |
permalink .silence_active_record ⇒ Object
[View source]
65 66 67 68 69 70 71 |
# File 'lib/thredded/db_tools.rb', line 65 def silence_active_record was = ActiveRecord::Base.logger.level ActiveRecord::Base.logger.level = Logger::WARN yield ensure ActiveRecord::Base.logger.level = was end |
permalink .username ⇒ Object
[View source]
85 86 87 |
# File 'lib/thredded/db_tools.rb', line 85 def username config['username'] end |