Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions entc/gen/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,30 @@ var (
},
}

// FeatureNoClientSchema keeps the migration engine off the generated
// Client, so that a binary which never migrates does not link it.
//
// `Client.Schema` is a `*migrate.Schema`, and `NewClient` builds one. That
// is an import, and the linker follows imports: the generated package pulls
// in `dialect/sql/schema`, which pulls in Atlas -- its diff planner, all
// three of its SQL dialects, and the HCL parser those import for a schema
// language most programs never write. Measured on one app compiled to
// `GOOS=js GOARCH=wasm`, that was 10.5 MB of a 57 MB module, linked to
// answer a question the program does not ask.
//
// With this on, `Client` has no `Schema` field and the caller says it:
//
// migrate.NewSchema(drv).Create(ctx)
//
// which needs the driver it already had to build the client. `migrate` is
// generated either way, so a program that does migrate is unaffected.
FeatureNoClientSchema = Feature{
Name: "sql/no-client-schema",
Stage: Experimental,
Default: false,
Description: "Keeps the migration engine off the generated Client, so a binary that never migrates does not link it",
}

// AllFeatures holds a list of all feature-flags.
AllFeatures = []Feature{
FeaturePrivacy,
Expand All @@ -174,6 +198,7 @@ var (
FeatureUpsert,
FeatureVersionedMigration,
FeatureGlobalId,
FeatureNoClientSchema,
}
// allFeatures includes all public and private features.
allFeatures = append(AllFeatures, featureMultiSchema)
Expand Down
6 changes: 4 additions & 2 deletions entc/gen/template/client.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ in the LICENSE file in the root directory of this source tree.
// Client is the client that holds all ent builders.
type Client struct {
config
{{- if $.SupportMigrate }}
{{- if and $.SupportMigrate (not ($.FeatureEnabled "sql/no-client-schema")) }}
// Schema is the client for creating, migrating and dropping schema.
Schema *migrate.Schema
{{- end }}
Expand All @@ -34,7 +34,7 @@ func NewClient(opts ...Option) *Client {
}

func (c *Client) init() {
{{- if $.SupportMigrate }}
{{- if and $.SupportMigrate (not ($.FeatureEnabled "sql/no-client-schema")) }}
c.Schema = migrate.NewSchema(c.driver)
{{- end }}
{{- range $n := $.Nodes }}
Expand All @@ -55,7 +55,9 @@ import (
"context"
"log"

{{- if not ($.FeatureEnabled "sql/no-client-schema") }}
"{{ $.Config.Package }}/migrate"
{{- end }}
{{ range $n := $.Nodes }}
{{ $n.PackageAlias }} "{{ $n.Config.Package }}/{{ $n.PackageDir }}"
{{- end }}
Expand Down
2 changes: 1 addition & 1 deletion entc/gen/template/enttest.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func NewClient(t TestingT, opts ...Option) *{{ $pkg }}.Client {
t.Error(err)
t.FailNow()
}
if err := migrate.Create(context.Background(), c.Schema, tables, o.migrateOpts...); err != nil {
if err := migrate.Create(context.Background(), {{ if $.FeatureEnabled "sql/no-client-schema" }}migrate.NewSchema(c.Driver()){{ else }}c.Schema{{ end }}, tables, o.migrateOpts...); err != nil {
t.Error(err)
t.FailNow()
}
Expand Down
Loading