sinatra-activerecord 1.7.0 → 2.0.0.rc
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sinatra/activerecord.rb +26 -55
- data/lib/sinatra/activerecord/rake.rb +33 -130
- data/lib/sinatra/activerecord/tasks.rake +26 -84
- metadata +20 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 73174bc1c11d020135e4dc1ad39349cb924a96b5
|
4
|
+
data.tar.gz: 41f81bf2f42246da958410ee0f36580d641ab755
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7bb21bcd938987562b04c529a8cfa37f0e68626fd832f488da1812a74493eb9fa6e58b7888363b153f3c7580a077cde84b439bc8e30ad8371c66d8b927c808af
|
7
|
+
data.tar.gz: b60744c2788b99a3b0d5a59a3ea03da15372a52a5cdd2fb571bf2980408e369fc6117db4401912a63dbdb893bd8bf817c47e56faa0beb85e67ae7cdc03455d3f
|
data/lib/sinatra/activerecord.rb
CHANGED
@@ -1,7 +1,11 @@
|
|
1
1
|
require 'sinatra/base'
|
2
2
|
require 'active_record'
|
3
|
+
require 'active_support/core_ext/hash/keys'
|
4
|
+
|
3
5
|
require 'logger'
|
4
|
-
require '
|
6
|
+
require 'pathname'
|
7
|
+
require 'yaml'
|
8
|
+
require 'erb'
|
5
9
|
|
6
10
|
module Sinatra
|
7
11
|
module ActiveRecordHelper
|
@@ -11,50 +15,11 @@ module Sinatra
|
|
11
15
|
end
|
12
16
|
|
13
17
|
module ActiveRecordExtension
|
14
|
-
def database=(spec)
|
15
|
-
set :database_spec, spec
|
16
|
-
@database = nil
|
17
|
-
database
|
18
|
-
end
|
19
|
-
|
20
|
-
def database
|
21
|
-
@database ||= begin
|
22
|
-
ActiveRecord::Base.logger = activerecord_logger
|
23
|
-
ActiveRecord::Base.establish_connection(resolve_spec(database_spec))
|
24
|
-
begin
|
25
|
-
ActiveRecord::Base.connection
|
26
|
-
rescue Exception => e
|
27
|
-
end
|
28
|
-
ActiveRecord::Base
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
def database_file=(path)
|
33
|
-
require 'pathname'
|
34
|
-
|
35
|
-
return if root.nil?
|
36
|
-
path = File.join(root, path) if Pathname.new(path).relative?
|
37
|
-
|
38
|
-
if File.exists?(path)
|
39
|
-
require 'yaml'
|
40
|
-
require 'erb'
|
41
|
-
|
42
|
-
database_hash = YAML.load(ERB.new(File.read(path)).result) || {}
|
43
|
-
ActiveRecord::Base.configurations = database_hash
|
44
|
-
|
45
|
-
database_hash = database_hash[environment.to_s] if database_hash[environment.to_s]
|
46
|
-
|
47
|
-
set :database, database_hash
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
protected
|
52
|
-
|
53
18
|
def self.registered(app)
|
54
|
-
app.set :
|
55
|
-
app.set :
|
56
|
-
|
57
|
-
|
19
|
+
app.set :database, ENV['DATABASE_URL'] if ENV['DATABASE_URL']
|
20
|
+
app.set :database_file, "#{Dir.pwd}/config/database.yml" if File.exists?("#{Dir.pwd}/config/database.yml")
|
21
|
+
ActiveRecord::Base.logger = Logger.new(STDOUT)
|
22
|
+
|
58
23
|
app.helpers ActiveRecordHelper
|
59
24
|
|
60
25
|
# re-connect if database connection dropped
|
@@ -62,21 +27,27 @@ module Sinatra
|
|
62
27
|
app.after { ActiveRecord::Base.clear_active_connections! }
|
63
28
|
end
|
64
29
|
|
65
|
-
|
30
|
+
def database_file=(path)
|
31
|
+
path = File.join(root, path) if Pathname(path).relative? and root
|
32
|
+
spec = YAML.load(ERB.new(File.read(path)).result) || {}
|
33
|
+
set :database, spec
|
34
|
+
end
|
66
35
|
|
67
|
-
def
|
68
|
-
if
|
69
|
-
|
70
|
-
|
71
|
-
It seems your database URL looks something like this: "sqlite3://<database_name>".
|
72
|
-
This doesn't work anymore, you need to use 3 slashes, like this: "sqlite3:///<database_name>".
|
73
|
-
MESSAGE
|
74
|
-
end
|
75
|
-
database_spec.sub(/^sqlite:/, "sqlite3:")
|
36
|
+
def database=(spec)
|
37
|
+
if spec.is_a?(Hash) and spec.symbolize_keys[environment]
|
38
|
+
ActiveRecord::Base.configurations = spec.stringify_keys
|
39
|
+
ActiveRecord::Base.establish_connection(environment)
|
76
40
|
else
|
77
|
-
|
41
|
+
ActiveRecord::Base.establish_connection(spec)
|
42
|
+
ActiveRecord::Base.configurations = {
|
43
|
+
environment.to_s => ActiveRecord::Base.connection.pool.spec.config
|
44
|
+
}
|
78
45
|
end
|
79
46
|
end
|
47
|
+
|
48
|
+
def database
|
49
|
+
ActiveRecord::Base
|
50
|
+
end
|
80
51
|
end
|
81
52
|
|
82
53
|
register ActiveRecordExtension
|
@@ -1,148 +1,51 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require 'fileutils'
|
1
|
+
case ActiveRecord::VERSION::MAJOR
|
2
|
+
when 4
|
4
3
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
def create
|
10
|
-
silence_activerecord do
|
11
|
-
ActiveRecord::Tasks::DatabaseTasks.create(config)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
def drop
|
16
|
-
silence_activerecord do
|
17
|
-
ActiveRecord::Tasks::DatabaseTasks.drop(config)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def seed
|
22
|
-
silence_activerecord do
|
23
|
-
load("#{db_dir}/seeds.rb")
|
24
|
-
end
|
4
|
+
seed_loader = Object.new
|
5
|
+
seed_loader.instance_eval do
|
6
|
+
def load_seed
|
7
|
+
load "#{ActiveRecord::Tasks::DatabaseTasks.db_dir}/seeds.rb"
|
25
8
|
end
|
9
|
+
end
|
26
10
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
11
|
+
ActiveRecord::Tasks::DatabaseTasks.tap do |config|
|
12
|
+
config.root = Rake.application.original_dir
|
13
|
+
config.env = ENV["RACK_ENV"] || "development"
|
14
|
+
config.db_dir = "db"
|
15
|
+
config.migrations_paths = ["db/migrate"]
|
16
|
+
config.fixtures_path = "test/fixtures"
|
17
|
+
config.seed_loader = seed_loader
|
18
|
+
config.database_configuration = ActiveRecord::Base.configurations
|
19
|
+
end
|
34
20
|
|
35
|
-
|
36
|
-
raise "No NAME specified. Example usage: `rake db:create_migration NAME=create_users`" if migration_name.nil?
|
21
|
+
when 3
|
37
22
|
|
38
|
-
|
39
|
-
migration_file = File.join(migrations_dir, "#{migration_number}_#{migration_name}.rb")
|
40
|
-
migration_class = migration_name.split("_").map(&:capitalize).join
|
23
|
+
require "active_support/string_inquirer"
|
41
24
|
|
42
|
-
|
43
|
-
|
44
|
-
file.write <<-MIGRATION.strip_heredoc
|
45
|
-
class #{migration_class} < ActiveRecord::Migration
|
46
|
-
def change
|
47
|
-
end
|
48
|
-
end
|
49
|
-
MIGRATION
|
50
|
-
end
|
51
|
-
end
|
25
|
+
module Rails
|
26
|
+
extend self
|
52
27
|
|
53
|
-
def
|
54
|
-
|
55
|
-
migration_version = version ? version.to_i : version
|
56
|
-
ActiveRecord::Migrator.migrate(migrations_dir, migration_version)
|
57
|
-
end
|
28
|
+
def root
|
29
|
+
Pathname.new(Rake.application.original_dir)
|
58
30
|
end
|
59
31
|
|
60
|
-
def
|
61
|
-
|
62
|
-
migration_step = step ? step.to_i : 1
|
63
|
-
ActiveRecord::Migrator.rollback(migrations_dir, migration_step)
|
64
|
-
end
|
32
|
+
def env
|
33
|
+
ActiveSupport::StringInquirer.new(ENV["RACK_ENV"] || "development")
|
65
34
|
end
|
66
35
|
|
67
|
-
def
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
# Load schema
|
74
|
-
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, out)
|
75
|
-
|
76
|
-
out.close
|
36
|
+
def application
|
37
|
+
seed_loader = Object.new
|
38
|
+
seed_loader.instance_eval do
|
39
|
+
def load_seed
|
40
|
+
load "db/seeds.rb"
|
77
41
|
end
|
78
42
|
end
|
79
|
-
|
80
|
-
|
81
|
-
def dump_structure(file_name = "#{db_dir}/structure.sql")
|
82
|
-
ActiveRecord::Tasks::DatabaseTasks.structure_dump(config, file_name)
|
83
|
-
end
|
84
|
-
|
85
|
-
def purge
|
86
|
-
if config
|
87
|
-
ActiveRecord::Tasks::DatabaseTasks.purge(config)
|
88
|
-
else
|
89
|
-
raise ActiveRecord::ConnectionNotEstablished
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
def load_schema(file_name = "#{db_dir}/schema.rb")
|
94
|
-
load(file_name)
|
95
|
-
end
|
96
|
-
|
97
|
-
def load_structure(file_name = "#{db_dir}/structure.sql")
|
98
|
-
ActiveRecord::Tasks::DatabaseTasks.structure_load(config, file_name)
|
99
|
-
end
|
100
|
-
|
101
|
-
def with_config_environment(environment, &block)
|
102
|
-
previous_environment = config_environment
|
103
|
-
begin
|
104
|
-
config_environment(environment)
|
105
|
-
yield
|
106
|
-
ensure
|
107
|
-
config_environment(previous_environment)
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
def db_dir
|
112
|
-
ActiveRecord::Tasks::DatabaseTasks.db_dir ||= 'db'
|
113
|
-
end
|
114
|
-
|
115
|
-
def db_dir=(dir)
|
116
|
-
ActiveRecord::Tasks::DatabaseTasks.db_dir = dir
|
117
|
-
end
|
118
|
-
|
119
|
-
private
|
120
|
-
|
121
|
-
def config_environment(env = nil)
|
122
|
-
if env
|
123
|
-
@config_environment = env
|
124
|
-
else
|
125
|
-
@config_environment ||= Sinatra::Application.environment.to_s
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
def config
|
130
|
-
ActiveRecord::Base.configurations[config_environment] ||
|
131
|
-
#active record expects the keys to be strings, but connection_config returns them as symbols
|
132
|
-
Hash[ActiveRecord::Base.connection_config.map{|key, value| [key.to_s, value]}]
|
133
|
-
end
|
134
|
-
|
135
|
-
def migrations_dir
|
136
|
-
ActiveRecord::Migrator.migrations_path
|
137
|
-
end
|
138
|
-
|
139
|
-
def silence_activerecord(&block)
|
140
|
-
old_logger = ActiveRecord::Base.logger
|
141
|
-
ActiveRecord::Base.logger = nil
|
142
|
-
yield if block_given?
|
143
|
-
ActiveRecord::Base.logger = old_logger
|
43
|
+
seed_loader
|
144
44
|
end
|
145
45
|
end
|
46
|
+
|
146
47
|
end
|
147
48
|
|
49
|
+
ActiveRecord::Base.logger = nil
|
50
|
+
|
148
51
|
load 'sinatra/activerecord/tasks.rake'
|
@@ -1,98 +1,40 @@
|
|
1
|
-
require
|
1
|
+
require "active_support/core_ext/string/strip"
|
2
|
+
require "pathname"
|
2
3
|
|
3
|
-
|
4
|
-
desc "create the database from config/database.yml from the current Sinatra env"
|
5
|
-
task :create do
|
6
|
-
Sinatra::ActiveRecordTasks.create
|
7
|
-
end
|
8
|
-
|
9
|
-
desc "drops the data from config/database.yml from the current Sinatra env"
|
10
|
-
task :drop do
|
11
|
-
Sinatra::ActiveRecordTasks.drop
|
12
|
-
end
|
13
|
-
|
14
|
-
desc "load the seed data from db/seeds.rb"
|
15
|
-
task :seed do
|
16
|
-
Sinatra::ActiveRecordTasks.seed
|
17
|
-
end
|
18
|
-
|
19
|
-
desc "create the database and load the schema"
|
20
|
-
task :setup do
|
21
|
-
Sinatra::ActiveRecordTasks.setup
|
22
|
-
end
|
4
|
+
load "active_record/railties/databases.rake"
|
23
5
|
|
24
|
-
|
6
|
+
namespace :db do
|
7
|
+
desc "Create a migration (parameters: NAME, VERSION)"
|
25
8
|
task :create_migration do
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
desc "migrate the database (use version with VERSION=n)"
|
30
|
-
task :migrate do
|
31
|
-
Sinatra::ActiveRecordTasks.migrate(ENV["VERSION"])
|
32
|
-
|
33
|
-
case ActiveRecord::Base.schema_format
|
34
|
-
when :ruby then Rake::Task["db:schema:dump"].invoke
|
35
|
-
when :sql then Rake::Task["db:structure:dump"].invoke
|
9
|
+
unless ENV["NAME"]
|
10
|
+
puts "No NAME specified. Example usage: `rake db:create_migration NAME=create_users`"
|
11
|
+
exit
|
36
12
|
end
|
37
|
-
end
|
38
|
-
|
39
|
-
namespace :migrate do
|
40
|
-
task :reset => ['db:drop', 'db:create', 'db:migrate']
|
41
|
-
end
|
42
13
|
|
43
|
-
|
14
|
+
name = ENV["NAME"]
|
15
|
+
version = ENV["VERSION"] || Time.now.utc.strftime("%Y%m%d%H%M%S")
|
44
16
|
|
45
|
-
|
46
|
-
|
47
|
-
|
17
|
+
filename = "#{version}_#{name}.rb"
|
18
|
+
dirname = ActiveRecord::Migrator.migrations_path
|
19
|
+
path = Pathname(File.join(dirname, filename))
|
48
20
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
desc "dump schema into file"
|
57
|
-
task :dump do
|
58
|
-
Sinatra::ActiveRecordTasks.dump_schema
|
59
|
-
end
|
60
|
-
|
61
|
-
desc "load schema into database"
|
62
|
-
task :load do
|
63
|
-
Sinatra::ActiveRecordTasks.load_schema
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
namespace :structure do
|
68
|
-
desc "dump schema into a file as SQL"
|
69
|
-
task :dump do
|
70
|
-
Sinatra::ActiveRecordTasks.dump_structure
|
71
|
-
end
|
21
|
+
path.dirname.mkpath
|
22
|
+
path.write <<-MIGRATION.strip_heredoc
|
23
|
+
class #{name.camelize} < ActiveRecord::Migration
|
24
|
+
def change
|
25
|
+
end
|
26
|
+
end
|
27
|
+
MIGRATION
|
72
28
|
|
73
|
-
|
74
|
-
task :load do
|
75
|
-
Sinatra::ActiveRecordTasks.load_structure
|
76
|
-
end
|
29
|
+
puts path
|
77
30
|
end
|
78
31
|
|
79
|
-
|
80
|
-
task :purge do
|
81
|
-
Sinatra::ActiveRecordTasks.with_config_environment 'test' do
|
82
|
-
Sinatra::ActiveRecordTasks.purge
|
83
|
-
end
|
84
|
-
end
|
32
|
+
task :environment
|
85
33
|
|
86
|
-
|
87
|
-
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
|
88
|
-
Sinatra::ActiveRecordTasks.with_config_environment 'test' do
|
89
|
-
Sinatra::ActiveRecordTasks.load_schema
|
90
|
-
end
|
91
|
-
end
|
34
|
+
Rake::Task["db:test:deprecated"].clear if Rake::Task.task_defined?("db:test:deprecated")
|
92
35
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
end
|
36
|
+
if ActiveRecord::VERSION::MAJOR == 3
|
37
|
+
Rake::Task["db:load_config"].clear
|
38
|
+
task :rails_env
|
97
39
|
end
|
98
40
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-activerecord
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0.rc
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Blake Mizerany
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-04-
|
12
|
+
date: 2014-04-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: sinatra
|
@@ -31,14 +31,14 @@ dependencies:
|
|
31
31
|
requirements:
|
32
32
|
- - ">="
|
33
33
|
- !ruby/object:Gem::Version
|
34
|
-
version: '3.
|
34
|
+
version: '3.2'
|
35
35
|
type: :runtime
|
36
36
|
prerelease: false
|
37
37
|
version_requirements: !ruby/object:Gem::Requirement
|
38
38
|
requirements:
|
39
39
|
- - ">="
|
40
40
|
- !ruby/object:Gem::Version
|
41
|
-
version: '3.
|
41
|
+
version: '3.2'
|
42
42
|
- !ruby/object:Gem::Dependency
|
43
43
|
name: rake
|
44
44
|
requirement: !ruby/object:Gem::Requirement
|
@@ -81,6 +81,20 @@ dependencies:
|
|
81
81
|
- - ">="
|
82
82
|
- !ruby/object:Gem::Version
|
83
83
|
version: '0'
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
name: appraisal
|
86
|
+
requirement: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
type: :development
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
84
98
|
description: Extends Sinatra with ActiveRecord helpers.
|
85
99
|
email: janko.marohnic@gmail.com
|
86
100
|
executables: []
|
@@ -107,9 +121,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
107
121
|
version: 1.9.2
|
108
122
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
123
|
requirements:
|
110
|
-
- - "
|
124
|
+
- - ">"
|
111
125
|
- !ruby/object:Gem::Version
|
112
|
-
version:
|
126
|
+
version: 1.3.1
|
113
127
|
requirements: []
|
114
128
|
rubyforge_project:
|
115
129
|
rubygems_version: 2.2.0
|