Refrain from loading incomplete foreign key references in SQLite.

Given INCLUDING and EXCLUDING support it might be possible that we migrate a
table from SQLite without having selecting tables pointed to by foreign
keys. In that case, pgloader should still be able to load the data
definition and content fine, just skipping the incomplete fkey definitions.

That's implemented in this patch, which has been tested thanks to a
reproducible data set being made available!

Fixes #681.
This commit is contained in:
Dimitri Fontaine 2017-11-25 16:28:18 -08:00
parent 62d776f5e8
commit 87f35e8852

View File

@ -198,18 +198,33 @@
:do (let* ((ftable (find-table (table-schema table) ftable-name))
(fkey (or (gethash id fkey-table)
(let ((pg-fkey
(make-fkey :table table
:columns nil
:foreign-table ftable
:foreign-columns nil
:update-rule on-update
:delete-rule on-delete)))
(setf (gethash id fkey-table) pg-fkey)
(add-fkey table pg-fkey)
pg-fkey))))
(push-to-end from (fkey-columns fkey))
(push-to-end to (fkey-foreign-columns fkey))))))
(when ftable
(let ((pg-fkey
(make-fkey :table table
:columns nil
:foreign-table ftable
:foreign-columns nil
:update-rule on-update
:delete-rule on-delete)))
(setf (gethash id fkey-table) pg-fkey)
(add-fkey table pg-fkey)
pg-fkey)))))
(if (and fkey from to)
(progn
(push-to-end from (fkey-columns fkey))
(push-to-end to (fkey-foreign-columns fkey)))
;; it might be INCLUDING/EXCLUDING clauses that make it we
;; don't have to care about the fkey definition, or it
;; might be that the SQLite fkey definition is missing
;; information, such as the `to` column, as seen in the
;; field (bug report #681)
(log-message :info
"Incomplete Foreign Key definition on table ~a(~a) referencing table ~a(~a)"
(when table (format-table-name table))
from
(when ftable (format-table-name ftable))
to))))))
(defun list-all-fkeys (schema &key (db *sqlite-db*))
"Get the list of SQLite foreign keys definitions per table."