wd_sinatra_active_record 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +7 -0
- data/lib/wd_sinatra_active_record.rb +7 -0
- data/lib/wd_sinatra_active_record/db.rake +12 -13
- data/lib/wd_sinatra_active_record/version.rb +1 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -18,6 +18,13 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
$ gem install wd_sinatra_active_record
|
20
20
|
|
21
|
+
|
22
|
+
Don't forget to set a gem dependency for the DB adapter you need.
|
23
|
+
For instance:
|
24
|
+
|
25
|
+
activerecord-mysql2-adapter
|
26
|
+
|
27
|
+
|
21
28
|
## Usage
|
22
29
|
|
23
30
|
Add an ActiveRecord `database.yml` file in your config folder and then require this
|
@@ -7,6 +7,13 @@ ActiveRecord::Base.default_timezone = :utc
|
|
7
7
|
|
8
8
|
module WdSinatraActiveRecord
|
9
9
|
|
10
|
+
# Path to the rake task file so it can be loaded as such:
|
11
|
+
# load WdSinatraActiveRecord.task_path
|
12
|
+
#
|
13
|
+
def self.task_path
|
14
|
+
File.join(File.expand_path(File.dirname(__FILE__), ".."), "wd_sinatra_active_record", "db.rake")
|
15
|
+
end
|
16
|
+
|
10
17
|
##### DB Connection ########
|
11
18
|
module DBConnector
|
12
19
|
DB_CONFIG = YAML.load_file(File.join(WDSinatra::AppLoader.root_path, "config", "database.yml"))
|
@@ -1,10 +1,11 @@
|
|
1
1
|
# require 'active_support/core_ext/object/inclusion'
|
2
|
+
require 'wd_sinatra_active_record'
|
2
3
|
|
3
4
|
db_namespace = namespace :db do
|
4
5
|
|
5
6
|
task :load_config => :setup_app do
|
6
7
|
require 'active_record'
|
7
|
-
ActiveRecord::Base.configurations = DBConnector.db_configuration
|
8
|
+
ActiveRecord::Base.configurations = WdSinatraActiveRecord::DBConnector.db_configuration
|
8
9
|
ActiveRecord::Migrator.migrations_paths = [File.join(WDSinatra::AppLoader.root_path, 'db/migrate')]
|
9
10
|
end
|
10
11
|
|
@@ -13,7 +14,7 @@ db_namespace = namespace :db do
|
|
13
14
|
old_connect_env = ENV['DONT_CONNECT'] ? 'true' : nil
|
14
15
|
ENV['DONT_CONNECT'] = 'true'
|
15
16
|
Rake::Task["db:load_config"].invoke
|
16
|
-
create_database(DBConnector.db_configuration)
|
17
|
+
create_database(WdSinatraActiveRecord::DBConnector.db_configuration)
|
17
18
|
ENV['DONT_CONNECT'] = old_connect_env
|
18
19
|
end
|
19
20
|
|
@@ -96,7 +97,7 @@ db_namespace = namespace :db do
|
|
96
97
|
desc 'Drops the database for the current RACK_ENV (use db:drop:all to drop all databases)'
|
97
98
|
task :drop do
|
98
99
|
Rake::Task["db:load_config"].invoke
|
99
|
-
config = DBConnector.db_configuration
|
100
|
+
config = WdSinatraActiveRecord::DBConnector.db_configuration
|
100
101
|
begin
|
101
102
|
drop_database(config)
|
102
103
|
rescue Exception => e
|
@@ -160,7 +161,7 @@ db_namespace = namespace :db do
|
|
160
161
|
|
161
162
|
desc 'Display status of migrations'
|
162
163
|
task :status => [:environment, :load_config] do
|
163
|
-
config = DBConnector.db_configuration
|
164
|
+
config = WdSinatraActiveRecord::DBConnector.db_configuration
|
164
165
|
ActiveRecord::Base.establish_connection(config)
|
165
166
|
unless ActiveRecord::Base.connection.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name)
|
166
167
|
puts 'Schema migrations table does not exist yet.'
|
@@ -208,7 +209,7 @@ db_namespace = namespace :db do
|
|
208
209
|
|
209
210
|
# desc "Retrieves the charset for the current environment's database"
|
210
211
|
task :charset => :setup_app do
|
211
|
-
config = DBConnector.db_configuration
|
212
|
+
config = WdSinatraActiveRecord::DBConnector.db_configuration
|
212
213
|
case config['adapter']
|
213
214
|
when /mysql/
|
214
215
|
ActiveRecord::Base.establish_connection(config)
|
@@ -226,7 +227,7 @@ db_namespace = namespace :db do
|
|
226
227
|
|
227
228
|
# desc "Retrieves the collation for the current environment's database"
|
228
229
|
task :collation => :setup_app do
|
229
|
-
config = DBConnector.db_configuration
|
230
|
+
config = WdSinatraActiveRecord::DBConnector.db_configuration
|
230
231
|
case config['adapter']
|
231
232
|
when /mysql/
|
232
233
|
ActiveRecord::Base.establish_connection(config)
|
@@ -319,16 +320,14 @@ db_namespace = namespace :db do
|
|
319
320
|
require 'active_record/schema_dumper'
|
320
321
|
filename = ENV['SCHEMA'] || "#{WDSinatra::AppLoader.root_path}/db/schema.rb"
|
321
322
|
File.open(filename, "w:utf-8") do |file|
|
322
|
-
ActiveRecord::Base.establish_connection(DBConnector.db_configuration)
|
323
|
+
ActiveRecord::Base.establish_connection(WdSinatraActiveRecord::DBConnector.db_configuration)
|
323
324
|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
|
324
325
|
end
|
325
326
|
db_namespace['schema:dump'].reenable
|
326
327
|
end
|
327
328
|
|
328
329
|
desc 'Load a schema.rb file into the database'
|
329
|
-
task :load do
|
330
|
-
root = File.expand_path('../..', File.dirname(__FILE__))
|
331
|
-
WDSinatra::AppLoader.console(root)
|
330
|
+
task :load => :environment do
|
332
331
|
file = ENV['SCHEMA'] || "#{WDSinatra::AppLoader.root_path}/db/schema.rb"
|
333
332
|
if File.exists?(file)
|
334
333
|
load(file)
|
@@ -341,7 +340,7 @@ db_namespace = namespace :db do
|
|
341
340
|
namespace :structure do
|
342
341
|
desc 'Dump the database structure to an SQL file'
|
343
342
|
task :dump => :setup_app do
|
344
|
-
abcs = DBConnector.db_configuration
|
343
|
+
abcs = WdSinatraActiveRecord::DBConnector.db_configuration
|
345
344
|
case abcs[RACK_ENV]['adapter']
|
346
345
|
when /mysql/, 'oci', 'oracle'
|
347
346
|
ActiveRecord::Base.establish_connection(config)
|
@@ -378,7 +377,7 @@ db_namespace = namespace :db do
|
|
378
377
|
namespace :test do
|
379
378
|
# desc "Recreate the test database from the current schema.rb"
|
380
379
|
task :load => 'db:test:purge' do
|
381
|
-
ActiveRecord::Base.establish_connection(DBConnector.db_configuration('test'))
|
380
|
+
ActiveRecord::Base.establish_connection(WdSinatraActiveRecord::DBConnector.db_configuration('test'))
|
382
381
|
ActiveRecord::Schema.verbose = false
|
383
382
|
db_namespace['schema:load'].invoke
|
384
383
|
end
|
@@ -422,7 +421,7 @@ db_namespace = namespace :db do
|
|
422
421
|
|
423
422
|
# desc "Empty the test database"
|
424
423
|
task :purge => :setup_app do
|
425
|
-
abcs = DBConnector.db_configuration('test')
|
424
|
+
abcs = WdSinatraActiveRecord::DBConnector.db_configuration('test')
|
426
425
|
case abcs['adapter']
|
427
426
|
when /mysql/
|
428
427
|
ActiveRecord::Base.establish_connection(abcs)
|