copydb 0.0.0 → 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.
Files changed (3) hide show
  1. data/lib/copydb.rb +77 -4
  2. data/lib/tasks/copydb.rake +15 -0
  3. metadata +3 -2
data/lib/copydb.rb CHANGED
@@ -1,5 +1,78 @@
1
- class Copydb
2
- def self.hi
3
- puts "Hello world!"
1
+ require 'rubygems'
2
+ require 'yaml'
3
+ require 'active_record'
4
+ require 'active_support/core_ext/kernel/reporting'
5
+ require 'rails/railtie'
6
+
7
+ module CopyDb
8
+
9
+ class DumpDb
10
+ def dump
11
+ o = File.new(File.expand_path('tmp/copydb_dumped_data.yml'), "w+")
12
+ yml = [self.schema_version]
13
+ self.tables.each do |table|
14
+ puts "Dumping table: #{table}"
15
+ yml << self.table_dump(table)
16
+ end
17
+ o.write(yml.to_yaml)
18
+ o.close
19
+ end
20
+
21
+ def tables
22
+ ActiveRecord::Base.connection.tables.reject { |table| ['schema_info', 'schema_migrations'].include?(table) }
23
+ end
24
+
25
+ def table_dump(table)
26
+ rs = ActiveRecord::Base.connection.execute("SELECT * FROM #{table}")
27
+ yml = Array.new
28
+ yml << table
29
+ rs.each do |r|
30
+ yml << r
31
+ end
32
+ yml
33
+ end
34
+
35
+ def schema_version
36
+ ActiveRecord::Migrator.current_version
37
+ end
4
38
  end
5
- end
39
+
40
+ class LoadDb
41
+ def load
42
+ if FileTest.exists?(File.expand_path('tmp/copydb_dumped_data.yml'))
43
+
44
+ yml = YAML.load_file(File.expand_path('tmp/copydb_dumped_data.yml'))
45
+
46
+
47
+ yml.each_with_index do |entry,i|
48
+ if i == 0
49
+ puts entry
50
+ end
51
+ unless entry[1].nil?
52
+ quoted_column_names = entry[1].each_key.to_a.map { |column| ActiveRecord::Base.connection.quote_column_name(column) }.join(',')
53
+
54
+ for i in 1..(entry.length-1)
55
+ quoted_column_values = entry[i].each_value.to_a.map { |record| ActiveRecord::Base.connection.quote(record) }.join(',')
56
+
57
+ sql_string = "INSERT INTO #{entry[0]} (#{quoted_column_names}) VALUES (#{quoted_column_values});"
58
+ #ActiveRecord::Base.connection.execute(sql_string)
59
+ end
60
+ end
61
+ end
62
+ else
63
+ puts "ERROR: dump file not found"
64
+ exit 1
65
+ end
66
+ end
67
+
68
+ def schema_version
69
+ ActiveRecord::Migrator.current_version
70
+ end
71
+ end
72
+
73
+ class Railtie < Rails::Railtie
74
+ rake_tasks do
75
+ load File.expand_path('../tasks/copydb.rake',__FILE__)
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,15 @@
1
+ namespace :db do
2
+ namespace :copydb do
3
+ desc "Dump database content"
4
+ task :dump => :environment do
5
+ dumper = CopyDb::DumpDb.new
6
+ dumper.dump
7
+ end
8
+
9
+ desc "Load database content"
10
+ task :load => :environment do
11
+ loader = CopyDb::LoadDb.new
12
+ loader.load
13
+ end
14
+ end
15
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: copydb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -20,8 +20,9 @@ extensions: []
20
20
  extra_rdoc_files: []
21
21
  files:
22
22
  - lib/copydb.rb
23
+ - lib/tasks/copydb.rake
23
24
  has_rdoc: true
24
- homepage: http://www.geisterstunde.org
25
+ homepage: https://github.com/ceicke/copydb
25
26
  licenses: []
26
27
  post_install_message:
27
28
  rdoc_options: []