Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hirokidaichi/schemabuilder
a simple sql schema builder for golang
https://github.com/hirokidaichi/schemabuilder
Last synced: 7 days ago
JSON representation
a simple sql schema builder for golang
- Host: GitHub
- URL: https://github.com/hirokidaichi/schemabuilder
- Owner: hirokidaichi
- Created: 2014-06-24T17:45:16.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-07-21T03:55:21.000Z (over 9 years ago)
- Last Synced: 2024-06-20T17:35:09.217Z (5 months ago)
- Language: Go
- Homepage:
- Size: 156 KB
- Stars: 7
- Watchers: 1
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
schemabuilder - a simple sql schema builder for golang
=====I want not O/R Mapper but a simple DDL builder.
## Feature
+ Get a SQL as string
+ Get a Migration SQL as string
+ MySQL and SQLite3 Supported
+ Composed index supported## Examples
### Scanning Struct
```
package mainimport (
"github.com/hirokidaichi/schemabuilder"
"time"
)type Person struct {
Id uint64 `pk:"true",autoincrement:"true"`
Name string `size:"200",unique:"true"`
Info *string
CreatedAt time.Time
UpdatedAt time.Time
}type PersonIndex struct {
key1 schemabuilder.IComposedKeys `columns:"CreatedAt,UpdatedAt"`
key2 schemabuilder.IUniqueKeys `columns:"CreatedAt,UpdatedAt"`
}var builder = schemabuilder.For(schemabuilder.NewMySQLDialect("utf8", "InnoDB"))
var personSchema = builder.DefineTable(Person{}, PersonIndex{})func main() {
println(personSchema.String())
}/*
CREATE TABLE IF NOT EXISTS `people`(
`id` BIGINT NOT NULL PRIMARY KEY,
`name` VARCHAR(200) NOT NULL,
`info` VARCHAR(255) ,
`created_at` DATETIME NOT NULL,
`updated_at` DATETIME NOT NULL
) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8;
CREATE INDEX `key1` ON `people` (`created_at`,`updated_at`);
CREATE UNIQUE INDEX `key2` ON `people` (`created_at`,`updated_at`);*/
```
### Migration
```
package mainimport (
"fmt"
"github.com/hirokidaichi/schemabuilder"
"time"
)type Person_old struct {
Id uint64 `pk:"true",autoincrement:"true"`
Name string `size:"200",unique:"true"`
Info *string
CreatedAt time.Time
UpdatedAt time.Time
}type Person_new struct {
Person_old
Json *string `size:"2048"`
Score *uint8
}type Person Person_new
type PersonIndex struct {
key1 schemabuilder.IComposedKeys `columns:"CreatedAt,UpdatedAt"`
key2 schemabuilder.IUniqueKeys `columns:"CreatedAt,UpdatedAt"`
}var builder = schemabuilder.For(schemabuilder.NewMySQLDialect("utf8", "InnoDB"))
var personSchema = builder.DefineTable(Person{}, PersonIndex{}).
AddHistory(Person_old{}, PersonIndex{}).
AddHistory(Person_new{}, PersonIndex{})func main() {
fmt.Println(personSchema.MigrateSQL("old", "new"))
// ALTER TABLE `people` ADD `json` VARCHAR(2048) , ADD `score` SMALLINT
}
```## SEE ALSO
+ gorp
+ genmai
+ and other o/r mappers## License
MIT
## Author
hirokidaichi [at] gmail.com