From a6339c7b5af692c5c450260ee8f3236d14db9b29 Mon Sep 17 00:00:00 2001 From: pluja Date: Fri, 10 Jan 2025 14:19:32 +0100 Subject: [PATCH] database upgrades and fix default timestamp --- src/go.sum | 9 ++++++ src/internal/ent/change.go | 12 +------- src/internal/ent/change/change.go | 5 ++-- src/internal/ent/change/where.go | 2 +- src/internal/ent/change_create.go | 4 +-- src/internal/ent/change_query.go | 8 +++--- src/internal/ent/change_update.go | 43 +++------------------------- src/internal/ent/client.go | 2 +- src/internal/ent/migrate/schema.go | 16 +++-------- src/internal/ent/schema/attribute.go | 8 +++++- src/internal/ent/schema/change.go | 14 +++++++-- src/internal/ent/schema/service.go | 8 +++++- 12 files changed, 54 insertions(+), 77 deletions(-) diff --git a/src/go.sum b/src/go.sum index 031217f..1d649c2 100644 --- a/src/go.sum +++ b/src/go.sum @@ -32,6 +32,7 @@ github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuP github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4= github.com/bmatcuk/doublestar v1.3.4 h1:gPypJ5xD31uhX6Tf54sDPUOBXTqKH4c9aPY66CyQrS0= github.com/bmatcuk/doublestar v1.3.4/go.mod h1:wiQtGV+rzVYxB7WIlirSN++5HPtPlXEo9MEoZQC/PmE= +github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -113,6 +114,8 @@ github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxec github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= +github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y= github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/microcosm-cc/bluemonday v1.0.27 h1:MpEUotklkwCSLeH+Qdx1VJgNqLlpY2KXwXFM08ygZfk= @@ -123,6 +126,8 @@ github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6 github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= +github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= +github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= github.com/phuslu/log v1.0.113 h1:Koq5A+8ourLX4vhkhW4HCJjo+jEtzMDhqvUUid/5m24= github.com/phuslu/log v1.0.113/go.mod h1:F8osGJADo5qLK/0F88djWwdyoZZ9xDJQL1HYRHFEkS0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= @@ -144,6 +149,10 @@ github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAm github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I= +github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= diff --git a/src/internal/ent/change.go b/src/internal/ent/change.go index 95cadf9..ee07b63 100644 --- a/src/internal/ent/change.go +++ b/src/internal/ent/change.go @@ -29,7 +29,6 @@ type Change struct { // Edges holds the relations/edges for other nodes in the graph. // The values are being populated by the ChangeQuery when eager-loading is set. Edges ChangeEdges `json:"edges"` - change_service *string service_changes *string selectValues sql.SelectValues } @@ -67,9 +66,7 @@ func (*Change) scanValues(columns []string) ([]any, error) { values[i] = new(sql.NullString) case change.FieldCreatedAt: values[i] = new(sql.NullTime) - case change.ForeignKeys[0]: // change_service - values[i] = new(sql.NullString) - case change.ForeignKeys[1]: // service_changes + case change.ForeignKeys[0]: // service_changes values[i] = new(sql.NullString) default: values[i] = new(sql.UnknownType) @@ -117,13 +114,6 @@ func (c *Change) assignValues(columns []string, values []any) error { c.Warning = value.Bool } case change.ForeignKeys[0]: - if value, ok := values[i].(*sql.NullString); !ok { - return fmt.Errorf("unexpected type %T for field change_service", values[i]) - } else if value.Valid { - c.change_service = new(string) - *c.change_service = value.String - } - case change.ForeignKeys[1]: if value, ok := values[i].(*sql.NullString); !ok { return fmt.Errorf("unexpected type %T for field service_changes", values[i]) } else if value.Valid { diff --git a/src/internal/ent/change/change.go b/src/internal/ent/change/change.go index e16505d..73b854a 100644 --- a/src/internal/ent/change/change.go +++ b/src/internal/ent/change/change.go @@ -32,7 +32,7 @@ const ( // It exists in this package in order to avoid circular dependency with the "service" package. ServiceInverseTable = "services" // ServiceColumn is the table column denoting the service relation/edge. - ServiceColumn = "change_service" + ServiceColumn = "service_changes" ) // Columns holds all SQL columns for change fields. @@ -47,7 +47,6 @@ var Columns = []string{ // ForeignKeys holds the SQL foreign-keys that are owned by the "changes" // table and are not defined as standalone fields in the schema. var ForeignKeys = []string{ - "change_service", "service_changes", } @@ -111,6 +110,6 @@ func newServiceStep() *sqlgraph.Step { return sqlgraph.NewStep( sqlgraph.From(Table, FieldID), sqlgraph.To(ServiceInverseTable, FieldID), - sqlgraph.Edge(sqlgraph.M2O, false, ServiceTable, ServiceColumn), + sqlgraph.Edge(sqlgraph.M2O, true, ServiceTable, ServiceColumn), ) } diff --git a/src/internal/ent/change/where.go b/src/internal/ent/change/where.go index 63c8336..f3da465 100644 --- a/src/internal/ent/change/where.go +++ b/src/internal/ent/change/where.go @@ -280,7 +280,7 @@ func HasService() predicate.Change { return predicate.Change(func(s *sql.Selector) { step := sqlgraph.NewStep( sqlgraph.From(Table, FieldID), - sqlgraph.Edge(sqlgraph.M2O, false, ServiceTable, ServiceColumn), + sqlgraph.Edge(sqlgraph.M2O, true, ServiceTable, ServiceColumn), ) sqlgraph.HasNeighbors(s, step) }) diff --git a/src/internal/ent/change_create.go b/src/internal/ent/change_create.go index 6e8411f..9444ad6 100644 --- a/src/internal/ent/change_create.go +++ b/src/internal/ent/change_create.go @@ -194,7 +194,7 @@ func (cc *ChangeCreate) createSpec() (*Change, *sqlgraph.CreateSpec) { if nodes := cc.mutation.ServiceIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, - Inverse: false, + Inverse: true, Table: change.ServiceTable, Columns: []string{change.ServiceColumn}, Bidi: false, @@ -205,7 +205,7 @@ func (cc *ChangeCreate) createSpec() (*Change, *sqlgraph.CreateSpec) { for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } - _node.change_service = &nodes[0] + _node.service_changes = &nodes[0] _spec.Edges = append(_spec.Edges, edge) } return _node, _spec diff --git a/src/internal/ent/change_query.go b/src/internal/ent/change_query.go index fb3c7f5..b16cc17 100644 --- a/src/internal/ent/change_query.go +++ b/src/internal/ent/change_query.go @@ -75,7 +75,7 @@ func (cq *ChangeQuery) QueryService() *ServiceQuery { step := sqlgraph.NewStep( sqlgraph.From(change.Table, change.FieldID, selector), sqlgraph.To(service.Table, service.FieldID), - sqlgraph.Edge(sqlgraph.M2O, false, change.ServiceTable, change.ServiceColumn), + sqlgraph.Edge(sqlgraph.M2O, true, change.ServiceTable, change.ServiceColumn), ) fromU = sqlgraph.SetNeighbors(cq.driver.Dialect(), step) return fromU, nil @@ -413,10 +413,10 @@ func (cq *ChangeQuery) loadService(ctx context.Context, query *ServiceQuery, nod ids := make([]string, 0, len(nodes)) nodeids := make(map[string][]*Change) for i := range nodes { - if nodes[i].change_service == nil { + if nodes[i].service_changes == nil { continue } - fk := *nodes[i].change_service + fk := *nodes[i].service_changes if _, ok := nodeids[fk]; !ok { ids = append(ids, fk) } @@ -433,7 +433,7 @@ func (cq *ChangeQuery) loadService(ctx context.Context, query *ServiceQuery, nod for _, n := range neighbors { nodes, ok := nodeids[n.ID] if !ok { - return fmt.Errorf(`unexpected foreign-key "change_service" returned %v`, n.ID) + return fmt.Errorf(`unexpected foreign-key "service_changes" returned %v`, n.ID) } for i := range nodes { assign(nodes[i], n) diff --git a/src/internal/ent/change_update.go b/src/internal/ent/change_update.go index 700dd7d..52a14f7 100644 --- a/src/internal/ent/change_update.go +++ b/src/internal/ent/change_update.go @@ -6,7 +6,6 @@ import ( "context" "errors" "fmt" - "time" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -69,20 +68,6 @@ func (cu *ChangeUpdate) ClearSummary() *ChangeUpdate { return cu } -// SetCreatedAt sets the "createdAt" field. -func (cu *ChangeUpdate) SetCreatedAt(t time.Time) *ChangeUpdate { - cu.mutation.SetCreatedAt(t) - return cu -} - -// SetNillableCreatedAt sets the "createdAt" field if the given value is not nil. -func (cu *ChangeUpdate) SetNillableCreatedAt(t *time.Time) *ChangeUpdate { - if t != nil { - cu.SetCreatedAt(*t) - } - return cu -} - // SetWarning sets the "warning" field. func (cu *ChangeUpdate) SetWarning(b bool) *ChangeUpdate { cu.mutation.SetWarning(b) @@ -175,16 +160,13 @@ func (cu *ChangeUpdate) sqlSave(ctx context.Context) (n int, err error) { if cu.mutation.SummaryCleared() { _spec.ClearField(change.FieldSummary, field.TypeString) } - if value, ok := cu.mutation.CreatedAt(); ok { - _spec.SetField(change.FieldCreatedAt, field.TypeTime, value) - } if value, ok := cu.mutation.Warning(); ok { _spec.SetField(change.FieldWarning, field.TypeBool, value) } if cu.mutation.ServiceCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, - Inverse: false, + Inverse: true, Table: change.ServiceTable, Columns: []string{change.ServiceColumn}, Bidi: false, @@ -197,7 +179,7 @@ func (cu *ChangeUpdate) sqlSave(ctx context.Context) (n int, err error) { if nodes := cu.mutation.ServiceIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, - Inverse: false, + Inverse: true, Table: change.ServiceTable, Columns: []string{change.ServiceColumn}, Bidi: false, @@ -270,20 +252,6 @@ func (cuo *ChangeUpdateOne) ClearSummary() *ChangeUpdateOne { return cuo } -// SetCreatedAt sets the "createdAt" field. -func (cuo *ChangeUpdateOne) SetCreatedAt(t time.Time) *ChangeUpdateOne { - cuo.mutation.SetCreatedAt(t) - return cuo -} - -// SetNillableCreatedAt sets the "createdAt" field if the given value is not nil. -func (cuo *ChangeUpdateOne) SetNillableCreatedAt(t *time.Time) *ChangeUpdateOne { - if t != nil { - cuo.SetCreatedAt(*t) - } - return cuo -} - // SetWarning sets the "warning" field. func (cuo *ChangeUpdateOne) SetWarning(b bool) *ChangeUpdateOne { cuo.mutation.SetWarning(b) @@ -406,16 +374,13 @@ func (cuo *ChangeUpdateOne) sqlSave(ctx context.Context) (_node *Change, err err if cuo.mutation.SummaryCleared() { _spec.ClearField(change.FieldSummary, field.TypeString) } - if value, ok := cuo.mutation.CreatedAt(); ok { - _spec.SetField(change.FieldCreatedAt, field.TypeTime, value) - } if value, ok := cuo.mutation.Warning(); ok { _spec.SetField(change.FieldWarning, field.TypeBool, value) } if cuo.mutation.ServiceCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, - Inverse: false, + Inverse: true, Table: change.ServiceTable, Columns: []string{change.ServiceColumn}, Bidi: false, @@ -428,7 +393,7 @@ func (cuo *ChangeUpdateOne) sqlSave(ctx context.Context) (_node *Change, err err if nodes := cuo.mutation.ServiceIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, - Inverse: false, + Inverse: true, Table: change.ServiceTable, Columns: []string{change.ServiceColumn}, Bidi: false, diff --git a/src/internal/ent/client.go b/src/internal/ent/client.go index 3f02b48..2555826 100644 --- a/src/internal/ent/client.go +++ b/src/internal/ent/client.go @@ -625,7 +625,7 @@ func (c *ChangeClient) QueryService(ch *Change) *ServiceQuery { step := sqlgraph.NewStep( sqlgraph.From(change.Table, change.FieldID, id), sqlgraph.To(service.Table, service.FieldID), - sqlgraph.Edge(sqlgraph.M2O, false, change.ServiceTable, change.ServiceColumn), + sqlgraph.Edge(sqlgraph.M2O, true, change.ServiceTable, change.ServiceColumn), ) fromV = sqlgraph.Neighbors(ch.driver.Dialect(), step) return fromV, nil diff --git a/src/internal/ent/migrate/schema.go b/src/internal/ent/migrate/schema.go index c7b0a37..cb45439 100644 --- a/src/internal/ent/migrate/schema.go +++ b/src/internal/ent/migrate/schema.go @@ -33,7 +33,7 @@ var ( // AttributesColumns holds the columns for the "attributes" table. AttributesColumns = []*schema.Column{ {Name: "id", Type: field.TypeString}, - {Name: "created_at", Type: field.TypeTime}, + {Name: "created_at", Type: field.TypeTime, Default: "CURRENT_TIMESTAMP"}, {Name: "title", Type: field.TypeString}, {Name: "bonus", Type: field.TypeFloat64, Default: 0}, {Name: "slug", Type: field.TypeString, Unique: true}, @@ -63,9 +63,8 @@ var ( {Name: "id", Type: field.TypeInt, Increment: true}, {Name: "title", Type: field.TypeString, Nullable: true}, {Name: "summary", Type: field.TypeString, Nullable: true, Size: 2147483647}, - {Name: "created_at", Type: field.TypeTime}, + {Name: "created_at", Type: field.TypeTime, Default: "CURRENT_TIMESTAMP"}, {Name: "warning", Type: field.TypeBool, Default: false}, - {Name: "change_service", Type: field.TypeString, Nullable: true}, {Name: "service_changes", Type: field.TypeString, Nullable: true}, } // ChangesTable holds the schema information for the "changes" table. @@ -74,15 +73,9 @@ var ( Columns: ChangesColumns, PrimaryKey: []*schema.Column{ChangesColumns[0]}, ForeignKeys: []*schema.ForeignKey{ - { - Symbol: "changes_services_service", - Columns: []*schema.Column{ChangesColumns[5]}, - RefColumns: []*schema.Column{ServicesColumns[0]}, - OnDelete: schema.SetNull, - }, { Symbol: "changes_services_changes", - Columns: []*schema.Column{ChangesColumns[6]}, + Columns: []*schema.Column{ChangesColumns[5]}, RefColumns: []*schema.Column{ServicesColumns[0]}, OnDelete: schema.SetNull, }, @@ -91,7 +84,7 @@ var ( // ServicesColumns holds the columns for the "services" table. ServicesColumns = []*schema.Column{ {Name: "id", Type: field.TypeString}, - {Name: "created_at", Type: field.TypeTime}, + {Name: "created_at", Type: field.TypeTime, Default: "CURRENT_TIMESTAMP"}, {Name: "name", Type: field.TypeString}, {Name: "slug", Type: field.TypeString, Unique: true}, {Name: "description", Type: field.TypeString, Size: 532}, @@ -195,7 +188,6 @@ var ( func init() { ChangesTable.ForeignKeys[0].RefTable = ServicesTable - ChangesTable.ForeignKeys[1].RefTable = ServicesTable ServiceAttributesTable.ForeignKeys[0].RefTable = ServicesTable ServiceAttributesTable.ForeignKeys[1].RefTable = AttributesTable } diff --git a/src/internal/ent/schema/attribute.go b/src/internal/ent/schema/attribute.go index e64ed52..072764e 100644 --- a/src/internal/ent/schema/attribute.go +++ b/src/internal/ent/schema/attribute.go @@ -4,6 +4,7 @@ import ( "time" "entgo.io/ent" + "entgo.io/ent/dialect/entsql" "entgo.io/ent/schema/edge" "entgo.io/ent/schema/field" "entgo.io/ent/schema/index" @@ -20,7 +21,12 @@ type Attribute struct { func (Attribute) Fields() []ent.Field { return []ent.Field{ field.String("id").DefaultFunc(ulid.UniqueULID).Immutable().StructTag(`json:"id"`), - field.Time("createdAt").Default(time.Now).StructTag(`json:"createdAt"`), + field.Time("createdAt"). + Default(time.Now). + StructTag(`json:"createdAt"`). + Annotations( + entsql.Default("CURRENT_TIMESTAMP"), + ), field.String("title"). Comment("Title of the attribute"). StructTag(`json:"title,omitempty"`), diff --git a/src/internal/ent/schema/change.go b/src/internal/ent/schema/change.go index bf53967..753fb49 100644 --- a/src/internal/ent/schema/change.go +++ b/src/internal/ent/schema/change.go @@ -4,6 +4,7 @@ import ( "time" "entgo.io/ent" + "entgo.io/ent/dialect/entsql" "entgo.io/ent/schema/edge" "entgo.io/ent/schema/field" ) @@ -22,8 +23,17 @@ func (Change) Fields() []ent.Field { Optional(). Comment("A summary of the change"). StructTag(`json:"Summary,omitempty"`), - field.Time("createdAt").Default(time.Now).StructTag(`json:"createdAt"`), - field.Bool("warning").Default(false).StructTag(`json:"warning"`), + field.Time("createdAt"). + Default(time.Now). + StructTag(`json:"createdAt"`). + Immutable(). + Annotations( + entsql.Default("CURRENT_TIMESTAMP"), + ), + + field.Bool("warning"). + Default(false). + StructTag(`json:"warning"`), } } diff --git a/src/internal/ent/schema/service.go b/src/internal/ent/schema/service.go index 45c89a8..85fa9f3 100644 --- a/src/internal/ent/schema/service.go +++ b/src/internal/ent/schema/service.go @@ -4,6 +4,7 @@ import ( "time" "entgo.io/ent" + "entgo.io/ent/dialect/entsql" "entgo.io/ent/schema/edge" "entgo.io/ent/schema/field" "entgo.io/ent/schema/index" @@ -34,7 +35,12 @@ type SupportMethod struct { func (Service) Fields() []ent.Field { return []ent.Field{ field.String("id").DefaultFunc(ulid.UniqueULID).Immutable().StructTag(`json:"id"`), - field.Time("createdAt").Default(time.Now).StructTag(`json:"createdAt"`), + field.Time("createdAt"). + Default(time.Now). + StructTag(`json:"createdAt"`). + Annotations( + entsql.Default("CURRENT_TIMESTAMP"), + ), field.String("name"). Comment("The name of the service"). StructTag(`json:"name,omitempty"`),