mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
43 lines
936 B
Cheetah
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 }} |