baza 0.0.35 → 0.0.36
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/Gemfile +1 -1
- data/Gemfile.lock +14 -17
- data/VERSION +1 -1
- data/baza.gemspec +8 -7
- data/lib/baza/base_sql_driver.rb +38 -8
- data/lib/baza/cloner.rb +10 -0
- data/lib/baza/commands/select.rb +15 -8
- data/lib/baza/db.rb +8 -4
- data/lib/baza/driver/active_record.rb +7 -0
- data/lib/baza/driver/mysql/column.rb +4 -6
- data/lib/baza/driver/mysql/columns.rb +4 -4
- data/lib/baza/driver/mysql/database.rb +4 -4
- data/lib/baza/driver/mysql/databases.rb +3 -3
- data/lib/baza/driver/mysql/foreign_key.rb +2 -2
- data/lib/baza/driver/mysql/index.rb +1 -1
- data/lib/baza/driver/mysql/sql/column.rb +4 -4
- data/lib/baza/driver/mysql/sql/create_indexes.rb +3 -3
- data/lib/baza/driver/mysql/sql/create_table.rb +1 -1
- data/lib/baza/driver/mysql/table.rb +16 -16
- data/lib/baza/driver/mysql_java.rb +3 -3
- data/lib/baza/driver/pg/column.rb +5 -13
- data/lib/baza/driver/pg/columns.rb +4 -4
- data/lib/baza/driver/pg/create_index_sql_creator.rb +3 -3
- data/lib/baza/driver/pg/database.rb +4 -4
- data/lib/baza/driver/pg/databases.rb +1 -1
- data/lib/baza/driver/pg/foreign_key.rb +2 -2
- data/lib/baza/driver/pg/index.rb +1 -1
- data/lib/baza/driver/pg/table.rb +9 -9
- data/lib/baza/driver/pg/tables.rb +1 -1
- data/lib/baza/driver/sqlite3/column.rb +1 -1
- data/lib/baza/driver/sqlite3/columns.rb +1 -1
- data/lib/baza/driver/sqlite3/index.rb +1 -1
- data/lib/baza/driver/sqlite3/table.rb +4 -4
- data/lib/baza/driver/tiny.rb +83 -8
- data/lib/baza/driver/tiny/result.rb +18 -2
- data/lib/baza/idquery.rb +3 -3
- data/lib/baza/mysql_base_driver.rb +3 -3
- data/lib/baza/sql_queries/generic_insert.rb +3 -3
- data/lib/baza/sql_queries/generic_update.rb +2 -2
- data/lib/baza/sql_queries/mysql_upsert.rb +5 -5
- data/lib/baza/sql_queries/mysql_upsert_duplicate_key.rb +3 -3
- data/lib/baza/sql_queries/postgres_upsert_duplicate_key.rb +7 -7
- data/lib/baza/sql_queries/select.rb +8 -8
- data/lib/baza/sql_queries/sqlite_upsert_duplicate_key.rb +6 -6
- data/lib/baza/table.rb +3 -3
- data/spec/baza/cloner_spec.rb +2 -2
- data/spec/drivers/tiny_spec.rb +27 -0
- data/spec/info_active_record_pg_peak_flow.rb +2 -2
- data/spec/info_active_record_sqlite3.rb +6 -13
- data/spec/info_sqlite3.rb +3 -12
- metadata +8 -7
@@ -5,7 +5,7 @@ class Baza::Driver::Mysql::Database < Baza::Database
|
|
5
5
|
end
|
6
6
|
|
7
7
|
def drop
|
8
|
-
sql = "DROP DATABASE
|
8
|
+
sql = "DROP DATABASE #{@db.quote_database(name)}"
|
9
9
|
@db.query(sql)
|
10
10
|
self
|
11
11
|
end
|
@@ -17,7 +17,7 @@ class Baza::Driver::Mysql::Database < Baza::Database
|
|
17
17
|
|
18
18
|
sql = "CREATE"
|
19
19
|
sql << " TEMPORARY" if data[:temp]
|
20
|
-
sql << " TABLE #{
|
20
|
+
sql << " TABLE #{@db.quote_table(name)} ("
|
21
21
|
|
22
22
|
first = true
|
23
23
|
data[:columns].each do |col_data|
|
@@ -66,10 +66,10 @@ private
|
|
66
66
|
@db.databases.create(name: new_name)
|
67
67
|
|
68
68
|
tables.each do |table|
|
69
|
-
@db.query("ALTER TABLE
|
69
|
+
@db.query("ALTER TABLE #{@db.quote_database(name_was)}.#{@db.quote_table(table.name)} RENAME #{@db.quote_database(name)}.#{@db.quote_table(table.name)}")
|
70
70
|
end
|
71
71
|
|
72
|
-
@db.query("DROP DATABASE
|
72
|
+
@db.query("DROP DATABASE #{@db.quote_database(name_was)}")
|
73
73
|
|
74
74
|
@name = new_name
|
75
75
|
@name_was = new_name
|
@@ -6,7 +6,7 @@ class Baza::Driver::Mysql::Databases
|
|
6
6
|
def create(args)
|
7
7
|
sql = "CREATE DATABASE"
|
8
8
|
sql << " IF NOT EXISTS" if args[:if_not_exists]
|
9
|
-
sql << " #{@db.
|
9
|
+
sql << " #{@db.quote_table(args.fetch(:name))}"
|
10
10
|
|
11
11
|
@db.query(sql)
|
12
12
|
true
|
@@ -40,13 +40,13 @@ class Baza::Driver::Mysql::Databases
|
|
40
40
|
end
|
41
41
|
|
42
42
|
previous_db_name = @db.current_database_name
|
43
|
-
@db.query("USE #{@db.
|
43
|
+
@db.query("USE #{@db.quote_database(name)}")
|
44
44
|
|
45
45
|
if block_given?
|
46
46
|
begin
|
47
47
|
yield
|
48
48
|
ensure
|
49
|
-
@db.query("USE #{@db.
|
49
|
+
@db.query("USE #{@db.quote_database(previous_db_name)}")
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
@@ -11,8 +11,8 @@ class Baza::Driver::Mysql::ForeignKey < Baza::ForeignKey
|
|
11
11
|
|
12
12
|
def drop
|
13
13
|
@db.query("
|
14
|
-
ALTER TABLE
|
15
|
-
DROP FOREIGN KEY
|
14
|
+
ALTER TABLE #{@db.quote_table(table_name)}
|
15
|
+
DROP FOREIGN KEY #{@db.quote_table(name)}
|
16
16
|
")
|
17
17
|
true
|
18
18
|
end
|
@@ -70,7 +70,7 @@ class Baza::Driver::Mysql::Index < Baza::Index
|
|
70
70
|
end
|
71
71
|
|
72
72
|
def reload
|
73
|
-
data = @db.query("SHOW INDEX FROM
|
73
|
+
data = @db.query("SHOW INDEX FROM #{@db.quote_table(@table_name)} WHERE #{@db.quote_column("Key_name")} = #{@db.quote_value(name)}").fetch
|
74
74
|
raise Baza::Errors::IndexNotFound unless data
|
75
75
|
@data = data
|
76
76
|
self
|
@@ -17,7 +17,7 @@ class Baza::Driver::Mysql::Sql::Column
|
|
17
17
|
|
18
18
|
data[:maxlength] = 255 if type == :varchar && data[:maxlength].to_s.strip.empty?
|
19
19
|
|
20
|
-
sql = "#{Baza::Driver::Mysql
|
20
|
+
sql = "#{Baza::Driver::Mysql.quote_column(data.fetch(:name))} #{type}"
|
21
21
|
sql << "(#{data[:maxlength]})" if data[:maxlength]
|
22
22
|
sql << " PRIMARY KEY" if data[:primarykey]
|
23
23
|
sql << " AUTO_INCREMENT" if data[:autoincr]
|
@@ -26,11 +26,11 @@ class Baza::Driver::Mysql::Sql::Column
|
|
26
26
|
if data.key?(:default_func)
|
27
27
|
sql << " DEFAULT #{data[:default_func]}"
|
28
28
|
elsif data.key?(:default) && !data[:default].nil?
|
29
|
-
sql << " DEFAULT #{Baza::Driver::Mysql.
|
29
|
+
sql << " DEFAULT #{Baza::Driver::Mysql.quote_value(data.fetch(:default))}"
|
30
30
|
end
|
31
31
|
|
32
|
-
sql << " COMMENT
|
33
|
-
sql << " AFTER #{Baza::Driver::Mysql
|
32
|
+
sql << " COMMENT #{Baza::Driver::Mysql.quote_value(data.fetch(:comment))}" if data.key?(:comment)
|
33
|
+
sql << " AFTER #{Baza::Driver::Mysql.quote_column(data.fetch(:after))}" if data[:after] && !data[:first]
|
34
34
|
sql << " FIRST" if data[:first]
|
35
35
|
sql << " STORAGE #{data[:storage].to_s.upcase}" if data[:storage]
|
36
36
|
|
@@ -27,10 +27,10 @@ class Baza::Driver::Mysql::Sql::CreateIndexes
|
|
27
27
|
end
|
28
28
|
|
29
29
|
sql << " UNIQUE" if index_data[:unique]
|
30
|
-
sql << " INDEX #{Baza::Driver::Mysql
|
30
|
+
sql << " INDEX #{Baza::Driver::Mysql.quote_index(index_data.fetch(:name))}"
|
31
31
|
|
32
32
|
if @on_table || @on_table.nil?
|
33
|
-
sql << " ON #{Baza::Driver::Mysql
|
33
|
+
sql << " ON #{Baza::Driver::Mysql.quote_table(@table_name)}"
|
34
34
|
end
|
35
35
|
|
36
36
|
sql << " ("
|
@@ -40,7 +40,7 @@ class Baza::Driver::Mysql::Sql::CreateIndexes
|
|
40
40
|
sql << ", " unless first
|
41
41
|
first = false if first
|
42
42
|
|
43
|
-
sql <<
|
43
|
+
sql << Baza::Driver::Mysql.quote_column(col_name)
|
44
44
|
end
|
45
45
|
|
46
46
|
sql << ")"
|
@@ -9,7 +9,7 @@ class Baza::Driver::Mysql::Sql::CreateTable
|
|
9
9
|
def sql
|
10
10
|
sql = "CREATE"
|
11
11
|
sql << " TEMPORARY" if @temporary
|
12
|
-
sql << " TABLE #{Baza::Driver::Mysql
|
12
|
+
sql << " TABLE #{Baza::Driver::Mysql.quote_table(@name)} ("
|
13
13
|
|
14
14
|
first = true
|
15
15
|
@columns.each do |col_data|
|
@@ -30,7 +30,7 @@ class Baza::Driver::Mysql::Table < Baza::Table
|
|
30
30
|
raise "Cant drop native table: '#{name}'" if native?
|
31
31
|
|
32
32
|
@db.with_database(database_name) do
|
33
|
-
@db.query("DROP TABLE
|
33
|
+
@db.query("DROP TABLE #{@db.quote_table(name)}")
|
34
34
|
end
|
35
35
|
|
36
36
|
@tables.__send__(:remove_from_list, self)
|
@@ -45,7 +45,7 @@ class Baza::Driver::Mysql::Table < Baza::Table
|
|
45
45
|
end
|
46
46
|
|
47
47
|
def optimize
|
48
|
-
@db.query("OPTIMIZE TABLE
|
48
|
+
@db.query("OPTIMIZE TABLE #{@db.quote_table(name)}")
|
49
49
|
self
|
50
50
|
end
|
51
51
|
|
@@ -65,7 +65,7 @@ class Baza::Driver::Mysql::Table < Baza::Table
|
|
65
65
|
def columns(args = nil)
|
66
66
|
@db.columns
|
67
67
|
ret = []
|
68
|
-
sql = "SHOW FULL COLUMNS FROM
|
68
|
+
sql = "SHOW FULL COLUMNS FROM #{@db.quote_table(name)}"
|
69
69
|
sql << " WHERE `Field` = '#{@db.esc(args.fetch(:name))}'" if args && args.key?(:name)
|
70
70
|
|
71
71
|
@db.q(sql) do |d_cols|
|
@@ -108,11 +108,11 @@ class Baza::Driver::Mysql::Table < Baza::Table
|
|
108
108
|
INFORMATION_SCHEMA.KEY_COLUMN_USAGE
|
109
109
|
|
110
110
|
WHERE
|
111
|
-
REFERENCED_TABLE_SCHEMA =
|
112
|
-
TABLE_NAME =
|
111
|
+
REFERENCED_TABLE_SCHEMA = #{@db.quote_value(@db.current_database_name)} AND
|
112
|
+
TABLE_NAME = #{@db.quote_value(name)}
|
113
113
|
"
|
114
114
|
|
115
|
-
sql << " AND CONSTRAINT_NAME =
|
115
|
+
sql << " AND CONSTRAINT_NAME = #{@db.quote_value(args.fetch(:name))}" if args[:name]
|
116
116
|
|
117
117
|
result = [] unless block_given?
|
118
118
|
|
@@ -143,8 +143,8 @@ class Baza::Driver::Mysql::Table < Baza::Table
|
|
143
143
|
def indexes(args = nil, &blk)
|
144
144
|
ret = {}
|
145
145
|
|
146
|
-
sql = "SHOW INDEX FROM
|
147
|
-
sql << " WHERE
|
146
|
+
sql = "SHOW INDEX FROM #{@db.quote_table(name)}"
|
147
|
+
sql << " WHERE #{@db.quote_column("Key_name")} = #{@db.quote_value(args.fetch(:name))}" if args && args.key?(:name)
|
148
148
|
|
149
149
|
@db.query(sql) do |d_indexes|
|
150
150
|
next if d_indexes[:Key_name] == "PRIMARY"
|
@@ -218,10 +218,10 @@ class Baza::Driver::Mysql::Table < Baza::Table
|
|
218
218
|
end
|
219
219
|
|
220
220
|
sql << " UNIQUE" if index_data[:unique]
|
221
|
-
sql << " INDEX #{db.
|
221
|
+
sql << " INDEX #{db.quote_index(index_data.fetch(:name))}"
|
222
222
|
|
223
223
|
if args[:on_table] || !args.key?(:on_table)
|
224
|
-
sql << " ON #{db.
|
224
|
+
sql << " ON #{db.quote_table(args.fetch(:table_name))}"
|
225
225
|
end
|
226
226
|
|
227
227
|
sql << " ("
|
@@ -231,7 +231,7 @@ class Baza::Driver::Mysql::Table < Baza::Table
|
|
231
231
|
sql << ", " unless first
|
232
232
|
first = false if first
|
233
233
|
|
234
|
-
sql <<
|
234
|
+
sql << db.quote_column(col_name)
|
235
235
|
end
|
236
236
|
|
237
237
|
sql << ")"
|
@@ -247,7 +247,7 @@ class Baza::Driver::Mysql::Table < Baza::Table
|
|
247
247
|
oldname = name
|
248
248
|
|
249
249
|
@tables.__send__(:remove_from_list, self)
|
250
|
-
@db.query("ALTER TABLE
|
250
|
+
@db.query("ALTER TABLE #{@db.quote_table(oldname)} RENAME TO #{@db.quote_table(newname)}")
|
251
251
|
|
252
252
|
@data[:name] = newname
|
253
253
|
@name = newname
|
@@ -283,7 +283,7 @@ class Baza::Driver::Mysql::Table < Baza::Table
|
|
283
283
|
def clone(newname, args = {})
|
284
284
|
raise "Invalid name." if newname.to_s.strip.empty?
|
285
285
|
|
286
|
-
sql = "CREATE TABLE #{@db.
|
286
|
+
sql = "CREATE TABLE #{@db.quote_table(newname)} ("
|
287
287
|
first = true
|
288
288
|
pkey_found = false
|
289
289
|
pkeys = []
|
@@ -316,7 +316,7 @@ class Baza::Driver::Mysql::Table < Baza::Table
|
|
316
316
|
pkeys.each do |pkey|
|
317
317
|
sql << ", " unless first
|
318
318
|
first = false if first
|
319
|
-
sql <<
|
319
|
+
sql << @db.quote_column(pkey)
|
320
320
|
end
|
321
321
|
|
322
322
|
sql << ")"
|
@@ -332,7 +332,7 @@ class Baza::Driver::Mysql::Table < Baza::Table
|
|
332
332
|
|
333
333
|
|
334
334
|
# Insert data of previous data in a single query.
|
335
|
-
@db.query("INSERT INTO #{@db.
|
335
|
+
@db.query("INSERT INTO #{@db.quote_table(newname)} SELECT * FROM #{@db.quote_table(name)}")
|
336
336
|
|
337
337
|
|
338
338
|
# Create indexes.
|
@@ -357,7 +357,7 @@ class Baza::Driver::Mysql::Table < Baza::Table
|
|
357
357
|
# Changes the engine for a table.
|
358
358
|
def engine=(newengine)
|
359
359
|
raise "Invalid engine: '#{newengine}'." unless newengine.to_s =~ /^[A-z]+$/
|
360
|
-
@db.query("ALTER TABLE
|
360
|
+
@db.query("ALTER TABLE #{@db.quote_table(name)} ENGINE = #{newengine}") if engine.to_s != newengine.to_s
|
361
361
|
@data[:ENGINE] = newengine
|
362
362
|
end
|
363
363
|
|
@@ -105,7 +105,7 @@ class Baza::Driver::MysqlJava < Baza::JdbcDriver
|
|
105
105
|
keys.each do |col_name|
|
106
106
|
sql << "," unless first
|
107
107
|
first = false if first
|
108
|
-
sql <<
|
108
|
+
sql << quote_column(col_name)
|
109
109
|
end
|
110
110
|
|
111
111
|
sql << ") VALUES ("
|
@@ -127,7 +127,7 @@ class Baza::Driver::MysqlJava < Baza::JdbcDriver
|
|
127
127
|
sql << ","
|
128
128
|
end
|
129
129
|
|
130
|
-
sql << @db.
|
130
|
+
sql << @db.quote_value(val)
|
131
131
|
end
|
132
132
|
else
|
133
133
|
hash.each do |_key, val|
|
@@ -137,7 +137,7 @@ class Baza::Driver::MysqlJava < Baza::JdbcDriver
|
|
137
137
|
sql << ","
|
138
138
|
end
|
139
139
|
|
140
|
-
sql << @db.
|
140
|
+
sql << @db.quote_value(val)
|
141
141
|
end
|
142
142
|
end
|
143
143
|
end
|
@@ -19,7 +19,7 @@ class Baza::Driver::Pg::Column < Baza::Column
|
|
19
19
|
other_table = other_column.table
|
20
20
|
|
21
21
|
sql = "
|
22
|
-
ALTER TABLE #{@db.
|
22
|
+
ALTER TABLE #{@db.quote_table(table_name)}
|
23
23
|
ADD CONSTRAINT #{@db.escape_table(fk_name)}
|
24
24
|
FOREIGN KEY (#{@db.escape_table(name)})
|
25
25
|
REFERENCES #{@db.escape_table(other_table.name)} (#{@db.escape_column(other_column.name)})
|
@@ -66,7 +66,7 @@ class Baza::Driver::Pg::Column < Baza::Column
|
|
66
66
|
end
|
67
67
|
|
68
68
|
def drop
|
69
|
-
@db.query("ALTER TABLE #{@db.
|
69
|
+
@db.query("ALTER TABLE #{@db.quote_table(table_name)} DROP COLUMN #{@db.quote_column(name)}")
|
70
70
|
nil
|
71
71
|
end
|
72
72
|
|
@@ -78,7 +78,7 @@ class Baza::Driver::Pg::Column < Baza::Column
|
|
78
78
|
|
79
79
|
def change(data)
|
80
80
|
if data.key?(:name) && data.fetch(:name).to_s != name
|
81
|
-
@db.query("#{alter_table_sql} RENAME #{
|
81
|
+
@db.query("#{alter_table_sql} RENAME #{@db.quote_column(name)} TO #{@db.quote_column(data.fetch(:name))}")
|
82
82
|
@name = data.fetch(:name).to_s
|
83
83
|
end
|
84
84
|
|
@@ -126,19 +126,11 @@ class Baza::Driver::Pg::Column < Baza::Column
|
|
126
126
|
|
127
127
|
private
|
128
128
|
|
129
|
-
def col_escaped
|
130
|
-
"#{@db.sep_col}#{@db.escape_column(name)}#{@db.sep_col}"
|
131
|
-
end
|
132
|
-
|
133
|
-
def table_escaped
|
134
|
-
"#{@db.sep_table}#{@db.escape_table(table_name)}#{@db.sep_table}"
|
135
|
-
end
|
136
|
-
|
137
129
|
def alter_table_sql
|
138
|
-
"ALTER TABLE #{
|
130
|
+
"ALTER TABLE #{@db.quote_table(table_name)}"
|
139
131
|
end
|
140
132
|
|
141
133
|
def alter_column_sql
|
142
|
-
"#{alter_table_sql} ALTER COLUMN #{
|
134
|
+
"#{alter_table_sql} ALTER COLUMN #{@db.quote_column(name)}"
|
143
135
|
end
|
144
136
|
end
|
@@ -32,7 +32,7 @@ class Baza::Driver::Pg::Columns
|
|
32
32
|
|
33
33
|
data[:maxlength] = 255 if type == :varchar && !data.key?(:maxlength)
|
34
34
|
|
35
|
-
sql = "#{@db.
|
35
|
+
sql = "#{@db.quote_column(data.fetch(:name))} #{type}"
|
36
36
|
sql << "(#{maxlength})" if maxlength
|
37
37
|
sql << " PRIMARY KEY" if data[:primarykey]
|
38
38
|
sql << " NOT NULL" if data.key?(:null) && !data[:null]
|
@@ -40,11 +40,11 @@ class Baza::Driver::Pg::Columns
|
|
40
40
|
if data.key?(:default_func)
|
41
41
|
sql << " DEFAULT #{data[:default_func]}"
|
42
42
|
elsif data.key?(:default) && data[:default]
|
43
|
-
sql << " DEFAULT #{@db.
|
43
|
+
sql << " DEFAULT #{@db.quote_value(data.fetch(:default))}"
|
44
44
|
end
|
45
45
|
|
46
|
-
sql << " COMMENT
|
47
|
-
sql << " AFTER #{@db.
|
46
|
+
sql << " COMMENT #{@db.quote_value(data.fetch(:comment))}" if data.key?(:comment)
|
47
|
+
sql << " AFTER #{@db.quote_column(data.fetch(:after))}" if data[:after] && !data[:first]
|
48
48
|
sql << " FIRST" if data[:first]
|
49
49
|
sql << " STORAGE #{data[:storage].to_s.upcase}" if data[:storage]
|
50
50
|
|
@@ -31,10 +31,10 @@ class Baza::Driver::Pg::CreateIndexSqlCreator
|
|
31
31
|
raise "No columns was given on index: '#{index_data.fetch(:name)}'." if !index_data[:columns] || index_data[:columns].empty?
|
32
32
|
|
33
33
|
sql << " UNIQUE" if index_data[:unique]
|
34
|
-
sql << " INDEX #{@db.
|
34
|
+
sql << " INDEX #{@db.quote_index(index_data.fetch(:name))}"
|
35
35
|
|
36
36
|
if args[:on_table] || !args.key?(:on_table)
|
37
|
-
sql << " ON #{@db.
|
37
|
+
sql << " ON #{@db.quote_table(args.fetch(:table_name))}"
|
38
38
|
end
|
39
39
|
|
40
40
|
sql << " ("
|
@@ -43,7 +43,7 @@ class Baza::Driver::Pg::CreateIndexSqlCreator
|
|
43
43
|
index_data.fetch(:columns).each do |col_name|
|
44
44
|
sql << ", " unless first
|
45
45
|
first = false if first
|
46
|
-
sql <<
|
46
|
+
sql << @db.quote_column(col_name)
|
47
47
|
end
|
48
48
|
|
49
49
|
sql << ")"
|
@@ -7,7 +7,7 @@ class Baza::Driver::Pg::Database < Baza::Database
|
|
7
7
|
def drop
|
8
8
|
with_cloned_conn_and_terminated_connections do |cloned_conn|
|
9
9
|
# Drop the database
|
10
|
-
cloned_conn.query("DROP DATABASE #{db.
|
10
|
+
cloned_conn.query("DROP DATABASE #{db.quote_database(name)}")
|
11
11
|
end
|
12
12
|
|
13
13
|
self
|
@@ -65,7 +65,7 @@ private
|
|
65
65
|
|
66
66
|
def rename(new_name)
|
67
67
|
with_cloned_conn_and_terminated_connections do |cloned_conn|
|
68
|
-
cloned_conn.query("ALTER DATABASE #{db.
|
68
|
+
cloned_conn.query("ALTER DATABASE #{db.quote_database(name_was)} RENAME TO #{db.quote_database(name)}")
|
69
69
|
end
|
70
70
|
|
71
71
|
@name = new_name.to_s
|
@@ -78,8 +78,8 @@ private
|
|
78
78
|
# Drop database through a cloned connection, because Postgres might bug up if dropping the current
|
79
79
|
db.clone_conn(db: other_db.name) do |cloned_conn|
|
80
80
|
# Close existing connections to avoid 'is being accessed by other users' errors
|
81
|
-
cloned_conn.query("REVOKE CONNECT ON DATABASE #{db.
|
82
|
-
cloned_conn.query("SELECT pid, pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = #{
|
81
|
+
cloned_conn.query("REVOKE CONNECT ON DATABASE #{db.quote_database(name_was)} FROM public") unless name_changed?
|
82
|
+
cloned_conn.query("SELECT pid, pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = #{@db.quote_value(name_was)} AND pid != pg_backend_pid()")
|
83
83
|
|
84
84
|
yield cloned_conn
|
85
85
|
end
|
@@ -11,8 +11,8 @@ class Baza::Driver::Pg::ForeignKey < Baza::ForeignKey
|
|
11
11
|
|
12
12
|
def drop
|
13
13
|
@db.query("
|
14
|
-
ALTER TABLE #{@db.
|
15
|
-
DROP CONSTRAINT #{@db.
|
14
|
+
ALTER TABLE #{@db.quote_table(table_name)}
|
15
|
+
DROP CONSTRAINT #{@db.quote_table(name)}
|
16
16
|
")
|
17
17
|
true
|
18
18
|
end
|
data/lib/baza/driver/pg/index.rb
CHANGED
@@ -24,7 +24,7 @@ class Baza::Driver::Pg::Index < Baza::Index
|
|
24
24
|
end
|
25
25
|
|
26
26
|
def rename(new_name)
|
27
|
-
@db.query("ALTER INDEX #{@db.
|
27
|
+
@db.query("ALTER INDEX #{@db.quote_index(name)} RENAME TO #{@db.quote_index(new_name)}")
|
28
28
|
@name = new_name.to_s
|
29
29
|
self
|
30
30
|
end
|
data/lib/baza/driver/pg/table.rb
CHANGED
@@ -9,7 +9,7 @@ class Baza::Driver::Pg::Table < Baza::Table
|
|
9
9
|
|
10
10
|
def drop
|
11
11
|
@db.with_database(database_name) do
|
12
|
-
@db.query("DROP TABLE
|
12
|
+
@db.query("DROP TABLE #{@db.quote_table(name)}")
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
@@ -72,7 +72,7 @@ class Baza::Driver::Pg::Table < Baza::Table
|
|
72
72
|
end
|
73
73
|
|
74
74
|
def truncate
|
75
|
-
@db.query("TRUNCATE #{@db.
|
75
|
+
@db.query("TRUNCATE #{@db.quote_table(name)} RESTART IDENTITY")
|
76
76
|
self
|
77
77
|
end
|
78
78
|
|
@@ -164,7 +164,7 @@ class Baza::Driver::Pg::Table < Baza::Table
|
|
164
164
|
end
|
165
165
|
|
166
166
|
def rename(new_name)
|
167
|
-
@db.query("ALTER TABLE #{@db.
|
167
|
+
@db.query("ALTER TABLE #{@db.quote_table(name)} RENAME TO #{@db.quote_table(new_name)}")
|
168
168
|
@name = new_name.to_s
|
169
169
|
self
|
170
170
|
end
|
@@ -184,13 +184,13 @@ class Baza::Driver::Pg::Table < Baza::Table
|
|
184
184
|
|
185
185
|
def rows_count
|
186
186
|
@db.databases.with_database(database_name) do
|
187
|
-
sql = "SELECT COUNT(*) AS count FROM #{@db.
|
187
|
+
sql = "SELECT COUNT(*) AS count FROM #{@db.quote_table(name)}"
|
188
188
|
return @db.query(sql).fetch.fetch(:count).to_i
|
189
189
|
end
|
190
190
|
end
|
191
191
|
|
192
192
|
def optimize
|
193
|
-
@db.query("VACUUM #{@db.
|
193
|
+
@db.query("VACUUM #{@db.quote_table(name)}")
|
194
194
|
self
|
195
195
|
end
|
196
196
|
|
@@ -220,13 +220,13 @@ class Baza::Driver::Pg::Table < Baza::Table
|
|
220
220
|
private
|
221
221
|
|
222
222
|
def clone_insert_from_original_table(newname, columns_list)
|
223
|
-
sql_clone = "INSERT INTO #{@db.
|
223
|
+
sql_clone = "INSERT INTO #{@db.quote_table(newname)} ("
|
224
224
|
|
225
225
|
first = true
|
226
226
|
columns_list.each do |column_data|
|
227
227
|
sql_clone << "," unless first
|
228
228
|
first = false if first
|
229
|
-
sql_clone <<
|
229
|
+
sql_clone << @db.quote_column(column_data.fetch(:name))
|
230
230
|
end
|
231
231
|
|
232
232
|
sql_clone << ") SELECT "
|
@@ -235,10 +235,10 @@ private
|
|
235
235
|
columns_list.each do |column_data|
|
236
236
|
sql_clone << "," unless first
|
237
237
|
first = false if first
|
238
|
-
sql_clone <<
|
238
|
+
sql_clone << @db.quote_column(column_data.fetch(:name))
|
239
239
|
end
|
240
240
|
|
241
|
-
sql_clone << " FROM #{@db.
|
241
|
+
sql_clone << " FROM #{@db.quote_table(name)}"
|
242
242
|
|
243
243
|
@db.query(sql_clone)
|
244
244
|
end
|