reactive 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +3 -0
- data/MIT-LICENSE +21 -0
- data/Manifest.txt +60 -0
- data/README.txt +130 -0
- data/Rakefile +14 -0
- data/app_generators/reactive/USAGE +11 -0
- data/app_generators/reactive/reactive_generator.rb +160 -0
- data/app_generators/reactive/templates/README +130 -0
- data/app_generators/reactive/templates/Rakefile +10 -0
- data/app_generators/reactive/templates/app/controllers/application_controller.rb +2 -0
- data/app_generators/reactive/templates/app/helpers/application_helper.rb +2 -0
- data/app_generators/reactive/templates/config/boot.rb +94 -0
- data/app_generators/reactive/templates/config/databases/frontbase.yml +28 -0
- data/app_generators/reactive/templates/config/databases/mysql.yml +54 -0
- data/app_generators/reactive/templates/config/databases/oracle.yml +39 -0
- data/app_generators/reactive/templates/config/databases/postgresql.yml +48 -0
- data/app_generators/reactive/templates/config/databases/sqlite2.yml +16 -0
- data/app_generators/reactive/templates/config/databases/sqlite3.yml +19 -0
- data/app_generators/reactive/templates/config/empty.log +0 -0
- data/app_generators/reactive/templates/config/environment.rb +11 -0
- data/app_generators/reactive/templates/script/destroy +12 -0
- data/app_generators/reactive/templates/script/generate +12 -0
- data/app_generators/reactive/templates/script/run +5 -0
- data/app_generators/reactive/templates/script/win_script.cmd +1 -0
- data/bin/reactive +16 -0
- data/lib/code_statistics.rb +107 -0
- data/lib/controller.rb +23 -0
- data/lib/controller/base.rb +442 -0
- data/lib/controller/filters.rb +767 -0
- data/lib/controller/flash.rb +161 -0
- data/lib/controller/helpers.rb +204 -0
- data/lib/controller/layout.rb +326 -0
- data/lib/dispatcher.rb +46 -0
- data/lib/generated_attribute.rb +40 -0
- data/lib/initializer.rb +425 -0
- data/lib/named_base_generator.rb +92 -0
- data/lib/reactive.rb +6 -0
- data/lib/request.rb +17 -0
- data/lib/source_annotation_extractor.rb +62 -0
- data/lib/tasks/annotations.rake +23 -0
- data/lib/tasks/databases.rake +347 -0
- data/lib/tasks/log.rake +9 -0
- data/lib/tasks/misc.rake +5 -0
- data/lib/tasks/reactive.rb +16 -0
- data/lib/tasks/statistics.rake +17 -0
- data/lib/tasks/testing.rake +118 -0
- data/lib/version.rb +9 -0
- data/lib/view.rb +1 -0
- data/lib/view/base.rb +33 -0
- data/reactive_generators/model/USAGE +27 -0
- data/reactive_generators/model/model_generator.rb +52 -0
- data/reactive_generators/model/templates/fixtures.yml +19 -0
- data/reactive_generators/model/templates/migration.rb +16 -0
- data/reactive_generators/model/templates/model.rb +2 -0
- data/reactive_generators/model/templates/unit_test.rb +8 -0
- data/reactive_generators/scaffold/USAGE +26 -0
- data/reactive_generators/scaffold/scaffold_generator.rb +75 -0
- data/reactive_generators/scaffold/templates/controller.rb +51 -0
- data/reactive_generators/scaffold/templates/functional_test.rb +49 -0
- data/reactive_generators/scaffold/templates/helper.rb +2 -0
- metadata +142 -0
@@ -0,0 +1,92 @@
|
|
1
|
+
require 'generated_attribute'
|
2
|
+
|
3
|
+
module Reactive
|
4
|
+
# The base generator for named components: models, controllers, mailers,
|
5
|
+
# etc. The target name is taken as the first argument and inflected to
|
6
|
+
# singular, plural, class, file, and table forms for your convenience.
|
7
|
+
# The remaining arguments are aliased to +actions+ as an array for
|
8
|
+
# controller and mailer convenience.
|
9
|
+
#
|
10
|
+
# Several useful local variables and methods are populated in the
|
11
|
+
# +initialize+ method. See below for a list of Attributes and
|
12
|
+
# External Aliases available to both the manifest and to all templates.
|
13
|
+
#
|
14
|
+
# If no name is provided, the generator raises a usage error with content
|
15
|
+
# optionally read from the USAGE file in the generator's base path.
|
16
|
+
#
|
17
|
+
# For example, the +controller+ generator takes the first argument as
|
18
|
+
# the name of the class and subsequent arguments as the names of
|
19
|
+
# actions to be generated:
|
20
|
+
#
|
21
|
+
# ./script/generate controller Article index new create
|
22
|
+
#
|
23
|
+
# See Rails::Generator::Base for a discussion of manifests,
|
24
|
+
# Rails::Generator::Commands::Create for methods available to the manifest,
|
25
|
+
# and Rails::Generator for a general discussion of generators.
|
26
|
+
class NamedBaseGenerator < RubiGen::Base
|
27
|
+
attr_reader :name, :class_name, :singular_name, :plural_name, :table_name
|
28
|
+
attr_reader :class_path, :file_path, :class_nesting, :class_nesting_depth
|
29
|
+
alias_method :file_name, :singular_name
|
30
|
+
alias_method :actions, :args
|
31
|
+
|
32
|
+
def initialize(runtime_args, runtime_options = {})
|
33
|
+
super
|
34
|
+
|
35
|
+
# Name argument is required.
|
36
|
+
usage if runtime_args.empty?
|
37
|
+
|
38
|
+
@args = runtime_args.dup
|
39
|
+
base_name = @args.shift
|
40
|
+
assign_names!(base_name)
|
41
|
+
end
|
42
|
+
|
43
|
+
protected
|
44
|
+
# Override with your own usage banner.
|
45
|
+
def banner
|
46
|
+
"Usage: #{$0} #{spec.name} #{spec.name.camelize}Name [options]"
|
47
|
+
end
|
48
|
+
|
49
|
+
def attributes
|
50
|
+
@attributes ||= @args.collect do |attribute|
|
51
|
+
GeneratedAttribute.new(*attribute.split(":"))
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
private
|
57
|
+
def assign_names!(name)
|
58
|
+
@name = name
|
59
|
+
base_name, @class_path, @file_path, @class_nesting, @class_nesting_depth = extract_modules(@name)
|
60
|
+
@class_name_without_nesting, @singular_name, @plural_name = inflect_names(base_name)
|
61
|
+
@table_name = (!defined?(ActiveRecord::Base) || ActiveRecord::Base.pluralize_table_names) ? plural_name : singular_name
|
62
|
+
@table_name.gsub! '/', '_'
|
63
|
+
if @class_nesting.empty?
|
64
|
+
@class_name = @class_name_without_nesting
|
65
|
+
else
|
66
|
+
@table_name = @class_nesting.underscore << "_" << @table_name
|
67
|
+
@class_name = "#{@class_nesting}::#{@class_name_without_nesting}"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# Extract modules from filesystem-style or ruby-style path:
|
72
|
+
# good/fun/stuff
|
73
|
+
# Good::Fun::Stuff
|
74
|
+
# produce the same results.
|
75
|
+
def extract_modules(name)
|
76
|
+
modules = name.include?('/') ? name.split('/') : name.split('::')
|
77
|
+
name = modules.pop
|
78
|
+
path = modules.map { |m| m.underscore }
|
79
|
+
file_path = (path + [name.underscore]).join('/')
|
80
|
+
nesting = modules.map { |m| m.camelize }.join('::')
|
81
|
+
[name, path, file_path, nesting, modules.size]
|
82
|
+
end
|
83
|
+
|
84
|
+
def inflect_names(name)
|
85
|
+
camel = name.camelize
|
86
|
+
under = camel.underscore
|
87
|
+
plural = under.pluralize
|
88
|
+
[camel, under, plural]
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
data/lib/reactive.rb
ADDED
data/lib/request.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
class SourceAnnotationExtractor
|
2
|
+
class Annotation < Struct.new(:line, :tag, :text)
|
3
|
+
def to_s(options={})
|
4
|
+
s = "[%3d] " % line
|
5
|
+
s << "[#{tag}] " if options[:tag]
|
6
|
+
s << text
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.enumerate(tag, options={})
|
11
|
+
extractor = new(tag)
|
12
|
+
extractor.display(extractor.find, options)
|
13
|
+
end
|
14
|
+
|
15
|
+
attr_reader :tag
|
16
|
+
|
17
|
+
def initialize(tag)
|
18
|
+
@tag = tag
|
19
|
+
end
|
20
|
+
|
21
|
+
def find(dirs=%w(app lib test))
|
22
|
+
dirs.inject({}) { |h, dir| h.update(find_in(dir)) }
|
23
|
+
end
|
24
|
+
|
25
|
+
def find_in(dir)
|
26
|
+
results = {}
|
27
|
+
|
28
|
+
Dir.glob("#{dir}/*") do |item|
|
29
|
+
next if File.basename(item)[0] == ?.
|
30
|
+
|
31
|
+
if File.directory?(item)
|
32
|
+
results.update(find_in(item))
|
33
|
+
elsif item =~ /\.(builder|(r(?:b|xml|js)))$/
|
34
|
+
results.update(extract_annotations_from(item, /#\s*(#{tag}):?\s*(.*)$/))
|
35
|
+
elsif item =~ /\.(rhtml|erb)$/
|
36
|
+
results.update(extract_annotations_from(item, /<%\s*#\s*(#{tag}):?\s*(.*?)\s*%>/))
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
results
|
41
|
+
end
|
42
|
+
|
43
|
+
def extract_annotations_from(file, pattern)
|
44
|
+
lineno = 0
|
45
|
+
result = File.readlines(file).inject([]) do |list, line|
|
46
|
+
lineno += 1
|
47
|
+
next list unless line =~ pattern
|
48
|
+
list << Annotation.new(lineno, $1, $2)
|
49
|
+
end
|
50
|
+
result.empty? ? {} : { file => result }
|
51
|
+
end
|
52
|
+
|
53
|
+
def display(results, options={})
|
54
|
+
results.keys.sort.each do |file|
|
55
|
+
puts "#{file}:"
|
56
|
+
results[file].each do |note|
|
57
|
+
puts " * #{note.to_s(options)}"
|
58
|
+
end
|
59
|
+
puts
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'source_annotation_extractor'
|
2
|
+
|
3
|
+
desc "Enumerate all annotations"
|
4
|
+
task :notes do
|
5
|
+
SourceAnnotationExtractor.enumerate "OPTIMIZE|FIXME|TODO", :tag => true
|
6
|
+
end
|
7
|
+
|
8
|
+
namespace :notes do
|
9
|
+
desc "Enumerate all OPTIMIZE annotations"
|
10
|
+
task :optimize do
|
11
|
+
SourceAnnotationExtractor.enumerate "OPTIMIZE"
|
12
|
+
end
|
13
|
+
|
14
|
+
desc "Enumerate all FIXME annotations"
|
15
|
+
task :fixme do
|
16
|
+
SourceAnnotationExtractor.enumerate "FIXME"
|
17
|
+
end
|
18
|
+
|
19
|
+
desc "Enumerate all TODO annotations"
|
20
|
+
task :todo do
|
21
|
+
SourceAnnotationExtractor.enumerate "TODO"
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,347 @@
|
|
1
|
+
namespace :db do
|
2
|
+
namespace :create do
|
3
|
+
desc 'Create all the local databases defined in config/database.yml'
|
4
|
+
task :all => :environment do
|
5
|
+
ActiveRecord::Base.configurations.each_value do |config|
|
6
|
+
# Skip entries that don't have a database key, such as the first entry here:
|
7
|
+
#
|
8
|
+
# defaults: &defaults
|
9
|
+
# adapter: mysql
|
10
|
+
# username: root
|
11
|
+
# password:
|
12
|
+
# host: localhost
|
13
|
+
#
|
14
|
+
# development:
|
15
|
+
# database: blog_development
|
16
|
+
# <<: *defaults
|
17
|
+
next unless config['database']
|
18
|
+
# Only connect to local databases
|
19
|
+
if %w( 127.0.0.1 localhost ).include?(config['host']) || config['host'].blank?
|
20
|
+
create_database(config)
|
21
|
+
else
|
22
|
+
p "This task only creates local databases. #{config['database']} is on a remote host."
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
desc 'Create the database defined in config/database.yml for the current REACTIVE_ENV'
|
29
|
+
task :create => :environment do
|
30
|
+
create_database(ActiveRecord::Base.configurations[REACTIVE_ENV])
|
31
|
+
end
|
32
|
+
|
33
|
+
def create_database(config)
|
34
|
+
begin
|
35
|
+
ActiveRecord::Base.establish_connection(config)
|
36
|
+
ActiveRecord::Base.connection
|
37
|
+
rescue
|
38
|
+
case config['adapter']
|
39
|
+
when 'mysql'
|
40
|
+
@charset = ENV['CHARSET'] || 'utf8'
|
41
|
+
@collation = ENV['COLLATION'] || 'utf8_general_ci'
|
42
|
+
begin
|
43
|
+
ActiveRecord::Base.establish_connection(config.merge({'database' => nil}))
|
44
|
+
ActiveRecord::Base.connection.create_database(config['database'], {:charset => @charset, :collation => @collation})
|
45
|
+
ActiveRecord::Base.establish_connection(config)
|
46
|
+
rescue
|
47
|
+
$stderr.puts "Couldn't create database for #{config.inspect}"
|
48
|
+
end
|
49
|
+
when 'postgresql'
|
50
|
+
`createdb "#{config['database']}" -E utf8`
|
51
|
+
when 'sqlite'
|
52
|
+
`sqlite "#{config['database']}"`
|
53
|
+
when 'sqlite3'
|
54
|
+
`sqlite3 "#{config['database']}"`
|
55
|
+
end
|
56
|
+
else
|
57
|
+
p "#{config['database']} already exists"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
namespace :drop do
|
62
|
+
desc 'Drops all the local databases defined in config/database.yml'
|
63
|
+
task :all => :environment do
|
64
|
+
ActiveRecord::Base.configurations.each_value do |config|
|
65
|
+
# Skip entries that don't have a database key
|
66
|
+
next unless config['database']
|
67
|
+
# Only connect to local databases
|
68
|
+
if config['host'] == 'localhost' || config['host'].blank?
|
69
|
+
drop_database(config)
|
70
|
+
else
|
71
|
+
p "This task only drops local databases. #{config['database']} is on a remote host."
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
desc 'Drops the database for the current REACTIVE_ENV'
|
78
|
+
task :drop => :environment do
|
79
|
+
drop_database(ActiveRecord::Base.configurations[REACTIVE_ENV || 'development'])
|
80
|
+
end
|
81
|
+
|
82
|
+
desc "Migrate the database through scripts in db/migrate. Target specific version with VERSION=x. Turn off output with VERBOSE=false."
|
83
|
+
task :migrate => :environment do
|
84
|
+
ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
|
85
|
+
ActiveRecord::Migrator.migrate("db/migrate/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
|
86
|
+
Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
|
87
|
+
end
|
88
|
+
|
89
|
+
namespace :migrate do
|
90
|
+
desc 'Rollbacks the database one migration and re migrate up. If you want to rollback more than one step, define STEP=x'
|
91
|
+
task :redo => [ 'db:rollback', 'db:migrate' ]
|
92
|
+
|
93
|
+
desc 'Resets your database using your migrations for the current environment'
|
94
|
+
task :reset => ["db:drop", "db:create", "db:migrate"]
|
95
|
+
end
|
96
|
+
|
97
|
+
desc 'Rolls the schema back to the previous version. Specify the number of steps with STEP=n'
|
98
|
+
task :rollback => :environment do
|
99
|
+
step = ENV['STEP'] ? ENV['STEP'].to_i : 1
|
100
|
+
version = ActiveRecord::Migrator.current_version - step
|
101
|
+
ActiveRecord::Migrator.migrate('db/migrate/', version)
|
102
|
+
end
|
103
|
+
|
104
|
+
desc 'Drops and recreates the database from db/schema.rb for the current environment.'
|
105
|
+
task :reset => ['db:drop', 'db:create', 'db:schema:load']
|
106
|
+
|
107
|
+
desc "Retrieves the charset for the current environment's database"
|
108
|
+
task :charset => :environment do
|
109
|
+
config = ActiveRecord::Base.configurations[REACTIVE_ENV || 'development']
|
110
|
+
case config['adapter']
|
111
|
+
when 'mysql'
|
112
|
+
ActiveRecord::Base.establish_connection(config)
|
113
|
+
puts ActiveRecord::Base.connection.charset
|
114
|
+
else
|
115
|
+
puts 'sorry, your database adapter is not supported yet, feel free to submit a patch'
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
desc "Retrieves the collation for the current environment's database"
|
120
|
+
task :collation => :environment do
|
121
|
+
config = ActiveRecord::Base.configurations[REACTIVE_ENV || 'development']
|
122
|
+
case config['adapter']
|
123
|
+
when 'mysql'
|
124
|
+
ActiveRecord::Base.establish_connection(config)
|
125
|
+
puts ActiveRecord::Base.connection.collation
|
126
|
+
else
|
127
|
+
puts 'sorry, your database adapter is not supported yet, feel free to submit a patch'
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
desc "Retrieves the current schema version number"
|
132
|
+
task :version => :environment do
|
133
|
+
puts "Current version: #{ActiveRecord::Migrator.current_version}"
|
134
|
+
end
|
135
|
+
|
136
|
+
desc "Raises an error if there are pending migrations"
|
137
|
+
task :abort_if_pending_migrations => :environment do
|
138
|
+
if defined? ActiveRecord
|
139
|
+
pending_migrations = ActiveRecord::Migrator.new(:up, 'db/migrate').pending_migrations
|
140
|
+
|
141
|
+
if pending_migrations.any?
|
142
|
+
puts "You have #{pending_migrations.size} pending migrations:"
|
143
|
+
pending_migrations.each do |pending_migration|
|
144
|
+
puts ' %4d %s' % [pending_migration.version, pending_migration.name]
|
145
|
+
end
|
146
|
+
abort "Run `rake db:migrate` to update your database then try again."
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
namespace :fixtures do
|
152
|
+
desc "Load fixtures into the current environment's database. Load specific fixtures using FIXTURES=x,y"
|
153
|
+
task :load => :environment do
|
154
|
+
require 'active_record/fixtures'
|
155
|
+
ActiveRecord::Base.establish_connection(REACTIVE_ENV.to_sym)
|
156
|
+
(ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/) : Dir.glob(File.join(REACTIVE_ROOT, 'test', 'fixtures', '*.{yml,csv}'))).each do |fixture_file|
|
157
|
+
Fixtures.create_fixtures('test/fixtures', File.basename(fixture_file, '.*'))
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
desc "Search for a fixture given a LABEL or ID."
|
162
|
+
task :identify => :environment do
|
163
|
+
require "active_record/fixtures"
|
164
|
+
|
165
|
+
label, id = ENV["LABEL"], ENV["ID"]
|
166
|
+
raise "LABEL or ID required" if label.blank? && id.blank?
|
167
|
+
|
168
|
+
puts %Q(The fixture ID for "#{label}" is #{Fixtures.identify(label)}.) if label
|
169
|
+
|
170
|
+
Dir["#{REACTIVE_ROOT}/test/fixtures/**/*.yml"].each do |file|
|
171
|
+
if data = YAML::load(ERB.new(IO.read(file)).result)
|
172
|
+
data.keys.each do |key|
|
173
|
+
key_id = Fixtures.identify(key)
|
174
|
+
|
175
|
+
if key == label || key_id == id.to_i
|
176
|
+
puts "#{file}: #{key} (#{key_id})"
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
namespace :schema do
|
185
|
+
desc "Create a db/schema.rb file that can be portably used against any DB supported by AR"
|
186
|
+
task :dump => :environment do
|
187
|
+
require 'active_record/schema_dumper'
|
188
|
+
File.open(ENV['SCHEMA'] || "db/schema.rb", "w") do |file|
|
189
|
+
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
desc "Load a schema.rb file into the database"
|
194
|
+
task :load => :environment do
|
195
|
+
file = ENV['SCHEMA'] || "db/schema.rb"
|
196
|
+
load(file)
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
namespace :structure do
|
201
|
+
desc "Dump the database structure to a SQL file"
|
202
|
+
task :dump => :environment do
|
203
|
+
abcs = ActiveRecord::Base.configurations
|
204
|
+
case abcs[REACTIVE_ENV]["adapter"]
|
205
|
+
when "mysql", "oci", "oracle"
|
206
|
+
ActiveRecord::Base.establish_connection(abcs[REACTIVE_ENV])
|
207
|
+
File.open("db/#{REACTIVE_ENV}_structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump }
|
208
|
+
when "postgresql"
|
209
|
+
ENV['PGHOST'] = abcs[REACTIVE_ENV]["host"] if abcs[REACTIVE_ENV]["host"]
|
210
|
+
ENV['PGPORT'] = abcs[REACTIVE_ENV]["port"].to_s if abcs[REACTIVE_ENV]["port"]
|
211
|
+
ENV['PGPASSWORD'] = abcs[REACTIVE_ENV]["password"].to_s if abcs[REACTIVE_ENV]["password"]
|
212
|
+
search_path = abcs[REACTIVE_ENV]["schema_search_path"]
|
213
|
+
search_path = "--schema=#{search_path}" if search_path
|
214
|
+
`pg_dump -i -U "#{abcs[REACTIVE_ENV]["username"]}" -s -x -O -f db/#{REACTIVE_ENV}_structure.sql #{search_path} #{abcs[REACTIVE_ENV]["database"]}`
|
215
|
+
raise "Error dumping database" if $?.exitstatus == 1
|
216
|
+
when "sqlite", "sqlite3"
|
217
|
+
dbfile = abcs[REACTIVE_ENV]["database"] || abcs[REACTIVE_ENV]["dbfile"]
|
218
|
+
`#{abcs[REACTIVE_ENV]["adapter"]} #{dbfile} .schema > db/#{REACTIVE_ENV}_structure.sql`
|
219
|
+
when "sqlserver"
|
220
|
+
`scptxfr /s #{abcs[REACTIVE_ENV]["host"]} /d #{abcs[REACTIVE_ENV]["database"]} /I /f db\\#{REACTIVE_ENV}_structure.sql /q /A /r`
|
221
|
+
`scptxfr /s #{abcs[REACTIVE_ENV]["host"]} /d #{abcs[REACTIVE_ENV]["database"]} /I /F db\ /q /A /r`
|
222
|
+
when "firebird"
|
223
|
+
set_firebird_env(abcs[REACTIVE_ENV])
|
224
|
+
db_string = firebird_db_string(abcs[REACTIVE_ENV])
|
225
|
+
sh "isql -a #{db_string} > db/#{REACTIVE_ENV}_structure.sql"
|
226
|
+
else
|
227
|
+
raise "Task not supported by '#{abcs["test"]["adapter"]}'"
|
228
|
+
end
|
229
|
+
|
230
|
+
if ActiveRecord::Base.connection.supports_migrations?
|
231
|
+
File.open("db/#{REACTIVE_ENV}_structure.sql", "a") { |f| f << ActiveRecord::Base.connection.dump_schema_information }
|
232
|
+
end
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
namespace :test do
|
237
|
+
desc "Recreate the test database from the current environment's database schema"
|
238
|
+
task :clone => %w(db:schema:dump db:test:purge) do
|
239
|
+
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
|
240
|
+
ActiveRecord::Schema.verbose = false
|
241
|
+
Rake::Task["db:schema:load"].invoke
|
242
|
+
end
|
243
|
+
|
244
|
+
|
245
|
+
desc "Recreate the test databases from the development structure"
|
246
|
+
task :clone_structure => [ "db:structure:dump", "db:test:purge" ] do
|
247
|
+
abcs = ActiveRecord::Base.configurations
|
248
|
+
case abcs["test"]["adapter"]
|
249
|
+
when "mysql"
|
250
|
+
ActiveRecord::Base.establish_connection(:test)
|
251
|
+
ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0')
|
252
|
+
IO.readlines("db/#{REACTIVE_ENV}_structure.sql").join.split("\n\n").each do |table|
|
253
|
+
ActiveRecord::Base.connection.execute(table)
|
254
|
+
end
|
255
|
+
when "postgresql"
|
256
|
+
ENV['PGHOST'] = abcs["test"]["host"] if abcs["test"]["host"]
|
257
|
+
ENV['PGPORT'] = abcs["test"]["port"].to_s if abcs["test"]["port"]
|
258
|
+
ENV['PGPASSWORD'] = abcs["test"]["password"].to_s if abcs["test"]["password"]
|
259
|
+
`psql -U "#{abcs["test"]["username"]}" -f db/#{REACTIVE_ENV}_structure.sql #{abcs["test"]["database"]}`
|
260
|
+
when "sqlite", "sqlite3"
|
261
|
+
dbfile = abcs["test"]["database"] || abcs["test"]["dbfile"]
|
262
|
+
`#{abcs["test"]["adapter"]} #{dbfile} < db/#{REACTIVE_ENV}_structure.sql`
|
263
|
+
when "sqlserver"
|
264
|
+
`osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{REACTIVE_ENV}_structure.sql`
|
265
|
+
when "oci", "oracle"
|
266
|
+
ActiveRecord::Base.establish_connection(:test)
|
267
|
+
IO.readlines("db/#{REACTIVE_ENV}_structure.sql").join.split(";\n\n").each do |ddl|
|
268
|
+
ActiveRecord::Base.connection.execute(ddl)
|
269
|
+
end
|
270
|
+
when "firebird"
|
271
|
+
set_firebird_env(abcs["test"])
|
272
|
+
db_string = firebird_db_string(abcs["test"])
|
273
|
+
sh "isql -i db/#{REACTIVE_ENV}_structure.sql #{db_string}"
|
274
|
+
else
|
275
|
+
raise "Task not supported by '#{abcs["test"]["adapter"]}'"
|
276
|
+
end
|
277
|
+
end
|
278
|
+
|
279
|
+
desc "Empty the test database"
|
280
|
+
task :purge => :environment do
|
281
|
+
abcs = ActiveRecord::Base.configurations
|
282
|
+
case abcs["test"]["adapter"]
|
283
|
+
when "mysql"
|
284
|
+
ActiveRecord::Base.establish_connection(:test)
|
285
|
+
ActiveRecord::Base.connection.recreate_database(abcs["test"]["database"])
|
286
|
+
when "postgresql"
|
287
|
+
ENV['PGHOST'] = abcs["test"]["host"] if abcs["test"]["host"]
|
288
|
+
ENV['PGPORT'] = abcs["test"]["port"].to_s if abcs["test"]["port"]
|
289
|
+
ENV['PGPASSWORD'] = abcs["test"]["password"].to_s if abcs["test"]["password"]
|
290
|
+
enc_option = "-E #{abcs["test"]["encoding"]}" if abcs["test"]["encoding"]
|
291
|
+
|
292
|
+
ActiveRecord::Base.clear_active_connections!
|
293
|
+
`dropdb -U "#{abcs["test"]["username"]}" #{abcs["test"]["database"]}`
|
294
|
+
`createdb #{enc_option} -U "#{abcs["test"]["username"]}" #{abcs["test"]["database"]}`
|
295
|
+
when "sqlite","sqlite3"
|
296
|
+
dbfile = abcs["test"]["database"] || abcs["test"]["dbfile"]
|
297
|
+
File.delete(dbfile) if File.exist?(dbfile)
|
298
|
+
when "sqlserver"
|
299
|
+
dropfkscript = "#{abcs["test"]["host"]}.#{abcs["test"]["database"]}.DP1".gsub(/\\/,'-')
|
300
|
+
`osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{dropfkscript}`
|
301
|
+
`osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{REACTIVE_ENV}_structure.sql`
|
302
|
+
when "oci", "oracle"
|
303
|
+
ActiveRecord::Base.establish_connection(:test)
|
304
|
+
ActiveRecord::Base.connection.structure_drop.split(";\n\n").each do |ddl|
|
305
|
+
ActiveRecord::Base.connection.execute(ddl)
|
306
|
+
end
|
307
|
+
when "firebird"
|
308
|
+
ActiveRecord::Base.establish_connection(:test)
|
309
|
+
ActiveRecord::Base.connection.recreate_database!
|
310
|
+
else
|
311
|
+
raise "Task not supported by '#{abcs["test"]["adapter"]}'"
|
312
|
+
end
|
313
|
+
end
|
314
|
+
|
315
|
+
desc 'Prepare the test database and load the schema'
|
316
|
+
task :prepare => %w(environment db:abort_if_pending_migrations) do
|
317
|
+
if defined?(ActiveRecord) && !ActiveRecord::Base.configurations.blank?
|
318
|
+
Rake::Task[{ :sql => "db:test:clone_structure", :ruby => "db:test:clone" }[ActiveRecord::Base.schema_format]].invoke
|
319
|
+
end
|
320
|
+
end
|
321
|
+
end
|
322
|
+
|
323
|
+
end
|
324
|
+
|
325
|
+
def drop_database(config)
|
326
|
+
case config['adapter']
|
327
|
+
when 'mysql'
|
328
|
+
ActiveRecord::Base.connection.drop_database config['database']
|
329
|
+
when /^sqlite/
|
330
|
+
FileUtils.rm_f(File.join(REACTIVE_ROOT, config['database']))
|
331
|
+
when 'postgresql'
|
332
|
+
`dropdb "#{config['database']}"`
|
333
|
+
end
|
334
|
+
end
|
335
|
+
|
336
|
+
def session_table_name
|
337
|
+
ActiveRecord::Base.pluralize_table_names ? :sessions : :session
|
338
|
+
end
|
339
|
+
|
340
|
+
def set_firebird_env(config)
|
341
|
+
ENV["ISC_USER"] = config["username"].to_s if config["username"]
|
342
|
+
ENV["ISC_PASSWORD"] = config["password"].to_s if config["password"]
|
343
|
+
end
|
344
|
+
|
345
|
+
def firebird_db_string(config)
|
346
|
+
FireRuby::Database.db_string_for(config.symbolize_keys)
|
347
|
+
end
|