yaml_db 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/{README → README.markdown} +20 -7
- data/Rakefile +4 -3
- data/VERSION +1 -1
- data/lib/serialization_helper.rb +3 -1
- data/{tasks → lib/tasks}/yaml_db_tasks.rake +0 -0
- data/lib/yaml_db.rb +9 -1
- data/spec/base.rb +7 -4
- data/spec/serialization_helper_load_spec.rb +14 -8
- data/spec/yaml_load_spec.rb +1 -0
- data/yaml_db.gemspec +7 -7
- metadata +20 -8
data/{README → README.markdown}
RENAMED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
# YamlDb
|
2
2
|
|
3
3
|
YamlDb is a database-independent format for dumping and restoring data. It complements the the database-independent schema format found in db/schema.rb. The data is saved into db/data.yml.
|
4
4
|
|
@@ -6,14 +6,27 @@ This can be used as a replacement for mysqldump or pg_dump, but only for the dat
|
|
6
6
|
|
7
7
|
Any database that has an ActiveRecord adapter should work.
|
8
8
|
|
9
|
-
|
9
|
+
## Installation
|
10
10
|
|
11
|
-
|
12
|
-
rake db:data:load -> Load contents of db/data.yml into the database
|
11
|
+
Simply add to your Gemfile:
|
13
12
|
|
14
|
-
|
13
|
+
gem 'yaml_db'
|
15
14
|
|
16
|
-
|
15
|
+
All rake tasks will then be available to you.
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
rake db:data:dump -> Dump contents of Rails database to db/data.yml
|
20
|
+
rake db:data:load -> Load contents of db/data.yml into the database
|
21
|
+
|
22
|
+
Further, there are tasks db:dump and db:load which do the entire database (the equivalent of running db:schema:dump followed by db:data:load). Also, there are other tasks recently added that allow the export of the database contents to/from multiple files (each one named after the table being dumped or loaded).
|
23
|
+
|
24
|
+
rake db:data:dump_dir -> Dump contents of database to curr_dir_name/tablename.extension (defaults to yaml)
|
25
|
+
rake db:data:load_dir -> Load contents of db/data_dir into database
|
26
|
+
|
27
|
+
In addition, we have plugins whereby you can export your database to/from various formats. We only deal with yaml and csv right now, but you can easily write tools for your own formats (such as Excel or XML). To use another format, just load setting the "class" parameter to the class you are using. This defaults to "YamlDb::Helper" which is a refactoring of the old yaml_db code. We'll shorten this to use class nicknames in a little bit.
|
28
|
+
|
29
|
+
## Examples
|
17
30
|
|
18
31
|
One common use would be to switch your data from one database backend to another. For example, let's say you wanted to switch from SQLite to MySQL. You might execute the following steps:
|
19
32
|
|
@@ -25,7 +38,7 @@ One common use would be to switch your data from one database backend to another
|
|
25
38
|
|
26
39
|
4. rake db:load
|
27
40
|
|
28
|
-
|
41
|
+
## Credits
|
29
42
|
|
30
43
|
Created by Orion Henry and Adam Wiggins. Major updates by Ricardo Chimal, Jr.
|
31
44
|
|
data/Rakefile
CHANGED
@@ -20,9 +20,10 @@ rescue LoadError
|
|
20
20
|
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
21
21
|
end
|
22
22
|
|
23
|
-
|
24
|
-
Spec::Rake::SpecTask.new(
|
25
|
-
|
23
|
+
require 'spec/rake/spectask'
|
24
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
25
|
+
spec.libs << 'lib' << 'spec'
|
26
|
+
spec.spec_files = FileList['spec/*_spec.rb']
|
26
27
|
end
|
27
28
|
|
28
29
|
task :default => :spec
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/serialization_helper.rb
CHANGED
@@ -79,10 +79,12 @@ module SerializationHelper
|
|
79
79
|
if column_names.nil?
|
80
80
|
return
|
81
81
|
end
|
82
|
+
columns = column_names.map{|cn| ActiveRecord::Base.connection.columns(table).detect{|c| c.name == cn}}
|
82
83
|
quoted_column_names = column_names.map { |column| ActiveRecord::Base.connection.quote_column_name(column) }.join(',')
|
83
84
|
quoted_table_name = SerializationHelper::Utils.quote_table(table)
|
84
85
|
records.each do |record|
|
85
|
-
|
86
|
+
quoted_values = record.zip(columns).map{|c| ActiveRecord::Base.connection.quote(c.first, c.last)}.join(',')
|
87
|
+
ActiveRecord::Base.connection.execute("INSERT INTO #{quoted_table_name} (#{quoted_column_names}) VALUES (#{quoted_values})")
|
86
88
|
end
|
87
89
|
end
|
88
90
|
|
File without changes
|
data/lib/yaml_db.rb
CHANGED
@@ -2,6 +2,7 @@ require 'rubygems'
|
|
2
2
|
require 'yaml'
|
3
3
|
require 'active_record'
|
4
4
|
require 'serialization_helper'
|
5
|
+
require 'active_support/core_ext/kernel/reporting'
|
5
6
|
|
6
7
|
module YamlDb
|
7
8
|
module Helper
|
@@ -64,4 +65,11 @@ module YamlDb
|
|
64
65
|
end
|
65
66
|
end
|
66
67
|
|
67
|
-
|
68
|
+
class Railtie < Rails::Railtie
|
69
|
+
rake_tasks do
|
70
|
+
load File.expand_path('../tasks/yaml_db_tasks.rake',
|
71
|
+
__FILE__)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
data/spec/base.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
|
-
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
3
|
require 'spec'
|
3
|
-
|
4
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
|
4
|
+
require 'spec/autorun'
|
5
5
|
require 'yaml_db'
|
6
|
-
|
6
|
+
|
7
|
+
Spec::Runner.configure do |config|
|
8
|
+
|
9
|
+
end
|
@@ -36,12 +36,15 @@ describe SerializationHelper::Load do
|
|
36
36
|
end
|
37
37
|
|
38
38
|
it "should insert records into a table" do
|
39
|
+
mca = mock('a',:name => 'a')
|
40
|
+
mcb = mock('b', :name => 'b')
|
41
|
+
ActiveRecord::Base.connection.stub!(:columns).with('mytable').and_return([mca , mcb ])
|
39
42
|
ActiveRecord::Base.connection.stub!(:quote_column_name).with('a').and_return('a')
|
40
43
|
ActiveRecord::Base.connection.stub!(:quote_column_name).with('b').and_return('b')
|
41
|
-
ActiveRecord::Base.connection.stub!(:quote).with(1).and_return("'1'")
|
42
|
-
ActiveRecord::Base.connection.stub!(:quote).with(2).and_return("'2'")
|
43
|
-
ActiveRecord::Base.connection.stub!(:quote).with(3).and_return("'3'")
|
44
|
-
ActiveRecord::Base.connection.stub!(:quote).with(4).and_return("'4'")
|
44
|
+
ActiveRecord::Base.connection.stub!(:quote).with(1, mca).and_return("'1'")
|
45
|
+
ActiveRecord::Base.connection.stub!(:quote).with(2, mcb).and_return("'2'")
|
46
|
+
ActiveRecord::Base.connection.stub!(:quote).with(3, mca).and_return("'3'")
|
47
|
+
ActiveRecord::Base.connection.stub!(:quote).with(4, mcb).and_return("'4'")
|
45
48
|
ActiveRecord::Base.connection.should_receive(:execute).with("INSERT INTO mytable (a,b) VALUES ('1','2')")
|
46
49
|
ActiveRecord::Base.connection.should_receive(:execute).with("INSERT INTO mytable (a,b) VALUES ('3','4')")
|
47
50
|
|
@@ -49,12 +52,15 @@ describe SerializationHelper::Load do
|
|
49
52
|
end
|
50
53
|
|
51
54
|
it "should quote column names that correspond to sql keywords" do
|
55
|
+
mca = mock('a',:name => 'a')
|
56
|
+
mccount = mock('count', :name => 'count')
|
57
|
+
ActiveRecord::Base.connection.stub!(:columns).with('mytable').and_return([mca , mccount ])
|
52
58
|
ActiveRecord::Base.connection.stub!(:quote_column_name).with('a').and_return('a')
|
53
59
|
ActiveRecord::Base.connection.stub!(:quote_column_name).with('count').and_return('"count"')
|
54
|
-
ActiveRecord::Base.connection.stub!(:quote).with(1).and_return("'1'")
|
55
|
-
ActiveRecord::Base.connection.stub!(:quote).with(2).and_return("'2'")
|
56
|
-
ActiveRecord::Base.connection.stub!(:quote).with(3).and_return("'3'")
|
57
|
-
ActiveRecord::Base.connection.stub!(:quote).with(4).and_return("'4'")
|
60
|
+
ActiveRecord::Base.connection.stub!(:quote).with(1, mca).and_return("'1'")
|
61
|
+
ActiveRecord::Base.connection.stub!(:quote).with(2, mccount).and_return("'2'")
|
62
|
+
ActiveRecord::Base.connection.stub!(:quote).with(3, mca).and_return("'3'")
|
63
|
+
ActiveRecord::Base.connection.stub!(:quote).with(4, mccount).and_return("'4'")
|
58
64
|
ActiveRecord::Base.connection.should_receive(:execute).with("INSERT INTO mytable (a,\"count\") VALUES ('1','2')")
|
59
65
|
ActiveRecord::Base.connection.should_receive(:execute).with("INSERT INTO mytable (a,\"count\") VALUES ('3','4')")
|
60
66
|
|
data/spec/yaml_load_spec.rb
CHANGED
data/yaml_db.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{yaml_db}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Adam Wiggins", "Orion Henry"]
|
12
|
-
s.date = %q{
|
12
|
+
s.date = %q{2010-09-27}
|
13
13
|
s.description = %q{
|
14
14
|
YamlDb is a database-independent format for dumping and restoring data. It complements the the database-independent schema format found in db/schema.rb. The data is saved into db/data.yml.
|
15
15
|
This can be used as a replacement for mysqldump or pg_dump, but only for the databases typically used by Rails apps. Users, permissions, schemas, triggers, and other advanced database features are not supported - by design.
|
@@ -17,16 +17,17 @@ Any database that has an ActiveRecord adapter should work
|
|
17
17
|
}
|
18
18
|
s.email = %q{nate@ludicast.com}
|
19
19
|
s.extra_rdoc_files = [
|
20
|
-
"README"
|
20
|
+
"README.markdown"
|
21
21
|
]
|
22
22
|
s.files = [
|
23
|
-
"README",
|
23
|
+
"README.markdown",
|
24
24
|
"Rakefile",
|
25
25
|
"VERSION",
|
26
26
|
"about.yml",
|
27
27
|
"init.rb",
|
28
28
|
"lib/csv_db.rb",
|
29
29
|
"lib/serialization_helper.rb",
|
30
|
+
"lib/tasks/yaml_db_tasks.rake",
|
30
31
|
"lib/yaml_db.rb",
|
31
32
|
"spec/base.rb",
|
32
33
|
"spec/serialization_helper_base_spec.rb",
|
@@ -36,13 +37,12 @@ Any database that has an ActiveRecord adapter should work
|
|
36
37
|
"spec/yaml_dump_spec.rb",
|
37
38
|
"spec/yaml_load_spec.rb",
|
38
39
|
"spec/yaml_utils_spec.rb",
|
39
|
-
"tasks/yaml_db_tasks.rake",
|
40
40
|
"yaml_db.gemspec"
|
41
41
|
]
|
42
42
|
s.homepage = %q{http://github.com/ludicast/yaml_db}
|
43
43
|
s.rdoc_options = ["--charset=UTF-8"]
|
44
44
|
s.require_paths = ["lib"]
|
45
|
-
s.rubygems_version = %q{1.3.
|
45
|
+
s.rubygems_version = %q{1.3.7}
|
46
46
|
s.summary = %q{yaml_db allows export/import of database into/from yaml files}
|
47
47
|
s.test_files = [
|
48
48
|
"spec/base.rb",
|
@@ -59,7 +59,7 @@ Any database that has an ActiveRecord adapter should work
|
|
59
59
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
60
60
|
s.specification_version = 3
|
61
61
|
|
62
|
-
if Gem::Version.new(Gem::
|
62
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
63
63
|
else
|
64
64
|
end
|
65
65
|
else
|
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yaml_db
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 23
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- Adam Wiggins
|
@@ -10,7 +16,7 @@ autorequire:
|
|
10
16
|
bindir: bin
|
11
17
|
cert_chain: []
|
12
18
|
|
13
|
-
date:
|
19
|
+
date: 2010-09-27 00:00:00 -04:00
|
14
20
|
default_executable:
|
15
21
|
dependencies: []
|
16
22
|
|
@@ -26,15 +32,16 @@ executables: []
|
|
26
32
|
extensions: []
|
27
33
|
|
28
34
|
extra_rdoc_files:
|
29
|
-
- README
|
35
|
+
- README.markdown
|
30
36
|
files:
|
31
|
-
- README
|
37
|
+
- README.markdown
|
32
38
|
- Rakefile
|
33
39
|
- VERSION
|
34
40
|
- about.yml
|
35
41
|
- init.rb
|
36
42
|
- lib/csv_db.rb
|
37
43
|
- lib/serialization_helper.rb
|
44
|
+
- lib/tasks/yaml_db_tasks.rake
|
38
45
|
- lib/yaml_db.rb
|
39
46
|
- spec/base.rb
|
40
47
|
- spec/serialization_helper_base_spec.rb
|
@@ -44,7 +51,6 @@ files:
|
|
44
51
|
- spec/yaml_dump_spec.rb
|
45
52
|
- spec/yaml_load_spec.rb
|
46
53
|
- spec/yaml_utils_spec.rb
|
47
|
-
- tasks/yaml_db_tasks.rake
|
48
54
|
- yaml_db.gemspec
|
49
55
|
has_rdoc: true
|
50
56
|
homepage: http://github.com/ludicast/yaml_db
|
@@ -56,21 +62,27 @@ rdoc_options:
|
|
56
62
|
require_paths:
|
57
63
|
- lib
|
58
64
|
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
59
66
|
requirements:
|
60
67
|
- - ">="
|
61
68
|
- !ruby/object:Gem::Version
|
69
|
+
hash: 3
|
70
|
+
segments:
|
71
|
+
- 0
|
62
72
|
version: "0"
|
63
|
-
version:
|
64
73
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
65
75
|
requirements:
|
66
76
|
- - ">="
|
67
77
|
- !ruby/object:Gem::Version
|
78
|
+
hash: 3
|
79
|
+
segments:
|
80
|
+
- 0
|
68
81
|
version: "0"
|
69
|
-
version:
|
70
82
|
requirements: []
|
71
83
|
|
72
84
|
rubyforge_project:
|
73
|
-
rubygems_version: 1.3.
|
85
|
+
rubygems_version: 1.3.7
|
74
86
|
signing_key:
|
75
87
|
specification_version: 3
|
76
88
|
summary: yaml_db allows export/import of database into/from yaml files
|