snapshots 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Simon Menke
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,18 @@
1
+ = snapshots
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but
13
+ bump version in a commit by itself I can ignore when I pull)
14
+ * Send me a pull request. Bonus points for topic branches.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2009 Simon Menke. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "snapshots"
8
+ gem.summary = %Q{Make snapshots of your ActiveRecord database}
9
+ gem.description = %Q{Dump your rails databases in a database agnostic format (ruby).}
10
+ gem.email = "simon@mrhenry.be"
11
+ gem.homepage = "http://github.com/simonmenke/snapshots"
12
+ gem.authors = ["Simon Menke"]
13
+ gem.add_development_dependency "thoughtbot-shoulda"
14
+ gem.add_development_dependency "yard"
15
+ gem.add_runtime_dependency "thor"
16
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
21
+ end
22
+
23
+ require 'rake/testtask'
24
+ Rake::TestTask.new(:test) do |test|
25
+ test.libs << 'lib' << 'test'
26
+ test.pattern = 'test/**/*_test.rb'
27
+ test.verbose = true
28
+ end
29
+
30
+ begin
31
+ require 'rcov/rcovtask'
32
+ Rcov::RcovTask.new do |test|
33
+ test.libs << 'test'
34
+ test.pattern = 'test/**/*_test.rb'
35
+ test.verbose = true
36
+ end
37
+ rescue LoadError
38
+ task :rcov do
39
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
40
+ end
41
+ end
42
+
43
+ task :test => :check_dependencies
44
+
45
+ task :default => :test
46
+
47
+ begin
48
+ require 'yard'
49
+ YARD::Rake::YardocTask.new
50
+ rescue LoadError
51
+ task :yardoc do
52
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
53
+ end
54
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/bin/snapshots ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ begin
4
+ require 'rubygems'
5
+ require 'snapshots'
6
+ rescue LoadError
7
+ require File.join(File.dirname(__FILE__), *%w[ .. lib snapshots ])
8
+ end
9
+
10
+ Snapshots::App.start
@@ -0,0 +1,92 @@
1
+
2
+ module Snapshots
3
+ class App < Thor
4
+
5
+ class_option :app, :default => '.', :type => :string,
6
+ :desc => 'Path to the rails application',
7
+ :banner => 'rails_root',
8
+ :group => 'Global'
9
+
10
+ desc 'list', 'List all snapshots for a rails app.'
11
+ def list
12
+ goto_rails(self.options.app) do
13
+
14
+ Snapshots.find.each do |snapshot|
15
+ snapshot =~ /snapshot_(\d+)\.rb$/
16
+ timestamp = Time.at($1.to_i)
17
+ puts "#{$1} @ #{timestamp} : #{snapshot}"
18
+ end
19
+
20
+ end
21
+ end
22
+
23
+ desc 'dump', 'Make a new snapshot.'
24
+ def dump
25
+ goto_rails(self.options.app) do
26
+ load_environment!
27
+
28
+ begin
29
+ Snapshots.dump
30
+ puts "Snapshot was successfully created."
31
+ rescue => e
32
+ puts "An error occured while creating snapshot."
33
+ puts "#{e.class}: #{e.message}"
34
+ puts e.backtrace
35
+ end
36
+ end
37
+ end
38
+
39
+ desc 'load VERSION', 'Restore a snapshot.'
40
+ def load(version)
41
+ goto_rails(self.options.app) do
42
+ load_environment!
43
+
44
+ begin
45
+ Snapshots.load(version)
46
+ puts "Snapshot #{version} was successfully restored."
47
+ rescue => e
48
+ puts "An error occured while restoring snapshot #{version}."
49
+ puts "#{e.class}: #{e.message}"
50
+ puts e.backtrace
51
+ end
52
+ end
53
+ end
54
+
55
+ desc 'install', 'Install rake tasks in rails app.'
56
+ def install
57
+ goto_rails(self.options.app) do
58
+ File.open('lib/tasks/snapshots.rake', 'w+') do |f|
59
+ f.write <<-EOR
60
+ begin
61
+ require 'rubygems'
62
+ require 'snapshots/tasks'
63
+ rescue LoadError
64
+ puts "Please install snapshots: [sudo] gem install snapshots"
65
+ end
66
+ EOR
67
+ end
68
+ puts "Rake tasks are successfully installed."
69
+ end
70
+ end
71
+
72
+ private
73
+
74
+ def load_environment!
75
+ $rails_rake_task = true
76
+ require(File.join(RAILS_ROOT, 'config', 'environment'))
77
+ end
78
+
79
+ def goto_rails(root_path, &proc)
80
+ root_path = File.expand_path(root_path)
81
+
82
+ unless File.directory?(root_path) and File.file?(File.join(root_path, 'config', 'environment.rb'))
83
+ $stderr.puts "This is not a rails application!"
84
+ exit(1)
85
+ end
86
+
87
+ Object.const_set('RAILS_ROOT', root_path)
88
+ Dir.chdir(root_path, &proc)
89
+ end
90
+
91
+ end
92
+ end
@@ -0,0 +1,74 @@
1
+
2
+ module Snapshots
3
+ class DatabaseDumper < ActiveRecord::SchemaDumper
4
+
5
+ def dump(stream)
6
+ super(stream)
7
+ tables_for_data(stream)
8
+ stream
9
+ end
10
+
11
+ def header(stream)
12
+ stream.puts <<-EOC
13
+ # This file was generated by the Snapshots::DatabaseDumper and
14
+ # can be loaded with the Snapshots::DatabaseLoader.
15
+
16
+ ActiveRecord::Schema.define do
17
+
18
+ create_table "schema_migrations", :id => false, :force => true do |t|
19
+ t.string "version", :null => false
20
+ end
21
+
22
+ add_index "schema_migrations", ["version"], :name => "unique_schema_migrations", :unique => true
23
+
24
+ EOC
25
+ end
26
+
27
+ private
28
+
29
+ def tables_for_data(stream)
30
+ stream.puts "\nSnapshots::DatabaseLoader.load do\n\n"
31
+ @connection.tables.sort.each do |tbl|
32
+ next if [ignore_tables].flatten.any? do |ignored|
33
+ case ignored
34
+ when String; tbl == ignored
35
+ when Regexp; tbl =~ ignored
36
+ else
37
+ raise StandardError, 'ActiveRecord::SchemaDumper.ignore_tables accepts an array of String and / or Regexp values.'
38
+ end
39
+ end
40
+ table_for_data(tbl, stream)
41
+ end
42
+ stream.puts "end\n"
43
+ end
44
+
45
+ def table_for_data(table, stream)
46
+ columns = @connection.columns(table)
47
+
48
+ column_names = columns.collect { |column| column.name }
49
+
50
+ rows = @connection.select_rows("SELECT #{column_names.join(', ')} FROM #{table}")
51
+
52
+ stream.puts " for_table #{table.inspect} do |t|"
53
+ rows.each do |row|
54
+ stream.print " t.insert(\n"
55
+ columns.each_with_index do |column, idx|
56
+ cell = column.type_cast(row[idx])
57
+ cell = case cell
58
+ when DateTime then "#{cell.to_formatted_s(:rfc822).inspect}.to_datetime"
59
+ when Date then "#{cell.to_formatted_s(:rfc822).inspect}.to_date"
60
+ when Time then "#{cell.to_formatted_s(:rfc822).inspect}.to_time"
61
+ else cell.inspect
62
+ end
63
+ stream.print " :#{column.name} => #{cell}"
64
+ stream.print ',' unless idx == (columns.size - 1)
65
+ stream.print "\n"
66
+ end
67
+ stream.print " )\n"
68
+ stream.flush
69
+ end
70
+ stream.puts " end\n\n"
71
+ end
72
+
73
+ end
74
+ end
@@ -0,0 +1,61 @@
1
+
2
+ module Snapshots
3
+ class DatabaseLoader
4
+
5
+ def self.load(connection=ActiveRecord::Base.connection, &proc)
6
+ new(connection).load(&proc)
7
+ end
8
+
9
+ def initialize(connection)
10
+ @connection = connection
11
+ end
12
+
13
+ def load(&proc)
14
+ instance_eval(&proc)
15
+ end
16
+
17
+ def for_table(name, &proc)
18
+ Snapshots::TableLoader.load(@connection, name, &proc)
19
+ end
20
+
21
+ end
22
+
23
+ class TableLoader
24
+
25
+ def self.load(connection, table, &proc)
26
+ new(connection, table).load(&proc)
27
+ end
28
+
29
+ def initialize(connection, table)
30
+ @table = table
31
+ @connection = connection
32
+ @columns = @connection.columns(table)
33
+ end
34
+
35
+ def load
36
+ @connection.execute("DELETE FROM #{@table}")
37
+ yield(self)
38
+ end
39
+
40
+ def insert(attributes={})
41
+ quoted_attributes = attributes_with_quotes(attributes)
42
+
43
+ return if quoted_attributes.empty?
44
+
45
+ column_names = @columns.collect { |column| column.name }
46
+ statement = "INSERT INTO #{@table} (#{column_names.join(', ')}) VALUES(#{quoted_attributes.join(', ')})"
47
+
48
+ @connection.insert(statement, "#{@table} Create")
49
+ end
50
+
51
+ def attributes_with_quotes(attributes={})
52
+ quoted = []
53
+ @columns.each do |column|
54
+ value = attributes[column.name.to_sym]
55
+ quoted.push @connection.quote(value, column)
56
+ end
57
+ quoted
58
+ end
59
+
60
+ end
61
+ end
@@ -0,0 +1,30 @@
1
+
2
+ require 'fileutils'
3
+
4
+ desc "List all snapshot"
5
+ task :snapshots => :environment do
6
+ Snapshots.find.each do |snapshot|
7
+ snapshot =~ /snapshot_(\d+)\.rb$/
8
+ timestamp = Time.at($1.to_i)
9
+ puts "#{snapshot} @ #{timestamp}"
10
+ end
11
+ end
12
+
13
+ namespace :snapshots do
14
+
15
+ desc "Create a snapshot"
16
+ task :dump => :environment do
17
+ Snapshots.dump
18
+ end
19
+
20
+ desc "Load a snapshot. VERSION specifies the snapshot"
21
+ task :load => :environment do
22
+ if ENV['VERSION'].blank?
23
+ puts "Please use VERSION to specify which snapshot you want to load."
24
+
25
+ else
26
+ Snapshots.load
27
+ end
28
+ end
29
+
30
+ end
data/lib/snapshots.rb ADDED
@@ -0,0 +1,44 @@
1
+
2
+ $:.unshift(File.expand_path(File.dirname(__FILE__))) unless $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require 'rubygems'
5
+ require 'thor'
6
+
7
+ module Snapshots
8
+ autoload :DatabaseDumper, 'snapshots/database_dumper'
9
+ autoload :DatabaseLoader, 'snapshots/database_loader'
10
+ autoload :App, 'snapshots/app'
11
+
12
+ class << self
13
+
14
+ def find(path="db/snapshots/snapshot_*.rb")
15
+ Dir.glob(path)
16
+ end
17
+
18
+ def load(version_or_path)
19
+ unless String === version_or_path
20
+ raise ArgumentError, "argument must be a String"
21
+ end
22
+
23
+ path = if version_or_path =~ /\.rb$/
24
+ version_or_path
25
+ else
26
+ "db/snapshots/snapshot_#{version_or_path}.rb"
27
+ end
28
+
29
+ unless File.exists?(path)
30
+ raise LoadError, "missing snapshot: #{path}"
31
+ end
32
+
33
+ Kernel.load(path)
34
+ end
35
+
36
+ def dump(snapshot_path="db/snapshots/snapshot_#{Time.now.to_i}.rb", connection=ActiveRecord::Base.connection)
37
+ FileUtils.mkdir_p(File.dirname(snapshot_path))
38
+ File.open(snapshot_path, 'w+') do |f|
39
+ Snapshots::DatabaseDumper.dump(connection, f)
40
+ end
41
+ end
42
+
43
+ end
44
+ end
data/snapshots.gemspec ADDED
@@ -0,0 +1,66 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{snapshots}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Simon Menke"]
12
+ s.date = %q{2009-09-21}
13
+ s.default_executable = %q{snapshots}
14
+ s.description = %q{Dump your rails databases in a database agnostic format (ruby).}
15
+ s.email = %q{simon@mrhenry.be}
16
+ s.executables = ["snapshots"]
17
+ s.extra_rdoc_files = [
18
+ "LICENSE",
19
+ "README.rdoc"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ ".gitignore",
24
+ "LICENSE",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "bin/snapshots",
29
+ "lib/snapshots.rb",
30
+ "lib/snapshots/app.rb",
31
+ "lib/snapshots/database_dumper.rb",
32
+ "lib/snapshots/database_loader.rb",
33
+ "lib/snapshots/tasks.rb",
34
+ "snapshots.gemspec",
35
+ "test/snapshots_test.rb",
36
+ "test/test_helper.rb"
37
+ ]
38
+ s.homepage = %q{http://github.com/simonmenke/snapshots}
39
+ s.rdoc_options = ["--charset=UTF-8"]
40
+ s.require_paths = ["lib"]
41
+ s.rubygems_version = %q{1.3.5}
42
+ s.summary = %q{Make snapshots of your ActiveRecord database}
43
+ s.test_files = [
44
+ "test/snapshots_test.rb",
45
+ "test/test_helper.rb"
46
+ ]
47
+
48
+ if s.respond_to? :specification_version then
49
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
50
+ s.specification_version = 3
51
+
52
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
53
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
54
+ s.add_development_dependency(%q<yard>, [">= 0"])
55
+ s.add_runtime_dependency(%q<thor>, [">= 0"])
56
+ else
57
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
58
+ s.add_dependency(%q<yard>, [">= 0"])
59
+ s.add_dependency(%q<thor>, [">= 0"])
60
+ end
61
+ else
62
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
63
+ s.add_dependency(%q<yard>, [">= 0"])
64
+ s.add_dependency(%q<thor>, [">= 0"])
65
+ end
66
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class SnapshotsTest < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'snapshots'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: snapshots
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Simon Menke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-21 00:00:00 +02:00
13
+ default_executable: snapshots
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: yard
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: thor
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ description: Dump your rails databases in a database agnostic format (ruby).
46
+ email: simon@mrhenry.be
47
+ engine_dependencies: {}
48
+
49
+ executables:
50
+ - snapshots
51
+ extensions: []
52
+
53
+ extra_rdoc_files:
54
+ - LICENSE
55
+ - README.rdoc
56
+ files:
57
+ - .document
58
+ - .gitignore
59
+ - LICENSE
60
+ - README.rdoc
61
+ - Rakefile
62
+ - VERSION
63
+ - bin/snapshots
64
+ - lib/snapshots.rb
65
+ - lib/snapshots/app.rb
66
+ - lib/snapshots/database_dumper.rb
67
+ - lib/snapshots/database_loader.rb
68
+ - lib/snapshots/tasks.rb
69
+ - snapshots.gemspec
70
+ - test/snapshots_test.rb
71
+ - test/test_helper.rb
72
+ has_rdoc: true
73
+ homepage: http://github.com/simonmenke/snapshots
74
+ licenses: []
75
+
76
+ post_install_message:
77
+ rdoc_options:
78
+ - --charset=UTF-8
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: "0"
86
+ version:
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: "0"
92
+ version:
93
+ requirements: []
94
+
95
+ rubyforge_project:
96
+ rubygems_version: 1.3.5
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: Make snapshots of your ActiveRecord database
100
+ test_files:
101
+ - test/snapshots_test.rb
102
+ - test/test_helper.rb