sqlize

package module
v0.0.0-...-892c78e Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

README

SQLize

github action

English | 中文

SQLize is a powerful migration generation tool that detects differences between two SQL state sources. It simplifies migration creation by comparing an existing SQL schema with Go models, ensuring seamless database updates.

Designed for flexibility, SQLize supports MySQL, PostgreSQL, and SQLite and integrates well with popular Go ORM and migration tools like gorm (gorm tag), golang-migrate/migrate (migration version), and more.

Additionally, SQLize offers advanced features, including Avro Schema export (MySQL only) and ERD diagram generation (MermaidJS).

Conventions

Default Behaviors
  • Database: mysql (use sql_builder.WithPostgresql() for PostgreSQL, etc.)
  • SQL syntax: Uppercase (e.g., "SELECT * FROM user WHERE id = ?")
    • For lowercase, use sql_builder.WithSqlLowercase()
  • Table naming: Singular
    • For plural (adding 's'), use sql_builder.WithPluralTableName()
  • Comment generation: Use sql_builder.WithCommentGenerate()
SQL Tag Options
  • Format: Supports both snake_case and camelCase (e.g., sql:"primary_key" equals sql:"primaryKey")
  • Custom column: sql:"column:column_name"
  • Primary key: sql:"primary_key"
  • Foreign key: sql:"foreign_key:user_id;references:user_id"
  • Auto increment: sql:"auto_increment"
  • Default value: sql:"default:CURRENT_TIMESTAMP"
  • Override datatype: sql:"type:VARCHAR(64)"
  • Ignore field: sql:"-"
Indexing
  • Basic index: sql:"index"
  • Custom index name: sql:"index:idx_col_name"
  • Unique index: sql:"unique"
  • Custom unique index: sql:"unique:idx_name"
  • Composite index: sql:"index_columns:col1,col2" (includes unique index and primary key)
  • Index type: sql:"index_type:btree"
Embedded Structs
  • Use sql:"embedded" or sql:"squash"
  • Cannot be a pointer
  • Supports prefix: sql:"embedded_prefix:base_"
  • Fields have lowest order, except for primary key (always first)
Data Types
  • MySQL data types are implicitly changed:
	TINYINT => tinyint(4)
	INT     => int(11)
	BIGINT  => bigint(20)
  • Pointer values must be declared in the struct or predefined data types.
// your struct
type Record struct {
	ID        int
	DeletedAt *time.Time
}

// =>
// the struct is declared with a value
now := time.Now()
Record{DeletedAt: &now}

// or predefined data type
type Record struct {
	ID        int
	DeletedAt *time.Time `sql:"type:DATETIME"`
}

// or using struct supported by "database/sql"
type Record struct {
	ID        int
	DeletedAt sql.NullTime
}

Usage

  • Add the following code to your project as a command.
  • Implement YourModels() to return the Go models affected by the migration.
  • Run the command whenever you need to generate a migration.
package main

import (
	"fmt"
	"log"
	"os"

	"github.com/sunary/sqlize"
)

func main() {
	migrationFolder := "migrations/"
	sqlLatest := sqlize.NewSqlize(sqlize.WithSqlTag("sql"),
		sqlize.WithMigrationFolder(migrationFolder),
		sqlize.WithCommentGenerate())

	ms := YourModels() // TODO: implement YourModels() function
	err := sqlLatest.FromObjects(ms...)
	if err != nil {
		log.Fatal("sqlize FromObjects", err)
	}
	sqlVersion := sqlLatest.HashValue()

	sqlMigrated := sqlize.NewSqlize(sqlize.WithMigrationFolder(migrationFolder))
	err = sqlMigrated.FromMigrationFolder()
	if err != nil {
		log.Fatal("sqlize FromMigrationFolder", err)
	}

	sqlLatest.Diff(*sqlMigrated)

	fmt.Println("sql version", sqlVersion)

	fmt.Println("\n\n### migration up")
	migrationUp := sqlLatest.StringUp()
	fmt.Println(migrationUp)

	fmt.Println("\n\n### migration down")
	fmt.Println(sqlLatest.StringDown())

	initVersion := false
	if initVersion {
		log.Println("write to init version")
		err = sqlLatest.WriteFilesVersion("new version", 0, false)
		if err != nil {
			log.Fatal("sqlize WriteFilesVersion", err)
		}
	}

	if len(os.Args) > 1 {
		log.Println("write to file", os.Args[1])
		err = sqlLatest.WriteFilesWithVersion(os.Args[1], sqlVersion, false)
		if err != nil {
			log.Fatal("sqlize WriteFilesWithVersion", err)
		}
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Sqlize

type Sqlize struct {
	// contains filtered or unexported fields
}

Sqlize ...

func NewSqlize

func NewSqlize(opts ...SqlizeOption) *Sqlize

NewSqlize ...

func (Sqlize) ArvoSchema

func (s Sqlize) ArvoSchema(needTables ...string) []string

ArvoSchema export arvo schema, support mysql only

func (Sqlize) Diff

func (s Sqlize) Diff(old Sqlize)

Diff differ between 2 migrations

func (*Sqlize) FromMigrationFolder

func (s *Sqlize) FromMigrationFolder() error

FromMigrationFolder load migration from folder `migrations`

func (*Sqlize) FromObjects

func (s *Sqlize) FromObjects(objs ...interface{}) error

FromObjects load from objects

func (*Sqlize) FromString

func (s *Sqlize) FromString(sql string) error

FromString load migration from sql

func (Sqlize) HashValue

func (s Sqlize) HashValue() int64

HashValue ...

func (Sqlize) MermaidJsErd

func (s Sqlize) MermaidJsErd(needTables ...string) string

MermaidJsErd export MermaidJs ERD

func (Sqlize) MermaidJsLive

func (s Sqlize) MermaidJsLive(needTables ...string) string

MermaidJsLive export MermaidJs Live

func (Sqlize) StringDown

func (s Sqlize) StringDown() string

StringDown migration down

func (Sqlize) StringDownWithVersion

func (s Sqlize) StringDownWithVersion(ver int64) string

StringDownWithVersion migration down with version

func (Sqlize) StringUp

func (s Sqlize) StringUp() string

StringUp migration up

func (Sqlize) StringUpWithVersion

func (s Sqlize) StringUpWithVersion(ver int64, dirty bool) string

StringUpWithVersion migration up with version

func (Sqlize) WriteFiles

func (s Sqlize) WriteFiles(name string) error

WriteFiles create migration files

func (Sqlize) WriteFilesVersion

func (s Sqlize) WriteFilesVersion(name string, ver int64, dirty bool) error

WriteFilesVersion create migration version only

func (Sqlize) WriteFilesWithVersion

func (s Sqlize) WriteFilesWithVersion(name string, ver int64, dirty bool) error

WriteFilesWithVersion create migration files with version

type SqlizeOption

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

SqlizeOption ...

func WithCommentGenerate

func WithCommentGenerate() SqlizeOption

WithCommentGenerate default is off

func WithIgnoreFieldOrder

func WithIgnoreFieldOrder() SqlizeOption

WithIgnoreFieldOrder ...

func WithMigrationFolder

func WithMigrationFolder(path string) SqlizeOption

WithMigrationFolder ...

func WithMigrationSuffix

func WithMigrationSuffix(upSuffix, downSuffix string) SqlizeOption

WithMigrationSuffix ...

func WithMigrationTable

func WithMigrationTable(table string) SqlizeOption

WithMigrationTable default is 'schema_migration'

func WithMysql

func WithMysql() SqlizeOption

WithMysql default

func WithPluralTableName

func WithPluralTableName() SqlizeOption

Table name plus s default

func WithPostgresql

func WithPostgresql() SqlizeOption

WithPostgresql ...

func WithSqlLowercase

func WithSqlLowercase() SqlizeOption

WithSqlLowercase ...

func WithSqlTag

func WithSqlTag(sqlTag string) SqlizeOption

WithSqlTag default is `sql`

func WithSqlUppercase

func WithSqlUppercase() SqlizeOption

WithSqlUppercase default

func WithSqlite

func WithSqlite() SqlizeOption

WithSqlite ...

func WithSqlserver

func WithSqlserver() SqlizeOption

WithSqlserver ...

Directories

Path Synopsis
export
test

Jump to

Keyboard shortcuts

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