sequel_plus 0.1.2 → 0.1.3
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/README.md +45 -4
- data/lib/tasks/databases.rake +167 -0
- data/lib/tasks/sequel.rb +3 -0
- metadata +3 -1
data/README.md
CHANGED
|
@@ -5,9 +5,10 @@ The library is in very early infancy stage, so there's not much presently, but w
|
|
|
5
5
|
in specs and tested and used in production-level deployments already.
|
|
6
6
|
|
|
7
7
|
|
|
8
|
-
Currently, sequel_plus contains:
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
### Currently, sequel_plus contains:
|
|
9
|
+
* plugin for Trees to mimic the Rails acts_as_tree plugin.
|
|
10
|
+
* extension for exporting data using Dataset#export.
|
|
11
|
+
* rake tasks to handle basic migrations and schema inspections (similar to Rails projects)
|
|
11
12
|
|
|
12
13
|
NOTE: Authors of other plugins and extensions for Sequel are welcome to contact me for inclusion
|
|
13
14
|
of your plugin and extension to this project.
|
|
@@ -46,9 +47,49 @@ This gem is released to gemcutter. Rubyforge is not utilized.
|
|
|
46
47
|
# Specific rows and columns
|
|
47
48
|
File.open("nodes.txt", "w"){|file| DB[:nodes].filter(:id < 5).select(:id, :name).export(file)}
|
|
48
49
|
|
|
50
|
+
## Use Rake Tasks
|
|
51
|
+
|
|
52
|
+
Several rake tasks are made available simply by including the "tasks/sequel" per below:
|
|
53
|
+
|
|
54
|
+
require 'sequel'
|
|
55
|
+
require 'sequel_plus'
|
|
56
|
+
require 'tasks/sequel'
|
|
57
|
+
|
|
58
|
+
task :environment do
|
|
59
|
+
DB = Sequel.sqlite
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Establish DB somewhere in your Rakefile or elsewhere before invoking the DB dependent tasks
|
|
63
|
+
# If you define in :environment, the sequel tasks will invoke :environment as needed.
|
|
64
|
+
|
|
65
|
+
task :environment do
|
|
66
|
+
DB = Sequel.sqlite
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
Example tasks that are available:
|
|
70
|
+
|
|
71
|
+
* rake db:desc[table] # Displays schema of table
|
|
72
|
+
* rake db:fields[table] # Displays simple list of fields of table in sorted order
|
|
73
|
+
* rake db:migrate # Migrate the database through scripts in db/migrate and update db/schema.rb by in...
|
|
74
|
+
* rake db:migrate:down[step] # Reverts to previous schema version.
|
|
75
|
+
* rake db:migrate:new[table,verb] # Creates a new migrate script.
|
|
76
|
+
* rake db:migrate:redo # Rollbacks the database one migration and re-migrates up.
|
|
77
|
+
* rake db:migrate:up[version] # Runs the "up" for a given migration VERSION.
|
|
78
|
+
* rake db:reset # Drops all tables and recreates the schema from db/schema.rb
|
|
79
|
+
* rake db:rollback # Rolls the schema back to the previous version.
|
|
80
|
+
* rake db:schema:drop # drops the schema, using schema.rb
|
|
81
|
+
* rake db:schema:dump # Dumps the schema to db/schema.db
|
|
82
|
+
* rake db:schema:load # loads the schema from db/schema.rb
|
|
83
|
+
* rake db:schema:version # Returns current schema version
|
|
84
|
+
* rake db:show[table] # Displays content of table or lists all tables
|
|
85
|
+
* rake db:tables # Displays a list of tables in the database
|
|
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 APP_ROOT folder to be something other than the folder the Rakefile is residing in.
|
|
88
|
+
|
|
49
89
|
# Note on Patches/Pull Requests
|
|
50
90
|
|
|
51
|
-
* This release adds
|
|
91
|
+
* This release adds rake tasks
|
|
92
|
+
* last release adds an export facility to the Sequel::Dataset
|
|
52
93
|
|
|
53
94
|
# Copyright
|
|
54
95
|
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
namespace :db do
|
|
2
|
+
task :app_root do
|
|
3
|
+
APP_ROOT = File.dirname File.expand_path Rake.application.rakefile
|
|
4
|
+
Rake::Task["environment"].invoke if Rake::Task.task_defined?("environment")
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
task :load_config => [:app_root] do
|
|
8
|
+
raise "no DB has been defined.\n Assign DB in your Rakefile or declare an :environment task and connect to your database." unless defined? DB
|
|
9
|
+
Sequel.extension :migration
|
|
10
|
+
Sequel.extension :schema_dumper
|
|
11
|
+
Sequel.extension :pretty_table
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
desc "Displays a list of tables in the database"
|
|
15
|
+
task :tables => :load_config do
|
|
16
|
+
puts "No tables in this database" if DB.tables.empty?
|
|
17
|
+
DB.tables.each_with_index do |table_name, i|
|
|
18
|
+
puts "#{'%3d' % (i+1)}: #{table_name}"
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
desc "Displays content of table or lists all tables"
|
|
23
|
+
task :show, [:table] => :load_config do |t, args|
|
|
24
|
+
args.table ? DB[args.table.to_sym].print : Rake::Task["db:tables"].invoke
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
desc "Displays simple list of fields of table in sorted order"
|
|
28
|
+
task :fields, [:table] => :load_config do |t, args|
|
|
29
|
+
raise "no table name given" unless args[:table]
|
|
30
|
+
|
|
31
|
+
puts '==[' << args.table << ']' << '=' * (80 - args.table.size - 4)
|
|
32
|
+
DB.schema(args.table.to_sym).sort{|a, b| a[0].to_s <=> b[0].to_s}.each{|col| puts col[0]}
|
|
33
|
+
puts '=' * 80
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
desc "Displays schema of table"
|
|
37
|
+
task :desc, [:table] => :load_config do |t, args|
|
|
38
|
+
def o(value, size = 25)
|
|
39
|
+
"%#{-1*size}s" % value.to_s
|
|
40
|
+
end
|
|
41
|
+
unless args[:table]
|
|
42
|
+
Rake::Task["db:list_tables"].invoke
|
|
43
|
+
else
|
|
44
|
+
puts '==[' << args.table << ']' << '=' * (80 - args.table.size - 4)
|
|
45
|
+
DB.schema(args.table.to_sym).each_with_index do |col, i|
|
|
46
|
+
name, info = col
|
|
47
|
+
puts "#{o i+1, -3}: #{o name}:#{o info[:type], 15}:#{o info[:db_type], 15}:#{' not null ' unless info[:allow_null]} #{' pk ' if info[:primary_key]} #{' default: ' << info[:default].to_s if info[:default]}"
|
|
48
|
+
end
|
|
49
|
+
puts '-' * 80
|
|
50
|
+
indexes = DB.indexes(args.table.to_sym)
|
|
51
|
+
if indexes.size == 0
|
|
52
|
+
puts " No indexes defined"
|
|
53
|
+
else
|
|
54
|
+
indexes.each_with_index do |idx, i|
|
|
55
|
+
name, attrs = idx
|
|
56
|
+
puts ' ' << o(name, 28) << ": unique? " << o(attrs[:unique] ? 'yes' : 'no', 6) << ': ' << attrs[:columns].join(', ')
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
puts '=' * 80
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
namespace :schema do
|
|
64
|
+
task :ensure_db_dir do
|
|
65
|
+
FileUtils.mkdir_p File.join(APP_ROOT, 'db', 'migrate')
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
desc "Dumps the schema to db/schema.db"
|
|
69
|
+
task :dump => [:load_config, :ensure_db_dir] do
|
|
70
|
+
schema = DB.dump_schema_migration
|
|
71
|
+
schema_file = File.open(File.join(APP_ROOT, 'db', 'schema.rb'), "w"){|f| f.write(schema)}
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
desc "drops the schema, using schema.rb"
|
|
75
|
+
task :drop => [:load_config, :dump] do
|
|
76
|
+
eval(File.read(File.join(APP_ROOT, 'db', 'schema.rb'))).apply(DB, :down)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
desc "loads the schema from db/schema.rb"
|
|
80
|
+
task :load => :load_config do
|
|
81
|
+
eval(File.read(File.join(APP_ROOT, 'db', 'schema.rb'))).apply(DB, :up)
|
|
82
|
+
latest_version = Sequel::Migrator.latest_migration_version(File.join(APP_ROOT, 'db', 'migrate'))
|
|
83
|
+
Sequel::Migrator.set_current_migration_version(DB, latest_version)
|
|
84
|
+
puts "Database schema loaded version #{latest_version}"
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
desc "Returns current schema version"
|
|
88
|
+
task :version => :load_config do
|
|
89
|
+
puts "Current Schema Version: #{Sequel::Migrator.get_current_migration_version(DB)}"
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
desc "Migrate the database through scripts in db/migrate and update db/schema.rb by invoking db:schema:dump."
|
|
94
|
+
task :migrate => :load_config do
|
|
95
|
+
Sequel::Migrator.apply(DB, File.join(APP_ROOT, 'db', 'migrate'))
|
|
96
|
+
Rake::Task["db:schema:dump"].invoke
|
|
97
|
+
Rake::Task["db:schema:version"].invoke
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
namespace :migrate do
|
|
101
|
+
desc 'Rollbacks the database one migration and re-migrates up.'
|
|
102
|
+
task :redo => :load_config do
|
|
103
|
+
Rake::Task["db:rollback"].invoke
|
|
104
|
+
Rake::Task["db:migrate"].invoke
|
|
105
|
+
Rake::Task["db:schema:dump"].invoke
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
desc 'Runs the "up" for a given migration VERSION.'
|
|
109
|
+
task :up, [:version] => :load_config do |t, args|
|
|
110
|
+
raise "VERSION is required" unless args[:version]
|
|
111
|
+
|
|
112
|
+
puts "migrating up to version #{args.version}"
|
|
113
|
+
Sequel::Migrator.apply(DB, File.join(APP_ROOT, 'db', 'migrate'), args.version)
|
|
114
|
+
Rake::Task["db:schema:dump"].invoke
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
desc 'Reverts to previous schema version. Specify the number of steps with STEP=n'
|
|
118
|
+
task :down, [:step] => :load_config do |t, args|
|
|
119
|
+
step = args[:step] ? args.step.to_i : 1
|
|
120
|
+
current_version = Sequel::Migrator.get_current_migration_version(DB)
|
|
121
|
+
down_version = current_version - step
|
|
122
|
+
down_version = 0 if down_version < 0
|
|
123
|
+
|
|
124
|
+
puts "migrating down to version #{down_version}"
|
|
125
|
+
Sequel::Migrator.apply(DB, File.join(APP_ROOT, 'db', 'migrate'), down_version)
|
|
126
|
+
Rake::Task["db:schema:dump"].invoke
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
desc "Creates a new migrate script. The verb is optional."
|
|
130
|
+
task :new, [:table, :verb] => :load_config do |t, args|
|
|
131
|
+
unless args[:table]
|
|
132
|
+
puts "need to provide a table name: rake db:migrate:new[new_table]"
|
|
133
|
+
else
|
|
134
|
+
table = args.table
|
|
135
|
+
verb = args.verb || 'create'
|
|
136
|
+
migrate_path = File.join(APP_ROOT,'db', 'migrate')
|
|
137
|
+
begin
|
|
138
|
+
last_file = File.basename(Dir.glob(File.join(migrate_path, '*.rb')).sort.last)
|
|
139
|
+
next_value = last_file.scan(/\d+/).first.to_i + 1
|
|
140
|
+
rescue
|
|
141
|
+
next_value = 1
|
|
142
|
+
end
|
|
143
|
+
filename = '%03d' % next_value << "_" << args.table << '.rb'
|
|
144
|
+
File.open(File.join(migrate_path, filename), 'w') do |file|
|
|
145
|
+
file.puts "class #{verb.capitalize}#{table.capitalize} < Sequel::Migration\n"
|
|
146
|
+
file.puts "\tdef up"
|
|
147
|
+
file.puts "\t\t#{verb}_table :#{table} do"
|
|
148
|
+
file.puts "\t\t\tprimary_key\t:id"
|
|
149
|
+
file.puts "\t\tend"
|
|
150
|
+
file.puts "\tend\n\n"
|
|
151
|
+
file.puts "\tdef down\n"
|
|
152
|
+
file.puts "\t\tdrop_table :#{table}"
|
|
153
|
+
file.puts "\tend"
|
|
154
|
+
file.puts "end"
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
desc 'Rolls the schema back to the previous version.'
|
|
161
|
+
task :rollback => :load_config do
|
|
162
|
+
Rake::Task["db:migrate:down"].invoke
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
desc 'Drops all tables and recreates the schema from db/schema.rb'
|
|
166
|
+
task :reset => ['db:schema:drop', 'db:schema:load']
|
|
167
|
+
end
|
data/lib/tasks/sequel.rb
ADDED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: sequel_plus
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Michael Lang
|
|
@@ -48,6 +48,8 @@ files:
|
|
|
48
48
|
- lib/extensions/export.rb
|
|
49
49
|
- lib/sequel_plus.rb
|
|
50
50
|
- lib/sequel_tree.rb
|
|
51
|
+
- lib/tasks/databases.rake
|
|
52
|
+
- lib/tasks/sequel.rb
|
|
51
53
|
- test/helper.rb
|
|
52
54
|
- test/test_export.rb
|
|
53
55
|
- test/test_sequel_tree.rb
|