schemalex

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 11, 2024 License: MIT Imports: 8 Imported by: 0

README

Test Go Reference Coverage Status

schemalex-deploy

database migration tool for MySQL.

SYNOPSIS

You want to deploy the following SQL schema:

-- schema.sql
CREATE TABLE hoge (
    id INTEGER NOT NULL AUTO_INCREMENT,
    PRIMARY KEY (id)
);

You can use schemalex-deploy to actually reflect it in the MySQL database.

$ schemalex-deploy -host 127.0.0.1 -port 3306 -user root -password password -database gotest schema.sql
CREATE TABLE `hoge` (
  `id` INT (11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
);
Do you want to perform these actions?
Only 'yes' will be accepted to confirm.
Enter a value: yes
2024/03/24 22:48:00 starting to deploy
2024/03/24 22:48:00 executing: CREATE TABLE `hoge` (
  `id` INT (11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
)
2024/03/24 22:48:00 updating the schema information
2024/03/24 22:48:00 done

Next, you wanted to update the schema as follows.

-- schema.sql
CREATE TABLE hoge (
    id INTEGER NOT NULL AUTO_INCREMENT,
    c VARCHAR (20) NOT NULL DEFAULT "hoge",
    PRIMARY KEY (id)
);

CREATE TABLE fuga (
    id INTEGER NOT NULL AUTO_INCREMENT,
    PRIMARY KEY (id)
);

To do this, you need to construct the necessary SQL, such as ALTER statements, for the update. If you use schemalex-deploy, it can automatically calculate the necessary SQL statements and actually reflect them.

$ schemalex-deploy -host 127.0.0.1 -port 3306 -user root -password password -database gotest schema.sql
2024/03/24 22:50:34 import table: hoge
2024/03/24 22:50:34 import table: schemalex_revision
CREATE TABLE `fuga` (
  `id` INT (11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
);
ALTER TABLE `hoge` ADD COLUMN `c` VARCHAR (20) NOT NULL DEFAULT 'hoge' AFTER `id`;
Do you want to perform these actions?
Only 'yes' will be accepted to confirm.
Enter a value: yes
2024/03/24 22:50:44 starting to deploy
2024/03/24 22:50:44 executing: CREATE TABLE `fuga` (
  `id` INT (11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
)
2024/03/24 22:50:44 executing: ALTER TABLE `hoge` ADD COLUMN `c` VARCHAR (20) NOT NULL DEFAULT 'hoge' AFTER `id`
2024/03/24 22:50:44 updating the schema information
2024/03/24 22:50:44 done

COMMAND LINE OPTIONS

-socket           the unix domain socket path for the database
-host             the host name of the database
-port             the port number(default: 3306)
-user             username
-password         password
-database         the database name
-version          show the version
-auto-approve     skips interactive approval of plan before deploying
-dry-run          outputs the schema difference, and then exit the program
-import           imports existing table schemas from running database

SEE ALSO

LICENSE

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option interface {
	Name() string
	Value() interface{}
}

Option is a generic interface for objects that passes optional parameters to the various format functions in this package

type ParseError

type ParseError interface {
	error
	File() string
	Line() int
	Col() int
	Message() string
	EOF() bool
}

ParseError is returned from the various `Parse` methods when an invalid or unsupported SQL is found. When stringified, the result will look something like this:

parse error: expected RPAREN at line 3 column 14
    "CREATE TABLE foo " <---- AROUND HERE

type Parser

type Parser struct{}

Parser is responsible to parse a set of SQL statements

func New

func New() *Parser

New creates a new Parser

func (*Parser) Parse

func (p *Parser) Parse(src []byte) (model.Stmts, error)

Parse parses the given set of SQL statements and creates a model.Stmts structure. If it encounters errors while parsing, the returned error will be a ParseError type.

func (*Parser) ParseString

func (p *Parser) ParseString(src string) (model.Stmts, error)

ParseString parses a string containing SQL statements and creates a mode.Stmts structure. See Parse for details.

type Token

type Token struct {
	Type  TokenType
	Value string
	Pos   int
	Line  int
	Col   int
	EOF   bool
}

Token represents a token

func NewToken

func NewToken(t TokenType, v string) *Token

NewToken creates a new token of type `t`, with value `v`

func (Token) Ident added in v0.0.5

func (t Token) Ident() model.Ident

Ident returns an identifier. It is only meaningful if the Type is IDENT or BACKTICK_IDENT. The caller must check it.

type TokenType

type TokenType int

TokenType describes the possible types of tokens that schemalex understands

const (
	ILLEGAL TokenType = iota
	EOF
	SPACE
	IDENT
	BACKTICK_IDENT
	DOUBLE_QUOTE_IDENT
	SINGLE_QUOTE_IDENT
	NUMBER
	LPAREN        // (
	RPAREN        // )
	COMMA         // ,
	SEMICOLON     // ;
	DOT           // .
	SLASH         // /
	ASTERISK      // *
	DASH          // -
	PLUS          // +
	SINGLE_QUOTE  // '
	DOUBLE_QUOTE  // "
	EQUAL         // =
	COMMENT_IDENT // // /*   */, --, #
	ACTION
	ASC
	AUTO_INCREMENT
	AVG_ROW_LENGTH
	BIGINT
	BINARY
	BIT
	BLOB
	BOOL
	BOOLEAN
	BTREE
	CASCADE
	CHAR
	CHARACTER
	CHARSET
	CHECK
	CHECKSUM
	COLLATE
	COMMENT
	COMPACT
	COMPRESSED
	CONNECTION
	CONSTRAINT
	CREATE
	CURRENT_TIMESTAMP
	DATA
	DATABASE
	DATE
	DATETIME
	DECIMAL
	DEFAULT
	DELAY_KEY_WRITE
	DELETE
	DESC
	DIRECTORY
	DISK
	DOUBLE
	DROP
	DYNAMIC
	ENGINE
	ENUM
	EXISTS
	FALSE
	FIRST
	FIXED
	FLOAT
	FOREIGN
	FULL
	FULLTEXT
	GEOMETRY
	GEOMETRYCOLLECTION
	HASH
	IF
	INDEX
	INSERT_METHOD
	INT
	INTEGER
	JSON
	KEY_BLOCK_SIZE
	KEY
	LAST
	LIKE
	LINESTRING
	LONGBLOB
	LONGTEXT
	MATCH
	MAX_ROWS
	MEDIUMBLOB
	MEDIUMINT
	MEDIUMTEXT
	MEMORY
	MIN_ROWS
	MULTILINESTRING
	MULTIPOINT
	MULTIPOLYGON
	NO
	NOT
	NOW
	NULL
	NUMERIC
	ON
	PACK_KEYS
	PARSER
	PARTIAL
	PASSWORD
	POINT
	POLYGON
	PRIMARY
	REAL
	REDUNDANT
	REFERENCES
	RESTRICT
	ROW_FORMAT
	SET
	SIMPLE
	SMALLINT
	SPATIAL
	SRID
	STATS_AUTO_RECALC
	STATS_PERSISTENT
	STATS_SAMPLE_PAGES
	STORAGE
	TABLE
	TABLESPACE
	TEMPORARY
	TEXT
	TIME
	TIMESTAMP
	TINYBLOB
	TINYINT
	TINYTEXT
	TRUE
	UNION
	UNIQUE
	UNSIGNED
	UPDATE
	USE
	USING
	VARBINARY
	VARCHAR
	WITH
	YEAR
	ZEROFILL
)

List of possible tokens

func (TokenType) String

func (t TokenType) String() string

Directories

Path Synopsis
cmd
Package diff contains functions to generate SQL statements to migrate an old schema to the new schema
Package diff contains functions to generate SQL statements to migrate an old schema to the new schema
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL