ruby-app-ar 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +3 -0
- data/LICENSE +20 -0
- data/README.md +29 -0
- data/Rakefile +8 -0
- data/lib/activerecord.rb +6 -0
- data/lib/config.rb +23 -0
- data/lib/ruby-app-ar/tasks.rake +402 -0
- data/lib/ruby-app-ar.rb +15 -0
- data/lib/version.rb +3 -0
- data/ruby-app-ar.gemspec +28 -0
- data/spec/app_spec.rb +14 -0
- data/spec/spec_helper.rb +5 -0
- data/spec/test_app/.gitignore +8 -0
- data/spec/test_app/app/models/.gitkeep +0 -0
- data/spec/test_app/app/models/task.rb +3 -0
- data/spec/test_app/config/boot.rb +14 -0
- data/spec/test_app/config/config.rb +9 -0
- data/spec/test_app/config/database.yml +4 -0
- data/spec/test_app/config/environment.rb +9 -0
- data/spec/test_app/config/environments/test.rb +8 -0
- data/spec/test_app/db/migrate/20120616161100_create_tasks.rb +15 -0
- data/spec/test_app/db/test.db +0 -0
- data/spec/test_app/lib/application.rb +11 -0
- metadata +158 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Makarchev K
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
RubyAppAr
|
2
|
+
=========
|
3
|
+
|
4
|
+
[RubyApp](http://github.com/kostya/ruby-app) extension, adds ActiveRecord support.
|
5
|
+
|
6
|
+
Use generator:
|
7
|
+
|
8
|
+
ruby-app new_app -ar
|
9
|
+
|
10
|
+
|
11
|
+
### OR Manually
|
12
|
+
|
13
|
+
|
14
|
+
Add to Gemfile:
|
15
|
+
|
16
|
+
gem 'ruby-app'
|
17
|
+
gem 'ruby-app-ar'
|
18
|
+
|
19
|
+
Add to Rakefile:
|
20
|
+
|
21
|
+
load "ruby-app/tasks.rake"
|
22
|
+
load "ruby-app-ar/tasks.rake"
|
23
|
+
|
24
|
+
Create
|
25
|
+
|
26
|
+
db/migrate
|
27
|
+
config/database.yml
|
28
|
+
|
29
|
+
|
data/Rakefile
ADDED
data/lib/activerecord.rb
ADDED
data/lib/config.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
|
3
|
+
# Defaults values
|
4
|
+
App.config.define :default_ar do
|
5
|
+
|
6
|
+
database_yml do
|
7
|
+
@database_yml ||= begin
|
8
|
+
require 'erb'
|
9
|
+
YAML.load(
|
10
|
+
ERB.new(
|
11
|
+
File.read(File.join(App.root, %w{config database.yml} ))
|
12
|
+
).result
|
13
|
+
)
|
14
|
+
rescue
|
15
|
+
raise "Not found config/database.yml"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
database_settings do
|
20
|
+
App.config.database_yml[App.env]
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,402 @@
|
|
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
|
+
local_database?(config) { create_database(config) }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
desc 'Create the database defined in config/database.yml for the current Application.env'
|
25
|
+
task :create => :environment do
|
26
|
+
create_database(App.config.database_settings)
|
27
|
+
end
|
28
|
+
|
29
|
+
def create_database(config)
|
30
|
+
begin
|
31
|
+
if config['adapter'] =~ /sqlite/
|
32
|
+
if File.exist?(config['database'])
|
33
|
+
$stderr.puts "#{config['database']} already exists"
|
34
|
+
else
|
35
|
+
begin
|
36
|
+
# Create the SQLite database
|
37
|
+
ActiveRecord::Base.establish_connection(config)
|
38
|
+
ActiveRecord::Base.connection
|
39
|
+
rescue
|
40
|
+
$stderr.puts $!, *($!.backtrace)
|
41
|
+
$stderr.puts "Couldn't create database for #{config.inspect}"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
return # Skip the else clause of begin/rescue
|
45
|
+
else
|
46
|
+
ActiveRecord::Base.establish_connection(config)
|
47
|
+
ActiveRecord::Base.connection
|
48
|
+
end
|
49
|
+
rescue
|
50
|
+
case config['adapter']
|
51
|
+
when 'mysql'
|
52
|
+
@charset = ENV['CHARSET'] || 'utf8'
|
53
|
+
@collation = ENV['COLLATION'] || 'utf8_general_ci'
|
54
|
+
begin
|
55
|
+
ActiveRecord::Base.establish_connection(config.merge('database' => nil))
|
56
|
+
ActiveRecord::Base.connection.create_database(config['database'], :charset => (config['charset'] || @charset), :collation => (config['collation'] || @collation))
|
57
|
+
ActiveRecord::Base.establish_connection(config)
|
58
|
+
rescue
|
59
|
+
$stderr.puts "Couldn't create database for #{config.inspect}, charset: #{config['charset'] || @charset}, collation: #{config['collation'] || @collation} (if you set the charset manually, make sure you have a matching collation)"
|
60
|
+
end
|
61
|
+
when 'postgresql'
|
62
|
+
@encoding = config[:encoding] || ENV['CHARSET'] || 'utf8'
|
63
|
+
begin
|
64
|
+
ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
|
65
|
+
ActiveRecord::Base.connection.create_database(config['database'], config.merge('encoding' => @encoding))
|
66
|
+
ActiveRecord::Base.establish_connection(config)
|
67
|
+
rescue
|
68
|
+
$stderr.puts $!, *($!.backtrace)
|
69
|
+
$stderr.puts "Couldn't create database for #{config.inspect}"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
else
|
73
|
+
$stderr.puts "#{config['database']} already exists"
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
namespace :drop do
|
78
|
+
desc 'Drops all the local databases defined in config/database.yml'
|
79
|
+
task :all => :environment do
|
80
|
+
ActiveRecord::Base.configurations.each_value do |config|
|
81
|
+
# Skip entries that don't have a database key
|
82
|
+
next unless config['database']
|
83
|
+
# Only connect to local databases
|
84
|
+
local_database?(config) { drop_database(config) }
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
desc 'Drops the database for the current Application.env'
|
90
|
+
task :drop => :environment do
|
91
|
+
config = ActiveRecord::Base.configurations[Application.env || 'development']
|
92
|
+
begin
|
93
|
+
drop_database(config)
|
94
|
+
rescue Exception => e
|
95
|
+
puts "Couldn't drop #{config['database']} : #{e.inspect}"
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def local_database?(config, &block)
|
100
|
+
if %w( 127.0.0.1 localhost ).include?(config['host']) || config['host'].blank?
|
101
|
+
yield
|
102
|
+
else
|
103
|
+
puts "This task only modifies local databases. #{config['database']} is on a remote host."
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
|
108
|
+
desc "Migrate the database through scripts in db/migrate. Target specific version with VERSION=x. Turn off output with VERBOSE=false."
|
109
|
+
task :migrate => :environment do
|
110
|
+
ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
|
111
|
+
ActiveRecord::Migrator.migrate("db/migrate/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
|
112
|
+
Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
|
113
|
+
end
|
114
|
+
|
115
|
+
namespace :migrate do
|
116
|
+
desc 'Rollbacks the database one migration and re migrate up. If you want to rollback more than one step, define STEP=x. Target specific version with VERSION=x.'
|
117
|
+
task :redo => :environment do
|
118
|
+
if ENV["VERSION"]
|
119
|
+
Rake::Task["db:migrate:down"].invoke
|
120
|
+
Rake::Task["db:migrate:up"].invoke
|
121
|
+
else
|
122
|
+
Rake::Task["db:rollback"].invoke
|
123
|
+
Rake::Task["db:migrate"].invoke
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
desc 'Resets your database using your migrations for the current environment'
|
128
|
+
task :reset => ["db:drop", "db:create", "db:migrate"]
|
129
|
+
|
130
|
+
desc 'Runs the "up" for a given migration VERSION.'
|
131
|
+
task :up => :environment do
|
132
|
+
version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
|
133
|
+
raise "VERSION is required" unless version
|
134
|
+
ActiveRecord::Migrator.run(:up, "db/migrate/", version)
|
135
|
+
Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
|
136
|
+
end
|
137
|
+
|
138
|
+
desc 'Runs the "down" for a given migration VERSION.'
|
139
|
+
task :down => :environment do
|
140
|
+
version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
|
141
|
+
raise "VERSION is required" unless version
|
142
|
+
ActiveRecord::Migrator.run(:down, "db/migrate/", version)
|
143
|
+
Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
desc 'Rolls the schema back to the previous version. Specify the number of steps with STEP=n'
|
148
|
+
task :rollback => :environment do
|
149
|
+
step = ENV['STEP'] ? ENV['STEP'].to_i : 1
|
150
|
+
ActiveRecord::Migrator.rollback('db/migrate/', step)
|
151
|
+
Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
|
152
|
+
end
|
153
|
+
|
154
|
+
desc 'Drops and recreates the database from db/schema.rb for the current environment.'
|
155
|
+
task :reset => ['db:drop', 'db:create', 'db:schema:load']
|
156
|
+
|
157
|
+
desc "Retrieves the charset for the current environment's database"
|
158
|
+
task :charset => :environment do
|
159
|
+
config = ActiveRecord::Base.configurations[Application.env || 'development']
|
160
|
+
case config['adapter']
|
161
|
+
when 'mysql'
|
162
|
+
ActiveRecord::Base.establish_connection(config)
|
163
|
+
puts ActiveRecord::Base.connection.charset
|
164
|
+
when 'postgresql'
|
165
|
+
ActiveRecord::Base.establish_connection(config)
|
166
|
+
puts ActiveRecord::Base.connection.encoding
|
167
|
+
else
|
168
|
+
puts 'sorry, your database adapter is not supported yet, feel free to submit a patch'
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
desc "Retrieves the collation for the current environment's database"
|
173
|
+
task :collation => :environment do
|
174
|
+
config = ActiveRecord::Base.configurations[Application.env || 'development']
|
175
|
+
case config['adapter']
|
176
|
+
when 'mysql'
|
177
|
+
ActiveRecord::Base.establish_connection(config)
|
178
|
+
puts ActiveRecord::Base.connection.collation
|
179
|
+
else
|
180
|
+
puts 'sorry, your database adapter is not supported yet, feel free to submit a patch'
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
desc "Retrieves the current schema version number"
|
185
|
+
task :version => :environment do
|
186
|
+
puts "Current version: #{ActiveRecord::Migrator.current_version}"
|
187
|
+
end
|
188
|
+
|
189
|
+
desc "Raises an error if there are pending migrations"
|
190
|
+
task :abort_if_pending_migrations => :environment do
|
191
|
+
if defined? ActiveRecord
|
192
|
+
pending_migrations = ActiveRecord::Migrator.new(:up, 'db/migrate').pending_migrations
|
193
|
+
|
194
|
+
if pending_migrations.any?
|
195
|
+
puts "You have #{pending_migrations.size} pending migrations:"
|
196
|
+
pending_migrations.each do |pending_migration|
|
197
|
+
puts ' %4d %s' % [pending_migration.version, pending_migration.name]
|
198
|
+
end
|
199
|
+
abort %{Run "rake db:migrate" to update your database then try again.}
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
namespace :fixtures do
|
205
|
+
desc "Load fixtures into the current environment's database. Load specific fixtures using FIXTURES=x,y. Load from subdirectory in test/fixtures using FIXTURES_DIR=z. Specify an alternative path (eg. spec/fixtures) using FIXTURES_PATH=spec/fixtures."
|
206
|
+
task :load => :environment do
|
207
|
+
require 'active_record/fixtures'
|
208
|
+
ActiveRecord::Base.establish_connection(App.env)
|
209
|
+
base_dir = ENV['FIXTURES_PATH'] ? File.join(App.root, ENV['FIXTURES_PATH']) : File.join(App.root, 'test', 'fixtures')
|
210
|
+
fixtures_dir = ENV['FIXTURES_DIR'] ? File.join(base_dir, ENV['FIXTURES_DIR']) : base_dir
|
211
|
+
|
212
|
+
(ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/).map {|f| File.join(fixtures_dir, f) } : Dir.glob(File.join(fixtures_dir, '*.{yml,csv}'))).each do |fixture_file|
|
213
|
+
Fixtures.create_fixtures(File.dirname(fixture_file), File.basename(fixture_file, '.*'))
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
desc "Search for a fixture given a LABEL or ID. Specify an alternative path (eg. spec/fixtures) using FIXTURES_PATH=spec/fixtures."
|
218
|
+
task :identify => :environment do
|
219
|
+
require "active_record/fixtures"
|
220
|
+
|
221
|
+
label, id = ENV["LABEL"], ENV["ID"]
|
222
|
+
raise "LABEL or ID required" if label.blank? && id.blank?
|
223
|
+
|
224
|
+
puts %Q(The fixture ID for "#{label}" is #{Fixtures.identify(label)}.) if label
|
225
|
+
|
226
|
+
base_dir = ENV['FIXTURES_PATH'] ? File.join(App.root, ENV['FIXTURES_PATH']) : File.join(App.root, 'test', 'fixtures')
|
227
|
+
Dir["#{base_dir}/**/*.yml"].each do |file|
|
228
|
+
if data = YAML::load(ERB.new(IO.read(file)).result)
|
229
|
+
data.keys.each do |key|
|
230
|
+
key_id = Fixtures.identify(key)
|
231
|
+
|
232
|
+
if key == label || key_id == id.to_i
|
233
|
+
puts "#{file}: #{key} (#{key_id})"
|
234
|
+
end
|
235
|
+
end
|
236
|
+
end
|
237
|
+
end
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
namespace :schema do
|
242
|
+
desc "Create a db/schema.rb file that can be portably used against any DB supported by AR"
|
243
|
+
task :dump => :environment do
|
244
|
+
require 'active_record/schema_dumper'
|
245
|
+
File.open(ENV['SCHEMA'] || "#{Application.root}/db/schema.rb", "w") do |file|
|
246
|
+
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
desc "Load a schema.rb file into the database"
|
251
|
+
task :load => :environment do
|
252
|
+
file = ENV['SCHEMA'] || "#{Application.root}/db/schema.rb"
|
253
|
+
load(file)
|
254
|
+
end
|
255
|
+
end
|
256
|
+
|
257
|
+
namespace :structure do
|
258
|
+
desc "Dump the database structure to a SQL file"
|
259
|
+
task :dump => :environment do
|
260
|
+
abcs = ActiveRecord::Base.configurations
|
261
|
+
case abcs[Application.env]["adapter"]
|
262
|
+
when "mysql", "oci", "oracle"
|
263
|
+
ActiveRecord::Base.establish_connection(abcs[Application.env])
|
264
|
+
File.open("#{Application.root}/db/#{Application.env}_structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump }
|
265
|
+
when "postgresql"
|
266
|
+
ENV['PGHOST'] = abcs[Application.env]["host"] if abcs[Application.env]["host"]
|
267
|
+
ENV['PGPORT'] = abcs[Application.env]["port"].to_s if abcs[Application.env]["port"]
|
268
|
+
ENV['PGPASSWORD'] = abcs[Application.env]["password"].to_s if abcs[Application.env]["password"]
|
269
|
+
search_path = abcs[Application.env]["schema_search_path"]
|
270
|
+
search_path = "--schema=#{search_path}" if search_path
|
271
|
+
`pg_dump -i -U "#{abcs[Application.env]["username"]}" -s -x -O -f db/#{Application.env}_structure.sql #{search_path} #{abcs[Application.env]["database"]}`
|
272
|
+
raise "Error dumping database" if $?.exitstatus == 1
|
273
|
+
when "sqlite", "sqlite3"
|
274
|
+
dbfile = abcs[Application.env]["database"] || abcs[Application.env]["dbfile"]
|
275
|
+
`#{abcs[Application.env]["adapter"]} #{dbfile} .schema > db/#{Application.env}_structure.sql`
|
276
|
+
when "sqlserver"
|
277
|
+
`scptxfr /s #{abcs[Application.env]["host"]} /d #{abcs[Application.env]["database"]} /I /f db\\#{Application.env}_structure.sql /q /A /r`
|
278
|
+
`scptxfr /s #{abcs[Application.env]["host"]} /d #{abcs[Application.env]["database"]} /I /F db\ /q /A /r`
|
279
|
+
when "firebird"
|
280
|
+
set_firebird_env(abcs[Application.env])
|
281
|
+
db_string = firebird_db_string(abcs[Application.env])
|
282
|
+
sh "isql -a #{db_string} > #{Application.root}/db/#{Application.env}_structure.sql"
|
283
|
+
else
|
284
|
+
raise "Task not supported by '#{abcs["test"]["adapter"]}'"
|
285
|
+
end
|
286
|
+
|
287
|
+
if ActiveRecord::Base.connection.supports_migrations?
|
288
|
+
File.open("#{Application.root}/db/#{Application.env}_structure.sql", "a") { |f| f << ActiveRecord::Base.connection.dump_schema_information }
|
289
|
+
end
|
290
|
+
end
|
291
|
+
end
|
292
|
+
|
293
|
+
namespace :test do
|
294
|
+
desc "Recreate the test database from the current schema.rb"
|
295
|
+
task :load => 'db:test:purge' do
|
296
|
+
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
|
297
|
+
ActiveRecord::Schema.verbose = false
|
298
|
+
Rake::Task["db:schema:load"].invoke
|
299
|
+
end
|
300
|
+
|
301
|
+
desc "Recreate the test database from the current environment's database schema"
|
302
|
+
task :clone => %w(db:schema:dump db:test:load)
|
303
|
+
|
304
|
+
desc "Recreate the test databases from the development structure"
|
305
|
+
task :clone_structure => [ "db:structure:dump", "db:test:purge" ] do
|
306
|
+
abcs = ActiveRecord::Base.configurations
|
307
|
+
case abcs["test"]["adapter"]
|
308
|
+
when "mysql"
|
309
|
+
ActiveRecord::Base.establish_connection(:test)
|
310
|
+
ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0')
|
311
|
+
IO.readlines("#{Application.root}/db/#{Application.env}_structure.sql").join.split("\n\n").each do |table|
|
312
|
+
ActiveRecord::Base.connection.execute(table)
|
313
|
+
end
|
314
|
+
when "postgresql"
|
315
|
+
ENV['PGHOST'] = abcs["test"]["host"] if abcs["test"]["host"]
|
316
|
+
ENV['PGPORT'] = abcs["test"]["port"].to_s if abcs["test"]["port"]
|
317
|
+
ENV['PGPASSWORD'] = abcs["test"]["password"].to_s if abcs["test"]["password"]
|
318
|
+
`psql -U "#{abcs["test"]["username"]}" -f #{Application.root}/db/#{Application.env}_structure.sql #{abcs["test"]["database"]}`
|
319
|
+
when "sqlite", "sqlite3"
|
320
|
+
dbfile = abcs["test"]["database"] || abcs["test"]["dbfile"]
|
321
|
+
`#{abcs["test"]["adapter"]} #{dbfile} < #{Application.root}/db/#{Application.env}_structure.sql`
|
322
|
+
when "sqlserver"
|
323
|
+
`osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{Application.env}_structure.sql`
|
324
|
+
when "oci", "oracle"
|
325
|
+
ActiveRecord::Base.establish_connection(:test)
|
326
|
+
IO.readlines("#{Application.root}/db/#{Application.env}_structure.sql").join.split(";\n\n").each do |ddl|
|
327
|
+
ActiveRecord::Base.connection.execute(ddl)
|
328
|
+
end
|
329
|
+
when "firebird"
|
330
|
+
set_firebird_env(abcs["test"])
|
331
|
+
db_string = firebird_db_string(abcs["test"])
|
332
|
+
sh "isql -i #{Application.root}/db/#{Application.env}_structure.sql #{db_string}"
|
333
|
+
else
|
334
|
+
raise "Task not supported by '#{abcs["test"]["adapter"]}'"
|
335
|
+
end
|
336
|
+
end
|
337
|
+
|
338
|
+
desc "Empty the test database"
|
339
|
+
task :purge => :environment do
|
340
|
+
abcs = ActiveRecord::Base.configurations
|
341
|
+
case abcs["test"]["adapter"]
|
342
|
+
when "mysql"
|
343
|
+
ActiveRecord::Base.establish_connection(:test)
|
344
|
+
ActiveRecord::Base.connection.recreate_database(abcs["test"]["database"], abcs["test"])
|
345
|
+
when "postgresql"
|
346
|
+
ActiveRecord::Base.clear_active_connections!
|
347
|
+
drop_database(abcs['test'])
|
348
|
+
create_database(abcs['test'])
|
349
|
+
when "sqlite","sqlite3"
|
350
|
+
dbfile = abcs["test"]["database"] || abcs["test"]["dbfile"]
|
351
|
+
File.delete(dbfile) if File.exist?(dbfile)
|
352
|
+
when "sqlserver"
|
353
|
+
dropfkscript = "#{abcs["test"]["host"]}.#{abcs["test"]["database"]}.DP1".gsub(/\\/,'-')
|
354
|
+
`osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{dropfkscript}`
|
355
|
+
`osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{Application.env}_structure.sql`
|
356
|
+
when "oci", "oracle"
|
357
|
+
ActiveRecord::Base.establish_connection(:test)
|
358
|
+
ActiveRecord::Base.connection.structure_drop.split(";\n\n").each do |ddl|
|
359
|
+
ActiveRecord::Base.connection.execute(ddl)
|
360
|
+
end
|
361
|
+
when "firebird"
|
362
|
+
ActiveRecord::Base.establish_connection(:test)
|
363
|
+
ActiveRecord::Base.connection.recreate_database!
|
364
|
+
else
|
365
|
+
raise "Task not supported by '#{abcs["test"]["adapter"]}'"
|
366
|
+
end
|
367
|
+
end
|
368
|
+
|
369
|
+
desc 'Check for pending migrations and load the test schema'
|
370
|
+
task :prepare => 'db:abort_if_pending_migrations' do
|
371
|
+
if defined?(ActiveRecord) && !ActiveRecord::Base.configurations.blank?
|
372
|
+
Rake::Task[{ :sql => "db:test:clone_structure", :ruby => "db:test:load" }[ActiveRecord::Base.schema_format]].invoke
|
373
|
+
end
|
374
|
+
end
|
375
|
+
end
|
376
|
+
|
377
|
+
end
|
378
|
+
|
379
|
+
def drop_database(config)
|
380
|
+
case config['adapter']
|
381
|
+
when 'mysql'
|
382
|
+
ActiveRecord::Base.connection.drop_database config['database']
|
383
|
+
when /^sqlite/
|
384
|
+
FileUtils.rm(File.join(Application.root, config['database']))
|
385
|
+
when 'postgresql'
|
386
|
+
ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
|
387
|
+
ActiveRecord::Base.connection.drop_database config['database']
|
388
|
+
end
|
389
|
+
end
|
390
|
+
|
391
|
+
def session_table_name
|
392
|
+
ActiveRecord::Base.pluralize_table_names ? :sessions : :session
|
393
|
+
end
|
394
|
+
|
395
|
+
def set_firebird_env(config)
|
396
|
+
ENV["ISC_USER"] = config["username"].to_s if config["username"]
|
397
|
+
ENV["ISC_PASSWORD"] = config["password"].to_s if config["password"]
|
398
|
+
end
|
399
|
+
|
400
|
+
def firebird_db_string(config)
|
401
|
+
FireRuby::Database.db_string_for(config.symbolize_keys)
|
402
|
+
end
|
data/lib/ruby-app-ar.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
|
2
|
+
if defined?(Application) && (Application.send(:identifier) rescue '-') == 'ruby-app'
|
3
|
+
|
4
|
+
# add rake paths
|
5
|
+
App.rake_paths << [
|
6
|
+
File.join(File.dirname(__FILE__), %w{ ruby-app-ar tasks.rake })
|
7
|
+
].map{|f| File.expand_path(f) }
|
8
|
+
|
9
|
+
# add initializers
|
10
|
+
App.initializer_paths << [
|
11
|
+
File.join(File.dirname(__FILE__), %w{ config.rb }),
|
12
|
+
File.join(File.dirname(__FILE__), %w{ activerecord.rb })
|
13
|
+
].map{|f| File.expand_path(f) }
|
14
|
+
|
15
|
+
end
|
data/lib/version.rb
ADDED
data/ruby-app-ar.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.dirname(__FILE__) + "/lib/version"
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = %q{ruby-app-ar}
|
6
|
+
s.version = RubyAppAr::VERSION
|
7
|
+
|
8
|
+
s.authors = ["Makarchev Konstantin"]
|
9
|
+
|
10
|
+
s.description = %q{RubyApp extension, adds ActiveRecord support}
|
11
|
+
s.summary = %q{RubyApp extension, adds ActiveRecord support}
|
12
|
+
|
13
|
+
s.email = %q{kostya27@gmail.com}
|
14
|
+
s.homepage = %q{http://github.com/kostya/ruby-app-ar}
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_dependency 'activerecord'
|
22
|
+
s.add_dependency 'ruby-app', ">= 0.1.4"
|
23
|
+
|
24
|
+
s.add_development_dependency "rspec"
|
25
|
+
s.add_development_dependency "rake"
|
26
|
+
s.add_development_dependency "sqlite3"
|
27
|
+
|
28
|
+
end
|
data/spec/app_spec.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe "RubyAppAr" do
|
4
|
+
|
5
|
+
before :all do
|
6
|
+
require File.join(File.dirname(__FILE__), %w{test_app config environment})
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should succesfully load" do
|
10
|
+
Task.count.should == 3
|
11
|
+
Task.last.a.should == 3
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
data/spec/spec_helper.rb
ADDED
File without changes
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
ENV["BUNDLE_GEMFILE"] = File.expand_path("../../Gemfile", __FILE__)
|
4
|
+
require 'bundler/setup'
|
5
|
+
|
6
|
+
class Application
|
7
|
+
class << self
|
8
|
+
def root
|
9
|
+
@root ||= File.expand_path("../..", __FILE__)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
require 'ruby-app/boot'
|
Binary file
|
metadata
ADDED
@@ -0,0 +1,158 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-app-ar
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 9
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: "0.1"
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Makarchev Konstantin
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2012-06-22 00:00:00 Z
|
18
|
+
dependencies:
|
19
|
+
- !ruby/object:Gem::Dependency
|
20
|
+
name: activerecord
|
21
|
+
prerelease: false
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
hash: 3
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
type: :runtime
|
32
|
+
version_requirements: *id001
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: ruby-app
|
35
|
+
prerelease: false
|
36
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
hash: 19
|
42
|
+
segments:
|
43
|
+
- 0
|
44
|
+
- 1
|
45
|
+
- 4
|
46
|
+
version: 0.1.4
|
47
|
+
type: :runtime
|
48
|
+
version_requirements: *id002
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: rspec
|
51
|
+
prerelease: false
|
52
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 3
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
type: :development
|
62
|
+
version_requirements: *id003
|
63
|
+
- !ruby/object:Gem::Dependency
|
64
|
+
name: rake
|
65
|
+
prerelease: false
|
66
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
hash: 3
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
version: "0"
|
75
|
+
type: :development
|
76
|
+
version_requirements: *id004
|
77
|
+
- !ruby/object:Gem::Dependency
|
78
|
+
name: sqlite3
|
79
|
+
prerelease: false
|
80
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
hash: 3
|
86
|
+
segments:
|
87
|
+
- 0
|
88
|
+
version: "0"
|
89
|
+
type: :development
|
90
|
+
version_requirements: *id005
|
91
|
+
description: RubyApp extension, adds ActiveRecord support
|
92
|
+
email: kostya27@gmail.com
|
93
|
+
executables: []
|
94
|
+
|
95
|
+
extensions: []
|
96
|
+
|
97
|
+
extra_rdoc_files: []
|
98
|
+
|
99
|
+
files:
|
100
|
+
- .gitignore
|
101
|
+
- Gemfile
|
102
|
+
- LICENSE
|
103
|
+
- README.md
|
104
|
+
- Rakefile
|
105
|
+
- lib/activerecord.rb
|
106
|
+
- lib/config.rb
|
107
|
+
- lib/ruby-app-ar.rb
|
108
|
+
- lib/ruby-app-ar/tasks.rake
|
109
|
+
- lib/version.rb
|
110
|
+
- ruby-app-ar.gemspec
|
111
|
+
- spec/app_spec.rb
|
112
|
+
- spec/spec_helper.rb
|
113
|
+
- spec/test_app/.gitignore
|
114
|
+
- spec/test_app/app/models/.gitkeep
|
115
|
+
- spec/test_app/app/models/task.rb
|
116
|
+
- spec/test_app/config/boot.rb
|
117
|
+
- spec/test_app/config/config.rb
|
118
|
+
- spec/test_app/config/database.yml
|
119
|
+
- spec/test_app/config/environment.rb
|
120
|
+
- spec/test_app/config/environments/test.rb
|
121
|
+
- spec/test_app/db/migrate/20120616161100_create_tasks.rb
|
122
|
+
- spec/test_app/db/test.db
|
123
|
+
- spec/test_app/lib/application.rb
|
124
|
+
homepage: http://github.com/kostya/ruby-app-ar
|
125
|
+
licenses: []
|
126
|
+
|
127
|
+
post_install_message:
|
128
|
+
rdoc_options: []
|
129
|
+
|
130
|
+
require_paths:
|
131
|
+
- lib
|
132
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
133
|
+
none: false
|
134
|
+
requirements:
|
135
|
+
- - ">="
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
hash: 3
|
138
|
+
segments:
|
139
|
+
- 0
|
140
|
+
version: "0"
|
141
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
142
|
+
none: false
|
143
|
+
requirements:
|
144
|
+
- - ">="
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
hash: 3
|
147
|
+
segments:
|
148
|
+
- 0
|
149
|
+
version: "0"
|
150
|
+
requirements: []
|
151
|
+
|
152
|
+
rubyforge_project:
|
153
|
+
rubygems_version: 1.8.24
|
154
|
+
signing_key:
|
155
|
+
specification_version: 3
|
156
|
+
summary: RubyApp extension, adds ActiveRecord support
|
157
|
+
test_files: []
|
158
|
+
|