Software /
code /
prosody
Comparison
util/sql.lua @ 10038:7dd0dddd8e02 0.11
util.sql: Ignore if tables and indices already exist on creation (fixes #1064)
Tested with SQLite3 3.16.2 and 3.27.2 and Postgres 11.
MySQL does not support IF NOT EXISTS for indices so not handled here.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 30 May 2019 23:50:28 +0200 |
parent | 9616:61376a3c0c1d |
child | 10109:c59d384b0959 |
comparison
equal
deleted
inserted
replaced
10036:045209b41b3a | 10038:7dd0dddd8e02 |
---|---|
236 end | 236 end |
237 return ok, ret; | 237 return ok, ret; |
238 end | 238 end |
239 function engine:_create_index(index) | 239 function engine:_create_index(index) |
240 local sql = "CREATE INDEX \""..index.name.."\" ON \""..index.table.."\" ("; | 240 local sql = "CREATE INDEX \""..index.name.."\" ON \""..index.table.."\" ("; |
241 if self.params.driver ~= "MySQL" then | |
242 sql = sql:gsub("^CREATE INDEX", "%1 IF NOT EXISTS"); | |
243 end | |
241 for i=1,#index do | 244 for i=1,#index do |
242 sql = sql.."\""..index[i].."\""; | 245 sql = sql.."\""..index[i].."\""; |
243 if i ~= #index then sql = sql..", "; end | 246 if i ~= #index then sql = sql..", "; end |
244 end | 247 end |
245 sql = sql..");" | 248 sql = sql..");" |
254 end | 257 end |
255 return self:execute(sql); | 258 return self:execute(sql); |
256 end | 259 end |
257 function engine:_create_table(table) | 260 function engine:_create_table(table) |
258 local sql = "CREATE TABLE \""..table.name.."\" ("; | 261 local sql = "CREATE TABLE \""..table.name.."\" ("; |
262 do | |
263 sql = sql:gsub("^CREATE TABLE", "%1 IF NOT EXISTS"); | |
264 end | |
259 for i,col in ipairs(table.c) do | 265 for i,col in ipairs(table.c) do |
260 local col_type = col.type; | 266 local col_type = col.type; |
261 if col_type == "MEDIUMTEXT" and self.params.driver ~= "MySQL" then | 267 if col_type == "MEDIUMTEXT" and self.params.driver ~= "MySQL" then |
262 col_type = "TEXT"; -- MEDIUMTEXT is MySQL-specific | 268 col_type = "TEXT"; -- MEDIUMTEXT is MySQL-specific |
263 end | 269 end |