Skip to content

Commit

Permalink
Generalize index support to handle constraints, fix #251.
Browse files Browse the repository at this point in the history
PostgreSQL rightfully forbifs DROP INDEX when the index is used to
enforce a constraint, the proper SQL to issue is then ALTER TABLE DROP
CONSTRAINT. Also, in such a case pg_dump issues a single ALTER TABLE ADD
CONSTRAINT statement to restore the situation.

Have pgloader do the same with indexes that are used to back a constraint.
  • Loading branch information
dimitri committed Jul 17, 2015
1 parent c3986b0 commit 8511294
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
11 changes: 8 additions & 3 deletions src/pgsql/queries.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,18 @@
(defun list-indexes (table-name)
"List all indexes for TABLE-NAME in SCHEMA. A PostgreSQL connection must
be already established when calling that function."
(loop :for (index-name table-name table-oid primary sql)
(loop :for (index-name table-name table-oid primary sql conname condef)
:in (pomo:query (format nil "
select i.relname,
indrelid::regclass,
indrelid,
indisprimary,
pg_get_indexdef(indexrelid)
pg_get_indexdef(indexrelid),
c.conname,
pg_get_constraintdef(c.oid)
from pg_index x
join pg_class i ON i.oid = x.indexrelid
left join pg_constraint c ON c.conindid = i.oid
where indrelid = '~@[~a.~]~a'::regclass"
(when (typep table-name 'cons)
(car table-name))
Expand All @@ -212,7 +215,9 @@ select i.relname,
:table-oid table-oid
:primary primary
:columns nil
:sql sql)))
:sql sql
:conname conname
:condef condef)))

(defun list-reserved-keywords (pgconn)
"Connect to PostgreSQL DBNAME and fetch reserved keywords."
Expand Down
14 changes: 11 additions & 3 deletions src/pgsql/schema.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,11 @@
;;;
;;; Index support
;;;
(defstruct pgsql-index name table-name table-oid primary unique columns sql)
(defstruct pgsql-index
;; the struct is used both for supporting new index creation from non
;; PostgreSQL system and for drop/create indexes when using the 'drop
;; indexes' option (in CSV mode and the like)
name table-name table-oid primary unique columns sql conname condef)

(defgeneric index-table-name (index)
(:documentation
Expand All @@ -289,6 +293,10 @@

(cols (mapcar #'apply-identifier-case (pgsql-index-columns index))))
(cond
((pgsql-index-condef index)
(format nil "ALTER TABLE ~a ADD ~a;"
table-name (pgsql-index-condef index)))

((pgsql-index-primary index)
(values
;; ensure good concurrency here, don't take the ACCESS EXCLUSIVE
Expand All @@ -312,9 +320,9 @@
"Generate the PostgreSQL statement to DROP the index."
(let* ((table-name (apply-identifier-case (pgsql-index-table-name index)))
(index-name (apply-identifier-case (pgsql-index-name index))))
(cond ((pgsql-index-primary index)
(cond ((pgsql-index-conname index)
(format nil "ALTER TABLE ~a DROP CONSTRAINT ~a;"
table-name index-name))
table-name (pgsql-index-conname index)))

(t
(format nil "DROP INDEX ~a;" index-name)))))
Expand Down

0 comments on commit 8511294

Please sign in to comment.