Skip to content

Commit

Permalink
Add and use rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
DrTom committed Jan 27, 2015
1 parent 7ea67b3 commit e5f44c2
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 87 deletions.
34 changes: 34 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# This configuration was generated by `rubocop --auto-gen-config`
# on 2015-01-27 15:35:54 +0100 using RuboCop version 0.28.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
Lint/RescueException:
Enabled: false

# Offense count: 1
# Configuration parameters: AllowURI, URISchemes.
Metrics/LineLength:
Max: 85

# Offense count: 5
Style/Documentation:
Enabled: false

# Offense count: 4
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.
Style/SignalException:
Enabled: false

# Offense count: 1
# Cop supports --auto-correct.
Style/SpecialGlobalVars:
Enabled: false

# Offense count: 4
Style/UnlessElse:
Enabled: false
114 changes: 77 additions & 37 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,69 +1,108 @@
PATH
remote: .
specs:
pg_db_tasks (1.0.0)
rails (= 4.1.8)
pg_tasks (1.0.1)
rails (>= 4.1.0, < 5.0.0)

GEM
remote: https://rubygems.org/
specs:
actionmailer (4.1.8)
actionpack (= 4.1.8)
actionview (= 4.1.8)
actionmailer (4.2.0)
actionpack (= 4.2.0)
actionview (= 4.2.0)
activejob (= 4.2.0)
mail (~> 2.5, >= 2.5.4)
actionpack (4.1.8)
actionview (= 4.1.8)
activesupport (= 4.1.8)
rack (~> 1.5.2)
rails-dom-testing (~> 1.0, >= 1.0.5)
actionpack (4.2.0)
actionview (= 4.2.0)
activesupport (= 4.2.0)
rack (~> 1.6.0)
rack-test (~> 0.6.2)
actionview (4.1.8)
activesupport (= 4.1.8)
rails-dom-testing (~> 1.0, >= 1.0.5)
rails-html-sanitizer (~> 1.0, >= 1.0.1)
actionview (4.2.0)
activesupport (= 4.2.0)
builder (~> 3.1)
erubis (~> 2.7.0)
activemodel (4.1.8)
activesupport (= 4.1.8)
rails-dom-testing (~> 1.0, >= 1.0.5)
rails-html-sanitizer (~> 1.0, >= 1.0.1)
activejob (4.2.0)
activesupport (= 4.2.0)
globalid (>= 0.3.0)
activemodel (4.2.0)
activesupport (= 4.2.0)
builder (~> 3.1)
activerecord (4.1.8)
activemodel (= 4.1.8)
activesupport (= 4.1.8)
arel (~> 5.0.0)
activesupport (4.1.8)
i18n (~> 0.6, >= 0.6.9)
activerecord (4.2.0)
activemodel (= 4.2.0)
activesupport (= 4.2.0)
arel (~> 6.0)
activesupport (4.2.0)
i18n (~> 0.7)
json (~> 1.7, >= 1.7.7)
minitest (~> 5.1)
thread_safe (~> 0.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
arel (5.0.1.20140414130214)
arel (6.0.0)
ast (2.0.0)
astrolabe (1.3.0)
parser (>= 2.2.0.pre.3, < 3.0)
builder (3.2.2)
erubis (2.7.0)
globalid (0.3.0)
activesupport (>= 4.1.0)
hike (1.2.3)
i18n (0.7.0)
json (1.8.2)
loofah (2.0.1)
nokogiri (>= 1.5.9)
mail (2.6.3)
mime-types (>= 1.16, < 3)
mime-types (2.4.3)
minitest (5.5.0)
mini_portile (0.6.2)
minitest (5.5.1)
multi_json (1.10.1)
nokogiri (1.6.5)
mini_portile (~> 0.6.0)
parser (2.2.0.2)
ast (>= 1.1, < 3.0)
pg (0.18.1)
rack (1.5.2)
rack-test (0.6.2)
powerpack (0.0.9)
rack (1.6.0)
rack-test (0.6.3)
rack (>= 1.0)
rails (4.1.8)
actionmailer (= 4.1.8)
actionpack (= 4.1.8)
actionview (= 4.1.8)
activemodel (= 4.1.8)
activerecord (= 4.1.8)
activesupport (= 4.1.8)
rails (4.2.0)
actionmailer (= 4.2.0)
actionpack (= 4.2.0)
actionview (= 4.2.0)
activejob (= 4.2.0)
activemodel (= 4.2.0)
activerecord (= 4.2.0)
activesupport (= 4.2.0)
bundler (>= 1.3.0, < 2.0)
railties (= 4.1.8)
sprockets-rails (~> 2.0)
railties (4.1.8)
actionpack (= 4.1.8)
activesupport (= 4.1.8)
railties (= 4.2.0)
sprockets-rails
rails-deprecated_sanitizer (1.0.3)
activesupport (>= 4.2.0.alpha)
rails-dom-testing (1.0.5)
activesupport (>= 4.2.0.beta, < 5.0)
nokogiri (~> 1.6.0)
rails-deprecated_sanitizer (>= 1.0.1)
rails-html-sanitizer (1.0.1)
loofah (~> 2.0)
railties (4.2.0)
actionpack (= 4.2.0)
activesupport (= 4.2.0)
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rainbow (2.0.0)
rake (10.4.2)
rubocop (0.28.0)
astrolabe (~> 1.3)
parser (>= 2.2.0.pre.7, < 3.0)
powerpack (~> 0.0.6)
rainbow (>= 1.99.1, < 3.0)
ruby-progressbar (~> 1.4)
ruby-progressbar (1.7.1)
sprockets (2.12.3)
hike (~> 1.2)
multi_json (~> 1.0)
Expand All @@ -84,4 +123,5 @@ PLATFORMS

DEPENDENCIES
pg
pg_db_tasks!
pg_tasks!
rubocop
2 changes: 0 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,4 @@ RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_files.include('lib/**/*.rb')
end


Bundler::GemHelper.install_tasks

39 changes: 19 additions & 20 deletions lib/pg_tasks.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
#--
# Copyright (c) 2015 Thomas Schank
#
# Released to the public under the terms of the MIT license.
# See MIT-LICENSE.
# Released to the public under the terms of the MIT license.
# See MIT-LICENSE.
#
#++

require 'active_record/tasks/database_tasks'
require 'active_record/tasks/postgresql_database_tasks'

module PgTasks

require 'pg_tasks/railtie' if defined?(Rails)

DEFAULT_BINARY_DATA_FILE_NAME = 'data.pgbin'
DEFAULT_BINARY_STRUCTURE_AND_DATA_FILE_NAME = 'structure_and_data.pgbin'

class << self

%w(data_dump data_restore).each do |method_name|
define_method method_name do |filename = nil|
ActiveRecord::Tasks::DatabaseTasks \
Expand All @@ -29,17 +27,17 @@ class << self

%w(structure_and_data_dump structure_and_data_restore).each do |method_name|
define_method method_name do |filename = nil|
ActiveRecord::Tasks::DatabaseTasks \
.perform_pg_db_task_for_config_and_filename \
method_name, current_config,
filename_or_default_binary_structure_and_data_file(filename)
ActiveRecord::Tasks::DatabaseTasks \
.perform_pg_db_task_for_config_and_filename \
method_name, current_config,
filename_or_default_binary_structure_and_data_file(filename)
end
end

def truncate_tables
ActiveRecord::Base.connection.tap do |connection|
connection.tables.reject { |tn| tn == 'schema_migrations' }
.join(', ').tap do |tables|
.join(', ').tap do |tables|
connection.execute " TRUNCATE TABLE #{tables} CASCADE; "
end
end
Expand All @@ -62,32 +60,36 @@ def filename_or_default_binary_structure_and_data_file(filename)
File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir,
DEFAULT_BINARY_STRUCTURE_AND_DATA_FILE_NAME)
end

end
end

module ActiveRecord
module Tasks
class PostgreSQLDatabaseTasks

def data_dump(filename)
set_psql_env
command = 'pg_dump -F c -a -T schema_migrations -x -O -f ' \
"#{Shellwords.escape(filename)} " \
"#{Shellwords.escape(configuration['database'])}"
raise 'Error during data_dump' unless Kernel.system(command)
$stdout.puts "The data of '#{configuration['database']} " \
"has been dumped to '#{filename}'"
unless Kernel.system(command)
raise 'Error during data_dump'
else
$stdout.puts "The data of '#{configuration['database']} " \
"has been dumped to '#{filename}'"
end
end

def data_restore(filename)
set_psql_env
command = 'pg_restore --disable-triggers -a -x -O -d ' \
"#{Shellwords.escape(configuration['database'])} " \
"#{Shellwords.escape(filename)}"
raise 'Error during data_restore ' unless Kernel.system(command)
$stdout.puts "Data from '#{filename}' has been restored to \
unless Kernel.system(command)
raise 'Error during data_restore '
else
$stdout.puts "Data from '#{filename}' has been restored to \
'#{configuration['database']}'"
end
end

def structure_and_data_dump(filename)
Expand Down Expand Up @@ -115,15 +117,13 @@ def structure_and_data_restore(filename)
"has been restored to '#{filename}'"
end
end

end
end
end

module ActiveRecord
module Tasks
module DatabaseTasks

def perform_pg_db_task_for_config_and_filename(task_name, *arguments)
configuration = arguments.first
filename = arguments.delete_at 1
Expand All @@ -134,7 +134,6 @@ def perform_pg_db_task_for_config_and_filename(task_name, *arguments)
rescue Exception => error
$stderr.puts error, *(error.backtrace)
end

end
end
end
4 changes: 2 additions & 2 deletions lib/pg_tasks/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#--
# Copyright (c) 2015 Thomas Schank
#
# Released to the public under the terms of the MIT license.
# See MIT-LICENSE.
# Released to the public under the terms of the MIT license.
# See MIT-LICENSE.
#
#++

Expand Down
6 changes: 3 additions & 3 deletions lib/pg_tasks/version.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#--
# Copyright (c) 2015 Thomas Schank
#
# Released to the public under the terms of the MIT license.
# See MIT-LICENSE.
# Released to the public under the terms of the MIT license.
# See MIT-LICENSE.
#
#++

module PgTasks
VERSION = "1.0.1"
VERSION = '1.0.1'
end
12 changes: 2 additions & 10 deletions lib/tasks/pg_tasks.rake
Original file line number Diff line number Diff line change
@@ -1,45 +1,37 @@
#--
# Copyright (c) 2015 Thomas Schank
#
# Released to the public under the terms of the MIT license.
# See MIT-LICENSE.
# Released to the public under the terms of the MIT license.
# See MIT-LICENSE.
#
#++

require 'pg_tasks'

namespace :db do

namespace :pg do

task truncate_tables: [:environment, :load_config] do
PgTasks.truncate_tables
end

namespace :data do

task dump: [:environment, :load_config] do
PgTasks.data_dump ENV['FILE']
end

task restore: [:environment, :load_config] do
PgTasks.data_restore ENV['FILE']
end

end

namespace :structure_and_data do

task dump: [:environment, :load_config] do
PgTasks.structure_and_data_dump ENV['FILE']
end

task restore: [:environment, :load_config] do
PgTasks.structure_and_data_restore ENV['FILE']
end

end

end

end
Loading

0 comments on commit e5f44c2

Please sign in to comment.