# HG changeset patch # User Kim Alvefur # Date 1559253028 -7200 # Node ID 7dd0dddd8e0226e7cfd4f0c9f0dabb81ed90369c # Parent 045209b41b3a23027d8d265e9da010a1710be5c2 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. diff -r 045209b41b3a -r 7dd0dddd8e02 util/sql.lua --- a/util/sql.lua Thu May 30 15:16:56 2019 +0200 +++ b/util/sql.lua Thu May 30 23:50:28 2019 +0200 @@ -238,6 +238,9 @@ 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 @@ 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