active-dump 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --markup markdown
data/README.md ADDED
@@ -0,0 +1,107 @@
1
+ []: {{{1
2
+
3
+ File : README.md
4
+ Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
+ Date : 2013-11-13
6
+
7
+ Copyright : Copyright (C) 2013 Felix C. Stegerman
8
+ Version : v0.0.1
9
+
10
+ []: }}}1
11
+
12
+ [![Gem Version](https://badge.fury.io/rb/active-dump.png)](http://badge.fury.io/rb/active-dump)
13
+
14
+ ## Description
15
+ []: {{{1
16
+
17
+ active-dump - dump and restore activerecord from/to yaml
18
+
19
+ active-dump provides 2 rake tasks that allow you to dump and restore
20
+ activerecord data from/to yaml:
21
+
22
+ * `rake db:data:dump` creates a yaml dump
23
+ * `rake db:data:restore` restores a yaml dump
24
+
25
+ #
26
+
27
+ NB: active-dump does not take migrations (or validations etc.) into
28
+ account -- it dumps and restores raw data from the ActiveRecord
29
+ models -- so make sure your migrations are in sync.
30
+
31
+ When using rails, add this to your Gemfile:
32
+
33
+ ```ruby
34
+ gem 'active-dump', require: 'active-dump/rails'
35
+ ```
36
+
37
+ []: {{{2
38
+
39
+ You can use these environment variables to configure active-dump:
40
+
41
+ * `RAILS_ENV`: the environment
42
+ * `FILE`: the file to dump to/restore from (defaults to
43
+ `db/data.yml`)
44
+ * `MODELS`: the models to dump (defaults to all models)
45
+
46
+ #
47
+
48
+ Configuration files:
49
+
50
+ * `config/active-dump.yml`: the default file and models
51
+ * `config/database.yml`: database connection
52
+
53
+ []: }}}2
54
+
55
+ []: }}}1
56
+
57
+ ## Examples
58
+ []: {{{1
59
+
60
+ $ rake db:data:dump MODELS=Foo,Bar
61
+ $ RAILS_ENV=production rake db:data:load
62
+
63
+ `config/active-dump.yml`:
64
+
65
+ ```yaml
66
+ file: db/dump.yml
67
+ models:
68
+ - Cms::Block
69
+ - Cms::Categorization
70
+ - Cms::Category
71
+ - Cms::File
72
+ - Cms::Layout
73
+ - Cms::Page
74
+ - Cms::Revision
75
+ - Cms::Site
76
+ - Cms::Snippet
77
+ ```
78
+
79
+ []: }}}1
80
+
81
+ ## Specs & Docs
82
+
83
+ $ rake spec # TODO
84
+ $ rake docs
85
+
86
+ ## TODO
87
+
88
+ * specs/docs?
89
+ * use model's database connection?!
90
+ * ...
91
+
92
+ ## License
93
+
94
+ GPLv2 [1] or EPLv1 [2].
95
+
96
+ ## References
97
+ []: {{{1
98
+
99
+ [1] GNU General Public License, version 2
100
+ --- http://www.opensource.org/licenses/GPL-2.0
101
+
102
+ [2] Eclipse Public License, version 1
103
+ --- http://www.opensource.org/licenses/EPL-1.0
104
+
105
+ []: }}}1
106
+
107
+ []: ! ( vim: set tw=70 sw=2 sts=2 et fdm=marker : )
data/Rakefile ADDED
@@ -0,0 +1,59 @@
1
+ desc 'Run specs'
2
+ task :spec do
3
+ sh 'rspec -c'
4
+ end
5
+
6
+ desc 'Run specs verbosely'
7
+ task 'spec:verbose' do
8
+ sh 'rspec -cfd'
9
+ end
10
+
11
+ desc 'Run specs verbosely, view w/ less'
12
+ task 'spec:less' do
13
+ sh 'rspec -cfd --tty | less -R'
14
+ end
15
+
16
+ desc 'Check for warnings'
17
+ task :warn do
18
+ reqs = %w{ config }.map { |x| "-r active-dump/#{x}" } * ' '
19
+ sh "ruby -w -I lib #{reqs} -e ''"
20
+ end
21
+
22
+ desc 'Check for warnings in specs'
23
+ task 'warn:spec' do
24
+ reqs = Dir['spec/**/*.rb'].sort.map { |x| "-r ./#{x}" } * ' '
25
+ sh "ruby -w -I lib -r rspec #{reqs} -e ''"
26
+ end
27
+
28
+ desc 'Check for warnings in specs (but not void context)'
29
+ task 'warn:spec:novoid' do
30
+ sh 'rake warn:spec 2>&1 | grep -v "void context"'
31
+ end
32
+
33
+ desc 'Generate docs'
34
+ task :docs do
35
+ sh 'yardoc | cat'
36
+ end
37
+
38
+ desc 'List undocumented objects'
39
+ task 'docs:undoc' do
40
+ sh 'yard stats --list-undoc'
41
+ end
42
+
43
+ desc 'Cleanup'
44
+ task :clean do
45
+ sh 'rm -rf .yardoc/ doc/ *.gem'
46
+ end
47
+
48
+ desc 'Build SNAPSHOT gem'
49
+ task :snapshot do
50
+ v = Time.new.strftime '%Y%m%d%H%M%S'
51
+ f = 'lib/active-dump/version.rb'
52
+ sh "sed -ri~ 's!(SNAPSHOT)!\\1.#{v}!' #{f}"
53
+ sh 'gem build active-dump.gemspec'
54
+ end
55
+
56
+ desc 'Undo SNAPSHOT gem'
57
+ task 'snapshot:undo' do
58
+ sh 'git checkout -- lib/active-dump/version.rb'
59
+ end
@@ -0,0 +1,32 @@
1
+ require File.expand_path('../lib/active-dump/version', __FILE__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'active-dump'
5
+ s.homepage = 'https://github.com/obfusk/active-dump'
6
+ s.summary = 'dump and restore activerecord from/to yaml'
7
+
8
+ s.description = <<-END.gsub(/^ {4}/, '')
9
+ dump and restore activerecord from/to yaml
10
+
11
+ ...
12
+ END
13
+
14
+ s.version = ActiveDump::VERSION
15
+ s.date = ActiveDump::DATE
16
+
17
+ s.authors = [ 'Felix C. Stegerman' ]
18
+ s.email = %w{ flx@obfusk.net }
19
+
20
+ s.licenses = %w{ GPLv2 EPLv1 }
21
+
22
+ s.files = %w{ .yardopts README.md Rakefile } \
23
+ + %w{ active-dump.gemspec } \
24
+ + Dir['{lib,spec}/**/*.rb']
25
+
26
+ s.add_runtime_dependency 'activerecord'
27
+ s.add_runtime_dependency 'rake'
28
+
29
+ s.add_development_dependency 'rspec'
30
+
31
+ s.required_ruby_version = '>= 1.9.1'
32
+ end
@@ -0,0 +1,110 @@
1
+ # -- ; {{{1
2
+ #
3
+ # File : active-dump.rb
4
+ # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
+ # Date : 2013-11-13
6
+ #
7
+ # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
+ # Licence : GPLv2 or EPLv1
9
+ #
10
+ # -- ; }}}1
11
+
12
+ require 'active_record'
13
+ require 'yaml'
14
+
15
+ # namespace
16
+ module ActiveDump
17
+
18
+ CFG_DB = 'config/database.yml'
19
+ CFG_DUMP = 'config/active-dump.yml'
20
+ DUMP = 'db/data.yml'
21
+
22
+ # --
23
+
24
+ # dump to yaml
25
+ def self.dump(c) # {{{1
26
+ data = Hash[ c['models'].map do |m|
27
+ model = m.constantize
28
+ records = model.all.map(&:attributes)
29
+ [m, records]
30
+ end ]
31
+ File.write c['file'], YAML.dump(data)
32
+ end # }}}1
33
+
34
+ # restore from yaml
35
+ def self.restore(c) # {{{1
36
+ data = YAML.load File.read(c['file'])
37
+ conn = connection
38
+ ActiveRecord::Base.transaction do
39
+ data.each do |m,records|
40
+ model = m.constantize
41
+ table = model.quoted_table_name
42
+ records.each do |record|
43
+ cols = record.keys.map { |k| conn.quote_column_name k }
44
+ vals = record.values.map { |v| conn.quote v }
45
+ insert conn, table, cols, vals
46
+ end
47
+ end
48
+ end
49
+ end # }}}1
50
+
51
+ # --
52
+
53
+ # insert data; make sure everything is quoted !!!
54
+ def self.insert(conn, table, cols, vals)
55
+ sql = "INSERT INTO #{table} (#{cols*','}) VALUES (#{vals*','})"
56
+ conn.execute sql
57
+ end
58
+
59
+ # --
60
+
61
+ # get all existing models' names
62
+ def self.all_models # {{{1
63
+ return @models if @models
64
+ eager_load!
65
+ @models = ActiveRecord::Base.descendants.select do |m|
66
+ (m.to_s != 'ActiveRecord::SchemaMigration') && \
67
+ m.table_exists? && m.exists?
68
+ end .map(&:to_s)
69
+ end # }}}1
70
+
71
+ # configuration: file + models
72
+ def self.config(file, models = nil) # {{{1
73
+ nb = ->(x) { x && !x.blank? }
74
+ ne = ->(x) { x && !x.empty? }
75
+ c = File.exists?(CFG_DUMP) ? YAML.load(File.read(CFG_DUMP)) : {}
76
+ c['file'] = file if nb[file]
77
+ c['models'] = models if ne[models]
78
+ c['file'] = DUMP unless nb[c['file']]
79
+ c['models'] = all_models unless ne[c['models']]
80
+ c
81
+ end # }}}1
82
+
83
+ # ActiveRecord connection
84
+ # @todo use model?!
85
+ def self.connection # {{{1
86
+ return @connection if @connection
87
+ unless ActiveRecord::Base.connected?
88
+ c = YAML.load File.read(CFG_DB)
89
+ ActiveRecord::Base.establish_connection c[env]
90
+ end
91
+ @connection = ActiveRecord::Base.connection
92
+ end # }}}1
93
+
94
+ # eager_load! all rails engines' models (if Rails is defined)
95
+ def self.eager_load! # {{{1
96
+ return false if @eager_load
97
+ Rails::Engine::Railties.engines.each do |e|
98
+ e.eager_load!
99
+ end if defined? Rails
100
+ @eager_load = true
101
+ end # }}}1
102
+
103
+ # like Rails.env; (cached)
104
+ def self.env
105
+ @env ||= ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
106
+ end
107
+
108
+ end
109
+
110
+ # vim: set tw=70 sw=2 sts=2 et fdm=marker :
@@ -0,0 +1,26 @@
1
+ # -- ; {{{1
2
+ #
3
+ # File : active-dump/rails.rb
4
+ # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
+ # Date : 2013-11-13
6
+ #
7
+ # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
+ # Licence : GPLv2 or EPLv1
9
+ #
10
+ # -- ; }}}1
11
+
12
+ require 'rails'
13
+
14
+ require 'active-dump/rake'
15
+
16
+ # namespace
17
+ module ActiveDump
18
+
19
+ # railtie that adds the rake tasks
20
+ class Railtie < Rails::Railtie
21
+ rake_tasks { ActiveDump::Rake.define_tasks }
22
+ end
23
+
24
+ end
25
+
26
+ # vim: set tw=70 sw=2 sts=2 et fdm=marker :
@@ -0,0 +1,47 @@
1
+ # -- ; {{{1
2
+ #
3
+ # File : active-dump/rake.rb
4
+ # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
+ # Date : 2013-11-13
6
+ #
7
+ # Copyright : Copyright (C) 2013 Felix C. Stegerman
8
+ # Licence : GPLv2 or EPLv1
9
+ #
10
+ # -- ; }}}1
11
+
12
+ require 'rake/dsl_definition'
13
+
14
+ require 'active-dump'
15
+
16
+ # namespace
17
+ module ActiveDump
18
+
19
+ # rake tasks
20
+ module Rake
21
+ extend ::Rake::DSL
22
+
23
+ # define rake tasks
24
+ def self.define_tasks
25
+ namespace :db do
26
+ namespace :data do
27
+ desc 'Dump data'
28
+ task :dump => :environment do
29
+ m = ENV['MODELS']
30
+ c = ActiveDump.config ENV['FILE'], (m && m.split(','))
31
+ ActiveDump.dump c
32
+ end
33
+
34
+ desc 'Restore data'
35
+ task :restore => :environment do
36
+ c = ActiveDump.config ENV['FILE']
37
+ ActiveDump.restore c
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ end
44
+
45
+ end
46
+
47
+ # vim: set tw=70 sw=2 sts=2 et fdm=marker :
@@ -0,0 +1,4 @@
1
+ module ActiveDump
2
+ VERSION = '0.0.1'
3
+ DATE = '2013-11-13'
4
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active-dump
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Felix C. Stegerman
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-11-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: &11520100 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *11520100
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &11519340 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *11519340
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &11517020 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *11517020
47
+ description: ! 'dump and restore activerecord from/to yaml
48
+
49
+
50
+ ...
51
+
52
+ '
53
+ email:
54
+ - flx@obfusk.net
55
+ executables: []
56
+ extensions: []
57
+ extra_rdoc_files: []
58
+ files:
59
+ - .yardopts
60
+ - README.md
61
+ - Rakefile
62
+ - active-dump.gemspec
63
+ - lib/active-dump/rake.rb
64
+ - lib/active-dump/version.rb
65
+ - lib/active-dump/rails.rb
66
+ - lib/active-dump.rb
67
+ homepage: https://github.com/obfusk/active-dump
68
+ licenses:
69
+ - GPLv2
70
+ - EPLv1
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: 1.9.1
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 1.8.11
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: dump and restore activerecord from/to yaml
93
+ test_files: []
94
+ has_rdoc: