fidius-common 0.0.4beta0 → 0.0.5
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/fidius-common.gemspec +2 -0
- data/lib/fidius-common/version.rb +1 -1
- data/test/t_helper.rb +0 -1
- data/test/test_yamldb/database.yml +5 -0
- data/test/test_yamldb/db-install.rb +101 -0
- data/test/test_yamldb/migrations/20110415111241_create_abcs.rb +16 -0
- data/test/test_yamldb/migrations/20110415111333_create_defs.rb +16 -0
- data/test/test_yamldb/migrations/20110421151300_create_default_abcs.rb +9 -0
- data/test/test_yamldb/migrations/20110421151500_create_default_defs.rb +9 -0
- data/test/test_yamldb/models/abc.rb +2 -0
- data/test/test_yamldb/models/def.rb +2 -0
- data/test/test_yamldb.rb +40 -0
- metadata +44 -9
data/.gitignore
CHANGED
data/fidius-common.gemspec
CHANGED
@@ -20,6 +20,8 @@ Gem::Specification.new do |s|
|
|
20
20
|
s.require_paths = ["lib"]
|
21
21
|
s.add_development_dependency 'simplecov', '>= 0.4.0'
|
22
22
|
s.add_runtime_dependency 'activesupport', '>= 3.0.0'
|
23
|
+
s.add_runtime_dependency 'activerecord', '>= 3.0.0'
|
24
|
+
s.add_runtime_dependency 'sqlite3'
|
23
25
|
|
24
26
|
s.rdoc_options << '--title' << s.name <<
|
25
27
|
'--main' << 'README.md' << '--show-hash' <<
|
data/test/t_helper.rb
CHANGED
@@ -0,0 +1,101 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'logger'
|
3
|
+
|
4
|
+
|
5
|
+
module DbInstall
|
6
|
+
def self.migrate migrations_path, db_config_path
|
7
|
+
@CFG_D = db_config_path
|
8
|
+
|
9
|
+
self.connection_data
|
10
|
+
|
11
|
+
begin
|
12
|
+
self.drop_database @connection_data['test_db']
|
13
|
+
rescue
|
14
|
+
puts "DB drop: Could not find database #{@connection_data['database']}"
|
15
|
+
end
|
16
|
+
|
17
|
+
self.create_database @connection_data['test_db']
|
18
|
+
|
19
|
+
self.with_db do
|
20
|
+
ActiveRecord::Migrator.migrate(migrations_path, ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def connection_data
|
25
|
+
@connection_data ||= YAML.load_file("#{@CFG_D}/database.yml")
|
26
|
+
end
|
27
|
+
|
28
|
+
def with_db &block
|
29
|
+
begin
|
30
|
+
ActiveRecord::Base.establish_connection(connection_data['test_db'])
|
31
|
+
ActiveRecord::Base.logger = Logger.new(STDOUT)
|
32
|
+
ActiveRecord::Base.logger.level = Logger::WARN
|
33
|
+
yield connection_data
|
34
|
+
rescue
|
35
|
+
raise
|
36
|
+
ensure
|
37
|
+
ActiveRecord::Base.connection.disconnect!
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# copied and modified activerecord-3.0.4/lib/active_record/railties/database.rake
|
42
|
+
def drop_database(config)
|
43
|
+
case config['adapter']
|
44
|
+
when /sqlite/
|
45
|
+
FileUtils.rm (config['database'])
|
46
|
+
when /mysql/
|
47
|
+
ActiveRecord::Base.establish_connection(config)
|
48
|
+
ActiveRecord::Base.connection.drop_database config['database']
|
49
|
+
when 'postgresql'
|
50
|
+
ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
|
51
|
+
ActiveRecord::Base.connection.drop_database config['database']
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# dito
|
56
|
+
def create_database(config)
|
57
|
+
case config['adapter']
|
58
|
+
|
59
|
+
when /mysql/
|
60
|
+
require 'mysql'
|
61
|
+
@charset = ENV['CHARSET'] || 'utf8'
|
62
|
+
@collation = ENV['COLLATION'] || 'utf8_unicode_ci'
|
63
|
+
creation_options = {:charset => (config['charset'] || @charset), :collation => (config['collation'] || @collation)}
|
64
|
+
error_class = config['adapter'] =~ /mysql2/ ? Mysql2::Error : Mysql::Error
|
65
|
+
access_denied_error = 1045
|
66
|
+
begin
|
67
|
+
ActiveRecord::Base.establish_connection(config.merge('database' => nil))
|
68
|
+
ActiveRecord::Base.connection.create_database(config['database'], creation_options)
|
69
|
+
ActiveRecord::Base.establish_connection(config)
|
70
|
+
rescue error_class => sqlerr
|
71
|
+
if sqlerr.errno == access_denied_error
|
72
|
+
print "#{sqlerr.error}. \nPlease provide the root password for your mysql installation\n>"
|
73
|
+
root_password = $stdin.gets.strip
|
74
|
+
grant_statement = "GRANT ALL PRIVILEGES ON #{config['database']}.* " \
|
75
|
+
"TO '#{config['username']}'@'localhost' " \
|
76
|
+
"IDENTIFIED BY '#{config['password']}' WITH GRANT OPTION;"
|
77
|
+
ActiveRecord::Base.establish_connection(config.merge('database' => nil, 'username' => 'root', 'password' => root_password))
|
78
|
+
ActiveRecord::Base.connection.create_database(config['database'], creation_options)
|
79
|
+
ActiveRecord::Base.connection.execute grant_statement
|
80
|
+
ActiveRecord::Base.establish_connection(config)
|
81
|
+
else
|
82
|
+
$stderr.puts sqlerr.error
|
83
|
+
$stderr.puts "Couldn't create database for #{config.inspect}, charset: #{config['charset'] || @charset}, collation: #{config['collation'] || @collation}"
|
84
|
+
$stderr.puts "(if you set the charset manually, make sure you have a matching collation)" if config['charset']
|
85
|
+
end
|
86
|
+
end
|
87
|
+
when 'postgresql'
|
88
|
+
@encoding = config['encoding'] || ENV['CHARSET'] || 'utf8'
|
89
|
+
begin
|
90
|
+
ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
|
91
|
+
ActiveRecord::Base.connection.create_database(config['database'], config.merge('encoding' => @encoding))
|
92
|
+
ActiveRecord::Base.establish_connection(config)
|
93
|
+
rescue Exception => e
|
94
|
+
$stderr.puts e, *(e.backtrace)
|
95
|
+
$stderr.puts "Couldn't create database for #{config.inspect}"
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
data/test/test_yamldb.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 't_helper'
|
2
|
+
require 'fidius-common/yamldb'
|
3
|
+
require 'sqlite3'
|
4
|
+
require 'fileutils'
|
5
|
+
|
6
|
+
require 'test_yamldb/db-install'
|
7
|
+
require 'test_yamldb/models/abc'
|
8
|
+
require 'test_yamldb/models/def'
|
9
|
+
|
10
|
+
BASE_DIR = File.join('test','test_yamldb')
|
11
|
+
MIGRATIONS = File.join(BASE_DIR, 'migrations')
|
12
|
+
DB_CONFIG = File.join(BASE_DIR, 'database.yml')
|
13
|
+
EXPORT_DIR = File.join(BASE_DIR, 'test_db')
|
14
|
+
TEST_DB = File.join(BASE_DIR, 'test_db.sqlite3')
|
15
|
+
|
16
|
+
include DbInstall
|
17
|
+
|
18
|
+
class YamlDbTest < Test::Unit::TestCase
|
19
|
+
|
20
|
+
def setup
|
21
|
+
DbInstall.migrate(MIGRATIONS, BASE_DIR)
|
22
|
+
|
23
|
+
FIDIUS::Common::Db.export(DB_CONFIG, 'test_db', BASE_DIR, false)
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_export
|
27
|
+
assert(File.exists?(EXPORT_DIR))
|
28
|
+
assert(File.exists?(File.join(EXPORT_DIR, 'test_db.yml')))
|
29
|
+
assert(File.exists?(File.join(EXPORT_DIR, 'schema.rb')))
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_import
|
33
|
+
FIDIUS::Common::Db.import(DB_CONFIG, 'test_db', EXPORT_DIR)
|
34
|
+
end
|
35
|
+
|
36
|
+
def teardown
|
37
|
+
FileUtils.rm_r EXPORT_DIR if File.exists? EXPORT_DIR
|
38
|
+
FileUtils.rm TEST_DB if File.exists? TEST_DB
|
39
|
+
end
|
40
|
+
end
|
metadata
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fidius-common
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
4
|
+
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 5
|
9
|
+
version: 0.0.5
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Dominik Menke
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-04-
|
18
|
+
date: 2011-04-21 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -48,6 +48,34 @@ dependencies:
|
|
48
48
|
version: 3.0.0
|
49
49
|
type: :runtime
|
50
50
|
version_requirements: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
name: activerecord
|
53
|
+
prerelease: false
|
54
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
segments:
|
60
|
+
- 3
|
61
|
+
- 0
|
62
|
+
- 0
|
63
|
+
version: 3.0.0
|
64
|
+
type: :runtime
|
65
|
+
version_requirements: *id003
|
66
|
+
- !ruby/object:Gem::Dependency
|
67
|
+
name: sqlite3
|
68
|
+
prerelease: false
|
69
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
version: "0"
|
77
|
+
type: :runtime
|
78
|
+
version_requirements: *id004
|
51
79
|
description: |-
|
52
80
|
Common useful libraries and methods for the FIDIUS C&C server, the FIDIUS 'architecture' and maybe other components.
|
53
81
|
|
@@ -80,6 +108,15 @@ files:
|
|
80
108
|
- test/t_helper.rb
|
81
109
|
- test/test_iphelper.rb
|
82
110
|
- test/test_json_addon.rb
|
111
|
+
- test/test_yamldb.rb
|
112
|
+
- test/test_yamldb/database.yml
|
113
|
+
- test/test_yamldb/db-install.rb
|
114
|
+
- test/test_yamldb/migrations/20110415111241_create_abcs.rb
|
115
|
+
- test/test_yamldb/migrations/20110415111333_create_defs.rb
|
116
|
+
- test/test_yamldb/migrations/20110421151300_create_default_abcs.rb
|
117
|
+
- test/test_yamldb/migrations/20110421151500_create_default_defs.rb
|
118
|
+
- test/test_yamldb/models/abc.rb
|
119
|
+
- test/test_yamldb/models/def.rb
|
83
120
|
has_rdoc: true
|
84
121
|
homepage: ""
|
85
122
|
licenses: []
|
@@ -114,13 +151,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
114
151
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
152
|
none: false
|
116
153
|
requirements:
|
117
|
-
- - "
|
154
|
+
- - ">="
|
118
155
|
- !ruby/object:Gem::Version
|
119
156
|
segments:
|
120
|
-
-
|
121
|
-
|
122
|
-
- 1
|
123
|
-
version: 1.3.1
|
157
|
+
- 0
|
158
|
+
version: "0"
|
124
159
|
requirements: []
|
125
160
|
|
126
161
|
rubyforge_project: ""
|