From ddda2f92ca985207f4836b284c439e49f10c86e2 Mon Sep 17 00:00:00 2001 From: Dimitri Fontaine Date: Sat, 28 Jan 2017 17:45:33 +0100 Subject: [PATCH] Force column ordering in SQLite support. In the case of targetting an already existing PostgreSQL database, columns might have been reordered. Add the column name list to the COPY command we send so that we figure the mapping out automatically. Fixes #509. --- src/sources/sqlite/sqlite.lisp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/sources/sqlite/sqlite.lisp b/src/sources/sqlite/sqlite.lisp index 5b81eaf..97ceec4 100644 --- a/src/sources/sqlite/sqlite.lisp +++ b/src/sources/sqlite/sqlite.lisp @@ -57,8 +57,11 @@ (defmethod map-rows ((sqlite copy-sqlite) &key process-row-fn) "Extract SQLite data and call PROCESS-ROW-FN function with a single argument (a list of column values) for each row" - (let ((sql (format nil "SELECT * FROM ~a" (table-source-name (source sqlite)))) - (pgtypes (map 'vector #'column-type-name (columns sqlite)))) + (let* ((table-name (table-source-name (source sqlite))) + (cols (mapcar #'coldef-name (fields sqlite))) + (sql (format nil "SELECT ~{`~a`~^, ~} FROM `~a`;" cols table-name)) + (pgtypes (map 'vector #'column-type-name (columns sqlite)))) + (log-message :sql "SQLite: ~a" sql) (with-connection (*sqlite-db* (source-db sqlite)) (let* ((db (conn-handle *sqlite-db*)) (encoding (sqlite-encoding db))) @@ -89,6 +92,10 @@ (log-message :error "~a" e) (update-stats :data (target sqlite) :errs 1))))))) +(defmethod copy-column-list ((sqlite copy-sqlite)) + "Send the data in the SQLite column ordering." + (mapcar #'apply-identifier-case (mapcar #'coldef-name (fields sqlite)))) + (defmethod fetch-metadata (sqlite catalog &key materialize-views