dohmysql 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/makedb +2 -2
- data/lib/doh/mysql/abstract_row.rb +1 -5
- data/lib/doh/mysql/cache_connector.rb +11 -19
- data/lib/doh/mysql/connector_instance.rb +8 -4
- data/lib/doh/mysql/database_creator.rb +24 -28
- data/lib/doh/mysql/db_date.rb +3 -2
- data/lib/doh/mysql/handle.rb +20 -12
- data/lib/doh/mysql/load_sql.rb +3 -9
- data/lib/doh/mysql/metadata_util.rb +6 -7
- data/lib/doh/mysql/smart_row.rb +4 -6
- data/lib/doh/mysql.rb +0 -1
- data/test/cache_connector.dt.rb +8 -9
- data/test/connector_instance.dt.rb +3 -2
- data/test/handle.dt.rb +22 -27
- data/test/helpers.rb +32 -0
- data/test/metadata_util.dt.rb +13 -12
- data/test/typecasting.dt.rb +24 -0
- metadata +28 -25
- data/lib/doh/mysql/activate.rb +0 -26
- data/lib/doh/mysql/convert.rb +0 -18
- data/lib/doh/mysql/require_dbtypes.rb +0 -8
- data/lib/doh/mysql/version.rb +0 -102
- data/test/connector.yml +0 -4
- data/test/connector.yml.tmpl +0 -4
- data/test/convert.dt.rb +0 -45
- data/test/db_unit_test.rb +0 -10
- data/test/types.dt.rb +0 -28
data/bin/makedb
CHANGED
@@ -5,9 +5,9 @@ require 'doh/mysql/database_creator'
|
|
5
5
|
|
6
6
|
opts = Doh::Options.new(
|
7
7
|
{'drop_first' => [false, "-z", "--drop_first", "if true, will drop the database or tables before creating"] \
|
8
|
-
,'database' => [Doh::config[
|
8
|
+
,'database' => [Doh::config[:default_database], "-d", "--database <database>", "name of the source database -- defaults to Doh::config[:default_database], currently '#{Doh::config[:default_database]}'"] \
|
9
9
|
,'all' => [false, "-a", "--all", "create all databases"] \
|
10
|
-
,'target' => [
|
10
|
+
,'target' => [nil, "-g", "--target <database>", "name of the target database -- defaults to same as source"] \
|
11
11
|
,'tables' => [nil, "-t", "--tables <tables>", "comma-delimited list of tables to create; if specified, no databases are dropped or created"]
|
12
12
|
})
|
13
13
|
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require 'doh/core_ext/
|
1
|
+
require 'doh/core_ext/force_deep_copy'
|
2
2
|
|
3
3
|
module DohDb
|
4
4
|
|
@@ -58,10 +58,6 @@ class AbstractRow
|
|
58
58
|
return val.nil? || (val.respond_to?(:empty?) && val.empty?)
|
59
59
|
end
|
60
60
|
|
61
|
-
def record_id
|
62
|
-
get('id')
|
63
|
-
end
|
64
|
-
|
65
61
|
protected
|
66
62
|
def parse_initialize_args(*args)
|
67
63
|
if args.empty?
|
@@ -1,21 +1,13 @@
|
|
1
|
-
require 'mysql2'
|
2
1
|
require 'doh/mysql/handle'
|
3
|
-
require 'doh/mysql/typed_row_builder'
|
4
|
-
Mysql2::Client.default_query_options[:cast_booleans] = true
|
5
2
|
|
6
3
|
module DohDb
|
7
4
|
|
8
5
|
class CacheConnector
|
9
|
-
attr_accessor :
|
10
|
-
|
11
|
-
def initialize(
|
12
|
-
@
|
13
|
-
@
|
14
|
-
@password = password
|
15
|
-
@database = database
|
16
|
-
@timeout = 1800
|
17
|
-
@port = nil
|
18
|
-
@row_builder = row_builder || TypedRowBuilder.new
|
6
|
+
attr_accessor :config
|
7
|
+
|
8
|
+
def initialize(config)
|
9
|
+
@config = config
|
10
|
+
@config[:timeout] ||= 1800
|
19
11
|
end
|
20
12
|
|
21
13
|
def request_handle(database = nil)
|
@@ -40,15 +32,15 @@ private
|
|
40
32
|
end
|
41
33
|
|
42
34
|
def get_new_handle(database = nil)
|
43
|
-
|
44
|
-
|
45
|
-
dohlog.info("connecting
|
46
|
-
|
47
|
-
Handle.new(
|
35
|
+
local_config = @config.dup
|
36
|
+
local_config[:database] = database if database
|
37
|
+
dohlog.info("connecting with config: #{local_config}")
|
38
|
+
local_config.delete(:timeout)
|
39
|
+
Handle.new(local_config)
|
48
40
|
end
|
49
41
|
|
50
42
|
def passed_timeout?
|
51
|
-
Time.now > @last_used + @timeout
|
43
|
+
Time.now > @last_used + @config[:timeout]
|
52
44
|
end
|
53
45
|
end
|
54
46
|
|
@@ -32,12 +32,16 @@ def self.insert(statement)
|
|
32
32
|
request_handle.insert(statement)
|
33
33
|
end
|
34
34
|
|
35
|
-
def self.insert_hash(hash, table,
|
36
|
-
request_handle.insert_hash(hash, table,
|
35
|
+
def self.insert_hash(hash, table, quote_strings = true)
|
36
|
+
request_handle.insert_hash(hash, table, quote_strings)
|
37
37
|
end
|
38
38
|
|
39
|
-
def self.
|
40
|
-
request_handle.
|
39
|
+
def self.insert_ignore_hash(hash, table, quote_strings = true)
|
40
|
+
request_handle.insert_ignore_hash(hash, table, quote_strings)
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.replace_hash(hash, table, quote_strings = true)
|
44
|
+
request_handle.replace_hash(hash, table, quote_strings)
|
41
45
|
end
|
42
46
|
|
43
47
|
def self.select(statement, row_builder = nil)
|
@@ -1,36 +1,35 @@
|
|
1
1
|
require 'doh/core_ext/dir'
|
2
2
|
require 'doh/mysql/handle'
|
3
3
|
require 'doh/mysql/load_sql'
|
4
|
-
require '
|
5
|
-
require 'doh/mysql/types'
|
4
|
+
require 'yaml'
|
6
5
|
|
7
6
|
module DohDb
|
8
7
|
|
9
8
|
class DatabaseCreator
|
10
|
-
def initialize(
|
11
|
-
@
|
9
|
+
def initialize(sqlfiles_directory = nil, connector = nil)
|
10
|
+
@sqlfiles_directory = sqlfiles_directory || File.join(Doh::root, 'database')
|
12
11
|
@connector = connector || DohDb::connector_instance
|
13
12
|
@include_scripts = true
|
14
13
|
end
|
15
14
|
|
16
15
|
def create_database(dbname, drop_first = false)
|
17
|
-
create_one_database(
|
16
|
+
create_one_database(get_handle(''), dbname, dbname, drop_first)
|
18
17
|
end
|
19
18
|
|
20
19
|
def create_database_copy(dest_db, source_db, drop_first = false)
|
21
|
-
create_one_database(
|
20
|
+
create_one_database(get_handle(''), dest_db, source_db, drop_first)
|
22
21
|
end
|
23
22
|
|
24
23
|
def create_all_databases(drop_first = false)
|
25
|
-
dbh =
|
26
|
-
Dir.directories(@
|
24
|
+
dbh = get_handle('')
|
25
|
+
Dir.directories(@sqlfiles_directory).each {|elem| create_one_database(dbh, elem, elem, drop_first)}
|
27
26
|
end
|
28
27
|
|
29
28
|
def create_tables(database, drop_first, *table_and_view_names)
|
30
|
-
|
29
|
+
dbh = get_handle(database)
|
31
30
|
views, tables = table_and_view_names.flatten.sort.partition {|name| File.exist?(sql_filename(database, 'views', name))}
|
32
|
-
tables.each {|name| create_base_table(database, name, drop_first)}
|
33
|
-
views.each {|name| create_view(database, name, drop_first)}
|
31
|
+
tables.each {|name| create_base_table(dbh, database, name, drop_first)}
|
32
|
+
views.each {|name| create_view(dbh, database, name, drop_first)}
|
34
33
|
end
|
35
34
|
|
36
35
|
def exclude_scripts
|
@@ -39,56 +38,53 @@ class DatabaseCreator
|
|
39
38
|
end
|
40
39
|
|
41
40
|
private
|
42
|
-
def
|
41
|
+
def get_handle(database)
|
43
42
|
@connector.reset
|
44
|
-
@connector.database =
|
43
|
+
@connector.config[:database] = database
|
45
44
|
@connector.request_handle
|
46
45
|
end
|
47
46
|
|
48
47
|
def sql_filename(database, subdir, name)
|
49
|
-
File.join(@
|
48
|
+
File.join(@sqlfiles_directory, database, subdir, name) + '.sql'
|
50
49
|
end
|
51
50
|
|
52
51
|
def find_files(source_db, subdir, ext = '.sql')
|
53
|
-
path = File.join(@
|
52
|
+
path = File.join(@sqlfiles_directory, source_db, subdir)
|
54
53
|
return [] unless File.exist?(path)
|
55
54
|
Dir.entries(path).find_all {|entry| entry.end_with?(ext)}.sort.collect {|elem| File.join(path, elem)}
|
56
55
|
end
|
57
56
|
|
58
57
|
def view_files(source_db)
|
59
|
-
path = File.join(@
|
58
|
+
path = File.join(@sqlfiles_directory, source_db, 'views')
|
60
59
|
return [] unless File.exist?(path)
|
61
60
|
ordered_filenames = YAML.load_file(File.join(path, 'order.yml')).collect {|uqfn| File.join(path, uqfn) + '.sql'}
|
62
61
|
ordered_filenames + (find_files(source_db, 'views') - ordered_filenames)
|
63
62
|
end
|
64
63
|
|
65
|
-
def create_base_table(database, table_name, drop_first)
|
66
|
-
|
64
|
+
def create_base_table(dbh, database, table_name, drop_first)
|
65
|
+
dbh.query("DROP TABLE IF EXISTS #{table_name}") if drop_first
|
67
66
|
files = [sql_filename(database, 'tables', table_name)]
|
68
67
|
inserts_file = sql_filename(database, 'insert_sql', table_name)
|
69
68
|
files.push(inserts_file) if File.exist?(inserts_file)
|
70
|
-
DohDb::
|
69
|
+
DohDb::load_sql(dbh.config, files)
|
71
70
|
end
|
72
71
|
|
73
|
-
def create_view(database, view_name, drop_first)
|
74
|
-
|
75
|
-
DohDb::
|
72
|
+
def create_view(dbh, database, view_name, drop_first)
|
73
|
+
dbh.query("DROP VIEW IF EXISTS #{view_name}") if drop_first
|
74
|
+
DohDb::load_sql(dbh.config, [sql_filename(database, 'views', view_name)])
|
76
75
|
end
|
77
76
|
|
78
77
|
def create_one_database(dbh, dest_db, source_db, drop_first)
|
79
|
-
dohlog.info("creating database " + dest_db + " from source files at " + File.join(@
|
78
|
+
dohlog.info("creating database " + dest_db + " from source files at " + File.join(@sqlfiles_directory, source_db))
|
80
79
|
dbh.query("DROP DATABASE IF EXISTS " + dest_db) if drop_first
|
81
80
|
|
82
81
|
dbh.query("CREATE DATABASE " + dest_db)
|
83
82
|
dbh.query("USE " + dest_db)
|
84
|
-
dbh.query("CREATE TABLE version (version INT UNSIGNED NOT NULL) ENGINE=MyISAM")
|
85
|
-
dbver = DohDb::latest_database_version(source_db)
|
86
|
-
dbh.query("INSERT INTO version VALUES (#{dbver})")
|
87
83
|
|
88
|
-
@connector.database = dest_db
|
84
|
+
@connector.config[:database] = dest_db
|
89
85
|
|
90
86
|
files = find_files(source_db, 'tables') + find_files(source_db, 'insert_sql') + view_files(source_db)
|
91
|
-
DohDb::
|
87
|
+
DohDb::load_sql(@connector.config, files)
|
92
88
|
return unless @include_scripts
|
93
89
|
find_files(source_db, 'insert_scripts', '.rb').each do |filename|
|
94
90
|
dohlog.info("loading file: #{filename}")
|
data/lib/doh/mysql/db_date.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'doh/core_ext/date'
|
1
2
|
require 'doh/core_ext/datewithtime'
|
2
3
|
require 'doh/mysql/to_sql'
|
3
4
|
|
@@ -16,12 +17,12 @@ class DateTimeNow < DateTime
|
|
16
17
|
end
|
17
18
|
|
18
19
|
def self.today
|
19
|
-
day = Date.
|
20
|
+
day = Date.utcday
|
20
21
|
DateToday.new(day.year, day.month, day.mday)
|
21
22
|
end
|
22
23
|
|
23
24
|
def self.now
|
24
|
-
dt = DateTime.
|
25
|
+
dt = DateTime.utcnow
|
25
26
|
DateTimeNow.new(dt.year, dt.month, dt.mday, dt.hour, dt.min, dt.sec, dt.zone)
|
26
27
|
end
|
27
28
|
|
data/lib/doh/mysql/handle.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'mysql2'
|
1
2
|
require 'doh/array_to_hash'
|
2
3
|
require 'doh/log/stub'
|
3
4
|
require 'doh/mysql/error'
|
@@ -6,18 +7,22 @@ require 'doh/mysql/writable_row'
|
|
6
7
|
require 'doh/mysql/hash_row'
|
7
8
|
require 'doh/mysql/smart_row'
|
8
9
|
require 'doh/mysql/to_sql'
|
10
|
+
Mysql2::Client.default_query_options[:cast_booleans] = true
|
9
11
|
|
10
12
|
module DohDb
|
11
13
|
|
12
14
|
class Handle
|
13
|
-
|
14
|
-
|
15
|
-
|
15
|
+
attr_reader :config, :mysqlh
|
16
|
+
|
17
|
+
def initialize(config)
|
18
|
+
@config = config
|
19
|
+
@mysqlh = Mysql2::Client.new(@config)
|
20
|
+
dohlog.info("new connection created: id #{@mysqlh.thread_id}")
|
16
21
|
end
|
17
22
|
|
18
23
|
def close
|
19
24
|
unless closed?
|
20
|
-
dohlog.info("closing
|
25
|
+
dohlog.info("closing connection: id #{@mysqlh.thread_id}")
|
21
26
|
@mysqlh.close
|
22
27
|
@mysqlh = nil
|
23
28
|
end
|
@@ -59,13 +64,16 @@ class Handle
|
|
59
64
|
retval
|
60
65
|
end
|
61
66
|
|
62
|
-
def insert_hash(hash, table,
|
63
|
-
|
64
|
-
|
67
|
+
def insert_hash(hash, table, quote_strings = true)
|
68
|
+
insert_hash_helper(hash, table, 'INSERT', quote_strings)
|
69
|
+
end
|
70
|
+
|
71
|
+
def insert_ignore_hash(hash, table, quote_strings = true)
|
72
|
+
insert_hash_helper(hash, table, 'INSERT IGNORE', quote_strings)
|
65
73
|
end
|
66
74
|
|
67
|
-
def replace_hash(hash, table)
|
68
|
-
insert_hash_helper(hash, table, 'REPLACE')
|
75
|
+
def replace_hash(hash, table, quote_strings = true)
|
76
|
+
insert_hash_helper(hash, table, 'REPLACE', quote_strings)
|
69
77
|
end
|
70
78
|
|
71
79
|
# The most generic form of select.
|
@@ -150,12 +158,12 @@ private
|
|
150
158
|
raise
|
151
159
|
end
|
152
160
|
|
153
|
-
def insert_hash_helper(hash, table, keyword)
|
161
|
+
def insert_hash_helper(hash, table, keyword, quote_strings)
|
154
162
|
names = []
|
155
163
|
values = []
|
156
164
|
hash.each_pair do |key, value|
|
157
165
|
names.push(key)
|
158
|
-
values.push(value.to_sql)
|
166
|
+
values.push(if quote_strings || !value.is_a?(String) then value.to_sql else value end)
|
159
167
|
end
|
160
168
|
|
161
169
|
insert("#{keyword} INTO #{table} (#{names.join(',')}) VALUES (#{values.join(',')})")
|
@@ -163,7 +171,7 @@ private
|
|
163
171
|
|
164
172
|
def get_row_builder(row_builder = nil)
|
165
173
|
if row_builder.nil?
|
166
|
-
|
174
|
+
TypedRowBuilder.new
|
167
175
|
elsif row_builder == :read
|
168
176
|
TypedRowBuilder.new(ReadOnlyRow)
|
169
177
|
elsif row_builder == :hash
|
data/lib/doh/mysql/load_sql.rb
CHANGED
@@ -3,12 +3,11 @@ require 'doh/mysql/connector_instance'
|
|
3
3
|
module DohDb
|
4
4
|
|
5
5
|
def self.mysql_arg(value, option_specifier)
|
6
|
-
|
7
|
-
' -' + option_specifier + value
|
6
|
+
if value.to_s.strip.empty? then '' else " -#{option_specifier}#{value}" end
|
8
7
|
end
|
9
8
|
|
10
|
-
def self.load_sql(
|
11
|
-
mysqlcmd = 'mysql' + mysql_arg(host, 'h') + mysql_arg(username, 'u') + mysql_arg(password, 'p') + ' ' + database
|
9
|
+
def self.load_sql(dbconfig, filenames)
|
10
|
+
mysqlcmd = 'mysql' + mysql_arg(dbconfig[:host], 'h') + mysql_arg(dbconfig[:username], 'u') + mysql_arg(dbconfig[:password], 'p') + ' ' + dbconfig[:database]
|
12
11
|
io = IO::popen(mysqlcmd, 'r+')
|
13
12
|
dohlog.debug("loading sql file: " + filenames.first) if filenames.size == 1
|
14
13
|
filenames.each do |elem|
|
@@ -17,10 +16,5 @@ def self.load_sql(filenames, host, username, password, database)
|
|
17
16
|
io.close
|
18
17
|
end
|
19
18
|
|
20
|
-
def self.load_sql_connector(filenames, connector = nil, alternate_database = nil)
|
21
|
-
connector ||= DohDb::connector_instance
|
22
|
-
load_sql(filenames, connector.host, connector.username, connector.password, alternate_database || connector.database)
|
23
|
-
end
|
24
|
-
|
25
19
|
end
|
26
20
|
|
@@ -1,14 +1,13 @@
|
|
1
|
-
require 'doh/mysql'
|
2
|
-
require 'doh/mysql/to_sql'
|
1
|
+
require 'doh/mysql/connector_instance'
|
3
2
|
|
4
3
|
module DohDb
|
5
4
|
|
6
5
|
@@cached_column_info = {}
|
7
6
|
def self.column_info(table, database = nil)
|
8
|
-
database ||= DohDb::connector_instance.database
|
7
|
+
database ||= DohDb::connector_instance.config[:database]
|
9
8
|
lookup_str = database + '.' + table
|
10
9
|
return @@cached_column_info[lookup_str] if @@cached_column_info[lookup_str]
|
11
|
-
stmt = "SELECT column_name, is_nullable, data_type, character_maximum_length, numeric_scale, column_type FROM information_schema.COLUMNS WHERE TABLE_SCHEMA
|
10
|
+
stmt = "SELECT column_name, is_nullable, data_type, character_maximum_length, numeric_scale, column_type FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='#{database}' AND TABLE_NAME='#{table}'"
|
12
11
|
@@cached_column_info[lookup_str] = DohDb::select_transpose(stmt)
|
13
12
|
end
|
14
13
|
|
@@ -40,12 +39,12 @@ def self.find_primary_key(table, database = nil)
|
|
40
39
|
database = table.before('.')
|
41
40
|
table = table.after('.')
|
42
41
|
else
|
43
|
-
database ||= DohDb::connector_instance.database
|
42
|
+
database ||= DohDb::connector_instance.config[:database]
|
44
43
|
end
|
45
44
|
|
46
45
|
dbhash = @@primary_keys[database]
|
47
46
|
if dbhash.nil?
|
48
|
-
dbhash = DohDb::select_transpose("SELECT table_name, column_name FROM information_schema.COLUMNS WHERE TABLE_SCHEMA
|
47
|
+
dbhash = DohDb::select_transpose("SELECT table_name, column_name FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='#{database}' AND ORDINAL_POSITION=1")
|
49
48
|
raise "no information found for database #{database}" if dbhash.empty?
|
50
49
|
@@primary_keys[database] = dbhash
|
51
50
|
end
|
@@ -65,7 +64,7 @@ end
|
|
65
64
|
|
66
65
|
@@tables_by_database = {}
|
67
66
|
def self.all_tables(database = nil)
|
68
|
-
database ||= DohDb::connector_instance.database
|
67
|
+
database ||= DohDb::connector_instance.config[:database]
|
69
68
|
@@tables_by_database[database] ||
|
70
69
|
@@tables_by_database[database] ||= DohDb::select_list("SELECT table_name FROM information_schema.tables WHERE table_schema = '#{database}'")
|
71
70
|
end
|
data/lib/doh/mysql/smart_row.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'doh/mysql/writable_row'
|
2
2
|
require 'doh/to_display'
|
3
3
|
require 'doh/mysql/virtual'
|
4
|
+
require 'sqlstmt/update'
|
4
5
|
|
5
6
|
module DohDb
|
6
7
|
|
@@ -81,10 +82,6 @@ class AbstractSmartRow < AbstractRow
|
|
81
82
|
@values.delete_at(index)
|
82
83
|
end
|
83
84
|
|
84
|
-
def record_id=(value)
|
85
|
-
set(primary_key, value)
|
86
|
-
end
|
87
|
-
|
88
85
|
def db_insert
|
89
86
|
newid = DohDb::insert_hash(self, @table)
|
90
87
|
if newid != 0
|
@@ -96,8 +93,9 @@ class AbstractSmartRow < AbstractRow
|
|
96
93
|
def db_update
|
97
94
|
return if @changed_keys.empty?
|
98
95
|
before_db_update
|
99
|
-
builder =
|
100
|
-
|
96
|
+
builder = SqlStmt::Update.new.table(@table)
|
97
|
+
builder.where("#{primary_key} = #{get(primary_key).to_sql}")
|
98
|
+
@changed_keys.each {|key| builder.field(key, get(key).to_sql)}
|
101
99
|
DohDb::query(builder)
|
102
100
|
after_db_update
|
103
101
|
@changed_keys.clear
|
data/lib/doh/mysql.rb
CHANGED
data/test/cache_connector.dt.rb
CHANGED
@@ -1,20 +1,19 @@
|
|
1
|
-
|
1
|
+
require 'doh/mysql/cache_connector'
|
2
|
+
require_relative 'helpers'
|
2
3
|
|
3
4
|
module DohDb
|
4
5
|
|
5
6
|
class Test_CacheConnector < DohTest::TestGroup
|
6
7
|
def create_table
|
7
|
-
@cc.request_handle.query("CREATE TEMPORARY TABLE
|
8
|
+
@cc.request_handle.query("CREATE TEMPORARY TABLE #{tbl} (id INT AUTO_INCREMENT KEY)")
|
8
9
|
end
|
9
10
|
|
10
11
|
def insert_record
|
11
|
-
@cc.request_handle.insert("INSERT INTO
|
12
|
+
@cc.request_handle.insert("INSERT INTO #{tbl} (id) VALUES (NULL)")
|
12
13
|
end
|
13
14
|
|
14
15
|
def test_stuff
|
15
|
-
|
16
|
-
@cc = DohDb::CacheConnector.new(sharedcc.host, sharedcc.username, sharedcc.password, sharedcc.database, sharedcc.row_builder)
|
17
|
-
@tbl = 'doh_mysql_cache_connector_stuff_test'
|
16
|
+
@cc = DohDb::CacheConnector.new(dbcfg)
|
18
17
|
|
19
18
|
create_table
|
20
19
|
assert_equal(1, insert_record)
|
@@ -22,10 +21,10 @@ class Test_CacheConnector < DohTest::TestGroup
|
|
22
21
|
dbh = @cc.request_handle
|
23
22
|
assert_equal(2, insert_record)
|
24
23
|
dbh.close
|
25
|
-
|
24
|
+
|
25
|
+
# temporary table should get deleted when the connection closes
|
26
26
|
create_table
|
27
27
|
|
28
|
-
#test it resets the handle
|
29
28
|
@cc.reset
|
30
29
|
create_table
|
31
30
|
@cc.reset
|
@@ -33,7 +32,7 @@ class Test_CacheConnector < DohTest::TestGroup
|
|
33
32
|
|
34
33
|
# test it still works after timeout
|
35
34
|
create_table
|
36
|
-
@cc.timeout = -1
|
35
|
+
@cc.config[:timeout] = -1
|
37
36
|
create_table
|
38
37
|
end
|
39
38
|
end
|
@@ -1,10 +1,11 @@
|
|
1
|
-
|
1
|
+
require 'doh/mysql/connector_instance'
|
2
|
+
require_relative 'helpers'
|
2
3
|
|
3
4
|
module DohDb
|
4
5
|
|
5
6
|
class Test_ConnectorInstance < DohTest::TestGroup
|
6
7
|
def test_stuff
|
7
|
-
|
8
|
+
init_global_connector
|
8
9
|
DohDb::query("CREATE TEMPORARY TABLE #{tbl} (id INT UNSIGNED AUTO_INCREMENT NOT NULL KEY, value INT NOT NULL DEFAULT 0, created_on DATE, created_at DATETIME, money DECIMAL(7,2) NOT NULL DEFAULT 0)")
|
9
10
|
assert_equal(1, DohDb::insert("INSERT INTO #{tbl} (id, created_on, created_at, money) VALUES (null, CURDATE(), '2007-01-01 10:30:05', 10.5)"))
|
10
11
|
assert_equal(2, DohDb::insert("INSERT INTO #{tbl} (id, created_on) VALUES (null, 0)"))
|
data/test/handle.dt.rb
CHANGED
@@ -1,12 +1,11 @@
|
|
1
|
-
require_relative '
|
2
|
-
require 'doh/
|
1
|
+
require_relative 'helpers'
|
2
|
+
require 'doh/mysql/handle'
|
3
3
|
|
4
4
|
module DohDb
|
5
5
|
|
6
6
|
class Test_Handle < DohTest::TestGroup
|
7
7
|
def test_stuff
|
8
|
-
dbh =
|
9
|
-
tbl = "doh_mysql_handle_stuff_test"
|
8
|
+
dbh = get_dbh
|
10
9
|
dbh.query("CREATE TEMPORARY TABLE #{tbl} (id INT UNSIGNED AUTO_INCREMENT NOT NULL KEY, value INT NOT NULL DEFAULT 0, created_on DATE, created_at DATETIME, money DECIMAL(7,2) NOT NULL DEFAULT 0)")
|
11
10
|
assert_equal(1, dbh.insert("INSERT INTO #{tbl} (id, created_on, created_at, money) VALUES (null, CURDATE(), '2007-01-01 10:30:05', 10.5)"))
|
12
11
|
assert_equal(2, dbh.insert("INSERT INTO #{tbl} (id, created_on, created_at) VALUES (null, 0, 0)"))
|
@@ -18,48 +17,44 @@ class Test_Handle < DohTest::TestGroup
|
|
18
17
|
end
|
19
18
|
|
20
19
|
def test_select_transpose_2fields
|
21
|
-
dbh =
|
22
|
-
tbl
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
hash = DohDb::select_transpose("SELECT field, value FROM #{tbl}")
|
20
|
+
dbh = get_dbh
|
21
|
+
dbh.query("CREATE TEMPORARY TABLE #{tbl} (field CHAR(30) NOT NULL, value CHAR(30) NOT NULL)")
|
22
|
+
dbh.query("INSERT INTO #{tbl} SET field = 'some_name', value = 'some_value'")
|
23
|
+
dbh.query("INSERT INTO #{tbl} SET field = 'other_name', value = 'matching_other_value'")
|
24
|
+
dbh.query("INSERT INTO #{tbl} SET field = 'yet_another_name', value = 'strange_value'")
|
25
|
+
hash = dbh.select_transpose("SELECT field, value FROM #{tbl}")
|
28
26
|
assert_equal('some_value', hash['some_name'])
|
29
27
|
assert_equal('matching_other_value', hash['other_name'])
|
30
28
|
assert_equal('strange_value', hash['yet_another_name'])
|
31
29
|
end
|
32
30
|
|
33
31
|
def test_select_transpose_3fields
|
34
|
-
dbh =
|
35
|
-
tbl
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
hash = DohDb::select_transpose("SELECT field, some_value, other_value FROM #{tbl}")
|
32
|
+
dbh = get_dbh
|
33
|
+
dbh.query("CREATE TEMPORARY TABLE #{tbl} (field CHAR(30), some_value CHAR(30), other_value CHAR(30))")
|
34
|
+
dbh.query("INSERT INTO #{tbl} SET field = 'some_name', some_value = 'some_value', other_value = 'blah'")
|
35
|
+
dbh.query("INSERT INTO #{tbl} SET field = 'other_name', some_value = 'matching_other_value', other_value = 'blee'")
|
36
|
+
dbh.query("INSERT INTO #{tbl} SET field = 'yet_another_name', some_value = 'strange_value', other_value = 'bloo'")
|
37
|
+
hash = dbh.select_transpose("SELECT field, some_value, other_value FROM #{tbl}")
|
41
38
|
assert_equal(hash['some_name'], {'some_value' => 'some_value', 'other_value' => 'blah'})
|
42
39
|
assert_equal(hash['other_name'], {'some_value' => 'matching_other_value', 'other_value' => 'blee'})
|
43
40
|
assert_equal(hash['yet_another_name'], {'some_value' => 'strange_value', 'other_value' => 'bloo'})
|
44
41
|
end
|
45
42
|
|
46
43
|
def test_select_values
|
47
|
-
dbh =
|
48
|
-
tbl
|
49
|
-
|
50
|
-
|
51
|
-
assert_equal([['some_name'
|
52
|
-
assert_equal([['some_name']], DohDb::select_values("SELECT field FROM #{tbl}"))
|
44
|
+
dbh = get_dbh
|
45
|
+
dbh.query("CREATE TEMPORARY TABLE #{tbl} (field CHAR(30), some_value CHAR(30))")
|
46
|
+
dbh.query("INSERT INTO #{tbl} SET field = 'some_name', some_value = 'some_value'")
|
47
|
+
assert_equal([['some_name', 'some_value']], dbh.select_values("SELECT field, some_value FROM #{tbl}"))
|
48
|
+
assert_equal([['some_name']], dbh.select_values("SELECT field FROM #{tbl}"))
|
53
49
|
end
|
54
50
|
|
55
51
|
def test_insert_hash
|
56
|
-
dbh =
|
57
|
-
tbl = "doh_mysql_insert_hash_test"
|
52
|
+
dbh = get_dbh
|
58
53
|
dbh.query("CREATE TEMPORARY TABLE #{tbl} (value INT KEY)")
|
59
54
|
hash1 = {'value' => 1}
|
60
55
|
assert_equal(0, dbh.insert_hash(hash1, tbl))
|
61
56
|
assert_raises(Mysql2::Error) { dbh.insert_hash(hash1, tbl) }
|
62
|
-
assert_equal(0, dbh.
|
57
|
+
assert_equal(0, dbh.insert_ignore_hash(hash1, tbl))
|
63
58
|
end
|
64
59
|
end
|
65
60
|
|
data/test/helpers.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
module InternalTestHelpers
|
2
|
+
def dbcfg
|
3
|
+
{ :host => 'localhost', :username => 'root', :database => 'test' }
|
4
|
+
end
|
5
|
+
|
6
|
+
def tbl
|
7
|
+
@tbl ||= self.class.to_s.gsub(/:/, '_').downcase
|
8
|
+
end
|
9
|
+
|
10
|
+
def drop_stmt
|
11
|
+
"DROP TABLE IF EXISTS #{tbl}"
|
12
|
+
end
|
13
|
+
|
14
|
+
def drop_tbl
|
15
|
+
get_dbh.query("DROP TABLE IF EXISTS #{tbl}")
|
16
|
+
end
|
17
|
+
|
18
|
+
def get_dbh
|
19
|
+
require 'doh/mysql/handle'
|
20
|
+
DohDb::Handle.new(dbcfg)
|
21
|
+
end
|
22
|
+
|
23
|
+
def init_global_connector
|
24
|
+
require 'doh/mysql/connector_instance'
|
25
|
+
require 'doh/mysql/cache_connector'
|
26
|
+
DohDb::set_connector_instance(DohDb::CacheConnector.new(dbcfg))
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class DohTest::TestGroup
|
31
|
+
include InternalTestHelpers
|
32
|
+
end
|
data/test/metadata_util.dt.rb
CHANGED
@@ -1,15 +1,24 @@
|
|
1
|
-
require_relative '
|
1
|
+
require_relative 'helpers'
|
2
2
|
require 'doh/mysql/metadata_util'
|
3
3
|
|
4
4
|
module DohDb
|
5
5
|
|
6
6
|
class Test_metadata_util < DohTest::TestGroup
|
7
|
-
def
|
8
|
-
|
7
|
+
def before_all
|
8
|
+
# TODO: shouldn't really have to do this, but for now metadata_util is dependent on it
|
9
|
+
init_global_connector
|
10
|
+
end
|
11
|
+
|
12
|
+
def before_each
|
13
|
+
drop_tbl
|
14
|
+
end
|
15
|
+
|
16
|
+
def after_each
|
17
|
+
drop_tbl
|
9
18
|
end
|
10
19
|
|
11
20
|
def test_stuff
|
12
|
-
dbh =
|
21
|
+
dbh = get_dbh
|
13
22
|
dbh.query("CREATE TABLE #{tbl} (num INT, str CHAR(7))")
|
14
23
|
column_info = DohDb::column_info(tbl)
|
15
24
|
info = column_info['str']
|
@@ -40,14 +49,6 @@ class Test_metadata_util < DohTest::TestGroup
|
|
40
49
|
assert_raises(RuntimeError) { DohDb::find_primary_key(tbl, "this_database_doesnt_exist") }
|
41
50
|
assert_raises(RuntimeError) { DohDb::find_primary_key("this table doesn't exist") }
|
42
51
|
end
|
43
|
-
|
44
|
-
def before_each
|
45
|
-
DohDb::query("DROP TABLE IF EXISTS #{tbl}")
|
46
|
-
end
|
47
|
-
|
48
|
-
def after_each
|
49
|
-
DohDb::query("DROP TABLE IF EXISTS #{tbl}")
|
50
|
-
end
|
51
52
|
end
|
52
53
|
|
53
54
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require_relative 'helpers'
|
2
|
+
require 'mysql2'
|
3
|
+
|
4
|
+
module DohDb
|
5
|
+
|
6
|
+
class Test_typecasting < DohTest::TestGroup
|
7
|
+
def test_stuff
|
8
|
+
Mysql2::Client.default_query_options[:database_timezone] = :utc
|
9
|
+
Mysql2::Client.default_query_options[:application_timezone] = :utc
|
10
|
+
|
11
|
+
dbh = get_dbh
|
12
|
+
dbh.query("CREATE TEMPORARY TABLE #{tbl} (char_field CHAR(1) NOT NULL, int_field INT, bool_field TINYINT(1), date_field DATE, datetime_field DATETIME, decimal_field DECIMAL(7,2))")
|
13
|
+
dbh.insert("INSERT INTO #{tbl} SET char_field = 'c', int_field = 5, bool_field = 1, date_field = '2012-02-20', datetime_field = '2012-02-20 21:06:00', decimal_field = 54.12")
|
14
|
+
row = dbh.select_row("SELECT * FROM #{tbl}")
|
15
|
+
assert_equal('c', row['char_field'])
|
16
|
+
assert_equal(5, row['int_field'])
|
17
|
+
assert_equal(true, row['bool_field'])
|
18
|
+
assert_equal(Date.new(2012,2,20), row['date_field'])
|
19
|
+
assert_equal(DateTime.new(2012,2,20, 21, 6, 0), row['datetime_field'])
|
20
|
+
assert_equal(BigDecimal('54.12'), row['decimal_field'])
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dohmysql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,22 +10,33 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-
|
13
|
+
date: 2012-03-14 00:00:00.000000000Z
|
14
14
|
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: dohroot
|
17
|
+
requirement: &70154500522900 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 0.1.0
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *70154500522900
|
15
26
|
- !ruby/object:Gem::Dependency
|
16
27
|
name: dohutil
|
17
|
-
requirement: &
|
28
|
+
requirement: &70154500522240 !ruby/object:Gem::Requirement
|
18
29
|
none: false
|
19
30
|
requirements:
|
20
31
|
- - ! '>='
|
21
32
|
- !ruby/object:Gem::Version
|
22
|
-
version: 0.1.
|
33
|
+
version: 0.1.7
|
23
34
|
type: :runtime
|
24
35
|
prerelease: false
|
25
|
-
version_requirements: *
|
36
|
+
version_requirements: *70154500522240
|
26
37
|
- !ruby/object:Gem::Dependency
|
27
38
|
name: mysql2
|
28
|
-
requirement: &
|
39
|
+
requirement: &70154500521540 !ruby/object:Gem::Requirement
|
29
40
|
none: false
|
30
41
|
requirements:
|
31
42
|
- - ! '>='
|
@@ -33,29 +44,29 @@ dependencies:
|
|
33
44
|
version: 0.3.11
|
34
45
|
type: :runtime
|
35
46
|
prerelease: false
|
36
|
-
version_requirements: *
|
47
|
+
version_requirements: *70154500521540
|
37
48
|
- !ruby/object:Gem::Dependency
|
38
49
|
name: sqlstmt
|
39
|
-
requirement: &
|
50
|
+
requirement: &70154500520920 !ruby/object:Gem::Requirement
|
40
51
|
none: false
|
41
52
|
requirements:
|
42
53
|
- - ! '>='
|
43
54
|
- !ruby/object:Gem::Version
|
44
|
-
version: 0.1.
|
55
|
+
version: 0.1.1
|
45
56
|
type: :runtime
|
46
57
|
prerelease: false
|
47
|
-
version_requirements: *
|
58
|
+
version_requirements: *70154500520920
|
48
59
|
- !ruby/object:Gem::Dependency
|
49
60
|
name: dohtest
|
50
|
-
requirement: &
|
61
|
+
requirement: &70154500520300 !ruby/object:Gem::Requirement
|
51
62
|
none: false
|
52
63
|
requirements:
|
53
64
|
- - ! '>='
|
54
65
|
- !ruby/object:Gem::Version
|
55
|
-
version: 0.1.
|
66
|
+
version: 0.1.7
|
56
67
|
type: :development
|
57
68
|
prerelease: false
|
58
|
-
version_requirements: *
|
69
|
+
version_requirements: *70154500520300
|
59
70
|
description: wrapper classes around low level mysql gem to provide a better interface
|
60
71
|
email:
|
61
72
|
- devinfo@atpsoft.com
|
@@ -67,11 +78,9 @@ extra_rdoc_files:
|
|
67
78
|
files:
|
68
79
|
- bin/makedb
|
69
80
|
- lib/doh/mysql/abstract_row.rb
|
70
|
-
- lib/doh/mysql/activate.rb
|
71
81
|
- lib/doh/mysql/cache_connector.rb
|
72
82
|
- lib/doh/mysql/connector_instance.rb
|
73
83
|
- lib/doh/mysql/connector_util.rb
|
74
|
-
- lib/doh/mysql/convert.rb
|
75
84
|
- lib/doh/mysql/current_date.rb
|
76
85
|
- lib/doh/mysql/database_creator.rb
|
77
86
|
- lib/doh/mysql/db_date.rb
|
@@ -83,29 +92,24 @@ files:
|
|
83
92
|
- lib/doh/mysql/parse.rb
|
84
93
|
- lib/doh/mysql/raw_row_builder.rb
|
85
94
|
- lib/doh/mysql/readonly_row.rb
|
86
|
-
- lib/doh/mysql/require_dbtypes.rb
|
87
95
|
- lib/doh/mysql/smart_row.rb
|
88
96
|
- lib/doh/mysql/to_sql.rb
|
89
97
|
- lib/doh/mysql/typed_row_builder.rb
|
90
98
|
- lib/doh/mysql/types.rb
|
91
99
|
- lib/doh/mysql/unquoted.rb
|
92
|
-
- lib/doh/mysql/version.rb
|
93
100
|
- lib/doh/mysql/virtual.rb
|
94
101
|
- lib/doh/mysql/writable_row.rb
|
95
102
|
- lib/doh/mysql.rb
|
96
103
|
- test/cache_connector.dt.rb
|
97
|
-
- test/connector.yml
|
98
|
-
- test/connector.yml.tmpl
|
99
104
|
- test/connector_instance.dt.rb
|
100
|
-
- test/convert.dt.rb
|
101
|
-
- test/db_unit_test.rb
|
102
105
|
- test/handle.dt.rb
|
106
|
+
- test/helpers.rb
|
103
107
|
- test/metadata_util.dt.rb
|
104
108
|
- test/parse.dt.rb
|
105
109
|
- test/readonly_row.dt.rb
|
106
110
|
- test/smart_row.dt.rb
|
107
111
|
- test/to_sql.dt.rb
|
108
|
-
- test/
|
112
|
+
- test/typecasting.dt.rb
|
109
113
|
- test/unquoted.dt.rb
|
110
114
|
- test/writable_row.dt.rb
|
111
115
|
- MIT-LICENSE
|
@@ -137,15 +141,14 @@ summary: friendly mysql client interface
|
|
137
141
|
test_files:
|
138
142
|
- test/cache_connector.dt.rb
|
139
143
|
- test/connector_instance.dt.rb
|
140
|
-
- test/convert.dt.rb
|
141
|
-
- test/db_unit_test.rb
|
142
144
|
- test/handle.dt.rb
|
145
|
+
- test/helpers.rb
|
143
146
|
- test/metadata_util.dt.rb
|
144
147
|
- test/parse.dt.rb
|
145
148
|
- test/readonly_row.dt.rb
|
146
149
|
- test/smart_row.dt.rb
|
147
150
|
- test/to_sql.dt.rb
|
148
|
-
- test/
|
151
|
+
- test/typecasting.dt.rb
|
149
152
|
- test/unquoted.dt.rb
|
150
153
|
- test/writable_row.dt.rb
|
151
154
|
has_rdoc:
|
data/lib/doh/mysql/activate.rb
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
require 'doh/mysql/cache_connector'
|
2
|
-
require 'doh/mysql/connector_instance'
|
3
|
-
require 'doh/mysql/handle'
|
4
|
-
require 'doh/mysql/to_sql'
|
5
|
-
require 'doh/mysql/db_date'
|
6
|
-
require 'doh/mysql/unquoted'
|
7
|
-
require 'doh/config'
|
8
|
-
|
9
|
-
module DohDb
|
10
|
-
|
11
|
-
def self.activate
|
12
|
-
root_cfg = Doh::config
|
13
|
-
return unless root_cfg.fetch('enable_database', true)
|
14
|
-
return unless root_cfg.key?('database')
|
15
|
-
db_cfg = root_cfg['database']
|
16
|
-
require 'doh/mysql'
|
17
|
-
require 'doh/mysql/require_dbtypes'
|
18
|
-
conn = DohDb::CacheConnector.new(db_cfg['host'], db_cfg['username'], db_cfg['password'], db_cfg['database'] || root_cfg['primary_database'])
|
19
|
-
conn.port = db_cfg['port']
|
20
|
-
conn.timeout = db_cfg['timeout'].to_i if db_cfg['timeout']
|
21
|
-
conn.row_builder = db_cfg['row_builder'] || root_cfg['row_builder']
|
22
|
-
DohDb::set_connector_instance(conn)
|
23
|
-
DohDb::require_dbtypes
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|
data/lib/doh/mysql/convert.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
require 'doh/mysql/metadata_util'
|
2
|
-
require 'doh/mysql/error'
|
3
|
-
|
4
|
-
module DohDb
|
5
|
-
|
6
|
-
def self.convert(table, column, value)
|
7
|
-
info = column_info(table)[column]
|
8
|
-
# raise UnknownColumn, "#{table}.#{column}" if info.nil?
|
9
|
-
return value if info.nil?
|
10
|
-
if value.nil?
|
11
|
-
raise CannotBeNull, "#{table}.#{column}" if info['is_nullable'] == 'NO'
|
12
|
-
return nil
|
13
|
-
end
|
14
|
-
return nil if value.is_a?(String) && value.empty? && info['is_nullable'] == 'YES'
|
15
|
-
value
|
16
|
-
end
|
17
|
-
|
18
|
-
end
|
data/lib/doh/mysql/version.rb
DELETED
@@ -1,102 +0,0 @@
|
|
1
|
-
require 'doh/mysql/connector_instance'
|
2
|
-
require 'yaml'
|
3
|
-
|
4
|
-
module DohDb
|
5
|
-
|
6
|
-
def self.locked_filename(database)
|
7
|
-
File.join(Doh::root, 'database', database, 'migrate/locked.yml')
|
8
|
-
end
|
9
|
-
|
10
|
-
def self.migration_filename(database, version, upordown = 'up')
|
11
|
-
version_str = version.to_s.rjust(3, '0')
|
12
|
-
File.join(Doh::root, 'database', database, "migrate/#{version_str}_#{upordown}.sql")
|
13
|
-
end
|
14
|
-
|
15
|
-
def self.current_database_version(database = nil)
|
16
|
-
if database.nil?
|
17
|
-
table = 'version'
|
18
|
-
else
|
19
|
-
table = database + '.version'
|
20
|
-
end
|
21
|
-
DohDb::select_field("SELECT version FROM #{table}").to_i
|
22
|
-
end
|
23
|
-
|
24
|
-
def self.latest_database_version(database = nil)
|
25
|
-
locked_version = locked_database_version(database)
|
26
|
-
unlocked_version = locked_version + 1
|
27
|
-
unlocked_migration_file = migration_filename(database, unlocked_version)
|
28
|
-
if File.exist?(unlocked_migration_file)
|
29
|
-
unlocked_version
|
30
|
-
else
|
31
|
-
locked_version
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def self.locked_version_info(database = nil)
|
36
|
-
database ||= (DohDb::connector_instance && DohDb::connector_instance.database) || Doh::config['primary_database']
|
37
|
-
filename = locked_filename(database)
|
38
|
-
return [0, nil] unless File.exist?(filename)
|
39
|
-
YAML.load_file(filename)
|
40
|
-
end
|
41
|
-
|
42
|
-
def self.locked_database_version(database = nil)
|
43
|
-
locked_version_info(database).first
|
44
|
-
end
|
45
|
-
|
46
|
-
def self.locked_svn_revision(database = nil)
|
47
|
-
locked_version_info(database).last
|
48
|
-
end
|
49
|
-
|
50
|
-
def self.update_locked_file(database)
|
51
|
-
locked_file = locked_filename(database)
|
52
|
-
if File.exist?(locked_file)
|
53
|
-
new_version = YAML.load_file(locked_file).first + 1
|
54
|
-
else
|
55
|
-
new_version = 0
|
56
|
-
need_to_add = true
|
57
|
-
end
|
58
|
-
|
59
|
-
file_to_check = ''
|
60
|
-
if need_to_add
|
61
|
-
file_to_check = File.join(Doh::root, "database")
|
62
|
-
else
|
63
|
-
unlocked_migration_file = migration_filename(database, new_version)
|
64
|
-
return [true, "nothing to lock"] unless File.exist?(unlocked_migration_file)
|
65
|
-
|
66
|
-
svnout = `svn st #{unlocked_migration_file}`
|
67
|
-
unless svnout.strip.empty?
|
68
|
-
return [false, "svn status shows local changes to #{unlocked_migration_file} -- this needs to be resolved before updating the migration locked file"]
|
69
|
-
end
|
70
|
-
|
71
|
-
file_to_check = unlocked_migration_file
|
72
|
-
end
|
73
|
-
|
74
|
-
`svn update #{file_to_check}`
|
75
|
-
svnout = `svn st -v #{file_to_check}`.split("\n")
|
76
|
-
svnout =~ /(\d+)/
|
77
|
-
revision = svnout.collect {|elem| elem =~ /(\d+)/; $1.to_i}.max
|
78
|
-
if !revision
|
79
|
-
return [false, "unable to extract svn revision from: '#{svnout}'"]
|
80
|
-
end
|
81
|
-
|
82
|
-
outfile = File.new(locked_file, 'w')
|
83
|
-
outfile.puts([new_version, revision.to_i].inspect)
|
84
|
-
outfile.close
|
85
|
-
|
86
|
-
if need_to_add
|
87
|
-
svnout = `svn add #{locked_file}`
|
88
|
-
if svnout[0,1] != 'A'
|
89
|
-
return [false, "failed to svn add #{locked_file}"]
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
msg = "migrate lock for #{database} database, version #{new_version}"
|
94
|
-
svnout = `svn ci -m \"#{msg}\" #{locked_file}`
|
95
|
-
unless svnout.index("Committed revision")
|
96
|
-
return [false, "failed to svn ci #{locked_file}"]
|
97
|
-
end
|
98
|
-
|
99
|
-
[true, "#{locked_file} successfully updated"]
|
100
|
-
end
|
101
|
-
|
102
|
-
end
|
data/test/connector.yml
DELETED
data/test/connector.yml.tmpl
DELETED
data/test/convert.dt.rb
DELETED
@@ -1,45 +0,0 @@
|
|
1
|
-
require_relative 'db_unit_test'
|
2
|
-
require 'doh/mysql/convert'
|
3
|
-
|
4
|
-
module DohDb
|
5
|
-
|
6
|
-
class Test_Convert < DohTest::TestGroup
|
7
|
-
def before_each
|
8
|
-
@tbl = "doh_mysql_convert_tests"
|
9
|
-
DohDb::query("DROP TABLE IF EXISTS #@tbl")
|
10
|
-
DohDb::request_handle.query("CREATE TABLE #{@tbl} (not_null_field CHAR(1) NOT NULL, null_ok_field CHAR(1), int_field INT, bool_field TINYINT(1), date_field DATE, datetime_field DATETIME)")
|
11
|
-
end
|
12
|
-
|
13
|
-
def after_each
|
14
|
-
DohDb::query("DROP TABLE IF EXISTS #@tbl")
|
15
|
-
end
|
16
|
-
|
17
|
-
def convert(value)
|
18
|
-
DohDb::convert(@tbl, @field, value)
|
19
|
-
end
|
20
|
-
|
21
|
-
def verify(converted, original)
|
22
|
-
assert_equal(converted, convert(original))
|
23
|
-
end
|
24
|
-
|
25
|
-
def verify_same(value)
|
26
|
-
verify(value, value)
|
27
|
-
end
|
28
|
-
|
29
|
-
def verify_exception(value, exception_class)
|
30
|
-
assert_raises(exception_class) { convert(value) }
|
31
|
-
end
|
32
|
-
|
33
|
-
def test_nil_null
|
34
|
-
# assert_raises(UnknownColumn) { DohDb::convert('some_table_that_doesnt_exist', 'some_column_that_doesnt_exist', 'blah') }
|
35
|
-
assert_equal('blah', DohDb::convert('some_table_that_doesnt_exist', 'some_column_that_doesnt_exist', 'blah'))
|
36
|
-
assert_equal('blah', DohDb::convert('', 'some_column_that_doesnt_exist', 'blah'))
|
37
|
-
assert_raises(CannotBeNull) { DohDb::convert(@tbl, 'not_null_field', nil) }
|
38
|
-
assert_equal(nil, DohDb::convert(@tbl, 'null_ok_field', nil))
|
39
|
-
assert_equal('', DohDb::convert(@tbl, 'not_null_field', ''))
|
40
|
-
assert_equal(nil, DohDb::convert(@tbl, 'null_ok_field', ''))
|
41
|
-
assert_equal(nil, DohDb::convert(@tbl, 'int_field', ''))
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
end
|
data/test/db_unit_test.rb
DELETED
@@ -1,10 +0,0 @@
|
|
1
|
-
require 'doh/mysql/connector_instance'
|
2
|
-
require 'doh/mysql/cache_connector'
|
3
|
-
require 'yaml'
|
4
|
-
|
5
|
-
config_filename = File.join(File.dirname(__FILE__), 'connector.yml')
|
6
|
-
raise RuntimeError.new("mysql connector configuration file (#{config_filename}) must exist (see #{config_filename}.tmpl for an example)") unless File.exist?(config_filename)
|
7
|
-
config = YAML.load_file(config_filename)
|
8
|
-
|
9
|
-
connector = DohDb::CacheConnector.new(config['host'], config['username'], config['password'], config['database'])
|
10
|
-
DohDb::set_connector_instance(connector)
|
data/test/types.dt.rb
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
require_relative 'db_unit_test'
|
2
|
-
require 'doh/mysql/types'
|
3
|
-
|
4
|
-
module DohDb
|
5
|
-
|
6
|
-
class Test_types < DohTest::TestGroup
|
7
|
-
def test_stuff
|
8
|
-
Mysql2::Client.default_query_options[:database_timezone] = :utc
|
9
|
-
Mysql2::Client.default_query_options[:application_timezone] = :utc
|
10
|
-
DohDb::connector_instance.reset
|
11
|
-
|
12
|
-
@tbl = "doh_mysql_type_conversion_tests"
|
13
|
-
DohDb::query("DROP TABLE IF EXISTS #@tbl")
|
14
|
-
DohDb::request_handle.query("CREATE TEMPORARY TABLE #{@tbl} (char_field CHAR(1) NOT NULL, int_field INT, bool_field TINYINT(1), date_field DATE, datetime_field DATETIME, decimal_field DECIMAL(7,2))")
|
15
|
-
|
16
|
-
dbh = DohDb::request_handle
|
17
|
-
dbh.insert("INSERT INTO #{@tbl} SET char_field = 'c', int_field = 5, bool_field = 1, date_field = '2012-02-20', datetime_field = '2012-02-20 21:06:00', decimal_field = 54.12")
|
18
|
-
row = dbh.select_row("SELECT * FROM #{@tbl}")
|
19
|
-
assert_equal('c', row['char_field'])
|
20
|
-
assert_equal(5, row['int_field'])
|
21
|
-
assert_equal(true, row['bool_field'])
|
22
|
-
assert_equal(Date.new(2012,2,20), row['date_field'])
|
23
|
-
assert_equal(DateTime.new(2012,2,20, 21, 6, 0).utc, row['datetime_field'])
|
24
|
-
assert_equal(BigDecimal('54.12'), row['decimal_field'])
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|