sequel_plus 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +33 -23
- data/lib/extensions/export.rb +29 -13
- data/lib/sequel_tree.rb +2 -2
- data/lib/tasks/{databases.rake → sequel.rake} +12 -6
- data/test/test_export.rb +21 -0
- data/test/test_sequel_tree.rb +1 -1
- metadata +4 -4
data/README.md
CHANGED
@@ -1,9 +1,8 @@
|
|
1
1
|
# sequel_plus
|
2
2
|
|
3
|
-
This library
|
4
|
-
The library is in
|
5
|
-
in specs and tested and used in production-level deployments already.
|
6
|
-
|
3
|
+
This library contains a growing collection of plugins and extensions I have assembled for the Ruby Sequel library.
|
4
|
+
The library is in its infancy stage with new things being added and updated semi-frequently. Even so,
|
5
|
+
what's here is fully covered in specs and tested and used in production-level deployments already.
|
7
6
|
|
8
7
|
### Currently, sequel_plus contains:
|
9
8
|
* plugin for Trees to mimic the Rails acts_as_tree plugin.
|
@@ -47,12 +46,15 @@ This gem is released to gemcutter. Rubyforge is not utilized.
|
|
47
46
|
# Specific rows and columns
|
48
47
|
File.open("nodes.txt", "w"){|file| DB[:nodes].filter(:id < 5).select(:id, :name).export(file)}
|
49
48
|
|
49
|
+
# Using pagination extension (for very large datasets)
|
50
|
+
File.open("nodes.txt", "w"){|file| DB[:nodes].export(file, :paginate => true, :page_size => 1000)}
|
51
|
+
|
50
52
|
## Use Rake Tasks
|
51
53
|
|
52
|
-
Several rake tasks are made available simply by
|
54
|
+
Several rake tasks are made available simply by requiring "tasks/sequel" in your Rakefile
|
55
|
+
(or loaded rake scripts) per below:
|
53
56
|
|
54
57
|
require 'sequel'
|
55
|
-
require 'sequel_plus'
|
56
58
|
require 'tasks/sequel'
|
57
59
|
|
58
60
|
task :environment do
|
@@ -68,26 +70,34 @@ Several rake tasks are made available simply by including the "tasks/sequel" per
|
|
68
70
|
|
69
71
|
Example tasks that are available:
|
70
72
|
|
71
|
-
* rake
|
72
|
-
* rake
|
73
|
-
* rake
|
74
|
-
* rake
|
75
|
-
* rake
|
76
|
-
* rake
|
77
|
-
* rake
|
78
|
-
* rake
|
79
|
-
* rake
|
80
|
-
* rake
|
81
|
-
* rake
|
82
|
-
* rake
|
83
|
-
* rake
|
84
|
-
* rake
|
85
|
-
* rake
|
86
|
-
|
87
|
-
These tasks will expect migrations to be in db/migration that is based off the folder your Rakefile resides in. If you wish to change the location of the "db" folder, simply declare :environment task and set
|
73
|
+
* rake sq:desc[table] # Displays schema of table
|
74
|
+
* rake sq:fields[table] # Displays simple list of fields of table in sorted order
|
75
|
+
* rake sq:migrate # Migrate the database through scripts in db/migrate and update db/schema.rb by in...
|
76
|
+
* rake sq:migrate:down[step] # Reverts to previous schema version.
|
77
|
+
* rake sq:migrate:new[table,verb] # Creates a new migrate script.
|
78
|
+
* rake sq:migrate:redo # Rollbacks the database one migration and re-migrates up.
|
79
|
+
* rake sq:migrate:up[version] # Runs the "up" for a given migration VERSION.
|
80
|
+
* rake sq:reset # Drops all tables and recreates the schema from db/schema.rb
|
81
|
+
* rake sq:rollback # Rolls the schema back to the previous version.
|
82
|
+
* rake sq:schema:drop # drops the schema, using schema.rb
|
83
|
+
* rake sq:schema:dump # Dumps the schema to db/schema.db
|
84
|
+
* rake sq:schema:load # loads the schema from db/schema.rb
|
85
|
+
* rake sq:schema:version # Returns current schema version
|
86
|
+
* rake sq:show[table] # Displays content of table or lists all tables
|
87
|
+
* rake sq:tables # Displays a list of tables in the database
|
88
|
+
|
89
|
+
These tasks will expect migrations to be in db/migration that is based off the folder your Rakefile resides in. If you wish to change the location of the "db" folder, simply declare :environment task and set SEQUEL_PLUS_APP_ROOT constant to be something other than the folder the Rakefile is residing in.
|
90
|
+
|
91
|
+
The rake tasks were constructed to fairly independent of the project environment they're injected into. To avoid name space collision,
|
92
|
+
the "db" namespace has been deprecated and the "sq" namespace adopted as of 0.2.0.
|
88
93
|
|
89
94
|
# Note on Patches/Pull Requests
|
90
95
|
|
96
|
+
0.2.0
|
97
|
+
* top-level namespace changed from "db" to "sq"
|
98
|
+
* designed and tested to work seamlessly with Padrino and Ramaze projects
|
99
|
+
|
100
|
+
0.1.5
|
91
101
|
* This release adds rake tasks
|
92
102
|
* last release adds an export facility to the Sequel::Dataset
|
93
103
|
|
data/lib/extensions/export.rb
CHANGED
@@ -13,9 +13,14 @@
|
|
13
13
|
module Sequel
|
14
14
|
class Dataset
|
15
15
|
def export(fd = $stdout, options = {})
|
16
|
+
|
16
17
|
opts[:delimiter] = options[:delimiter] || "\t"
|
17
18
|
opts[:quote_char] = options[:quote_char] || ''
|
18
19
|
opts[:headers] = options[:headers] != false
|
20
|
+
opts[:paginate] = options[:paginate] || false
|
21
|
+
opts[:page_size] = options[:page_size] || 5000
|
22
|
+
|
23
|
+
Sequel.extension :pagination if opts[:paginate]
|
19
24
|
|
20
25
|
Sequel::Export::Writer.new(fd, self, opts).output
|
21
26
|
end
|
@@ -29,25 +34,18 @@ module Sequel
|
|
29
34
|
@dataset = dataset
|
30
35
|
@options = options
|
31
36
|
end
|
32
|
-
|
33
|
-
def output
|
34
|
-
first_row = @dataset.first
|
35
|
-
return unless first_row
|
36
|
-
|
37
|
-
quot = @options[:quote_char]
|
38
|
-
@columns ||= first_row.keys.sort_by{|x|x.to_s}
|
39
37
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
@dataset.each do |row|
|
38
|
+
def export_data(ds)
|
39
|
+
quot = @options[:quote_char]
|
40
|
+
ds.each do |row|
|
45
41
|
data = @columns.map do |col|
|
46
42
|
case row[col]
|
47
43
|
when Date then
|
48
44
|
"#{quot}#{row[col].strftime('%Y-%m-%d')}#{quot}"
|
49
|
-
when DateTime
|
45
|
+
when DateTime then
|
50
46
|
"#{quot}#{row[col].localtime.strftime('%Y-%m-%dT%H:%M%Z')}#{quot}"
|
47
|
+
when Time then
|
48
|
+
"#{quot}#{row[col].localtime.strftime('%H:%M%Z')}#{quot}"
|
51
49
|
when Float, BigDecimal then
|
52
50
|
row[col].to_f
|
53
51
|
when BigDecimal, Bignum, Fixnum then
|
@@ -59,6 +57,24 @@ module Sequel
|
|
59
57
|
@file.puts data.join(@options[:delimiter])
|
60
58
|
end
|
61
59
|
end
|
60
|
+
|
61
|
+
def output
|
62
|
+
first_row = @dataset.first
|
63
|
+
return unless first_row
|
64
|
+
|
65
|
+
quot = @options[:quote_char]
|
66
|
+
@columns ||= first_row.keys.sort_by{|x|x.to_s}
|
67
|
+
|
68
|
+
if @options[:headers] == true
|
69
|
+
@file.puts @columns.map{|col| "#{quot}#{col}#{quot}"}.join(@options[:delimiter])
|
70
|
+
end
|
71
|
+
|
72
|
+
if @options[:paginate]
|
73
|
+
@dataset.each_page(@options[:page_size]){|paged_ds| export_data paged_ds}
|
74
|
+
else
|
75
|
+
export_data @dataset
|
76
|
+
end
|
77
|
+
end
|
62
78
|
|
63
79
|
def build_row(row)
|
64
80
|
quot = @options[:quote_char]
|
data/lib/sequel_tree.rb
CHANGED
@@ -60,8 +60,8 @@ module Sequel
|
|
60
60
|
#
|
61
61
|
# subchild1.ancestors # => [child1, root]
|
62
62
|
def descendants
|
63
|
-
nodes = self.children
|
64
|
-
nodes.each{|child| nodes
|
63
|
+
nodes = self.children.dup
|
64
|
+
nodes.each{|child| nodes.concat(child.descendants)}
|
65
65
|
nodes
|
66
66
|
end
|
67
67
|
|
@@ -40,16 +40,22 @@ namespace :sq do
|
|
40
40
|
|
41
41
|
desc "Displays schema of table"
|
42
42
|
task :desc, [:table] => :load_config do |t, args|
|
43
|
-
def o(value, size = 25)
|
44
|
-
"%#{-1*size}s" % value.to_s
|
45
|
-
end
|
46
43
|
unless args[:table]
|
47
44
|
Rake::Task["sq:tables"].invoke
|
48
45
|
else
|
49
46
|
puts '==[' << args.table << ']' << '=' * (80 - args.table.size - 4)
|
50
47
|
DB.schema(args.table.to_sym).each_with_index do |col, i|
|
51
|
-
|
52
|
-
|
48
|
+
name, info = col
|
49
|
+
values = [
|
50
|
+
"%3s:" % (i + 1),
|
51
|
+
(" %-12s:" % "#{info[:db_type]}#{('(' + info[:max_chars].to_s + ')') if info[:max_chars]}"),
|
52
|
+
("%15s:" % info[:type]),
|
53
|
+
"%-25s: " % name,
|
54
|
+
(' not null ' unless info[:allow_null]),
|
55
|
+
(' pk ' if info[:primary_key]),
|
56
|
+
(" default: %s" % info[:default] if info[:default]),
|
57
|
+
]
|
58
|
+
puts values.join
|
53
59
|
end
|
54
60
|
puts '-' * 80
|
55
61
|
indexes = DB.indexes(args.table.to_sym)
|
@@ -58,7 +64,7 @@ namespace :sq do
|
|
58
64
|
else
|
59
65
|
indexes.each_with_index do |idx, i|
|
60
66
|
name, attrs = idx
|
61
|
-
puts ' ' <<
|
67
|
+
puts ' ' << "%-28s" % name << ": unique? " << "%-6s" % (attrs[:unique] ? 'yes' : 'no') << ': ' << attrs[:columns].join(', ')
|
62
68
|
end
|
63
69
|
end
|
64
70
|
puts '=' * 80
|
data/test/test_export.rb
CHANGED
@@ -116,6 +116,27 @@ id name parent_id position
|
|
116
116
|
9,"five","",5
|
117
117
|
10,"four","",4
|
118
118
|
11,"five.one",9,1
|
119
|
+
12,"two.three",2,3
|
120
|
+
TEXT
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should export paginated data with comma delimiter" do
|
124
|
+
mem_stream = StringIO.new("", "w+")
|
125
|
+
DB[:nodes].export(mem_stream, :quote_char => '"', :delimiter => ',', :paginate => true, :page_size => 2)
|
126
|
+
mem_stream.pos = 0
|
127
|
+
mem_stream.read.should == <<-TEXT
|
128
|
+
"id","name","parent_id","position"
|
129
|
+
1,"one","",1
|
130
|
+
2,"two","",2
|
131
|
+
3,"three","",3
|
132
|
+
4,"two.one",2,1
|
133
|
+
5,"two.two",2,2
|
134
|
+
6,"two.two.one",5,1
|
135
|
+
7,"one.two",1,2
|
136
|
+
8,"one.one",1,1
|
137
|
+
9,"five","",5
|
138
|
+
10,"four","",4
|
139
|
+
11,"five.one",9,1
|
119
140
|
12,"two.three",2,3
|
120
141
|
TEXT
|
121
142
|
end
|
data/test/test_sequel_tree.rb
CHANGED
@@ -71,7 +71,7 @@ module SequelTreeTest
|
|
71
71
|
it "should find all descendants of a node" do
|
72
72
|
two = Node.find(:id => 2)
|
73
73
|
two.name.should == "two"
|
74
|
-
two.descendants.map{|m| m[:id]}.should == [4, 5, 12]
|
74
|
+
two.descendants.map{|m| m[:id]}.should == [4, 5, 12, 6]
|
75
75
|
end
|
76
76
|
|
77
77
|
it "should find all ancestors of a node" do
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
7
|
+
- 3
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 0.3.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Michael Lang
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-12-21 00:00:00 -05:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -61,7 +61,7 @@ files:
|
|
61
61
|
- lib/extensions/export.rb
|
62
62
|
- lib/sequel_plus.rb
|
63
63
|
- lib/sequel_tree.rb
|
64
|
-
- lib/tasks/
|
64
|
+
- lib/tasks/sequel.rake
|
65
65
|
- lib/tasks/sequel.rb
|
66
66
|
- test/helper.rb
|
67
67
|
- test/test_export.rb
|