baza 0.0.19 → 0.0.20
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.
- checksums.yaml +4 -4
- data/.rubocop_todo.yml +157 -0
- data/Gemfile +11 -9
- data/Gemfile.lock +41 -9
- data/README.md +1 -1
- data/Rakefile +19 -16
- data/VERSION +1 -1
- data/baza.gemspec +31 -7
- data/config/best_project_practice_rubocop.yml +2 -0
- data/config/best_project_practice_rubocop_todo.yml +157 -0
- data/lib/baza/base_sql_driver.rb +79 -20
- data/lib/baza/cloner.rb +2 -2
- data/lib/baza/column.rb +6 -0
- data/lib/baza/database.rb +33 -0
- data/lib/baza/database_model.rb +26 -0
- data/lib/baza/database_model_functionality.rb +10 -0
- data/lib/baza/database_model_name.rb +16 -0
- data/lib/baza/db.rb +210 -413
- data/lib/baza/dbtime.rb +20 -20
- data/lib/baza/driver.rb +3 -3
- data/lib/baza/drivers/active_record/columns.rb +1 -1
- data/lib/baza/drivers/active_record/indexes.rb +1 -1
- data/lib/baza/drivers/active_record/result.rb +3 -5
- data/lib/baza/drivers/active_record/tables.rb +2 -2
- data/lib/baza/drivers/active_record.rb +30 -16
- data/lib/baza/drivers/mysql/column.rb +51 -48
- data/lib/baza/drivers/mysql/columns.rb +8 -8
- data/lib/baza/drivers/mysql/database.rb +28 -0
- data/lib/baza/drivers/mysql/databases.rb +35 -0
- data/lib/baza/drivers/mysql/index.rb +32 -22
- data/lib/baza/drivers/mysql/result.rb +24 -24
- data/lib/baza/drivers/mysql/sqlspecs.rb +1 -1
- data/lib/baza/drivers/mysql/table.rb +80 -91
- data/lib/baza/drivers/mysql/tables.rb +30 -32
- data/lib/baza/drivers/mysql/unbuffered_result.rb +5 -5
- data/lib/baza/drivers/mysql.rb +127 -220
- data/lib/baza/drivers/mysql2/database.rb +2 -0
- data/lib/baza/drivers/mysql2/databases.rb +2 -0
- data/lib/baza/drivers/mysql2/result.rb +3 -5
- data/lib/baza/drivers/mysql2.rb +21 -106
- data/lib/baza/drivers/mysql_java/database.rb +2 -0
- data/lib/baza/drivers/mysql_java/databases.rb +2 -0
- data/lib/baza/drivers/mysql_java.rb +16 -12
- data/lib/baza/drivers/sqlite3/column.rb +33 -33
- data/lib/baza/drivers/sqlite3/columns.rb +6 -6
- data/lib/baza/drivers/sqlite3/database.rb +2 -0
- data/lib/baza/drivers/sqlite3/databases.rb +25 -0
- data/lib/baza/drivers/sqlite3/index.rb +20 -6
- data/lib/baza/drivers/sqlite3/result.rb +16 -17
- data/lib/baza/drivers/sqlite3/sqlspecs.rb +1 -1
- data/lib/baza/drivers/sqlite3/table.rb +99 -100
- data/lib/baza/drivers/sqlite3/tables.rb +30 -23
- data/lib/baza/drivers/sqlite3/unbuffered_result.rb +2 -2
- data/lib/baza/drivers/sqlite3.rb +27 -14
- data/lib/baza/drivers/sqlite3_java/database.rb +2 -0
- data/lib/baza/drivers/sqlite3_java/unbuffered_result.rb +2 -2
- data/lib/baza/drivers/sqlite3_java.rb +12 -10
- data/lib/baza/drivers/sqlite3_rhodes.rb +9 -7
- data/lib/baza/dump.rb +55 -55
- data/lib/baza/errors.rb +5 -0
- data/lib/baza/idquery.rb +25 -25
- data/lib/baza/index.rb +6 -0
- data/lib/baza/jdbc_driver.rb +6 -8
- data/lib/baza/jdbc_result.rb +1 -1
- data/lib/baza/model.rb +299 -309
- data/lib/baza/model_custom.rb +74 -72
- data/lib/baza/model_handler.rb +244 -255
- data/lib/baza/model_handler_sqlhelper.rb +122 -132
- data/lib/baza/mysql_base_driver.rb +112 -0
- data/lib/baza/query_buffer.rb +23 -23
- data/lib/baza/result_base.rb +2 -2
- data/lib/baza/revision.rb +67 -67
- data/lib/baza/row.rb +82 -84
- data/lib/baza/sqlspecs.rb +1 -1
- data/lib/baza/table.rb +31 -0
- data/lib/baza.rb +43 -10
- data/shippable.yml +1 -0
- data/spec/cloner_spec.rb +4 -4
- data/spec/drivers/active_record_mysql2_spec.rb +3 -3
- data/spec/drivers/active_record_mysql_spec.rb +2 -2
- data/spec/drivers/active_record_sqlite3_spec.rb +2 -2
- data/spec/drivers/mysql2_spec.rb +1 -0
- data/spec/drivers/mysql_spec.rb +1 -0
- data/spec/drivers/sqlite3_spec.rb +4 -5
- data/spec/info_active_record_example.rb +4 -6
- data/spec/info_active_record_mysql.rb +6 -7
- data/spec/info_active_record_mysql2.rb +2 -4
- data/spec/info_active_record_mysql2_shippable.rb +2 -4
- data/spec/info_active_record_mysql_shippable.rb +2 -4
- data/spec/info_active_record_sqlite3.rb +5 -6
- data/spec/info_mysql2_example.rb +1 -1
- data/spec/info_mysql2_shippable.rb +1 -1
- data/spec/info_mysql_example.rb +1 -1
- data/spec/info_mysql_shippable.rb +1 -1
- data/spec/info_sqlite3.rb +4 -3
- data/spec/model_handler_spec.rb +102 -115
- data/spec/spec_helper.rb +5 -5
- data/spec/support/driver_collection.rb +98 -95
- data/spec/support/driver_columns_collection.rb +18 -22
- data/spec/support/driver_databases_collection.rb +29 -0
- data/spec/support/driver_indexes_collection.rb +7 -6
- data/spec/support/driver_tables_collection.rb +38 -26
- metadata +49 -3
data/lib/baza/query_buffer.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
#This class buffers a lot of queries and flushes them out via transactions.
|
1
|
+
# This class buffers a lot of queries and flushes them out via transactions.
|
2
2
|
class Baza::QueryBuffer
|
3
3
|
attr_reader :thread_async
|
4
4
|
|
5
5
|
INITIALIZE_ARGS_ALLOWED = [:db, :debug, :flush_async]
|
6
|
-
#Constructor. Takes arguments to be used and a block.
|
6
|
+
# Constructor. Takes arguments to be used and a block.
|
7
7
|
def initialize(args)
|
8
8
|
@args = args
|
9
9
|
@queries = []
|
@@ -37,7 +37,7 @@ class Baza::QueryBuffer
|
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
|
-
#Adds a query to the buffer.
|
40
|
+
# Adds a query to the buffer.
|
41
41
|
def query(str)
|
42
42
|
@lock.synchronize do
|
43
43
|
STDOUT.print "Adding to buffer: #{str}\n" if @debug
|
@@ -46,44 +46,44 @@ class Baza::QueryBuffer
|
|
46
46
|
end
|
47
47
|
|
48
48
|
flush if @queries_count >= 1000
|
49
|
-
|
49
|
+
nil
|
50
50
|
end
|
51
51
|
|
52
|
-
#Delete as on a normal Baza::Db.
|
52
|
+
# Delete as on a normal Baza::Db.
|
53
53
|
#===Example
|
54
54
|
# buffer.delete(:users, {:id => 5})
|
55
55
|
def delete(table, where)
|
56
56
|
STDOUT.puts "Delete called on table #{table} with arguments: '#{where}'." if @debug
|
57
|
-
query(@args[:db].delete(table, where, :
|
58
|
-
|
57
|
+
query(@args[:db].delete(table, where, return_sql: true))
|
58
|
+
nil
|
59
59
|
end
|
60
60
|
|
61
|
-
#Update as on a normal Baza::Db.
|
61
|
+
# Update as on a normal Baza::Db.
|
62
62
|
#===Example
|
63
63
|
# buffer.update(:users, {:name => "Kasper"}, {:id => 5})
|
64
64
|
def update(table, update, terms)
|
65
65
|
STDOUT.puts "Update called on table #{table}." if @debug
|
66
|
-
query(@args[:db].update(table, update, terms, :
|
67
|
-
|
66
|
+
query(@args[:db].update(table, update, terms, return_sql: true))
|
67
|
+
nil
|
68
68
|
end
|
69
69
|
|
70
|
-
#Shortcut to doing upsert through the buffer instead of through the db-object with the buffer as an argument.
|
70
|
+
# Shortcut to doing upsert through the buffer instead of through the db-object with the buffer as an argument.
|
71
71
|
#===Example
|
72
72
|
# buffer.upsert(:users, {:id => 5}, {:name => "Kasper"})
|
73
73
|
def upsert(table, data, terms)
|
74
|
-
@args[:db].upsert(table, data, terms, :
|
75
|
-
|
74
|
+
@args[:db].upsert(table, data, terms, buffer: self)
|
75
|
+
nil
|
76
76
|
end
|
77
77
|
|
78
|
-
#Plans to inset a hash into a table. It will only be inserted when flush is called.
|
78
|
+
# Plans to inset a hash into a table. It will only be inserted when flush is called.
|
79
79
|
#===Examples
|
80
80
|
# buffer.insert(:users, {:name => "John Doe"})
|
81
81
|
def insert(table, data)
|
82
|
-
query(@args[:db].insert(table, data, :
|
83
|
-
|
82
|
+
query(@args[:db].insert(table, data, return_sql: true))
|
83
|
+
nil
|
84
84
|
end
|
85
85
|
|
86
|
-
#Flushes all queries out in a transaction. This will automatically be called for every 1000 queries.
|
86
|
+
# Flushes all queries out in a transaction. This will automatically be called for every 1000 queries.
|
87
87
|
def flush
|
88
88
|
if @args[:flush_async]
|
89
89
|
flush_async
|
@@ -94,7 +94,7 @@ class Baza::QueryBuffer
|
|
94
94
|
|
95
95
|
private
|
96
96
|
|
97
|
-
#Runs the flush in a thread in the background.
|
97
|
+
# Runs the flush in a thread in the background.
|
98
98
|
def flush_async
|
99
99
|
thread_async_join
|
100
100
|
|
@@ -114,14 +114,14 @@ private
|
|
114
114
|
end
|
115
115
|
end
|
116
116
|
|
117
|
-
#Flushes the queries for real.
|
117
|
+
# Flushes the queries for real.
|
118
118
|
def flush_real(db = nil)
|
119
119
|
return nil if @queries_count <= 0
|
120
120
|
db = @args[:db] if db == nil
|
121
121
|
|
122
122
|
@lock.synchronize do
|
123
|
-
|
124
|
-
|
123
|
+
unless @queries.empty?
|
124
|
+
until @queries.empty?
|
125
125
|
db.transaction do
|
126
126
|
@queries.shift(1000).each do |str|
|
127
127
|
STDOUT.print "Executing via buffer: #{str}\n" if @debug
|
@@ -132,7 +132,7 @@ private
|
|
132
132
|
end
|
133
133
|
|
134
134
|
@inserts.each do |table, datas_arr|
|
135
|
-
|
135
|
+
until datas_arr.empty?
|
136
136
|
datas_chunk_arr = datas_arr.shift(1000)
|
137
137
|
@db.insert_multi(table, datas_chunk_arr)
|
138
138
|
end
|
@@ -142,6 +142,6 @@ private
|
|
142
142
|
@queries_count = 0
|
143
143
|
end
|
144
144
|
|
145
|
-
|
145
|
+
nil
|
146
146
|
end
|
147
147
|
end
|
data/lib/baza/result_base.rb
CHANGED
@@ -10,7 +10,7 @@ class Baza::ResultBase
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def to_a_enum
|
13
|
-
require
|
13
|
+
require "array_enumerator"
|
14
14
|
@a_enum ||= ArrayEnumerator.new(to_enum)
|
15
15
|
end
|
16
16
|
|
@@ -20,6 +20,6 @@ class Baza::ResultBase
|
|
20
20
|
array << row_data
|
21
21
|
end
|
22
22
|
|
23
|
-
|
23
|
+
array
|
24
24
|
end
|
25
25
|
end
|
data/lib/baza/revision.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#This class takes a database-schema from a hash and runs it against the database. It then checks that the database matches the given schema.
|
1
|
+
# This class takes a database-schema from a hash and runs it against the database. It then checks that the database matches the given schema.
|
2
2
|
#
|
3
3
|
#===Examples
|
4
4
|
# db = Baza::Db.new(type: :sqlite3, path: "test_db.sqlite3")
|
@@ -31,12 +31,12 @@ class Baza::Revision
|
|
31
31
|
INIT_DB_ALLOWED_ARGS = [:db, :schema, :tables_cache, :debug]
|
32
32
|
INIT_DB_SCHEMA_ALLOWED_ARGS = [:tables]
|
33
33
|
INIT_DB_TABLE_ALLOWED_ARGS = [:columns, :indexes, :rows, :renames]
|
34
|
-
#This initializes a database-structure and content based on a schema-hash.
|
34
|
+
# This initializes a database-structure and content based on a schema-hash.
|
35
35
|
#===Examples
|
36
36
|
# dbrev = Baza::Revision.new
|
37
37
|
# dbrev.init_db("db" => db_obj, "schema" => schema_hash)
|
38
38
|
def init_db(args)
|
39
|
-
args.each do |key,
|
39
|
+
args.each do |key, _val|
|
40
40
|
raise "Invalid key: '#{key}' (#{key.class.name})." unless INIT_DB_ALLOWED_ARGS.include?(key)
|
41
41
|
end
|
42
42
|
|
@@ -44,15 +44,15 @@ class Baza::Revision
|
|
44
44
|
db = args[:db]
|
45
45
|
raise "No 'db' was given." unless db
|
46
46
|
|
47
|
-
schema.each do |key,
|
47
|
+
schema.each do |key, _val|
|
48
48
|
raise "Invalid key for schema: '#{key}' (#{key.class.name})." unless INIT_DB_SCHEMA_ALLOWED_ARGS.include?(key)
|
49
49
|
end
|
50
50
|
|
51
|
-
#Check for normal bugs and raise apropiate error.
|
51
|
+
# Check for normal bugs and raise apropiate error.
|
52
52
|
raise "'schema' argument was not a Hash: '#{schema.class.name}'." unless schema.is_a?(Hash)
|
53
|
-
raise "No tables given." unless schema.
|
53
|
+
raise "No tables given." unless schema.key?(:tables)
|
54
54
|
|
55
|
-
#Cache tables to avoid constant reloading.
|
55
|
+
# Cache tables to avoid constant reloading.
|
56
56
|
if !args.key?(:tables_cache) || args[:tables_cache]
|
57
57
|
puts "Caching tables-list." if args[:debug]
|
58
58
|
tables = db.tables.list
|
@@ -61,7 +61,7 @@ class Baza::Revision
|
|
61
61
|
end
|
62
62
|
|
63
63
|
schema[:tables].each do |table_name, table_data|
|
64
|
-
table_data.each do |key,
|
64
|
+
table_data.each do |key, _val|
|
65
65
|
raise "Invalid key: '#{key}' (#{key.class.name})." unless INIT_DB_TABLE_ALLOWED_ARGS.include?(key)
|
66
66
|
end
|
67
67
|
|
@@ -72,7 +72,7 @@ class Baza::Revision
|
|
72
72
|
puts "Getting table-object for table: '#{table_name}'." if args[:debug]
|
73
73
|
table_obj = db.tables[table_name]
|
74
74
|
|
75
|
-
#Cache indexes- and column-objects to avoid constant reloading.
|
75
|
+
# Cache indexes- and column-objects to avoid constant reloading.
|
76
76
|
cols = table_obj.columns
|
77
77
|
indexes = table_obj.indexes
|
78
78
|
|
@@ -86,12 +86,12 @@ class Baza::Revision
|
|
86
86
|
type = col_data[:type].to_sym
|
87
87
|
dochange = false
|
88
88
|
|
89
|
-
if !first_col
|
90
|
-
#Try to find out the previous column - if so we can set "after" which makes the column being created in the right order as defined.
|
91
|
-
|
89
|
+
if !first_col && !col_data[:after]
|
90
|
+
# Try to find out the previous column - if so we can set "after" which makes the column being created in the right order as defined.
|
91
|
+
unless col_data.key?(:after)
|
92
92
|
prev_no = table_data[:columns].index(col_data)
|
93
|
-
if prev_no
|
94
|
-
prev_no
|
93
|
+
if prev_no.nil? && prev_no != 0
|
94
|
+
prev_no -= 1
|
95
95
|
prev_col_data = table_data[:columns][prev_no]
|
96
96
|
col_data[:after] = prev_col_data[:name]
|
97
97
|
end
|
@@ -99,7 +99,7 @@ class Baza::Revision
|
|
99
99
|
|
100
100
|
actual_after = nil
|
101
101
|
set_next = false
|
102
|
-
cols.each do |
|
102
|
+
cols.each do |_col_name, col_iter|
|
103
103
|
if col_iter.name == col_obj.name
|
104
104
|
break
|
105
105
|
else
|
@@ -113,49 +113,49 @@ class Baza::Revision
|
|
113
113
|
end
|
114
114
|
end
|
115
115
|
|
116
|
-
#BUGFIX: When using SQLite3 the primary-column or a autoincr-column may never change type from int... This will break it!
|
117
|
-
if db.opts[:type] == "sqlite3"
|
116
|
+
# BUGFIX: When using SQLite3 the primary-column or a autoincr-column may never change type from int... This will break it!
|
117
|
+
if db.opts[:type] == "sqlite3" && col_obj.type.to_s == "int" && (col_data[:primarykey] || col_data[:autoincr]) && db.int_types.index(col_data[:type].to_s)
|
118
118
|
type = :int
|
119
119
|
end
|
120
120
|
|
121
|
-
if type
|
121
|
+
if type && col_obj.type.to_s != type
|
122
122
|
print "Type mismatch on #{col_str}: #{col_data[:type]}, #{col_obj.type}\n" if args[:debug]
|
123
123
|
dochange = true
|
124
124
|
end
|
125
125
|
|
126
|
-
if col_data.
|
126
|
+
if col_data.key?(:primarykey) && col_obj.primarykey? != col_data[:primarykey]
|
127
127
|
print "Primary-key mismatch for #{col_str}: #{col_data[:primarykey]}, #{col_obj.primarykey?}\n" if args[:debug]
|
128
128
|
dochange = true
|
129
129
|
end
|
130
130
|
|
131
|
-
if col_data.
|
131
|
+
if col_data.key?(:autoincr) && col_obj.autoincr? != col_data[:autoincr]
|
132
132
|
print "Auto-increment mismatch for #{col_str}: #{col_data[:autoincr]}, #{col_obj.autoincr?}\n" if args[:debug]
|
133
133
|
dochange = true
|
134
134
|
end
|
135
135
|
|
136
|
-
if col_data.
|
136
|
+
if col_data.key?(:maxlength) && col_obj.maxlength.to_s != col_data[:maxlength].to_s
|
137
137
|
print "Maxlength mismatch on #{col_str}: #{col_data[:maxlength]}, #{col_obj.maxlength}\n" if args[:debug]
|
138
138
|
dochange = true
|
139
139
|
end
|
140
140
|
|
141
|
-
if col_data.
|
141
|
+
if col_data.key?(:null) && col_obj.null?.to_s != col_data[:null].to_s
|
142
142
|
print "Null mismatch on #{col_str}: #{col_data[:null]}, #{col_obj.null?}\n" if args[:debug]
|
143
143
|
dochange = true
|
144
144
|
end
|
145
145
|
|
146
|
-
if col_data.
|
146
|
+
if col_data.key?(:default) && col_obj.default.to_s != col_data[:default].to_s
|
147
147
|
print "Default mismatch on #{col_str}: #{col_data[:default]}, #{col_obj.default}\n" if args[:debug]
|
148
148
|
dochange = true
|
149
149
|
end
|
150
150
|
|
151
|
-
if col_data.
|
151
|
+
if col_data.key?(:comment) && col_obj.respond_to?(:comment) && col_obj.comment.to_s != col_data[:comment].to_s
|
152
152
|
print "Comment mismatch on #{col_str}: #{col_data[:comment]}, #{col_obj.comment}\n" if args[:debug]
|
153
153
|
dochange = true
|
154
154
|
end
|
155
155
|
|
156
|
-
if col_data.is_a?(Hash)
|
157
|
-
callback_data = col_data[:on_before_alter].call(:
|
158
|
-
if callback_data
|
156
|
+
if col_data.is_a?(Hash) && col_data[:on_before_alter]
|
157
|
+
callback_data = col_data[:on_before_alter].call(db: db, table: table_obj, col: col_obj, col_data: col_data)
|
158
|
+
if callback_data && callback_data[:action]
|
159
159
|
if callback_data[:action] == :retry
|
160
160
|
raise Baza::Errors::Retry
|
161
161
|
end
|
@@ -168,28 +168,28 @@ class Baza::Revision
|
|
168
168
|
|
169
169
|
col_obj.change(col_data_change)
|
170
170
|
|
171
|
-
#Change has been made - update cache.
|
171
|
+
# Change has been made - update cache.
|
172
172
|
cols = table_obj.columns
|
173
173
|
end
|
174
174
|
|
175
175
|
first_col = false
|
176
|
-
rescue
|
176
|
+
rescue Baza::Errors::ColumnNotFound => e
|
177
177
|
print "Column not found: #{table_obj.name}.#{col_data[:name]}.\n" if args[:debug]
|
178
178
|
|
179
|
-
if col_data.
|
180
|
-
raise "'renames' was not an array for column '#{table_obj.name}.#{col_data[:name]}'."
|
179
|
+
if col_data.key?(:renames)
|
180
|
+
raise "'renames' was not an array for column '#{table_obj.name}.#{col_data[:name]}'." unless col_data[:renames].is_a?(Array)
|
181
181
|
|
182
182
|
rename_found = false
|
183
|
-
col_data[:renames].each do |
|
183
|
+
col_data[:renames].each do |col_name_rename|
|
184
184
|
begin
|
185
|
-
col_rename = table_obj.column(
|
186
|
-
rescue
|
185
|
+
col_rename = table_obj.column(col_name_rename)
|
186
|
+
rescue Baza::Errors::ColumnNotFound
|
187
187
|
next
|
188
188
|
end
|
189
189
|
|
190
|
-
print "Rename #{table_obj.name}.#{
|
191
|
-
if col_data.is_a?(Hash)
|
192
|
-
col_data[:on_before_rename].call(:
|
190
|
+
print "Rename #{table_obj.name}.#{col_name_rename} to #{table_obj.name}.#{col_data[:name]}\n" if args[:debug]
|
191
|
+
if col_data.is_a?(Hash) && col_data[:on_before_rename]
|
192
|
+
col_data[:on_before_rename].call(db: db, table: table_obj, col: col_rename, col_data: col_data)
|
193
193
|
end
|
194
194
|
|
195
195
|
col_data_change = col_data.clone
|
@@ -197,11 +197,11 @@ class Baza::Revision
|
|
197
197
|
|
198
198
|
col_rename.change(col_data_change)
|
199
199
|
|
200
|
-
#Change has been made - update cache.
|
200
|
+
# Change has been made - update cache.
|
201
201
|
cols = table_obj.columns
|
202
202
|
|
203
|
-
if col_data.is_a?(Hash)
|
204
|
-
col_data[:on_after_rename].call(:
|
203
|
+
if col_data.is_a?(Hash) && col_data[:on_after_rename]
|
204
|
+
col_data[:on_after_rename].call(db: db, table: table_obj, col: col_rename, col_data: col_data)
|
205
205
|
end
|
206
206
|
|
207
207
|
rename_found = true
|
@@ -219,10 +219,10 @@ class Baza::Revision
|
|
219
219
|
|
220
220
|
col_obj = table_obj.create_columns([col_data])
|
221
221
|
|
222
|
-
#Change has been made - update cache.
|
222
|
+
# Change has been made - update cache.
|
223
223
|
cols = table_obj.columns
|
224
224
|
|
225
|
-
oncreated.call(:
|
225
|
+
oncreated.call(db: db, table: table_obj) if oncreated
|
226
226
|
end
|
227
227
|
end
|
228
228
|
end
|
@@ -231,32 +231,32 @@ class Baza::Revision
|
|
231
231
|
table_data[:columns_remove].each do |column_name, column_data|
|
232
232
|
begin
|
233
233
|
col_obj = table_obj.column(column_name)
|
234
|
-
rescue
|
234
|
+
rescue Baza::Errors::ColumnNotFound => e
|
235
235
|
next
|
236
236
|
end
|
237
237
|
|
238
|
-
column_data[:callback].call if column_data.is_a?(Hash)
|
238
|
+
column_data[:callback].call if column_data.is_a?(Hash) && column_data[:callback]
|
239
239
|
col_obj.drop
|
240
240
|
end
|
241
241
|
end
|
242
242
|
|
243
243
|
if table_data[:indexes]
|
244
244
|
table_data[:indexes].each do |index_data|
|
245
|
-
if index_data.is_a?(String)
|
246
|
-
index_data = {:
|
245
|
+
if index_data.is_a?(String) || index_data.is_a?(Symbol)
|
246
|
+
index_data = {name: index_data, columns: [index_data]}
|
247
247
|
end
|
248
248
|
|
249
249
|
begin
|
250
250
|
index_obj = table_obj.index(index_data[:name])
|
251
251
|
|
252
252
|
rewrite_index = false
|
253
|
-
rewrite_index = true if index_data.key?(:unique)
|
253
|
+
rewrite_index = true if index_data.key?(:unique) && index_data[:unique] != index_obj.unique?
|
254
254
|
|
255
255
|
if rewrite_index
|
256
256
|
index_obj.drop
|
257
257
|
table_obj.create_indexes([index_data])
|
258
258
|
end
|
259
|
-
rescue
|
259
|
+
rescue Baza::Errors::IndexNotFound => e
|
260
260
|
table_obj.create_indexes([index_data])
|
261
261
|
end
|
262
262
|
end
|
@@ -266,11 +266,11 @@ class Baza::Revision
|
|
266
266
|
table_data[:indexes_remove].each do |index_name, index_data|
|
267
267
|
begin
|
268
268
|
index_obj = table_obj.index(index_name)
|
269
|
-
rescue
|
269
|
+
rescue Baza::Errors::IndexNotFound => e
|
270
270
|
next
|
271
271
|
end
|
272
272
|
|
273
|
-
if index_data.is_a?(Hash)
|
273
|
+
if index_data.is_a?(Hash) && index_data[:callback]
|
274
274
|
index_data[:callback].call if index_data[:callback]
|
275
275
|
end
|
276
276
|
|
@@ -278,8 +278,8 @@ class Baza::Revision
|
|
278
278
|
end
|
279
279
|
end
|
280
280
|
|
281
|
-
rows_init(:
|
282
|
-
rescue
|
281
|
+
rows_init(db: db, table: table_obj, rows: table_data[:rows]) if table_data && table_data[:rows]
|
282
|
+
rescue Baza::Errors::TableNotFound => e
|
283
283
|
puts "Table did not exist: '#{table_name}'." if args[:debug]
|
284
284
|
|
285
285
|
if table_data.key?(:renames)
|
@@ -289,19 +289,19 @@ class Baza::Revision
|
|
289
289
|
table_rename = db.tables[table_name_rename.to_sym]
|
290
290
|
table_rename.rename(table_name)
|
291
291
|
raise Baza::Errors::Retry
|
292
|
-
rescue
|
292
|
+
rescue Baza::Errors::TableNotFound
|
293
293
|
next
|
294
294
|
end
|
295
295
|
end
|
296
296
|
end
|
297
297
|
|
298
|
-
|
298
|
+
unless table_data.key?(:columns)
|
299
299
|
puts "Notice: Skipping creation of '#{table_name}' because no columns were given in hash." if args[:debug]
|
300
300
|
next
|
301
301
|
end
|
302
302
|
|
303
303
|
if table_data[:on_create]
|
304
|
-
table_data[:on_create].call(:
|
304
|
+
table_data[:on_create].call(db: db, table_name: table_name, table_data: table_data)
|
305
305
|
end
|
306
306
|
|
307
307
|
table_data_create = table_data.clone
|
@@ -312,10 +312,10 @@ class Baza::Revision
|
|
312
312
|
table_obj = db.tables[table_name.to_sym]
|
313
313
|
|
314
314
|
if table_data[:on_create_after]
|
315
|
-
table_data[:on_create_after].call(:
|
315
|
+
table_data[:on_create_after].call(db: db, table_name: table_name, table_data: table_data)
|
316
316
|
end
|
317
317
|
|
318
|
-
rows_init(:
|
318
|
+
rows_init(db: db, table: table_obj, rows: table_data[:rows]) if table_data[:rows]
|
319
319
|
end
|
320
320
|
rescue Baza::Errors::Retry
|
321
321
|
retry
|
@@ -326,34 +326,34 @@ class Baza::Revision
|
|
326
326
|
schema[:tables_remove].each do |table_name, table_data|
|
327
327
|
begin
|
328
328
|
table_obj = db.tables[table_name.to_sym]
|
329
|
-
table_data[:callback].call(:
|
329
|
+
table_data[:callback].call(db: db, table: table_obj) if table_data.is_a?(Hash) && table_data[:callback]
|
330
330
|
table_obj.drop
|
331
|
-
rescue
|
331
|
+
rescue Baza::Errors::TableNotFound => e
|
332
332
|
next
|
333
333
|
end
|
334
334
|
end
|
335
335
|
end
|
336
336
|
|
337
337
|
|
338
|
-
#Free cache.
|
338
|
+
# Free cache.
|
339
339
|
tables.clear if tables
|
340
340
|
tables = nil
|
341
341
|
end
|
342
342
|
|
343
|
-
|
343
|
+
private
|
344
344
|
|
345
345
|
ROWS_INIT_ALLOWED_ARGS = [:db, :table, :rows]
|
346
|
-
#This method checks if certain rows are present in a table based on a hash.
|
346
|
+
# This method checks if certain rows are present in a table based on a hash.
|
347
347
|
def rows_init(args)
|
348
|
-
args.each do |key,
|
348
|
+
args.each do |key, _val|
|
349
349
|
raise "Invalid key: '#{key}' (#{key.class.name})." unless ROWS_INIT_ALLOWED_ARGS.include?(key)
|
350
350
|
end
|
351
351
|
|
352
352
|
db = args[:db]
|
353
353
|
table = args[:table]
|
354
354
|
|
355
|
-
raise "No db given."
|
356
|
-
raise "No table given."
|
355
|
+
raise "No db given." unless db
|
356
|
+
raise "No table given." unless table
|
357
357
|
|
358
358
|
args[:rows].each do |row_data|
|
359
359
|
if row_data[:find_by]
|
@@ -368,7 +368,7 @@ class Baza::Revision
|
|
368
368
|
args[:db].select(table.name, find_by) do |d_rows|
|
369
369
|
rows_found += 1
|
370
370
|
|
371
|
-
if Knj::ArrayExt.hash_diff?(Knj::ArrayExt.hash_sym(row_data[:data]), Knj::ArrayExt.hash_sym(d_rows),
|
371
|
+
if Knj::ArrayExt.hash_diff?(Knj::ArrayExt.hash_sym(row_data[:data]), Knj::ArrayExt.hash_sym(d_rows), "h2_to_h1" => false)
|
372
372
|
print "Data was not right - updating row: #{JSON.generate(row_data[:data])}\n" if args[:debug]
|
373
373
|
args[:db].update(table.name, row_data[:data], d_rows)
|
374
374
|
end
|
@@ -380,4 +380,4 @@ class Baza::Revision
|
|
380
380
|
end
|
381
381
|
end
|
382
382
|
end
|
383
|
-
end
|
383
|
+
end
|