Skip to main content

Module mysql

Module mysql 

Source
Expand description

MySQL / MariaDB DDL (MySQL 8.0.31+).

The editor mirrors the PostgreSQL one in super, with the dialect differences MySQL forces on a schema tool:

  • Quoting. Identifiers use backticks (embedded backticks doubled). String literals escape backslashes as well as quotes, because MySQL treats \ as an escape character unless NO_BACKSLASH_ESCAPES is set.
  • No transactional DDL. Every DDL statement commits implicitly, so a migration that fails half way leaves its earlier statements applied and no history row is written. Migrator does not wrap MySQL migrations in a transaction for that reason. Keep MySQL migrations small (one schema change each) so a failure is easy to repair.
  • AUTO_INCREMENT primary keys: BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY.
  • MODIFY COLUMN replaces the whole column definition, so an AlterField always restates type, nullability and default.
  • Foreign keys are table-level CONSTRAINT .. FOREIGN KEY clauses named <table>_<column>_fk. MySQL parses an inline REFERENCES on a column but ignores it, so it is never used. InnoDB also rejects ON DELETE SET DEFAULT, which is reported as an error.
  • Keyed TEXT. TEXT, BLOB and JSON columns cannot be a primary key, UNIQUE or indexed without a prefix length, and this editor never guesses one. Give such fields a max_length (a VARCHAR) instead. Literal defaults on these types are written as DEFAULT ('..').
  • DROP INDEX needs its table (DROP INDEX name ON table); a unique constraint is dropped as an index and a check constraint with DROP CHECK.
  • DbDefault::Now is CURRENT_TIMESTAMP(6), matching DATETIME(6).

Column types follow the MySQL backend’s storage table: TINYINT(1), DATETIME(6) (UTC), TIME(6), CHAR(36) for UUIDs, JSON, DOUBLE. A decimal without precision becomes DECIMAL(38,10) (a bare DECIMAL is DECIMAL(10,0) and would round to an integer).

Functions§

quote_ident
Quote ident with backticks; embed a backtick by doubling it.
quote_string
Quote a MySQL string literal (quotes doubled, backslashes escaped).