Skip to content

Commit

Permalink
[MySQL]Fix constraints/indexes detection bug
Browse files Browse the repository at this point in the history
- When FK and UNIQUE are assigned to the same column
- When PRIMARY KEY and UNIQUE are assigned to the same column
  • Loading branch information
k1LoW committed Feb 17, 2020
1 parent 91afe4b commit 0ccde33
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 5 deletions.
10 changes: 5 additions & 5 deletions drivers/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ LEFT JOIN
kcu.table_name,
kcu.constraint_name,
kcu.column_name,
(CASE WHEN c.column_key='PRI' AND kcu.referenced_table_name IS NULL THEN 'PRIMARY KEY'
WHEN c.column_key='UNI' THEN 'UNIQUE'
WHEN c.column_key='MUL' AND kcu.referenced_table_name IS NULL THEN 'UNIQUE'
WHEN c.column_key='MUL' AND kcu.referenced_table_name IS NOT NULL THEN 'FOREIGN KEY'
WHEN c.column_key='PRI' AND kcu.referenced_table_name IS NOT NULL THEN 'FOREIGN KEY'
(CASE WHEN kcu.referenced_table_name IS NOT NULL THEN 'FOREIGN KEY'
WHEN c.column_key = 'PRI' AND kcu.constraint_name = 'PRIMARY' THEN 'PRIMARY KEY'
WHEN c.column_key = 'PRI' AND kcu.constraint_name != 'PRIMARY' THEN 'UNIQUE'
WHEN c.column_key = 'UNI' THEN 'UNIQUE'
WHEN c.column_key = 'MUL' THEN 'UNIQUE'
ELSE 'UNKNOWN'
END) AS costraint_type
FROM information_schema.key_column_usage AS kcu
Expand Down
3 changes: 3 additions & 0 deletions sample/exclude/user_options.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ CREATE TABLE `user_options` (
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updated` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`user_id`),
UNIQUE KEY `user_id` (`user_id`),
CONSTRAINT `user_options_user_id_fk` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='User options table'
```
Expand All @@ -34,13 +35,15 @@ CREATE TABLE `user_options` (
| Name | Type | Definition |
| ---- | ---- | ---------- |
| PRIMARY | PRIMARY KEY | PRIMARY KEY (user_id) |
| user_id | UNIQUE | UNIQUE KEY user_id (user_id) |
| user_options_user_id_fk | FOREIGN KEY | FOREIGN KEY (user_id) REFERENCES users (id) |

## Indexes

| Name | Definition |
| ---- | ---------- |
| PRIMARY | PRIMARY KEY (user_id) USING BTREE |
| user_id | UNIQUE KEY user_id (user_id) USING BTREE |

## Relations

Expand Down
3 changes: 3 additions & 0 deletions sample/mysql/user_options.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ CREATE TABLE `user_options` (
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updated` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`user_id`),
UNIQUE KEY `user_id` (`user_id`),
CONSTRAINT `user_options_user_id_fk` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='User options table'
```
Expand All @@ -34,13 +35,15 @@ CREATE TABLE `user_options` (
| Name | Type | Definition |
| ---- | ---- | ---------- |
| PRIMARY | PRIMARY KEY | PRIMARY KEY (user_id) |
| user_id | UNIQUE | UNIQUE KEY user_id (user_id) |
| user_options_user_id_fk | FOREIGN KEY | FOREIGN KEY (user_id) REFERENCES users (id) |

## Indexes

| Name | Definition |
| ---- | ---------- |
| PRIMARY | PRIMARY KEY (user_id) USING BTREE |
| user_id | UNIQUE KEY user_id (user_id) USING BTREE |

## Relations

Expand Down
3 changes: 3 additions & 0 deletions sample/mysql8/user_options.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ CREATE TABLE `user_options` (
`created` timestamp NOT NULL,
`updated` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`user_id`),
UNIQUE KEY `user_id` (`user_id`),
CONSTRAINT `user_options_user_id_fk` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='User options table'
```
Expand All @@ -34,13 +35,15 @@ CREATE TABLE `user_options` (
| Name | Type | Definition |
| ---- | ---- | ---------- |
| PRIMARY | PRIMARY KEY | PRIMARY KEY (user_id) |
| user_id | UNIQUE | UNIQUE KEY user_id (user_id) |
| user_options_user_id_fk | FOREIGN KEY | FOREIGN KEY (user_id) REFERENCES users (id) |

## Indexes

| Name | Definition |
| ---- | ---------- |
| PRIMARY | PRIMARY KEY (user_id) USING BTREE |
| user_id | UNIQUE KEY user_id (user_id) USING BTREE |

## Relations

Expand Down
3 changes: 3 additions & 0 deletions sample/svg/user_options.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ CREATE TABLE `user_options` (
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updated` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`user_id`),
UNIQUE KEY `user_id` (`user_id`),
CONSTRAINT `user_options_user_id_fk` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='User options table'
```
Expand All @@ -34,13 +35,15 @@ CREATE TABLE `user_options` (
| Name | Type | Definition |
| ---- | ---- | ---------- |
| PRIMARY | PRIMARY KEY | PRIMARY KEY (user_id) |
| user_id | UNIQUE | UNIQUE KEY user_id (user_id) |
| user_options_user_id_fk | FOREIGN KEY | FOREIGN KEY (user_id) REFERENCES users (id) |

## Indexes

| Name | Definition |
| ---- | ---------- |
| PRIMARY | PRIMARY KEY (user_id) USING BTREE |
| user_id | UNIQUE KEY user_id (user_id) USING BTREE |

## Relations

Expand Down
1 change: 1 addition & 0 deletions testdata/my.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ CREATE TABLE user_options (
show_email boolean NOT NULL DEFAULT false,
created timestamp NOT NULL,
updated timestamp,
UNIQUE(user_id),
CONSTRAINT user_options_user_id_fk FOREIGN KEY(user_id) REFERENCES users(id) ON UPDATE NO ACTION ON DELETE CASCADE
) COMMENT = 'User options table';

Expand Down

0 comments on commit 0ccde33

Please sign in to comment.