pressly / goose

A database migration tool. Supports SQL migrations and Go functions.
http://pressly.github.io/goose/
Other
7.16k stars 523 forks source link

mysql: do not use SERIAL alias in goose_db_version table creation #816

Closed JuozasVainauskas closed 2 months ago

JuozasVainauskas commented 2 months ago

We are using goose to run migrations on Vitess with MySQL setup. Unfortunately, Vitess does not support SERIAL keyword. Right now our work-around is to create goose_db_version table manually without using SERIAL keyword. We can solve this problem by NOT using SERIAL keyword and explicitly define the type of the column.

This change is backwards-compatible

As stated in the MySQL documentation:

SERIAL is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE.

After table is created with SERIAL keyword, it automatically resolves to bigint(20) unsigned NOT NULL AUTO_INCREMENT:

mysql> create table mytable (id serial primary key);

mysql> show create table mytable\G
*************************** 1. row ***************************
       Table: mytable
Create Table: CREATE TABLE `mytable` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`) -- this one is superfluous
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
mfridman commented 2 months ago

I think this sounds reasonable, will try to get to it tonight. Why bigint(20) ?

JuozasVainauskas commented 2 months ago

I think this sounds reasonable, will try to get to it tonight. Why bigint(20) ?

20 is a default display width (it has nothing to do with storage). It is equal to the number of characters in the largest negative BIGINT (-9223372036854775808). Official documentation: https://dev.mysql.com/doc/refman/8.4/en/numeric-type-attributes.html. I like to define it for a better readability of mysql table schemas.

Explanation: https://stackoverflow.com/questions/22014869/mysql-difference-between-bigint-and-bigint20

JuozasVainauskas commented 2 months ago

Hi, thank You for merging the PR. When are You planning to release this? @mfridman

mfridman commented 2 months ago

Can do a release within the next few days.

mfridman commented 2 months ago

Cut a release this morning

https://github.com/pressly/goose/releases/tag/v3.22.0

JuozasVainauskas commented 2 months ago

Cut a release this morning

https://github.com/pressly/goose/releases/tag/v3.22.0

Thank You very much!