diff --git a/storagenode/storagenodedb/database.go b/storagenode/storagenodedb/database.go index 564710249..e5bec79e7 100644 --- a/storagenode/storagenodedb/database.go +++ b/storagenode/storagenodedb/database.go @@ -1,7 +1,7 @@ // Copyright (C) 2019 Storj Labs, Inc. // See LICENSE for copying information. -//go:generate sh -c "go run schemagen.go > schema.go.tmp && mv schema.go.tmp schema.go" +//go:generate go run ./schemagen -o schema.go package storagenodedb diff --git a/storagenode/storagenodedb/schema.go b/storagenode/storagenodedb/schema.go index 4b5286961..a1edef394 100644 --- a/storagenode/storagenodedb/schema.go +++ b/storagenode/storagenodedb/schema.go @@ -8,53 +8,53 @@ import "storj.io/private/dbutil/dbschema" func Schema() map[string]*dbschema.Schema { return map[string]*dbschema.Schema{ - "bandwidth": &dbschema.Schema{ + "bandwidth": { Tables: []*dbschema.Table{ - &dbschema.Table{ + { Name: "bandwidth_usage", Columns: []*dbschema.Column{ - &dbschema.Column{ + { Name: "action", Type: "INTEGER", IsNullable: false, }, - &dbschema.Column{ + { Name: "amount", Type: "BIGINT", IsNullable: false, }, - &dbschema.Column{ + { Name: "created_at", Type: "TIMESTAMP", IsNullable: false, }, - &dbschema.Column{ + { Name: "satellite_id", Type: "BLOB", IsNullable: false, }, }, }, - &dbschema.Table{ + { Name: "bandwidth_usage_rollups", PrimaryKey: []string{"action", "interval_start", "satellite_id"}, Columns: []*dbschema.Column{ - &dbschema.Column{ + { Name: "action", Type: "INTEGER", IsNullable: false, }, - &dbschema.Column{ + { Name: "amount", Type: "BIGINT", IsNullable: false, }, - &dbschema.Column{ + { Name: "interval_start", Type: "TIMESTAMP", IsNullable: false, }, - &dbschema.Column{ + { Name: "satellite_id", Type: "BLOB", IsNullable: false, @@ -63,163 +63,163 @@ func Schema() map[string]*dbschema.Schema { }, }, Indexes: []*dbschema.Index{ - &dbschema.Index{Name: "idx_bandwidth_usage_created", Table: "bandwidth_usage", Columns: []string{"created_at"}, Unique: false, Partial: ""}, - &dbschema.Index{Name: "idx_bandwidth_usage_satellite", Table: "bandwidth_usage", Columns: []string{"satellite_id"}, Unique: false, Partial: ""}, + {Name: "idx_bandwidth_usage_created", Table: "bandwidth_usage", Columns: []string{"created_at"}, Unique: false, Partial: ""}, + {Name: "idx_bandwidth_usage_satellite", Table: "bandwidth_usage", Columns: []string{"satellite_id"}, Unique: false, Partial: ""}, }, }, - "heldamount": &dbschema.Schema{ + "heldamount": { Tables: []*dbschema.Table{ - &dbschema.Table{ + { Name: "payments", PrimaryKey: []string{"id"}, Columns: []*dbschema.Column{ - &dbschema.Column{ + { Name: "amount", Type: "bigint", IsNullable: false, }, - &dbschema.Column{ + { Name: "created_at", Type: "timestamp", IsNullable: false, }, - &dbschema.Column{ + { Name: "id", Type: "bigserial", IsNullable: false, }, - &dbschema.Column{ + { Name: "notes", Type: "text", IsNullable: true, }, - &dbschema.Column{ + { Name: "period", Type: "text", IsNullable: true, }, - &dbschema.Column{ + { Name: "receipt", Type: "text", IsNullable: true, }, - &dbschema.Column{ + { Name: "satellite_id", Type: "bytea", IsNullable: false, }, }, }, - &dbschema.Table{ + { Name: "paystubs", PrimaryKey: []string{"period", "satellite_id"}, Columns: []*dbschema.Column{ - &dbschema.Column{ + { Name: "codes", Type: "text", IsNullable: false, }, - &dbschema.Column{ + { Name: "comp_at_rest", Type: "bigint", IsNullable: false, }, - &dbschema.Column{ + { Name: "comp_get", Type: "bigint", IsNullable: false, }, - &dbschema.Column{ + { Name: "comp_get_audit", Type: "bigint", IsNullable: false, }, - &dbschema.Column{ + { Name: "comp_get_repair", Type: "bigint", IsNullable: false, }, - &dbschema.Column{ + { Name: "comp_put", Type: "bigint", IsNullable: false, }, - &dbschema.Column{ + { Name: "comp_put_repair", Type: "bigint", IsNullable: false, }, - &dbschema.Column{ + { Name: "created_at", Type: "timestamp", IsNullable: false, }, - &dbschema.Column{ + { Name: "disposed", Type: "bigint", IsNullable: false, }, - &dbschema.Column{ + { Name: "distributed", Type: "bigint", IsNullable: false, }, - &dbschema.Column{ + { Name: "held", Type: "bigint", IsNullable: false, }, - &dbschema.Column{ + { Name: "owed", Type: "bigint", IsNullable: false, }, - &dbschema.Column{ + { Name: "paid", Type: "bigint", IsNullable: false, }, - &dbschema.Column{ + { Name: "period", Type: "text", IsNullable: false, }, - &dbschema.Column{ + { Name: "satellite_id", Type: "bytea", IsNullable: false, }, - &dbschema.Column{ + { Name: "surge_percent", Type: "bigint", IsNullable: false, }, - &dbschema.Column{ + { Name: "usage_at_rest", Type: "double precision", IsNullable: false, }, - &dbschema.Column{ + { Name: "usage_get", Type: "bigint", IsNullable: false, }, - &dbschema.Column{ + { Name: "usage_get_audit", Type: "bigint", IsNullable: false, }, - &dbschema.Column{ + { Name: "usage_get_repair", Type: "bigint", IsNullable: false, }, - &dbschema.Column{ + { Name: "usage_put", Type: "bigint", IsNullable: false, }, - &dbschema.Column{ + { Name: "usage_put_repair", Type: "bigint", IsNullable: false, @@ -228,44 +228,44 @@ func Schema() map[string]*dbschema.Schema { }, }, }, - "info": &dbschema.Schema{}, - "notifications": &dbschema.Schema{ + "info": {}, + "notifications": { Tables: []*dbschema.Table{ - &dbschema.Table{ + { Name: "notifications", PrimaryKey: []string{"id"}, Columns: []*dbschema.Column{ - &dbschema.Column{ + { Name: "created_at", Type: "TIMESTAMP", IsNullable: false, }, - &dbschema.Column{ + { Name: "id", Type: "BLOB", IsNullable: false, }, - &dbschema.Column{ + { Name: "message", Type: "TEXT", IsNullable: false, }, - &dbschema.Column{ + { Name: "read_at", Type: "TIMESTAMP", IsNullable: true, }, - &dbschema.Column{ + { Name: "sender_id", Type: "BLOB", IsNullable: false, }, - &dbschema.Column{ + { Name: "title", Type: "TEXT", IsNullable: false, }, - &dbschema.Column{ + { Name: "type", Type: "INTEGER", IsNullable: false, @@ -274,42 +274,42 @@ func Schema() map[string]*dbschema.Schema { }, }, }, - "orders": &dbschema.Schema{ + "orders": { Tables: []*dbschema.Table{ - &dbschema.Table{ + { Name: "order_archive_", Columns: []*dbschema.Column{ - &dbschema.Column{ + { Name: "archived_at", Type: "TIMESTAMP", IsNullable: false, }, - &dbschema.Column{ + { Name: "order_limit_serialized", Type: "BLOB", IsNullable: false, }, - &dbschema.Column{ + { Name: "order_serialized", Type: "BLOB", IsNullable: false, }, - &dbschema.Column{ + { Name: "satellite_id", Type: "BLOB", IsNullable: false, }, - &dbschema.Column{ + { Name: "serial_number", Type: "BLOB", IsNullable: false, }, - &dbschema.Column{ + { Name: "status", Type: "INTEGER", IsNullable: false, }, - &dbschema.Column{ + { Name: "uplink_cert_id", Type: "INTEGER", IsNullable: false, @@ -317,35 +317,35 @@ func Schema() map[string]*dbschema.Schema { }, }, }, - &dbschema.Table{ + { Name: "unsent_order", Columns: []*dbschema.Column{ - &dbschema.Column{ + { Name: "order_limit_expiration", Type: "TIMESTAMP", IsNullable: false, }, - &dbschema.Column{ + { Name: "order_limit_serialized", Type: "BLOB", IsNullable: false, }, - &dbschema.Column{ + { Name: "order_serialized", Type: "BLOB", IsNullable: false, }, - &dbschema.Column{ + { Name: "satellite_id", Type: "BLOB", IsNullable: false, }, - &dbschema.Column{ + { Name: "serial_number", Type: "BLOB", IsNullable: false, }, - &dbschema.Column{ + { Name: "uplink_cert_id", Type: "INTEGER", IsNullable: false, @@ -355,37 +355,37 @@ func Schema() map[string]*dbschema.Schema { }, }, Indexes: []*dbschema.Index{ - &dbschema.Index{Name: "idx_order_archived_at", Table: "order_archive_", Columns: []string{"archived_at"}, Unique: false, Partial: ""}, - &dbschema.Index{Name: "idx_orders", Table: "unsent_order", Columns: []string{"satellite_id", "serial_number"}, Unique: true, Partial: ""}, + {Name: "idx_order_archived_at", Table: "order_archive_", Columns: []string{"archived_at"}, Unique: false, Partial: ""}, + {Name: "idx_orders", Table: "unsent_order", Columns: []string{"satellite_id", "serial_number"}, Unique: true, Partial: ""}, }, }, - "piece_expiration": &dbschema.Schema{ + "piece_expiration": { Tables: []*dbschema.Table{ - &dbschema.Table{ + { Name: "piece_expirations", PrimaryKey: []string{"piece_id", "satellite_id"}, Columns: []*dbschema.Column{ - &dbschema.Column{ + { Name: "deletion_failed_at", Type: "TIMESTAMP", IsNullable: true, }, - &dbschema.Column{ + { Name: "piece_expiration", Type: "TIMESTAMP", IsNullable: false, }, - &dbschema.Column{ + { Name: "piece_id", Type: "BLOB", IsNullable: false, }, - &dbschema.Column{ + { Name: "satellite_id", Type: "BLOB", IsNullable: false, }, - &dbschema.Column{ + { Name: "trash", Type: "INTEGER", IsNullable: false, @@ -394,27 +394,27 @@ func Schema() map[string]*dbschema.Schema { }, }, Indexes: []*dbschema.Index{ - &dbschema.Index{Name: "idx_piece_expirations_deletion_failed_at", Table: "piece_expirations", Columns: []string{"deletion_failed_at"}, Unique: false, Partial: ""}, - &dbschema.Index{Name: "idx_piece_expirations_piece_expiration", Table: "piece_expirations", Columns: []string{"piece_expiration"}, Unique: false, Partial: ""}, - &dbschema.Index{Name: "idx_piece_expirations_trashed", Table: "piece_expirations", Columns: []string{"satellite_id", "trash"}, Unique: false, Partial: "trash = 1"}, + {Name: "idx_piece_expirations_deletion_failed_at", Table: "piece_expirations", Columns: []string{"deletion_failed_at"}, Unique: false, Partial: ""}, + {Name: "idx_piece_expirations_piece_expiration", Table: "piece_expirations", Columns: []string{"piece_expiration"}, Unique: false, Partial: ""}, + {Name: "idx_piece_expirations_trashed", Table: "piece_expirations", Columns: []string{"satellite_id", "trash"}, Unique: false, Partial: "trash = 1"}, }, }, - "piece_spaced_used": &dbschema.Schema{ + "piece_spaced_used": { Tables: []*dbschema.Table{ - &dbschema.Table{ + { Name: "piece_space_used", Columns: []*dbschema.Column{ - &dbschema.Column{ + { Name: "content_size", Type: "INTEGER", IsNullable: false, }, - &dbschema.Column{ + { Name: "satellite_id", Type: "BLOB", IsNullable: true, }, - &dbschema.Column{ + { Name: "total", Type: "INTEGER", IsNullable: false, @@ -423,56 +423,56 @@ func Schema() map[string]*dbschema.Schema { }, }, Indexes: []*dbschema.Index{ - &dbschema.Index{Name: "idx_piece_space_used_satellite_id", Table: "piece_space_used", Columns: []string{"satellite_id"}, Unique: true, Partial: ""}, + {Name: "idx_piece_space_used_satellite_id", Table: "piece_space_used", Columns: []string{"satellite_id"}, Unique: true, Partial: ""}, }, }, - "pieceinfo": &dbschema.Schema{ + "pieceinfo": { Tables: []*dbschema.Table{ - &dbschema.Table{ + { Name: "pieceinfo_", Columns: []*dbschema.Column{ - &dbschema.Column{ + { Name: "deletion_failed_at", Type: "TIMESTAMP", IsNullable: true, }, - &dbschema.Column{ + { Name: "order_limit", Type: "BLOB", IsNullable: false, }, - &dbschema.Column{ + { Name: "piece_creation", Type: "TIMESTAMP", IsNullable: false, }, - &dbschema.Column{ + { Name: "piece_expiration", Type: "TIMESTAMP", IsNullable: true, }, - &dbschema.Column{ + { Name: "piece_id", Type: "BLOB", IsNullable: false, }, - &dbschema.Column{ + { Name: "piece_size", Type: "BIGINT", IsNullable: false, }, - &dbschema.Column{ + { Name: "satellite_id", Type: "BLOB", IsNullable: false, }, - &dbschema.Column{ + { Name: "uplink_cert_id", Type: "INTEGER", IsNullable: false, Reference: &dbschema.Reference{Table: "certificate", Column: "cert_id", OnDelete: "", OnUpdate: ""}, }, - &dbschema.Column{ + { Name: "uplink_piece_hash", Type: "BLOB", IsNullable: false, @@ -481,37 +481,37 @@ func Schema() map[string]*dbschema.Schema { }, }, Indexes: []*dbschema.Index{ - &dbschema.Index{Name: "idx_pieceinfo__expiration", Table: "pieceinfo_", Columns: []string{"piece_expiration"}, Unique: false, Partial: "piece_expiration IS NOT NULL"}, - &dbschema.Index{Name: "pk_pieceinfo_", Table: "pieceinfo_", Columns: []string{"satellite_id", "piece_id"}, Unique: true, Partial: ""}, + {Name: "idx_pieceinfo__expiration", Table: "pieceinfo_", Columns: []string{"piece_expiration"}, Unique: false, Partial: "piece_expiration IS NOT NULL"}, + {Name: "pk_pieceinfo_", Table: "pieceinfo_", Columns: []string{"satellite_id", "piece_id"}, Unique: true, Partial: ""}, }, }, - "pricing": &dbschema.Schema{ + "pricing": { Tables: []*dbschema.Table{ - &dbschema.Table{ + { Name: "pricing", PrimaryKey: []string{"satellite_id"}, Columns: []*dbschema.Column{ - &dbschema.Column{ + { Name: "audit_bandwidth_price", Type: "bigint", IsNullable: false, }, - &dbschema.Column{ + { Name: "disk_space_price", Type: "bigint", IsNullable: false, }, - &dbschema.Column{ + { Name: "egress_bandwidth_price", Type: "bigint", IsNullable: false, }, - &dbschema.Column{ + { Name: "repair_bandwidth_price", Type: "bigint", IsNullable: false, }, - &dbschema.Column{ + { Name: "satellite_id", Type: "BLOB", IsNullable: false, @@ -520,98 +520,98 @@ func Schema() map[string]*dbschema.Schema { }, }, }, - "reputation": &dbschema.Schema{ + "reputation": { Tables: []*dbschema.Table{ - &dbschema.Table{ + { Name: "reputation", PrimaryKey: []string{"satellite_id"}, Columns: []*dbschema.Column{ - &dbschema.Column{ + { Name: "audit_history", Type: "BLOB", IsNullable: true, }, - &dbschema.Column{ + { Name: "audit_reputation_alpha", Type: "REAL", IsNullable: false, }, - &dbschema.Column{ + { Name: "audit_reputation_beta", Type: "REAL", IsNullable: false, }, - &dbschema.Column{ + { Name: "audit_reputation_score", Type: "REAL", IsNullable: false, }, - &dbschema.Column{ + { Name: "audit_success_count", Type: "INTEGER", IsNullable: false, }, - &dbschema.Column{ + { Name: "audit_total_count", Type: "INTEGER", IsNullable: false, }, - &dbschema.Column{ + { Name: "audit_unknown_reputation_alpha", Type: "REAL", IsNullable: false, }, - &dbschema.Column{ + { Name: "audit_unknown_reputation_beta", Type: "REAL", IsNullable: false, }, - &dbschema.Column{ + { Name: "audit_unknown_reputation_score", Type: "REAL", IsNullable: false, }, - &dbschema.Column{ + { Name: "disqualified_at", Type: "TIMESTAMP", IsNullable: true, }, - &dbschema.Column{ + { Name: "joined_at", Type: "TIMESTAMP", IsNullable: false, }, - &dbschema.Column{ + { Name: "offline_suspended_at", Type: "TIMESTAMP", IsNullable: true, }, - &dbschema.Column{ + { Name: "offline_under_review_at", Type: "TIMESTAMP", IsNullable: true, }, - &dbschema.Column{ + { Name: "online_score", Type: "REAL", IsNullable: false, }, - &dbschema.Column{ + { Name: "satellite_id", Type: "BLOB", IsNullable: false, }, - &dbschema.Column{ + { Name: "suspended_at", Type: "TIMESTAMP", IsNullable: true, }, - &dbschema.Column{ + { Name: "updated_at", Type: "TIMESTAMP", IsNullable: false, }, - &dbschema.Column{ + { Name: "vetted_at", Type: "TIMESTAMP", IsNullable: true, @@ -620,64 +620,64 @@ func Schema() map[string]*dbschema.Schema { }, }, }, - "satellites": &dbschema.Schema{ + "satellites": { Tables: []*dbschema.Table{ - &dbschema.Table{ + { Name: "satellite_exit_progress", Columns: []*dbschema.Column{ - &dbschema.Column{ + { Name: "bytes_deleted", Type: "INTEGER", IsNullable: false, }, - &dbschema.Column{ + { Name: "completion_receipt", Type: "BLOB", IsNullable: true, }, - &dbschema.Column{ + { Name: "finished_at", Type: "TIMESTAMP", IsNullable: true, }, - &dbschema.Column{ + { Name: "initiated_at", Type: "TIMESTAMP", IsNullable: true, }, - &dbschema.Column{ + { Name: "satellite_id", Type: "BLOB", IsNullable: false, Reference: &dbschema.Reference{Table: "satellites", Column: "node_id", OnDelete: "", OnUpdate: ""}, }, - &dbschema.Column{ + { Name: "starting_disk_usage", Type: "INTEGER", IsNullable: false, }, }, }, - &dbschema.Table{ + { Name: "satellites", PrimaryKey: []string{"node_id"}, Columns: []*dbschema.Column{ - &dbschema.Column{ + { Name: "added_at", Type: "TIMESTAMP", IsNullable: false, }, - &dbschema.Column{ + { Name: "address", Type: "TEXT", IsNullable: true, }, - &dbschema.Column{ + { Name: "node_id", Type: "BLOB", IsNullable: false, }, - &dbschema.Column{ + { Name: "status", Type: "INTEGER", IsNullable: false, @@ -686,18 +686,18 @@ func Schema() map[string]*dbschema.Schema { }, }, }, - "secret": &dbschema.Schema{ + "secret": { Tables: []*dbschema.Table{ - &dbschema.Table{ + { Name: "secret", PrimaryKey: []string{"token"}, Columns: []*dbschema.Column{ - &dbschema.Column{ + { Name: "created_at", Type: "timestamp with time zone", IsNullable: false, }, - &dbschema.Column{ + { Name: "token", Type: "bytea", IsNullable: false, @@ -706,23 +706,23 @@ func Schema() map[string]*dbschema.Schema { }, }, }, - "storage_usage": &dbschema.Schema{ + "storage_usage": { Tables: []*dbschema.Table{ - &dbschema.Table{ + { Name: "storage_usage", PrimaryKey: []string{"interval_start", "satellite_id"}, Columns: []*dbschema.Column{ - &dbschema.Column{ + { Name: "at_rest_total", Type: "REAL", IsNullable: false, }, - &dbschema.Column{ + { Name: "interval_start", Type: "TIMESTAMP", IsNullable: false, }, - &dbschema.Column{ + { Name: "satellite_id", Type: "BLOB", IsNullable: false, @@ -731,6 +731,6 @@ func Schema() map[string]*dbschema.Schema { }, }, }, - "used_serial": &dbschema.Schema{}, + "used_serial": {}, } } diff --git a/storagenode/storagenodedb/schemagen.go b/storagenode/storagenodedb/schemagen/main.go similarity index 72% rename from storagenode/storagenodedb/schemagen.go rename to storagenode/storagenodedb/schemagen/main.go index 1c79bcbfc..52b43a246 100644 --- a/storagenode/storagenodedb/schemagen.go +++ b/storagenode/storagenodedb/schemagen/main.go @@ -1,14 +1,12 @@ // Copyright (C) 2020 Storj Labs, Inc. // See LICENSE for copying information. -//go:build ignore -// +build ignore - package main import ( "bytes" "context" + "flag" "fmt" "go/format" "io" @@ -16,6 +14,7 @@ import ( "os" "path/filepath" "sort" + "strings" "github.com/zeebo/errs" "go.uber.org/zap" @@ -26,20 +25,40 @@ import ( ) func main() { + outfile := flag.String("o", "", "output file") + flag.Parse() + ctx := context.Background() log := zap.L() - err := runSchemaGen(ctx, log) + out, err := runSchemaGen(ctx, log) if err != nil { + printWithLines(os.Stderr, out) fmt.Fprintf(os.Stderr, "%v", err) os.Exit(1) } + + if *outfile == "" { + fmt.Print(string(out)) + } else { + err := os.WriteFile(*outfile, out, 0644) + if err != nil { + fmt.Fprintf(os.Stderr, "%v", err) + os.Exit(1) + } + } } -func runSchemaGen(ctx context.Context, log *zap.Logger) (err error) { +func printWithLines(w io.Writer, data []byte) { + for i, line := range strings.Split(string(data), "\n") { + fmt.Fprintf(w, "%3d: %s\n", i, line) + } +} + +func runSchemaGen(ctx context.Context, log *zap.Logger) (_ []byte, err error) { storagePath, err := ioutil.TempDir("", "testdb") if err != nil { - return errs.New("Error getting test storage path: %+w", err) + return nil, errs.New("Error getting test storage path: %+w", err) } defer func() { removeErr := os.RemoveAll(storagePath) @@ -55,7 +74,7 @@ func runSchemaGen(ctx context.Context, log *zap.Logger) (err error) { Pieces: storagePath, }) if err != nil { - return errs.New("Error creating new storagenode db: %+w", err) + return nil, errs.New("Error creating new storagenode db: %+w", err) } defer func() { closeErr := db.Close() @@ -66,7 +85,7 @@ func runSchemaGen(ctx context.Context, log *zap.Logger) (err error) { err = db.MigrateToLatest(ctx) if err != nil { - return errs.New("Error creating tables for storagenode db: %+w", err) + return nil, errs.New("Error creating tables for storagenode db: %+w", err) } // get schemas @@ -78,7 +97,7 @@ func runSchemaGen(ctx context.Context, log *zap.Logger) (err error) { nextDB := dbContainer.GetDB() schema, err := sqliteutil.QuerySchema(ctx, nextDB) if err != nil { - return errs.New("Error getting schema for db: %+w", err) + return nil, errs.New("Error getting schema for db: %+w", err) } // we don't care about changes in versions table schema.DropTable("versions") @@ -119,16 +138,16 @@ func runSchemaGen(ctx context.Context, log *zap.Logger) (err error) { for _, schemaName := range schemaList { schema := allSchemas[schemaName] (func() { - printf("%q: &dbschema.Schema{\n", schemaName) + printf("%q: {\n", schemaName) defer printf("},\n") - writeErr := WriteSchemaGoStruct(&buf, schema) + writeErr := writeSchemaGoStruct(&buf, schema) if writeErr != nil { err = errs.New("Error writing schema struct: %+w", writeErr) } })() if err != nil { - return err + return nil, err } } @@ -139,14 +158,13 @@ func runSchemaGen(ctx context.Context, log *zap.Logger) (err error) { formatted, err := format.Source(buf.Bytes()) if err != nil { - return errs.New("Error formatting: %+w", err) + return buf.Bytes(), errs.New("Error formatting: %+w", err) } - fmt.Println(string(formatted)) - return err + return formatted, nil } -func WriteSchemaGoStruct(w io.Writer, schema *dbschema.Schema) (err error) { +func writeSchemaGoStruct(w io.Writer, schema *dbschema.Schema) (err error) { printf := func(format string, args ...interface{}) { if err != nil { return @@ -160,7 +178,7 @@ func WriteSchemaGoStruct(w io.Writer, schema *dbschema.Schema) (err error) { defer printf("},\n") for _, table := range schema.Tables { - err = WriteTableGoStruct(w, table) + err = writeTableGoStruct(w, table) if err != nil { return } @@ -175,7 +193,7 @@ func WriteSchemaGoStruct(w io.Writer, schema *dbschema.Schema) (err error) { defer printf("},\n") for _, index := range schema.Indexes { - printf("%#v,\n", index) + printf("%v,\n", prettyValue(index)) } })() } @@ -183,7 +201,7 @@ func WriteSchemaGoStruct(w io.Writer, schema *dbschema.Schema) (err error) { return err } -func WriteTableGoStruct(w io.Writer, table *dbschema.Table) (err error) { +func writeTableGoStruct(w io.Writer, table *dbschema.Table) (err error) { printf := func(format string, args ...interface{}) { if err != nil { return @@ -191,7 +209,7 @@ func WriteTableGoStruct(w io.Writer, table *dbschema.Table) (err error) { _, err = fmt.Fprintf(w, format, args...) } - printf("&dbschema.Table{\n") + printf("{\n") defer printf("}") printf("Name: %q,\n", table.Name) @@ -199,7 +217,7 @@ func WriteTableGoStruct(w io.Writer, table *dbschema.Table) (err error) { printf("PrimaryKey: %#v,\n", table.PrimaryKey) } if table.Unique != nil { - printf("Unique: %#v,\n", table.Unique) + printf("Unique: %v,\n", prettyValue(table.Unique)) } if len(table.Columns) > 0 { (func() { @@ -207,7 +225,7 @@ func WriteTableGoStruct(w io.Writer, table *dbschema.Table) (err error) { defer printf("},\n") for _, column := range table.Columns { - err = WriteColumnGoStruct(w, column) + err = writeColumnGoStruct(w, column) if err != nil { return } @@ -218,7 +236,7 @@ func WriteTableGoStruct(w io.Writer, table *dbschema.Table) (err error) { return err } -func WriteColumnGoStruct(w io.Writer, column *dbschema.Column) (err error) { +func writeColumnGoStruct(w io.Writer, column *dbschema.Column) (err error) { printf := func(format string, args ...interface{}) { if err != nil { return @@ -226,7 +244,7 @@ func WriteColumnGoStruct(w io.Writer, column *dbschema.Column) (err error) { _, err = fmt.Fprintf(w, format, args...) } - printf("&dbschema.Column{\n") + printf("{\n") defer printf("},\n") printf("Name: %q,\n", column.Name) @@ -238,3 +256,11 @@ func WriteColumnGoStruct(w io.Writer, column *dbschema.Column) (err error) { return err } + +// prettyValue converts to string without the outer type +// definition. +func prettyValue(v interface{}) string { + s := fmt.Sprintf("%#v", v) + p := strings.Index(s, "{") + return s[p:] +}