dimitri / pgloader

Migrate to PostgreSQL in a single command!
http://pgloader.io
Other
5.45k stars 548 forks source link

mysql8.0, miss on-update-current-timestamp trigger when the column has default value #1560

Open optionals opened 9 months ago

optionals commented 9 months ago

mysql

CREATE TABLE `onupdate` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `patient_id` varchar(50) NOT NULL,
  `update_date1` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `update_date2` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE current_timestamp(),
  `update_date3` timestamp NULL DEFAULT NULL ON UPDATE current_timestamp(),
  `update_date4` timestamp NULL ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `patient_id` (`patient_id`)
);

then show COLUMNS

SHOW COLUMNS FROM onupdate; 
+--------------+-------------+------+-----+-------------------+-----------------------------------------------+
| Field        | Type        | Null | Key | Default           | Extra                                         |
+--------------+-------------+------+-----+-------------------+-----------------------------------------------+
| id           | int         | NO   | PRI | NULL              | auto_increment                                |
| patient_id   | varchar(50) | NO   | UNI | NULL              |                                               |
| update_date1 | timestamp   | NO   |     | CURRENT_TIMESTAMP | DEFAULT_GENERATED on update CURRENT_TIMESTAMP |
| update_date2 | timestamp   | NO   |     | CURRENT_TIMESTAMP | DEFAULT_GENERATED on update CURRENT_TIMESTAMP |
| update_date3 | timestamp   | YES  |     | NULL              | on update CURRENT_TIMESTAMP                   |
| update_date4 | timestamp   | YES  |     | NULL              | on update CURRENT_TIMESTAMP                   |
+--------------+-------------+------+-----+-------------------+-----------------------------------------------+

pg

loader@/tmp:loader> \df
+--------+--------------------------------------+------------------+---------------------+---------+
| Schema | Name                                 | Result data type | Argument data types | Type    |
|--------+--------------------------------------+------------------+---------------------+---------|
| loader | on_update_current_timestamp_onupdate | trigger          |                     | trigger |
+--------+--------------------------------------+------------------+---------------------+---------+
SELECT 1
Time: 0.092s

Extra

Any additional information that is available about a given column. The value is nonempty in these cases:

fix

I add a judgmental condition as a temporary solution

;;; src/sources/mysql/mysql-cast-rules.lisp
(defun normalize-extra (extra)
  "Normalize MySQL strings into pgloader CL keywords for internal processing."
  (cond ((string= "auto_increment" extra)
         :auto-increment)

        ((or (string= extra "on update CURRENT_TIMESTAMP")
             (string= extra "on update current_timestamp()")
             (string= extra "DEFAULT_GENERATED on update CURRENT_TIMESTAMP"))
         :on-update-current-timestamp)))