rake-ar 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.
- data/.gitignore +1 -0
- data/.rspec +2 -0
- data/Gemfile +3 -0
- data/Rakefile +4 -0
- data/lib/rake_ar.rb +84 -0
- data/lib/rake_ar/rake.rb +75 -0
- data/lib/rake_ar/version.rb +3 -0
- data/rake_ar.gemspec +23 -0
- data/readme.md +38 -0
- data/spec/rake_ar_spec.rb +92 -0
- data/spec/spec_helper.rb +7 -0
- metadata +125 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rake-ar-*.gem
|
data/.rspec
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
data/lib/rake_ar.rb
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'rake_ar/version'
|
2
|
+
require 'active_support/inflector'
|
3
|
+
require 'fileutils'
|
4
|
+
|
5
|
+
class RakeAR
|
6
|
+
def initialize(settings = {})
|
7
|
+
@settings = {
|
8
|
+
connect_file: "#{Dir.pwd}/db/connection.rb",
|
9
|
+
migration_path: "#{Dir.pwd}/db/migrate/",
|
10
|
+
seed_file: "#{Dir.pwd}/db/seeds.rb",
|
11
|
+
schema_file: "#{Dir.pwd}/db/schema.rb",
|
12
|
+
models_path: "#{Dir.pwd}/app/models"
|
13
|
+
}.merge(settings)
|
14
|
+
|
15
|
+
FileUtils.mkdir_p(@settings[:migration_path])
|
16
|
+
end
|
17
|
+
|
18
|
+
def connect_db
|
19
|
+
@settings[:connect_file].tap do |connection|
|
20
|
+
require connection
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def load_models
|
25
|
+
"#{@settings[:models_path]}/*.rb".tap do |path|
|
26
|
+
Dir[path].each do |model|
|
27
|
+
require model
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def dump_schema
|
33
|
+
File.open(@settings[:schema_file], 'w:utf-8') do |schema_file|
|
34
|
+
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, schema_file)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def load_schema
|
39
|
+
load(@settings[:schema_file]) if File.exists? @settings[:schema_file]
|
40
|
+
end
|
41
|
+
|
42
|
+
def seed_db
|
43
|
+
load(@settings[:seed_file]) if File.exists? @settings[:seed_file]
|
44
|
+
end
|
45
|
+
|
46
|
+
def clear_db
|
47
|
+
ActiveRecord::Base.descendants.each do |model|
|
48
|
+
model.delete_all
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def drop_db
|
53
|
+
(ActiveRecord::Base.descendants << 'schema_migration').each do |table|
|
54
|
+
sql = "DROP TABLE #{table.to_s.pluralize.downcase};"
|
55
|
+
ActiveRecord::Base.connection.execute(sql) rescue 1
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def migrate_db
|
60
|
+
version = ENV['VERSION'] ? ENV['VERSION'].to_i : nil
|
61
|
+
ActiveRecord::Migrator.migrate(@settings[:migration_path], version)
|
62
|
+
end
|
63
|
+
|
64
|
+
def create_migration
|
65
|
+
name = ENV['NAME']
|
66
|
+
abort 'No NAME specified. Use `rake db:create_migration NAME=create_users`' if !name
|
67
|
+
|
68
|
+
version = ENV['VERSION'] || Time.now.utc.strftime('%Y%m%d%H%M%S')
|
69
|
+
migration_file = "#{version}_#{name}.rb"
|
70
|
+
migration_name = name.gsub(/_(.)/) { $1.upcase }.gsub(/^(.)/) { $1.upcase }
|
71
|
+
|
72
|
+
File.open("#{@settings[:migration_path]}/#{migration_file}", 'w') do |migration|
|
73
|
+
migration << (<<-EOS).gsub(' ', '')
|
74
|
+
class #{migration_name} < ActiveRecord::Migration
|
75
|
+
def self.up
|
76
|
+
end
|
77
|
+
|
78
|
+
def self.down
|
79
|
+
end
|
80
|
+
end
|
81
|
+
EOS
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
data/lib/rake_ar/rake.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler.require
|
3
|
+
|
4
|
+
require 'rake_ar'
|
5
|
+
|
6
|
+
def rake_ar
|
7
|
+
@rake_ar ||= RakeAR.new
|
8
|
+
end
|
9
|
+
|
10
|
+
namespace :db do
|
11
|
+
task :connect_db do
|
12
|
+
rake_ar.connect_db
|
13
|
+
end
|
14
|
+
|
15
|
+
task :load_models => [:connect_db] do
|
16
|
+
rake_ar.load_models
|
17
|
+
end
|
18
|
+
|
19
|
+
desc 'Load the console'
|
20
|
+
task :console => [:load_models] do
|
21
|
+
require 'irb'
|
22
|
+
require 'irb/completion'
|
23
|
+
ARGV.clear
|
24
|
+
IRB.start
|
25
|
+
end
|
26
|
+
|
27
|
+
desc 'Dump a schema file'
|
28
|
+
task :schema => [:load_models] do
|
29
|
+
rake_ar.dump_schema
|
30
|
+
end
|
31
|
+
|
32
|
+
desc 'Load a schema.rb file into the database'
|
33
|
+
task :load => [:load_models] do
|
34
|
+
rake_ar.load_schema
|
35
|
+
end
|
36
|
+
|
37
|
+
desc 'Load the seed data'
|
38
|
+
task :seed => [:load_models] do
|
39
|
+
rake_ar.seed_db
|
40
|
+
end
|
41
|
+
|
42
|
+
desc 'Clear all database records'
|
43
|
+
task :clear => [:load_models] do
|
44
|
+
rake_ar.clear_db
|
45
|
+
end
|
46
|
+
|
47
|
+
desc 'Drop all database tables'
|
48
|
+
task :drop => [:load_models] do
|
49
|
+
rake_ar.drop_db
|
50
|
+
end
|
51
|
+
|
52
|
+
desc 'migrate your database'
|
53
|
+
task :migrate => [:load_models] do
|
54
|
+
rake_ar.migrate_db
|
55
|
+
end
|
56
|
+
|
57
|
+
desc 'create an ActiveRecord migration in ./db/migrate'
|
58
|
+
task :create_migration => [:load_models] do
|
59
|
+
rake_ar.create_migration
|
60
|
+
end
|
61
|
+
|
62
|
+
desc 'Reload and reseed the database from schema'
|
63
|
+
task :reseed => [:load_models] do
|
64
|
+
Rake::Task['db:load'].invoke
|
65
|
+
Rake::Task['db:seed'].invoke
|
66
|
+
end
|
67
|
+
|
68
|
+
desc 'Regenerates the database from migrations'
|
69
|
+
task :regen => [:load_models] do
|
70
|
+
Rake::Task['db:drop'].invoke
|
71
|
+
Rake::Task['db:migrate'].invoke
|
72
|
+
Rake::Task['db:schema'].invoke
|
73
|
+
Rake::Task['db:seed'].invoke
|
74
|
+
end
|
75
|
+
end
|
data/rake_ar.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'rake_ar/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.name = 'rake-ar'
|
7
|
+
gem.date = '2012-09-22'
|
8
|
+
gem.version = RakeAR::VERSION
|
9
|
+
gem.authors = ['Nick Barth']
|
10
|
+
gem.email = ['nick@nickbarth.ca']
|
11
|
+
gem.summary = 'A Ruby Gem for common ActiveRecord Rake tasks.'
|
12
|
+
gem.description = 'RakeAR is a Ruby Gem containing some common Rake tasks to help manage your ActiveRecord database independant of Rails.'
|
13
|
+
gem.homepage = 'https://github.com/nickbarth/RakeAR'
|
14
|
+
|
15
|
+
gem.add_dependency('rake')
|
16
|
+
gem.add_dependency('activerecord')
|
17
|
+
gem.add_dependency('activesupport')
|
18
|
+
gem.add_development_dependency('rspec')
|
19
|
+
|
20
|
+
gem.files = `git ls-files`.split($/)
|
21
|
+
gem.test_files = gem.files.grep /spec/
|
22
|
+
gem.require_paths = ['lib']
|
23
|
+
end
|
data/readme.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# RakeAR
|
2
|
+
RakeAR is a Ruby Gem containing some common Rake tasks to help manage your ActiveRecord database independant of Rails.
|
3
|
+
|
4
|
+
# How To Use
|
5
|
+
|
6
|
+
Install the gem
|
7
|
+
|
8
|
+
gem install rake-ar
|
9
|
+
|
10
|
+
Add a require to your `Rakefile`
|
11
|
+
|
12
|
+
require 'rake_ar/rake'
|
13
|
+
|
14
|
+
You will now have some rake tasks to manage your ActiveRecord database.
|
15
|
+
|
16
|
+
rake -T
|
17
|
+
|
18
|
+
rake db:clear # Clear all database records
|
19
|
+
rake db:console # Loads the console with your ActiveRecord models
|
20
|
+
rake db:create_migration # Creates a new ActiveRecord migration
|
21
|
+
rake db:drop # Drops all database tables
|
22
|
+
rake db:load # Loads your schema file into the database
|
23
|
+
rake db:migrate # Migrates your database
|
24
|
+
rake db:regen # Regenerates the database from migrations
|
25
|
+
rake db:reseed # Reloads the database from your schema file and reseeds it
|
26
|
+
rake db:schema # Dumps a new schema file
|
27
|
+
rake db:seed # Loads your seed data file
|
28
|
+
|
29
|
+
To configure them just initialize a new instance of RakeAR in your `Rakefile` to override the defaults.
|
30
|
+
|
31
|
+
@rake_ar = RakeAR.new connect_file: "#{Dir.pwd}/db/connect.rb", # File containing a valid ActiveRecord connection
|
32
|
+
migration_path: "#{Dir.pwd}/db/migrate/", # Path to migrations folder
|
33
|
+
seed_file: "#{Dir.pwd}/db/seeds.rb", # Ruby database seed script
|
34
|
+
schema_file: "#{Dir.pwd}/db/schema.rb", # Schema file the database is written too and loaded from
|
35
|
+
models_path: "#{Dir.pwd}/app/models" # Path to the applications ActiveRecord models
|
36
|
+
|
37
|
+
### License
|
38
|
+
WTFPL © 2012 Nick Barth
|
@@ -0,0 +1,92 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe RakeAR do
|
4
|
+
before(:all) do
|
5
|
+
ActiveRecord = Module.new
|
6
|
+
ActiveRecord::Base = Struct.new(:connection, :descendants).new
|
7
|
+
module FileUtils; def self.mkdir_p(*args); true; end; end
|
8
|
+
class File; def self.open(*args); yield; end; end
|
9
|
+
end
|
10
|
+
|
11
|
+
before(:each) do
|
12
|
+
@rake_ar = RakeAR.new connect_file: 'CONNECT_FILE',
|
13
|
+
migration_path: 'MIG_PATH',
|
14
|
+
seed_file: 'SEED_FILE',
|
15
|
+
schema_file: 'SCHEMA_FILE',
|
16
|
+
models_path: 'MODELS_PATH'
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'on create connection' do
|
20
|
+
it 'should return correct path' do
|
21
|
+
@rake_ar.stub(:require)
|
22
|
+
@rake_ar.connect_db.should == 'CONNECT_FILE'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'on model path' do
|
27
|
+
it 'should return correct path' do
|
28
|
+
@rake_ar.load_models.should == 'MODELS_PATH/*.rb'
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'on schema dump' do
|
33
|
+
it 'should dump to the schema file' do
|
34
|
+
ActiveRecord::SchemaDumper = double(:schema)
|
35
|
+
ActiveRecord::SchemaDumper.should_receive(:dump)
|
36
|
+
@rake_ar.dump_schema
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'on load schema' do
|
41
|
+
it 'should load the schema file' do
|
42
|
+
File.stub(:exists?).and_return(true)
|
43
|
+
@rake_ar.should_receive(:load).with('SCHEMA_FILE')
|
44
|
+
@rake_ar.load_schema
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context 'on seed db' do
|
49
|
+
it 'should load the schema file' do
|
50
|
+
File.stub(:exists?).and_return(true)
|
51
|
+
@rake_ar.should_receive(:load).with('SEED_FILE')
|
52
|
+
@rake_ar.seed_db
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context 'on clear db' do
|
57
|
+
it 'should send all models delete_all' do
|
58
|
+
model = stub(:model)
|
59
|
+
model.should_receive(:delete_all)
|
60
|
+
ActiveRecord::Base.descendants = [model]
|
61
|
+
@rake_ar.clear_db
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context 'on drop db' do
|
66
|
+
it 'should execute drop tables' do
|
67
|
+
ActiveRecord::Base.descendants = [ Struct.new(:to_s).new('model') ]
|
68
|
+
ActiveRecord::Base.connection = stub
|
69
|
+
ActiveRecord::Base.connection.should_receive(:execute).with('DROP TABLE models;')
|
70
|
+
ActiveRecord::Base.connection.should_receive(:execute).with('DROP TABLE schema_migrations;')
|
71
|
+
@rake_ar.drop_db
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
context 'on migrate' do
|
76
|
+
it 'should call migrate' do
|
77
|
+
ENV['VERSION'] = '42'
|
78
|
+
ActiveRecord::Migrator = double(:migrator)
|
79
|
+
ActiveRecord::Migrator.should_receive(:migrate).with('MIG_PATH', 42)
|
80
|
+
@rake_ar.migrate_db
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
context 'on create migration' do
|
85
|
+
it 'should write a new migration file' do
|
86
|
+
ENV['VERSION'] = '42'
|
87
|
+
ENV['NAME'] = 'add_models'
|
88
|
+
File.should_receive(:open).with('MIG_PATH/42_add_models.rb', 'w')
|
89
|
+
@rake_ar.create_migration
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rake-ar
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Nick Barth
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-22 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: activerecord
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: activesupport
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: RakeAR is a Ruby Gem containing some common Rake tasks to help manage
|
79
|
+
your ActiveRecord database independant of Rails.
|
80
|
+
email:
|
81
|
+
- nick@nickbarth.ca
|
82
|
+
executables: []
|
83
|
+
extensions: []
|
84
|
+
extra_rdoc_files: []
|
85
|
+
files:
|
86
|
+
- .gitignore
|
87
|
+
- .rspec
|
88
|
+
- Gemfile
|
89
|
+
- Rakefile
|
90
|
+
- lib/rake_ar.rb
|
91
|
+
- lib/rake_ar/rake.rb
|
92
|
+
- lib/rake_ar/version.rb
|
93
|
+
- rake_ar.gemspec
|
94
|
+
- readme.md
|
95
|
+
- spec/rake_ar_spec.rb
|
96
|
+
- spec/spec_helper.rb
|
97
|
+
homepage: https://github.com/nickbarth/RakeAR
|
98
|
+
licenses: []
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ! '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ! '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 1.8.24
|
118
|
+
signing_key:
|
119
|
+
specification_version: 3
|
120
|
+
summary: A Ruby Gem for common ActiveRecord Rake tasks.
|
121
|
+
test_files:
|
122
|
+
- .rspec
|
123
|
+
- rake_ar.gemspec
|
124
|
+
- spec/rake_ar_spec.rb
|
125
|
+
- spec/spec_helper.rb
|