aboutsummaryrefslogtreecommitdiffstats
path: root/util/sql.lua
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2019-05-30 23:50:28 +0200
committerKim Alvefur <zash@zash.se>2019-05-30 23:50:28 +0200
commit1cb7ec78b27ef93ae4256f5df6f7046c34a944aa (patch)
treed0ed5a6abf5647a948acc7bc34f6af37806115d6 /util/sql.lua
parentc0263dfb30f2a59c096a0ae70664a219d3a756c4 (diff)
downloadprosody-1cb7ec78b27ef93ae4256f5df6f7046c34a944aa.tar.gz
prosody-1cb7ec78b27ef93ae4256f5df6f7046c34a944aa.zip
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.
Diffstat (limited to 'util/sql.lua')
-rw-r--r--util/sql.lua6
1 files changed, 6 insertions, 0 deletions
diff --git a/util/sql.lua b/util/sql.lua
index 47900102..00c7b57f 100644
--- a/util/sql.lua
+++ b/util/sql.lua
@@ -238,6 +238,9 @@ function engine:transaction(...)
end
function engine:_create_index(index)
local sql = "CREATE INDEX \""..index.name.."\" ON \""..index.table.."\" (";
+ if self.params.driver ~= "MySQL" then
+ sql = sql:gsub("^CREATE INDEX", "%1 IF NOT EXISTS");
+ end
for i=1,#index do
sql = sql.."\""..index[i].."\"";
if i ~= #index then sql = sql..", "; end
@@ -256,6 +259,9 @@ function engine:_create_index(index)
end
function engine:_create_table(table)
local sql = "CREATE TABLE \""..table.name.."\" (";
+ do
+ sql = sql:gsub("^CREATE TABLE", "%1 IF NOT EXISTS");
+ end
for i,col in ipairs(table.c) do
local col_type = col.type;
if col_type == "MEDIUMTEXT" and self.params.driver ~= "MySQL" then