All Projects → sunary → sqlize

sunary / sqlize

Licence: other
sql migration schema generate from models

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to sqlize

migration
TYPO3 Migration Framework for every kind of migration/imports from CLI (e.g. Templavoila to Gridelements, tt_news to news, etc...)
Stars: ✭ 52 (+10.64%)
Mutual labels:  migration
migrations
Migrations is a database migration tool that uses go's database/sql from the standard library
Stars: ✭ 17 (-63.83%)
Mutual labels:  migration
MigrateGitlabToGogs
Migrate repositories from Gitlab to Gogs or Gitea
Stars: ✭ 49 (+4.26%)
Mutual labels:  migration
maintenance job
Mechanism to run testable one-off jobs in Rails at deploy time to manipulate data
Stars: ✭ 27 (-42.55%)
Mutual labels:  migration
alembic utils
An alembic/sqlalchemy extension for migrating sql views, functions, triggers, and policies
Stars: ✭ 105 (+123.4%)
Mutual labels:  migration
exodus
Migration tools for Tabular Data to Oracle JSON/Tabular Data
Stars: ✭ 19 (-59.57%)
Mutual labels:  migration
data-migrator
A declarative data-migration package
Stars: ✭ 15 (-68.09%)
Mutual labels:  migration
yii2-console-migration
yii2命令行中使用migration备份和还原数据库
Stars: ✭ 35 (-25.53%)
Mutual labels:  migration
r2dbc-migrate
R2DBC database migration tool & library
Stars: ✭ 83 (+76.6%)
Mutual labels:  migration
Migrate2Postgres
Easily migrate from other DBMSs to PostgreSQL
Stars: ✭ 47 (+0%)
Mutual labels:  migration
laravel-migrate-check
An artisan command to check for pending migrations with proper exit code
Stars: ✭ 53 (+12.77%)
Mutual labels:  migration
camunda-bpm-migration
Fluent Java API for Camunda Platform 7 process instance migration
Stars: ✭ 18 (-61.7%)
Mutual labels:  migration
modoboa-imap-migration
An extension to ease the migration between 2 IMAP servers using offlineimap
Stars: ✭ 14 (-70.21%)
Mutual labels:  migration
SuperGrate
💾 Get moving with Super Grate; a free & open source Windows Profile Migration & Backup Utility. Super Grate is a GUI (Graphical User Interface) that assists Microsoft's USMT (User State Migration Utility) in performing remote migrations over a network connection.
Stars: ✭ 91 (+93.62%)
Mutual labels:  migration
upscheme
Database migrations and schema updates made easy
Stars: ✭ 737 (+1468.09%)
Mutual labels:  migration
cigration
Citus shard migration tool
Stars: ✭ 28 (-40.43%)
Mutual labels:  migration
list-of-tech-migrations
list of public tech migrations
Stars: ✭ 428 (+810.64%)
Mutual labels:  migration
harmonica
Kotlin Database Migration Tool. This tool makes it really easy to create table, index, add columns, and so on, with Kotlin DSL.
Stars: ✭ 123 (+161.7%)
Mutual labels:  migration
django-wordpress-parser
Wordpress eXtended RSS Parser (in Python for Django)
Stars: ✭ 18 (-61.7%)
Mutual labels:  migration
couchmove
Java data migration tool for Couchbase
Stars: ✭ 36 (-23.4%)
Mutual labels:  migration

SQLize

github action

Generate MySQL/PostgreSQL Migration from golang struct and existing sql

Features

  • Sql parser
  • Sql builder from objects
  • Generate sql migration from diff between existed sql and objects
  • Generate arvo schema (Mysql only)
  • Support embedded struct
  • Generate migration version - compatible with golang-migrate/migrate
  • Tag options - compatible with gorm tag

WARNING: some functions doesn't work on PostgreSQL, let me know of any issues

Getting Started

package main

import (
	"time"
	
	"github.com/sunary/sqlize"
)

type user struct {
	ID          int32  `sql:"primary_key;auto_increment"`
	Alias       string `sql:"type:VARCHAR(64)"`
	Name        string `sql:"type:VARCHAR(64);unique;index_columns:name,age"`
	Age         int
	Bio         string
	IgnoreMe    string     `sql:"-"`
	AcceptTncAt *time.Time `sql:"index:idx_accept_tnc_at"`
	CreatedAt   time.Time  `sql:"default:CURRENT_TIMESTAMP"`
	UpdatedAt   time.Time  `sql:"default:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;index:idx_updated_at"`
}

func (user) TableName() string {
	return "user"
}

var createStm = `
CREATE TABLE user (
  id            INT AUTO_INCREMENT PRIMARY KEY,
  name          VARCHAR(64),
  age           INT,
  bio           TEXT,
  gender        BOOL,
  accept_tnc_at DATETIME NULL,
  created_at    DATETIME DEFAULT CURRENT_TIMESTAMP,
  updated_at    DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
CREATE UNIQUE INDEX idx_name_age ON user(name, age);
CREATE INDEX idx_updated_at ON user(updated_at);`

func main() {
	n := time.Now()
	newMigration := sqlize.NewSqlize(sqlize.WithSqlTag("sql"), sqlize.WithMigrationFolder(""))
	_ = newMigration.FromObjects(user{AcceptTncAt: &n})

	println(newMigration.StringUp())
	//CREATE TABLE `user` (
	//	`id`            int(11) AUTO_INCREMENT PRIMARY KEY,
	//	`alias`         varchar(64),
	//	`name`          varchar(64),
	//	`age`           int(11),
	//	`bio`           text,
	//	`accept_tnc_at` datetime NULL,
	//	`created_at`    datetime DEFAULT CURRENT_TIMESTAMP(),
	//	`updated_at`    datetime DEFAULT CURRENT_TIMESTAMP() ON UPDATE CURRENT_TIMESTAMP()
	//);
	//CREATE UNIQUE INDEX `idx_name_age` ON `user`(`name`, `age`);
	//CREATE INDEX `idx_accept_tnc_at` ON `user`(`accept_tnc_at`);
	//CREATE INDEX `idx_updated_at` ON `user`(`updated_at`);

	println(newMigration.StringDown())
	//DROP TABLE IF EXISTS `user`;

	oldMigration := sqlize.NewSqlize(sqlize.WithMigrationFolder(""))
	//_ = oldMigration.FromMigrationFolder()
	_ = oldMigration.FromString(createStm)

	newMigration.Diff(*oldMigration)

	println(newMigration.StringUp())
	//ALTER TABLE `user` ADD COLUMN `alias` varchar(64) AFTER `id`;
	//ALTER TABLE `user` DROP COLUMN `gender`;
	//CREATE INDEX `idx_accept_tnc_at` ON `user`(`accept_tnc_at`);

	println(newMigration.StringDown())
	//ALTER TABLE `user` DROP COLUMN `alias`;
	//ALTER TABLE `user` ADD COLUMN `gender` tinyint(1) AFTER `age`;
	//DROP INDEX `idx_accept_tnc_at` ON `user`;

	println(newMigration.ArvoSchema())
	//...

	_ = newMigration.WriteFiles("demo migration")
}

Convention

  • mysql by default, using option sql_builder.WithPostgresql() for postgresql
  • sql uppercase default, using option sql_builder.WithSqlLowercase() for sql lowercase
  • support generate comment, using option sql_builder.WithCommentGenerate()
  • primary key for this field: sql:"primary_key"
  • auto increment: sql:"auto_increment"
  • indexing this field: sql:"index"
  • custom index name: sql:"index:idx_col_name"
  • unique indexing this field: sql:"unique"
  • custome unique index name: sql:"unique:idx_name"
  • composite index (include unique index and primary key): sql:"index_columns:col1,col2"
  • index type: sql:"index_type:btree"
  • set default value: sql:"default:CURRENT_TIMESTAMP"
  • override datatype: sql:"type:VARCHAR(64)"
  • ignore: sql:"-"
  • pointer value must be declare in struct
type sample struct {
	ID        int32 `sql:"primary_key"`
	DeletedAt *time.Time
}

now := time.Now()
newMigration.FromObjects(sample{DeletedAt: &now})
  • mysql data type will be changed implicitly:
TINYINT => tinyint(4)
INT     => int(11)
BIGINT  => bigint(20)
  • fields belong to embedded struct have the lowest order, except primary key always first
  • an embedded struct (sql:"squash") can not be pointer, also support prefix: sql:"embedded_prefix:base_"
type Base struct {
	ID        int32 `sql:"primary_key"`
	CreatedAt time.Time
}
type sample struct {
	Base `sql:"squash"`
	User string
}

newMigration.FromObjects(sample{})

/*
CREATE TABLE sample (
 id         int(11) PRIMARY KEY,
 user       text,
 created_at datetime
);
*/
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].