diff

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package diff contains functions to generate SQL statements to migrate an old schema to the new schema

Example
package main

import (
	"os"

	"github.com/shogo82148/schemalex-deploy/diff"
)

func main() {
	const sql1 = `CREATE TABLE hoge (
    id INTEGER NOT NULL AUTO_INCREMENT,
    PRIMARY KEY (id)
);`
	const sql2 = `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)
);`

	diff.Strings(os.Stdout, sql1, sql2, diff.WithTransaction(true))

}
Output:

BEGIN;
SET FOREIGN_KEY_CHECKS = 0;
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`;
SET FOREIGN_KEY_CHECKS = 1;
COMMIT;

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Statements

func Statements(dst io.Writer, from, to model.Stmts, options ...Option) error

Statements compares two model.Stmts and generates a series of statements to migrate from the old one to the new one, writing the result to `dst`

func Strings

func Strings(dst io.Writer, from, to string, options ...Option) error

Strings compares two strings and generates a series of statements to migrate from the old one to the new one, writing the result to `dst`

Types

type Option

type Option interface {
	// contains filtered or unexported methods
}

func WithCurrentSchema added in v0.0.8

func WithCurrentSchema(schema string) Option

WithCurrentSchema specifies the current schema deployed in MySQL.

func WithIndent added in v0.0.8

func WithIndent(s string, n int) Option

WithIndent specifies the indent string to use, and the length. For example, if you specify WithIndent(" " /* single space */, 2), the indent will be 2 spaces per level.

Please note that no check on the string will be performed, so anything you specify will be used as-is.

func WithParser

func WithParser(p *schemalex.Parser) Option

WithParser specifies the parser instance to use when parsing the statements given to the diffing functions. If unspecified, a default parser will be used

func WithTransaction

func WithTransaction(b bool) Option

WithTransaction specifies if statements to control transactions should be included in the diff.

type Stmt

type Stmt string

Stmt is an SQL statement.

func (Stmt) String

func (s Stmt) String() string

type Stmts

type Stmts []Stmt

Stmts is a list of diff.Stmt.

func Diff

func Diff(from, to model.Stmts, options ...Option) (Stmts, error)

Diff compares two model.Stmts, and generates a series of statements as `diff.Stmts` so the consumer can, for example, analyze or use these statements standalone by themselves.

func (*Stmts) Append

func (stmts *Stmts) Append(s Stmt) Stmts

Append appends a Stmt to the list.

func (Stmts) WriteTo

func (stmts Stmts) WriteTo(dst io.Writer) (int64, error)

WriteTo writes the statements to dst.

Jump to

Keyboard shortcuts

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