baza 0.0.20 → 0.0.21
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +84 -0
- data/.rubocop_todo.yml +17 -135
- data/.travis.yml +21 -0
- data/Gemfile +10 -7
- data/Gemfile.lock +39 -44
- data/README.md +61 -3
- data/VERSION +1 -1
- data/baza.gemspec +146 -98
- data/config/best_project_practice_rubocop.yml +8 -0
- data/config/best_project_practice_rubocop_todo.yml +6 -0
- data/lib/baza.rb +8 -12
- data/lib/baza/base_sql_driver.rb +198 -52
- data/lib/baza/cloner.rb +1 -0
- data/lib/baza/column.rb +26 -0
- data/lib/baza/database.rb +19 -3
- data/lib/baza/db.rb +69 -271
- data/lib/baza/driver.rb +1 -6
- data/lib/baza/{drivers → driver}/active_record.rb +65 -21
- data/lib/baza/{drivers → driver}/active_record/columns.rb +0 -0
- data/lib/baza/driver/active_record/commands.rb +10 -0
- data/lib/baza/driver/active_record/databases.rb +10 -0
- data/lib/baza/{drivers → driver}/active_record/indexes.rb +0 -0
- data/lib/baza/{drivers → driver}/active_record/result.rb +3 -1
- data/lib/baza/{drivers → driver}/active_record/tables.rb +0 -0
- data/lib/baza/driver/active_record/users.rb +12 -0
- data/lib/baza/{drivers → driver}/mysql.rb +9 -26
- data/lib/baza/{drivers → driver}/mysql/column.rb +14 -35
- data/lib/baza/{drivers → driver}/mysql/columns.rb +9 -12
- data/lib/baza/driver/mysql/commands.rb +39 -0
- data/lib/baza/driver/mysql/database.rb +64 -0
- data/lib/baza/driver/mysql/databases.rb +63 -0
- data/lib/baza/{drivers → driver}/mysql/index.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql/indexes.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql/result.rb +15 -7
- data/lib/baza/{drivers → driver}/mysql/sqlspecs.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql/table.rb +27 -43
- data/lib/baza/{drivers → driver}/mysql/tables.rb +5 -34
- data/lib/baza/{drivers → driver}/mysql/unbuffered_result.rb +8 -2
- data/lib/baza/driver/mysql/user.rb +22 -0
- data/lib/baza/driver/mysql/users.rb +39 -0
- data/lib/baza/{drivers → driver}/mysql2.rb +19 -49
- data/lib/baza/{drivers → driver}/mysql2/column.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql2/columns.rb +0 -0
- data/lib/baza/driver/mysql2/commands.rb +2 -0
- data/lib/baza/{drivers → driver}/mysql2/database.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql2/databases.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql2/index.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql2/indexes.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql2/result.rb +3 -1
- data/lib/baza/{drivers → driver}/mysql2/table.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql2/tables.rb +0 -0
- data/lib/baza/driver/mysql2/user.rb +2 -0
- data/lib/baza/driver/mysql2/users.rb +2 -0
- data/lib/baza/{drivers → driver}/mysql_java.rb +60 -38
- data/lib/baza/{drivers → driver}/mysql_java/column.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql_java/columns.rb +0 -0
- data/lib/baza/driver/mysql_java/commands.rb +2 -0
- data/lib/baza/driver/mysql_java/database.rb +2 -0
- data/lib/baza/driver/mysql_java/databases.rb +2 -0
- data/lib/baza/{drivers → driver}/mysql_java/index.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql_java/indexes.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql_java/table.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql_java/tables.rb +0 -0
- data/lib/baza/driver/mysql_java/user.rb +2 -0
- data/lib/baza/driver/mysql_java/users.rb +2 -0
- data/lib/baza/driver/pg.rb +80 -0
- data/lib/baza/driver/pg/column.rb +125 -0
- data/lib/baza/driver/pg/columns.rb +37 -0
- data/lib/baza/driver/pg/commands.rb +35 -0
- data/lib/baza/driver/pg/create_index_sql_creator.rb +51 -0
- data/lib/baza/driver/pg/database.rb +89 -0
- data/lib/baza/driver/pg/databases.rb +79 -0
- data/lib/baza/driver/pg/index.rb +35 -0
- data/lib/baza/driver/pg/indexes.rb +5 -0
- data/lib/baza/driver/pg/result.rb +139 -0
- data/lib/baza/driver/pg/table.rb +184 -0
- data/lib/baza/driver/pg/tables.rb +45 -0
- data/lib/baza/{drivers → driver}/sqlite3.rb +6 -24
- data/lib/baza/{drivers → driver}/sqlite3/column.rb +22 -24
- data/lib/baza/{drivers → driver}/sqlite3/columns.rb +6 -6
- data/lib/baza/driver/sqlite3/commands.rb +28 -0
- data/lib/baza/{drivers → driver}/sqlite3/database.rb +0 -0
- data/lib/baza/{drivers → driver}/sqlite3/databases.rb +0 -1
- data/lib/baza/{drivers → driver}/sqlite3/index.rb +0 -0
- data/lib/baza/{drivers → driver}/sqlite3/indexes.rb +0 -0
- data/lib/baza/{drivers → driver}/sqlite3/result.rb +14 -6
- data/lib/baza/{drivers → driver}/sqlite3/sqlspecs.rb +0 -0
- data/lib/baza/{drivers → driver}/sqlite3/table.rb +25 -16
- data/lib/baza/{drivers → driver}/sqlite3/tables.rb +5 -6
- data/lib/baza/{drivers → driver}/sqlite3/unbuffered_result.rb +8 -2
- data/lib/baza/{drivers → driver}/sqlite3_java.rb +13 -23
- data/lib/baza/{drivers → driver}/sqlite3_java/column.rb +0 -0
- data/lib/baza/{drivers → driver}/sqlite3_java/columns.rb +0 -0
- data/lib/baza/driver/sqlite3_java/commands.rb +2 -0
- data/lib/baza/{drivers → driver}/sqlite3_java/database.rb +0 -0
- data/lib/baza/{drivers → driver}/sqlite3_java/index.rb +0 -0
- data/lib/baza/{drivers → driver}/sqlite3_java/indexes.rb +0 -0
- data/lib/baza/{drivers → driver}/sqlite3_java/table.rb +0 -0
- data/lib/baza/{drivers → driver}/sqlite3_java/tables.rb +0 -0
- data/lib/baza/{drivers → driver}/sqlite3_java/unbuffered_result.rb +14 -9
- data/lib/baza/{drivers → driver}/sqlite3_rhodes.rb +6 -24
- data/lib/baza/errors.rb +2 -0
- data/lib/baza/idquery.rb +15 -8
- data/lib/baza/index.rb +7 -0
- data/lib/baza/jdbc_driver.rb +4 -16
- data/lib/baza/jdbc_result.rb +20 -12
- data/lib/baza/mysql_base_driver.rb +7 -7
- data/lib/baza/query_buffer.rb +20 -19
- data/lib/baza/row.rb +16 -16
- data/lib/baza/sql_queries.rb +3 -0
- data/lib/baza/sql_queries/generic_insert.rb +81 -0
- data/lib/baza/sql_queries/generic_update.rb +31 -0
- data/lib/baza/sql_queries/mysql_upsert.rb +52 -0
- data/lib/baza/sql_queries/mysql_upsert_duplicate_key.rb +57 -0
- data/lib/baza/sql_queries/non_atomic_upsert.rb +25 -0
- data/lib/baza/sql_queries/postgres_upsert_duplicate_key.rb +118 -0
- data/lib/baza/sql_queries/select.rb +170 -0
- data/lib/baza/sql_queries/sqlite_upsert_duplicate_key.rb +99 -0
- data/lib/baza/table.rb +35 -8
- data/spec/active_record/models/user.rb +3 -0
- data/spec/{cloner_spec.rb → baza/cloner_spec.rb} +0 -0
- data/spec/drivers/active_record_mysql2_spec.rb +5 -3
- data/spec/drivers/active_record_mysql_spec.rb +2 -1
- data/spec/drivers/active_record_pg_spec.rb +20 -0
- data/spec/drivers/active_record_sqlite3_spec.rb +2 -1
- data/spec/drivers/mysql2_spec.rb +1 -1
- data/spec/drivers/mysql_spec.rb +10 -10
- data/spec/drivers/pg_spec.rb +18 -0
- data/spec/drivers/sqlite3_spec.rb +7 -8
- data/spec/info_active_record_example.rb +1 -1
- data/spec/{info_active_record_mysql2.rb → info_active_record_mysql2_example.rb} +3 -2
- data/spec/info_active_record_mysql2_travis.rb +35 -0
- data/spec/{info_active_record_mysql.rb → info_active_record_mysql_example.rb} +5 -4
- data/spec/info_active_record_mysql_travis.rb +36 -0
- data/spec/info_active_record_pg_example.rb +36 -0
- data/spec/info_active_record_pg_travis.rb +34 -0
- data/spec/info_active_record_sqlite3.rb +1 -1
- data/spec/info_mysql2_example.rb +1 -3
- data/spec/{info_mysql2_shippable.rb → info_mysql2_travis.rb} +2 -4
- data/spec/info_mysql_example.rb +1 -3
- data/spec/{info_mysql_shippable.rb → info_mysql_travis.rb} +2 -4
- data/spec/info_pg_example.rb +22 -0
- data/spec/info_pg_travis.rb +20 -0
- data/spec/info_sqlite3.rb +1 -3
- data/spec/spec_helper.rb +1 -1
- data/spec/support/driver_active_record_collection.rb +62 -0
- data/spec/support/driver_collection.rb +136 -121
- data/spec/support/driver_columns_collection.rb +19 -10
- data/spec/support/driver_databases_collection.rb +23 -1
- data/spec/support/driver_indexes_collection.rb +2 -2
- data/spec/support/driver_tables_collection.rb +24 -4
- data/spec/support/driver_users_collection.rb +53 -0
- metadata +185 -104
- data/lib/baza/drivers/mysql/database.rb +0 -28
- data/lib/baza/drivers/mysql/databases.rb +0 -35
- data/lib/baza/drivers/mysql_java/database.rb +0 -2
- data/lib/baza/drivers/mysql_java/databases.rb +0 -2
- data/lib/baza/model.rb +0 -875
- data/lib/baza/model_custom.rb +0 -155
- data/lib/baza/model_handler.rb +0 -910
- data/lib/baza/model_handler_sqlhelper.rb +0 -484
- data/lib/baza/revision.rb +0 -383
- data/shippable.yml +0 -17
- data/spec/info_active_record_mysql2_shippable.rb +0 -34
- data/spec/info_active_record_mysql_shippable.rb +0 -34
- data/spec/model_handler_spec.rb +0 -431
data/lib/baza/revision.rb
DELETED
@@ -1,383 +0,0 @@
|
|
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
|
-
#
|
3
|
-
#===Examples
|
4
|
-
# db = Baza::Db.new(type: :sqlite3, path: "test_db.sqlite3")
|
5
|
-
# schema = {
|
6
|
-
# "tables" => {
|
7
|
-
# "User" => {
|
8
|
-
# "columns" => [
|
9
|
-
# {"name" => "id", "type" => "int", "autoincr" => true, "primarykey" => true},
|
10
|
-
# {"name" => "name", "type" => "varchar"},
|
11
|
-
# {"name" => "lastname", "type" => "varchar"}
|
12
|
-
# ],
|
13
|
-
# "indexes" => [
|
14
|
-
# "name",
|
15
|
-
# {"name" => "lastname", "columns" => ["lastname"]}
|
16
|
-
# ],
|
17
|
-
# "on_create_after" => proc{|d|
|
18
|
-
# d["db"].insert("User", {"name" => "John", "lastname" => "Doe"})
|
19
|
-
# }
|
20
|
-
# }
|
21
|
-
# }
|
22
|
-
# }
|
23
|
-
#
|
24
|
-
# rev = Baza::Revision.new
|
25
|
-
# rev.init_db("db" => db, "schema" => schema)
|
26
|
-
class Baza::Revision
|
27
|
-
def initialize(args = {})
|
28
|
-
@args = args
|
29
|
-
end
|
30
|
-
|
31
|
-
INIT_DB_ALLOWED_ARGS = [:db, :schema, :tables_cache, :debug]
|
32
|
-
INIT_DB_SCHEMA_ALLOWED_ARGS = [:tables]
|
33
|
-
INIT_DB_TABLE_ALLOWED_ARGS = [:columns, :indexes, :rows, :renames]
|
34
|
-
# This initializes a database-structure and content based on a schema-hash.
|
35
|
-
#===Examples
|
36
|
-
# dbrev = Baza::Revision.new
|
37
|
-
# dbrev.init_db("db" => db_obj, "schema" => schema_hash)
|
38
|
-
def init_db(args)
|
39
|
-
args.each do |key, _val|
|
40
|
-
raise "Invalid key: '#{key}' (#{key.class.name})." unless INIT_DB_ALLOWED_ARGS.include?(key)
|
41
|
-
end
|
42
|
-
|
43
|
-
schema = args[:schema]
|
44
|
-
db = args[:db]
|
45
|
-
raise "No 'db' was given." unless db
|
46
|
-
|
47
|
-
schema.each do |key, _val|
|
48
|
-
raise "Invalid key for schema: '#{key}' (#{key.class.name})." unless INIT_DB_SCHEMA_ALLOWED_ARGS.include?(key)
|
49
|
-
end
|
50
|
-
|
51
|
-
# Check for normal bugs and raise apropiate error.
|
52
|
-
raise "'schema' argument was not a Hash: '#{schema.class.name}'." unless schema.is_a?(Hash)
|
53
|
-
raise "No tables given." unless schema.key?(:tables)
|
54
|
-
|
55
|
-
# Cache tables to avoid constant reloading.
|
56
|
-
if !args.key?(:tables_cache) || args[:tables_cache]
|
57
|
-
puts "Caching tables-list." if args[:debug]
|
58
|
-
tables = db.tables.list
|
59
|
-
else
|
60
|
-
puts "Skipping tables-cache." if args[:debug]
|
61
|
-
end
|
62
|
-
|
63
|
-
schema[:tables].each do |table_name, table_data|
|
64
|
-
table_data.each do |key, _val|
|
65
|
-
raise "Invalid key: '#{key}' (#{key.class.name})." unless INIT_DB_TABLE_ALLOWED_ARGS.include?(key)
|
66
|
-
end
|
67
|
-
|
68
|
-
begin
|
69
|
-
begin
|
70
|
-
table_name = table_name.to_sym
|
71
|
-
|
72
|
-
puts "Getting table-object for table: '#{table_name}'." if args[:debug]
|
73
|
-
table_obj = db.tables[table_name]
|
74
|
-
|
75
|
-
# Cache indexes- and column-objects to avoid constant reloading.
|
76
|
-
cols = table_obj.columns
|
77
|
-
indexes = table_obj.indexes
|
78
|
-
|
79
|
-
if table_data[:columns]
|
80
|
-
first_col = true
|
81
|
-
table_data[:columns].each do |col_data|
|
82
|
-
begin
|
83
|
-
col_name = col_data[:name].to_sym
|
84
|
-
col_obj = table_obj.column(col_name)
|
85
|
-
col_str = "#{table_name}.#{col_obj.name}"
|
86
|
-
type = col_data[:type].to_sym
|
87
|
-
dochange = false
|
88
|
-
|
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
|
-
prev_no = table_data[:columns].index(col_data)
|
93
|
-
if prev_no.nil? && prev_no != 0
|
94
|
-
prev_no -= 1
|
95
|
-
prev_col_data = table_data[:columns][prev_no]
|
96
|
-
col_data[:after] = prev_col_data[:name]
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
actual_after = nil
|
101
|
-
set_next = false
|
102
|
-
cols.each do |_col_name, col_iter|
|
103
|
-
if col_iter.name == col_obj.name
|
104
|
-
break
|
105
|
-
else
|
106
|
-
actual_after = col_iter.name
|
107
|
-
end
|
108
|
-
end
|
109
|
-
|
110
|
-
if actual_after != col_data[:after]
|
111
|
-
print "Changing '#{col_str}' after from '#{actual_after}' to '#{col_data[:after]}'.\n" if args[:debug]
|
112
|
-
dochange = true
|
113
|
-
end
|
114
|
-
end
|
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" && col_obj.type.to_s == "int" && (col_data[:primarykey] || col_data[:autoincr]) && db.int_types.index(col_data[:type].to_s)
|
118
|
-
type = :int
|
119
|
-
end
|
120
|
-
|
121
|
-
if type && col_obj.type.to_s != type
|
122
|
-
print "Type mismatch on #{col_str}: #{col_data[:type]}, #{col_obj.type}\n" if args[:debug]
|
123
|
-
dochange = true
|
124
|
-
end
|
125
|
-
|
126
|
-
if col_data.key?(:primarykey) && col_obj.primarykey? != col_data[:primarykey]
|
127
|
-
print "Primary-key mismatch for #{col_str}: #{col_data[:primarykey]}, #{col_obj.primarykey?}\n" if args[:debug]
|
128
|
-
dochange = true
|
129
|
-
end
|
130
|
-
|
131
|
-
if col_data.key?(:autoincr) && col_obj.autoincr? != col_data[:autoincr]
|
132
|
-
print "Auto-increment mismatch for #{col_str}: #{col_data[:autoincr]}, #{col_obj.autoincr?}\n" if args[:debug]
|
133
|
-
dochange = true
|
134
|
-
end
|
135
|
-
|
136
|
-
if col_data.key?(:maxlength) && col_obj.maxlength.to_s != col_data[:maxlength].to_s
|
137
|
-
print "Maxlength mismatch on #{col_str}: #{col_data[:maxlength]}, #{col_obj.maxlength}\n" if args[:debug]
|
138
|
-
dochange = true
|
139
|
-
end
|
140
|
-
|
141
|
-
if col_data.key?(:null) && col_obj.null?.to_s != col_data[:null].to_s
|
142
|
-
print "Null mismatch on #{col_str}: #{col_data[:null]}, #{col_obj.null?}\n" if args[:debug]
|
143
|
-
dochange = true
|
144
|
-
end
|
145
|
-
|
146
|
-
if col_data.key?(:default) && col_obj.default.to_s != col_data[:default].to_s
|
147
|
-
print "Default mismatch on #{col_str}: #{col_data[:default]}, #{col_obj.default}\n" if args[:debug]
|
148
|
-
dochange = true
|
149
|
-
end
|
150
|
-
|
151
|
-
if col_data.key?(:comment) && col_obj.respond_to?(:comment) && col_obj.comment.to_s != col_data[:comment].to_s
|
152
|
-
print "Comment mismatch on #{col_str}: #{col_data[:comment]}, #{col_obj.comment}\n" if args[:debug]
|
153
|
-
dochange = true
|
154
|
-
end
|
155
|
-
|
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
|
-
if callback_data[:action] == :retry
|
160
|
-
raise Baza::Errors::Retry
|
161
|
-
end
|
162
|
-
end
|
163
|
-
end
|
164
|
-
|
165
|
-
if dochange
|
166
|
-
col_data_change = col_data.clone
|
167
|
-
col_data_change.delete(:renames)
|
168
|
-
|
169
|
-
col_obj.change(col_data_change)
|
170
|
-
|
171
|
-
# Change has been made - update cache.
|
172
|
-
cols = table_obj.columns
|
173
|
-
end
|
174
|
-
|
175
|
-
first_col = false
|
176
|
-
rescue Baza::Errors::ColumnNotFound => e
|
177
|
-
print "Column not found: #{table_obj.name}.#{col_data[:name]}.\n" if args[:debug]
|
178
|
-
|
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
|
-
|
182
|
-
rename_found = false
|
183
|
-
col_data[:renames].each do |col_name_rename|
|
184
|
-
begin
|
185
|
-
col_rename = table_obj.column(col_name_rename)
|
186
|
-
rescue Baza::Errors::ColumnNotFound
|
187
|
-
next
|
188
|
-
end
|
189
|
-
|
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
|
-
end
|
194
|
-
|
195
|
-
col_data_change = col_data.clone
|
196
|
-
col_data_change.delete(:renames)
|
197
|
-
|
198
|
-
col_rename.change(col_data_change)
|
199
|
-
|
200
|
-
# Change has been made - update cache.
|
201
|
-
cols = table_obj.columns
|
202
|
-
|
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
|
-
end
|
206
|
-
|
207
|
-
rename_found = true
|
208
|
-
break
|
209
|
-
end
|
210
|
-
|
211
|
-
retry if rename_found
|
212
|
-
end
|
213
|
-
|
214
|
-
oncreated = col_data[:on_created]
|
215
|
-
col_data.delete(:on_created) if col_data[:oncreated]
|
216
|
-
|
217
|
-
col_data_create = col_data
|
218
|
-
col_data_create.delete(:renames)
|
219
|
-
|
220
|
-
col_obj = table_obj.create_columns([col_data])
|
221
|
-
|
222
|
-
# Change has been made - update cache.
|
223
|
-
cols = table_obj.columns
|
224
|
-
|
225
|
-
oncreated.call(db: db, table: table_obj) if oncreated
|
226
|
-
end
|
227
|
-
end
|
228
|
-
end
|
229
|
-
|
230
|
-
if table_data[:columns_remove]
|
231
|
-
table_data[:columns_remove].each do |column_name, column_data|
|
232
|
-
begin
|
233
|
-
col_obj = table_obj.column(column_name)
|
234
|
-
rescue Baza::Errors::ColumnNotFound => e
|
235
|
-
next
|
236
|
-
end
|
237
|
-
|
238
|
-
column_data[:callback].call if column_data.is_a?(Hash) && column_data[:callback]
|
239
|
-
col_obj.drop
|
240
|
-
end
|
241
|
-
end
|
242
|
-
|
243
|
-
if table_data[:indexes]
|
244
|
-
table_data[:indexes].each do |index_data|
|
245
|
-
if index_data.is_a?(String) || index_data.is_a?(Symbol)
|
246
|
-
index_data = {name: index_data, columns: [index_data]}
|
247
|
-
end
|
248
|
-
|
249
|
-
begin
|
250
|
-
index_obj = table_obj.index(index_data[:name])
|
251
|
-
|
252
|
-
rewrite_index = false
|
253
|
-
rewrite_index = true if index_data.key?(:unique) && index_data[:unique] != index_obj.unique?
|
254
|
-
|
255
|
-
if rewrite_index
|
256
|
-
index_obj.drop
|
257
|
-
table_obj.create_indexes([index_data])
|
258
|
-
end
|
259
|
-
rescue Baza::Errors::IndexNotFound => e
|
260
|
-
table_obj.create_indexes([index_data])
|
261
|
-
end
|
262
|
-
end
|
263
|
-
end
|
264
|
-
|
265
|
-
if table_data[:indexes_remove]
|
266
|
-
table_data[:indexes_remove].each do |index_name, index_data|
|
267
|
-
begin
|
268
|
-
index_obj = table_obj.index(index_name)
|
269
|
-
rescue Baza::Errors::IndexNotFound => e
|
270
|
-
next
|
271
|
-
end
|
272
|
-
|
273
|
-
if index_data.is_a?(Hash) && index_data[:callback]
|
274
|
-
index_data[:callback].call if index_data[:callback]
|
275
|
-
end
|
276
|
-
|
277
|
-
index_obj.drop
|
278
|
-
end
|
279
|
-
end
|
280
|
-
|
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
|
-
puts "Table did not exist: '#{table_name}'." if args[:debug]
|
284
|
-
|
285
|
-
if table_data.key?(:renames)
|
286
|
-
table_data[:renames].each do |table_name_rename|
|
287
|
-
begin
|
288
|
-
puts "Renaming table: '#{table_name_rename}' to '#{table_name}'." if args[:debug]
|
289
|
-
table_rename = db.tables[table_name_rename.to_sym]
|
290
|
-
table_rename.rename(table_name)
|
291
|
-
raise Baza::Errors::Retry
|
292
|
-
rescue Baza::Errors::TableNotFound
|
293
|
-
next
|
294
|
-
end
|
295
|
-
end
|
296
|
-
end
|
297
|
-
|
298
|
-
unless table_data.key?(:columns)
|
299
|
-
puts "Notice: Skipping creation of '#{table_name}' because no columns were given in hash." if args[:debug]
|
300
|
-
next
|
301
|
-
end
|
302
|
-
|
303
|
-
if table_data[:on_create]
|
304
|
-
table_data[:on_create].call(db: db, table_name: table_name, table_data: table_data)
|
305
|
-
end
|
306
|
-
|
307
|
-
table_data_create = table_data.clone
|
308
|
-
table_data_create.delete(:rows)
|
309
|
-
|
310
|
-
puts "Creating table: '#{table_name}'." if args[:debug]
|
311
|
-
db.tables.create(table_name, table_data_create)
|
312
|
-
table_obj = db.tables[table_name.to_sym]
|
313
|
-
|
314
|
-
if table_data[:on_create_after]
|
315
|
-
table_data[:on_create_after].call(db: db, table_name: table_name, table_data: table_data)
|
316
|
-
end
|
317
|
-
|
318
|
-
rows_init(db: db, table: table_obj, rows: table_data[:rows]) if table_data[:rows]
|
319
|
-
end
|
320
|
-
rescue Baza::Errors::Retry
|
321
|
-
retry
|
322
|
-
end
|
323
|
-
end
|
324
|
-
|
325
|
-
if schema[:tables_remove]
|
326
|
-
schema[:tables_remove].each do |table_name, table_data|
|
327
|
-
begin
|
328
|
-
table_obj = db.tables[table_name.to_sym]
|
329
|
-
table_data[:callback].call(db: db, table: table_obj) if table_data.is_a?(Hash) && table_data[:callback]
|
330
|
-
table_obj.drop
|
331
|
-
rescue Baza::Errors::TableNotFound => e
|
332
|
-
next
|
333
|
-
end
|
334
|
-
end
|
335
|
-
end
|
336
|
-
|
337
|
-
|
338
|
-
# Free cache.
|
339
|
-
tables.clear if tables
|
340
|
-
tables = nil
|
341
|
-
end
|
342
|
-
|
343
|
-
private
|
344
|
-
|
345
|
-
ROWS_INIT_ALLOWED_ARGS = [:db, :table, :rows]
|
346
|
-
# This method checks if certain rows are present in a table based on a hash.
|
347
|
-
def rows_init(args)
|
348
|
-
args.each do |key, _val|
|
349
|
-
raise "Invalid key: '#{key}' (#{key.class.name})." unless ROWS_INIT_ALLOWED_ARGS.include?(key)
|
350
|
-
end
|
351
|
-
|
352
|
-
db = args[:db]
|
353
|
-
table = args[:table]
|
354
|
-
|
355
|
-
raise "No db given." unless db
|
356
|
-
raise "No table given." unless table
|
357
|
-
|
358
|
-
args[:rows].each do |row_data|
|
359
|
-
if row_data[:find_by]
|
360
|
-
find_by = row_data[:find_by]
|
361
|
-
elsif row_data[:data]
|
362
|
-
find_by = row_data[:data]
|
363
|
-
else
|
364
|
-
raise "Could not figure out the find-by."
|
365
|
-
end
|
366
|
-
|
367
|
-
rows_found = 0
|
368
|
-
args[:db].select(table.name, find_by) do |d_rows|
|
369
|
-
rows_found += 1
|
370
|
-
|
371
|
-
if Knj::ArrayExt.hash_diff?(Knj::ArrayExt.hash_sym(row_data[:data]), Knj::ArrayExt.hash_sym(d_rows), "h2_to_h1" => false)
|
372
|
-
print "Data was not right - updating row: #{JSON.generate(row_data[:data])}\n" if args[:debug]
|
373
|
-
args[:db].update(table.name, row_data[:data], d_rows)
|
374
|
-
end
|
375
|
-
end
|
376
|
-
|
377
|
-
if rows_found == 0
|
378
|
-
print "Inserting row: #{JSON.generate(row_data[:data])}\n" if args[:debug]
|
379
|
-
table.insert(row_data[:data])
|
380
|
-
end
|
381
|
-
end
|
382
|
-
end
|
383
|
-
end
|
data/shippable.yml
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
language: ruby
|
2
|
-
rvm:
|
3
|
-
- ruby-1.9.3
|
4
|
-
- ruby-2.1.2
|
5
|
-
- jruby-head
|
6
|
-
before_script:
|
7
|
-
- sudo apt-get -f install libmysql-java
|
8
|
-
- cp spec/info_mysql_shippable.rb spec/info_mysql.rb
|
9
|
-
- cp spec/info_mysql2_shippable.rb spec/info_mysql2.rb
|
10
|
-
- cp spec/info_active_record_shippable_mysql.rb spec/info_active_record_mysql.rb
|
11
|
-
- cp spec/info_active_record_shippable_mysql2.rb spec/info_active_record_mysql2.rb
|
12
|
-
- mysql -e 'CREATE DATABASE baza;'
|
13
|
-
script:
|
14
|
-
- CODECLIMATE_REPO_TOKEN=98c06de7271764c9cd7bbde7cee4ddfa0d3e7db4bd9933f17779100276672e5f bundle exec rspec
|
15
|
-
- bundle exec rake best_practice_project:run
|
16
|
-
notifications:
|
17
|
-
email: false
|
@@ -1,34 +0,0 @@
|
|
1
|
-
class Baza::InfoActiveRecordMysql2
|
2
|
-
attr_reader :db
|
3
|
-
|
4
|
-
def self.connection
|
5
|
-
require "active_record"
|
6
|
-
|
7
|
-
conn_pool ||= ::ActiveRecord::Base.establish_connection(
|
8
|
-
adapter: "mysql2",
|
9
|
-
host: "localhost",
|
10
|
-
database: "baza",
|
11
|
-
username: "shippa"
|
12
|
-
)
|
13
|
-
conn = conn_pool.connection
|
14
|
-
|
15
|
-
{pool: conn_pool, conn: conn}
|
16
|
-
end
|
17
|
-
|
18
|
-
def initialize
|
19
|
-
data = Baza::InfoActiveRecord.connection
|
20
|
-
|
21
|
-
@db = Baza::Db.new(
|
22
|
-
type: :active_record,
|
23
|
-
conn: data[:conn]
|
24
|
-
)
|
25
|
-
end
|
26
|
-
|
27
|
-
def before
|
28
|
-
@db.tables.list(&:drop)
|
29
|
-
end
|
30
|
-
|
31
|
-
def after
|
32
|
-
@db.close
|
33
|
-
end
|
34
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
class Baza::InfoActiveRecordMysql
|
2
|
-
attr_reader :db
|
3
|
-
|
4
|
-
def self.connection
|
5
|
-
require "active_record"
|
6
|
-
|
7
|
-
conn_pool ||= ::ActiveRecord::Base.establish_connection(
|
8
|
-
adapter: "mysql",
|
9
|
-
host: "localhost",
|
10
|
-
database: "baza",
|
11
|
-
username: "shippa"
|
12
|
-
)
|
13
|
-
conn = conn_pool.connection
|
14
|
-
|
15
|
-
{pool: conn_pool, conn: conn}
|
16
|
-
end
|
17
|
-
|
18
|
-
def initialize
|
19
|
-
data = Baza::InfoActiveRecord.connection
|
20
|
-
|
21
|
-
@db = Baza::Db.new(
|
22
|
-
type: :active_record,
|
23
|
-
conn: data[:conn]
|
24
|
-
)
|
25
|
-
end
|
26
|
-
|
27
|
-
def before
|
28
|
-
@db.tables.list(&:drop)
|
29
|
-
end
|
30
|
-
|
31
|
-
def after
|
32
|
-
@db.close
|
33
|
-
end
|
34
|
-
end
|