Files
ent/entc/gen/template/migrate/schema.tmpl
facebook-github-bot 267e3c15bd Initial commit
fbshipit-source-id: c79a38536e3c128dce1b2948615b72ec9779ed22
2019-06-16 04:37:51 -07:00

43 lines
936 B
Cheetah

{{ define "schema" }}
{{ template "header" "migrate" }}
{{ $pkg := base $.Config.Package }}
import (
"context"
"fmt"
"fbc/ent/dialect"
"fbc/ent/dialect/sql/schema"
)
// SQLDialect wraps the dialect.Driver with additional migration methods.
type SQLDriver interface {
Create(context.Context, ...*schema.Table) error
}
// Schema is the API for creating, migrating and dropping a schema.
type Schema struct {
drv SQLDriver
}
// NewSchema creates a new schema client.
func NewSchema(drv dialect.Driver) *Schema {
s := &Schema{}
switch drv.Dialect() {
case dialect.MySQL:
s.drv = &schema.MySQL{Driver: drv}
case dialect.SQLite:
s.drv = &schema.SQLite{Driver: drv}
}
return s
}
// Create creates all schema resources.
func (s *Schema) Create(ctx context.Context) error {
if s.drv == nil {
return fmt.Errorf("{{ $pkg }}/migrate: dialect does not support migration")
}
return s.drv.Create(ctx, Tables...)
}
{{ end }}