Documentation
¶
Overview ¶
Package fmt provides the `spirit fmt` subcommand, which canonicalizes CREATE TABLE .sql files by round-tripping them through a real MySQL server.
MySQL normalizes many SQL constructs internally. For example:
- BOOLEAN becomes TINYINT(1)
- SERIAL becomes BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE
- NVARCHAR becomes VARCHAR with utf8mb3 charset
These normalizations cause spurious diffs when comparing schema files against a live database. `spirit fmt` solves this by applying each .sql file to a local MySQL server, reading back the canonical form via SHOW CREATE TABLE, and updating the file if it differs.
Modelled on `go fmt`: if changes were made, the filename is printed to stdout. If no changes were needed, nothing is printed.
The connection is opened with dbconn, so it uses the same session settings as every other spirit connection, with STRICT_TRANS_TABLES and NO_ENGINE_SUBSTITUTION added to spirit's sql_mode (NO_AUTO_VALUE_ON_ZERO). Without them MySQL accepts some invalid definitions by changing them: an index that is too long becomes a prefix index, a VARCHAR that is too long becomes a TEXT type, and an unknown engine becomes InnoDB. fmt would then write a different table into the file. The zero-date checks (NO_ZERO_DATE, NO_ZERO_IN_DATE) stay off, as in spirit, so DEFAULT '0000-00-00' is accepted. TLS is PREFERRED: it is used when the server supports it.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FmtCmd ¶
type FmtCmd struct {
// Files is the list of .sql files (or directories) to format.
Files []string `` /* 226-byte string literal not displayed */
// MySQL connection options
Host string `` /* 126-byte string literal not displayed */
Username string `name:"username" help:"MySQL username" default:"root" env:"MYSQL_USER"`
Password string `name:"password" help:"MySQL password" default:"" env:"MYSQL_PASSWORD"`
Database string `name:"database" help:"Temporary database to use for formatting" default:"spirit_fmt" env:"MYSQL_DATABASE"`
}
FmtCmd is the Kong CLI struct for the fmt command. It canonicalizes CREATE TABLE .sql files by round-tripping them through MySQL, under formatSQLMode.