From 926ae518b0f93e4135c21b1a92e1ad279e0a3a83 Mon Sep 17 00:00:00 2001 From: Jakub Date: Mon, 26 Sep 2022 11:52:26 +0200 Subject: [PATCH] fix(GODT-1600): increase UIDVALIDITY for folder names created again. --- README.md | 9 + internal/db/ent/client.go | 97 ++++ internal/db/ent/config.go | 1 + internal/db/ent/ent.go | 2 + internal/db/ent/hook/hook.go | 13 + internal/db/ent/migrate/schema.go | 12 + internal/db/ent/mutation.go | 349 ++++++++++++++ internal/db/ent/predicate/predicate.go | 3 + internal/db/ent/schema/uidvalidity.go | 25 + internal/db/ent/tx.go | 3 + internal/db/ent/uidvalidity.go | 98 ++++ internal/db/ent/uidvalidity/uidvalidity.go | 30 ++ internal/db/ent/uidvalidity/where.go | 190 ++++++++ internal/db/ent/uidvalidity_create.go | 227 +++++++++ internal/db/ent/uidvalidity_delete.go | 115 +++++ internal/db/ent/uidvalidity_query.go | 528 +++++++++++++++++++++ internal/db/ent/uidvalidity_update.go | 300 ++++++++++++ internal/db/mailbox.go | 61 +++ tests/create_test.go | 42 +- 19 files changed, 2090 insertions(+), 15 deletions(-) create mode 100644 internal/db/ent/schema/uidvalidity.go create mode 100644 internal/db/ent/uidvalidity.go create mode 100644 internal/db/ent/uidvalidity/uidvalidity.go create mode 100644 internal/db/ent/uidvalidity/where.go create mode 100644 internal/db/ent/uidvalidity_create.go create mode 100644 internal/db/ent/uidvalidity_delete.go create mode 100644 internal/db/ent/uidvalidity_query.go create mode 100644 internal/db/ent/uidvalidity_update.go diff --git a/README.md b/README.md index a5304157..2eaf6b99 100644 --- a/README.md +++ b/README.md @@ -51,3 +51,12 @@ tag OK [CAPABILITY IDLE IMAP4rev1 MOVE UIDPLUS UNSELECT] (^_^) The demo accounts contain no messages. You can connect an IMAP client (e.g. thunderbird) and use it to copy in messages from another mail server. + + +# Changing DB schema + +Do not forget to re-generate ent code after changing the DB schema in `./internal/db/ent/schema`. + +``` +pushd ./internal/db/ent && go generate . && popd + diff --git a/internal/db/ent/client.go b/internal/db/ent/client.go index 77848974..aad46b52 100644 --- a/internal/db/ent/client.go +++ b/internal/db/ent/client.go @@ -18,6 +18,7 @@ import ( "github.com/ProtonMail/gluon/internal/db/ent/message" "github.com/ProtonMail/gluon/internal/db/ent/messageflag" "github.com/ProtonMail/gluon/internal/db/ent/uid" + "github.com/ProtonMail/gluon/internal/db/ent/uidvalidity" "entgo.io/ent/dialect" "entgo.io/ent/dialect/sql" @@ -43,6 +44,8 @@ type Client struct { MessageFlag *MessageFlagClient // UID is the client for interacting with the UID builders. UID *UIDClient + // UIDValidity is the client for interacting with the UIDValidity builders. + UIDValidity *UIDValidityClient } // NewClient creates a new client configured with the given options. @@ -63,6 +66,7 @@ func (c *Client) init() { c.Message = NewMessageClient(c.config) c.MessageFlag = NewMessageFlagClient(c.config) c.UID = NewUIDClient(c.config) + c.UIDValidity = NewUIDValidityClient(c.config) } // Open opens a database/sql.DB specified by the driver name and @@ -103,6 +107,7 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) { Message: NewMessageClient(cfg), MessageFlag: NewMessageFlagClient(cfg), UID: NewUIDClient(cfg), + UIDValidity: NewUIDValidityClient(cfg), }, nil } @@ -129,6 +134,7 @@ func (c *Client) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error) Message: NewMessageClient(cfg), MessageFlag: NewMessageFlagClient(cfg), UID: NewUIDClient(cfg), + UIDValidity: NewUIDValidityClient(cfg), }, nil } @@ -165,6 +171,7 @@ func (c *Client) Use(hooks ...Hook) { c.Message.Use(hooks...) c.MessageFlag.Use(hooks...) c.UID.Use(hooks...) + c.UIDValidity.Use(hooks...) } // MailboxClient is a client for the Mailbox schema. @@ -940,3 +947,93 @@ func (c *UIDClient) QueryMailbox(u *UID) *MailboxQuery { func (c *UIDClient) Hooks() []Hook { return c.hooks.UID } + +// UIDValidityClient is a client for the UIDValidity schema. +type UIDValidityClient struct { + config +} + +// NewUIDValidityClient returns a client for the UIDValidity from the given config. +func NewUIDValidityClient(c config) *UIDValidityClient { + return &UIDValidityClient{config: c} +} + +// Use adds a list of mutation hooks to the hooks stack. +// A call to `Use(f, g, h)` equals to `uidvalidity.Hooks(f(g(h())))`. +func (c *UIDValidityClient) Use(hooks ...Hook) { + c.hooks.UIDValidity = append(c.hooks.UIDValidity, hooks...) +} + +// Create returns a builder for creating a UIDValidity entity. +func (c *UIDValidityClient) Create() *UIDValidityCreate { + mutation := newUIDValidityMutation(c.config, OpCreate) + return &UIDValidityCreate{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// CreateBulk returns a builder for creating a bulk of UIDValidity entities. +func (c *UIDValidityClient) CreateBulk(builders ...*UIDValidityCreate) *UIDValidityCreateBulk { + return &UIDValidityCreateBulk{config: c.config, builders: builders} +} + +// Update returns an update builder for UIDValidity. +func (c *UIDValidityClient) Update() *UIDValidityUpdate { + mutation := newUIDValidityMutation(c.config, OpUpdate) + return &UIDValidityUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// UpdateOne returns an update builder for the given entity. +func (c *UIDValidityClient) UpdateOne(uv *UIDValidity) *UIDValidityUpdateOne { + mutation := newUIDValidityMutation(c.config, OpUpdateOne, withUIDValidity(uv)) + return &UIDValidityUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// UpdateOneID returns an update builder for the given id. +func (c *UIDValidityClient) UpdateOneID(id int) *UIDValidityUpdateOne { + mutation := newUIDValidityMutation(c.config, OpUpdateOne, withUIDValidityID(id)) + return &UIDValidityUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// Delete returns a delete builder for UIDValidity. +func (c *UIDValidityClient) Delete() *UIDValidityDelete { + mutation := newUIDValidityMutation(c.config, OpDelete) + return &UIDValidityDelete{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// DeleteOne returns a builder for deleting the given entity. +func (c *UIDValidityClient) DeleteOne(uv *UIDValidity) *UIDValidityDeleteOne { + return c.DeleteOneID(uv.ID) +} + +// DeleteOne returns a builder for deleting the given entity by its id. +func (c *UIDValidityClient) DeleteOneID(id int) *UIDValidityDeleteOne { + builder := c.Delete().Where(uidvalidity.ID(id)) + builder.mutation.id = &id + builder.mutation.op = OpDeleteOne + return &UIDValidityDeleteOne{builder} +} + +// Query returns a query builder for UIDValidity. +func (c *UIDValidityClient) Query() *UIDValidityQuery { + return &UIDValidityQuery{ + config: c.config, + } +} + +// Get returns a UIDValidity entity by its id. +func (c *UIDValidityClient) Get(ctx context.Context, id int) (*UIDValidity, error) { + return c.Query().Where(uidvalidity.ID(id)).Only(ctx) +} + +// GetX is like Get, but panics if an error occurs. +func (c *UIDValidityClient) GetX(ctx context.Context, id int) *UIDValidity { + obj, err := c.Get(ctx, id) + if err != nil { + panic(err) + } + return obj +} + +// Hooks returns the client hooks. +func (c *UIDValidityClient) Hooks() []Hook { + return c.hooks.UIDValidity +} diff --git a/internal/db/ent/config.go b/internal/db/ent/config.go index d4e33a83..243c0c7d 100644 --- a/internal/db/ent/config.go +++ b/internal/db/ent/config.go @@ -31,6 +31,7 @@ type hooks struct { Message []ent.Hook MessageFlag []ent.Hook UID []ent.Hook + UIDValidity []ent.Hook } // Options applies the options on the config object. diff --git a/internal/db/ent/ent.go b/internal/db/ent/ent.go index 142b861d..3ec4e004 100644 --- a/internal/db/ent/ent.go +++ b/internal/db/ent/ent.go @@ -17,6 +17,7 @@ import ( "github.com/ProtonMail/gluon/internal/db/ent/message" "github.com/ProtonMail/gluon/internal/db/ent/messageflag" "github.com/ProtonMail/gluon/internal/db/ent/uid" + "github.com/ProtonMail/gluon/internal/db/ent/uidvalidity" ) // ent aliases to avoid import conflicts in user's code. @@ -44,6 +45,7 @@ func columnChecker(table string) func(string) error { message.Table: message.ValidColumn, messageflag.Table: messageflag.ValidColumn, uid.Table: uid.ValidColumn, + uidvalidity.Table: uidvalidity.ValidColumn, } check, ok := checks[table] if !ok { diff --git a/internal/db/ent/hook/hook.go b/internal/db/ent/hook/hook.go index 580b6f38..0788845b 100644 --- a/internal/db/ent/hook/hook.go +++ b/internal/db/ent/hook/hook.go @@ -100,6 +100,19 @@ func (f UIDFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) return f(ctx, mv) } +// The UIDValidityFunc type is an adapter to allow the use of ordinary +// function as UIDValidity mutator. +type UIDValidityFunc func(context.Context, *ent.UIDValidityMutation) (ent.Value, error) + +// Mutate calls f(ctx, m). +func (f UIDValidityFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { + mv, ok := m.(*ent.UIDValidityMutation) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.UIDValidityMutation", m) + } + return f(ctx, mv) +} + // Condition is a hook condition function. type Condition func(context.Context, ent.Mutation) bool diff --git a/internal/db/ent/migrate/schema.go b/internal/db/ent/migrate/schema.go index 6b434dfe..4af36ccc 100644 --- a/internal/db/ent/migrate/schema.go +++ b/internal/db/ent/migrate/schema.go @@ -185,6 +185,17 @@ var ( }, }, } + // UIDValiditiesColumns holds the columns for the "uid_validities" table. + UIDValiditiesColumns = []*schema.Column{ + {Name: "id", Type: field.TypeInt, Increment: true}, + {Name: "uid_validity", Type: field.TypeUint32}, + } + // UIDValiditiesTable holds the schema information for the "uid_validities" table. + UIDValiditiesTable = &schema.Table{ + Name: "uid_validities", + Columns: UIDValiditiesColumns, + PrimaryKey: []*schema.Column{UIDValiditiesColumns[0]}, + } // Tables holds all the tables in the schema. Tables = []*schema.Table{ MailboxesTable, @@ -194,6 +205,7 @@ var ( MessagesTable, MessageFlagsTable, UIDsTable, + UIDValiditiesTable, } ) diff --git a/internal/db/ent/mutation.go b/internal/db/ent/mutation.go index 3c14a6c3..cc37fbee 100644 --- a/internal/db/ent/mutation.go +++ b/internal/db/ent/mutation.go @@ -18,6 +18,7 @@ import ( "github.com/ProtonMail/gluon/internal/db/ent/messageflag" "github.com/ProtonMail/gluon/internal/db/ent/predicate" "github.com/ProtonMail/gluon/internal/db/ent/uid" + "github.com/ProtonMail/gluon/internal/db/ent/uidvalidity" "entgo.io/ent" ) @@ -38,6 +39,7 @@ const ( TypeMessage = "Message" TypeMessageFlag = "MessageFlag" TypeUID = "UID" + TypeUIDValidity = "UIDValidity" ) // MailboxMutation represents an operation that mutates the Mailbox nodes in the graph. @@ -3776,3 +3778,350 @@ func (m *UIDMutation) ResetEdge(name string) error { } return fmt.Errorf("unknown UID edge %s", name) } + +// UIDValidityMutation represents an operation that mutates the UIDValidity nodes in the graph. +type UIDValidityMutation struct { + config + op Op + typ string + id *int + _UIDValidity *imap.UID + add_UIDValidity *imap.UID + clearedFields map[string]struct{} + done bool + oldValue func(context.Context) (*UIDValidity, error) + predicates []predicate.UIDValidity +} + +var _ ent.Mutation = (*UIDValidityMutation)(nil) + +// uidvalidityOption allows management of the mutation configuration using functional options. +type uidvalidityOption func(*UIDValidityMutation) + +// newUIDValidityMutation creates new mutation for the UIDValidity entity. +func newUIDValidityMutation(c config, op Op, opts ...uidvalidityOption) *UIDValidityMutation { + m := &UIDValidityMutation{ + config: c, + op: op, + typ: TypeUIDValidity, + clearedFields: make(map[string]struct{}), + } + for _, opt := range opts { + opt(m) + } + return m +} + +// withUIDValidityID sets the ID field of the mutation. +func withUIDValidityID(id int) uidvalidityOption { + return func(m *UIDValidityMutation) { + var ( + err error + once sync.Once + value *UIDValidity + ) + m.oldValue = func(ctx context.Context) (*UIDValidity, error) { + once.Do(func() { + if m.done { + err = errors.New("querying old values post mutation is not allowed") + } else { + value, err = m.Client().UIDValidity.Get(ctx, id) + } + }) + return value, err + } + m.id = &id + } +} + +// withUIDValidity sets the old UIDValidity of the mutation. +func withUIDValidity(node *UIDValidity) uidvalidityOption { + return func(m *UIDValidityMutation) { + m.oldValue = func(context.Context) (*UIDValidity, error) { + return node, nil + } + m.id = &node.ID + } +} + +// Client returns a new `ent.Client` from the mutation. If the mutation was +// executed in a transaction (ent.Tx), a transactional client is returned. +func (m UIDValidityMutation) Client() *Client { + client := &Client{config: m.config} + client.init() + return client +} + +// Tx returns an `ent.Tx` for mutations that were executed in transactions; +// it returns an error otherwise. +func (m UIDValidityMutation) Tx() (*Tx, error) { + if _, ok := m.driver.(*txDriver); !ok { + return nil, errors.New("ent: mutation is not running in a transaction") + } + tx := &Tx{config: m.config} + tx.init() + return tx, nil +} + +// ID returns the ID value in the mutation. Note that the ID is only available +// if it was provided to the builder or after it was returned from the database. +func (m *UIDValidityMutation) ID() (id int, exists bool) { + if m.id == nil { + return + } + return *m.id, true +} + +// IDs queries the database and returns the entity ids that match the mutation's predicate. +// That means, if the mutation is applied within a transaction with an isolation level such +// as sql.LevelSerializable, the returned ids match the ids of the rows that will be updated +// or updated by the mutation. +func (m *UIDValidityMutation) IDs(ctx context.Context) ([]int, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []int{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().UIDValidity.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + +// SetUIDValidity sets the "UIDValidity" field. +func (m *UIDValidityMutation) SetUIDValidity(i imap.UID) { + m._UIDValidity = &i + m.add_UIDValidity = nil +} + +// UIDValidity returns the value of the "UIDValidity" field in the mutation. +func (m *UIDValidityMutation) UIDValidity() (r imap.UID, exists bool) { + v := m._UIDValidity + if v == nil { + return + } + return *v, true +} + +// OldUIDValidity returns the old "UIDValidity" field's value of the UIDValidity entity. +// If the UIDValidity object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *UIDValidityMutation) OldUIDValidity(ctx context.Context) (v imap.UID, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldUIDValidity is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldUIDValidity requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldUIDValidity: %w", err) + } + return oldValue.UIDValidity, nil +} + +// AddUIDValidity adds i to the "UIDValidity" field. +func (m *UIDValidityMutation) AddUIDValidity(i imap.UID) { + if m.add_UIDValidity != nil { + *m.add_UIDValidity += i + } else { + m.add_UIDValidity = &i + } +} + +// AddedUIDValidity returns the value that was added to the "UIDValidity" field in this mutation. +func (m *UIDValidityMutation) AddedUIDValidity() (r imap.UID, exists bool) { + v := m.add_UIDValidity + if v == nil { + return + } + return *v, true +} + +// ResetUIDValidity resets all changes to the "UIDValidity" field. +func (m *UIDValidityMutation) ResetUIDValidity() { + m._UIDValidity = nil + m.add_UIDValidity = nil +} + +// Where appends a list predicates to the UIDValidityMutation builder. +func (m *UIDValidityMutation) Where(ps ...predicate.UIDValidity) { + m.predicates = append(m.predicates, ps...) +} + +// Op returns the operation name. +func (m *UIDValidityMutation) Op() Op { + return m.op +} + +// Type returns the node type of this mutation (UIDValidity). +func (m *UIDValidityMutation) Type() string { + return m.typ +} + +// Fields returns all fields that were changed during this mutation. Note that in +// order to get all numeric fields that were incremented/decremented, call +// AddedFields(). +func (m *UIDValidityMutation) Fields() []string { + fields := make([]string, 0, 1) + if m._UIDValidity != nil { + fields = append(fields, uidvalidity.FieldUIDValidity) + } + return fields +} + +// Field returns the value of a field with the given name. The second boolean +// return value indicates that this field was not set, or was not defined in the +// schema. +func (m *UIDValidityMutation) Field(name string) (ent.Value, bool) { + switch name { + case uidvalidity.FieldUIDValidity: + return m.UIDValidity() + } + return nil, false +} + +// OldField returns the old value of the field from the database. An error is +// returned if the mutation operation is not UpdateOne, or the query to the +// database failed. +func (m *UIDValidityMutation) OldField(ctx context.Context, name string) (ent.Value, error) { + switch name { + case uidvalidity.FieldUIDValidity: + return m.OldUIDValidity(ctx) + } + return nil, fmt.Errorf("unknown UIDValidity field %s", name) +} + +// SetField sets the value of a field with the given name. It returns an error if +// the field is not defined in the schema, or if the type mismatched the field +// type. +func (m *UIDValidityMutation) SetField(name string, value ent.Value) error { + switch name { + case uidvalidity.FieldUIDValidity: + v, ok := value.(imap.UID) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetUIDValidity(v) + return nil + } + return fmt.Errorf("unknown UIDValidity field %s", name) +} + +// AddedFields returns all numeric fields that were incremented/decremented during +// this mutation. +func (m *UIDValidityMutation) AddedFields() []string { + var fields []string + if m.add_UIDValidity != nil { + fields = append(fields, uidvalidity.FieldUIDValidity) + } + return fields +} + +// AddedField returns the numeric value that was incremented/decremented on a field +// with the given name. The second boolean return value indicates that this field +// was not set, or was not defined in the schema. +func (m *UIDValidityMutation) AddedField(name string) (ent.Value, bool) { + switch name { + case uidvalidity.FieldUIDValidity: + return m.AddedUIDValidity() + } + return nil, false +} + +// AddField adds the value to the field with the given name. It returns an error if +// the field is not defined in the schema, or if the type mismatched the field +// type. +func (m *UIDValidityMutation) AddField(name string, value ent.Value) error { + switch name { + case uidvalidity.FieldUIDValidity: + v, ok := value.(imap.UID) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.AddUIDValidity(v) + return nil + } + return fmt.Errorf("unknown UIDValidity numeric field %s", name) +} + +// ClearedFields returns all nullable fields that were cleared during this +// mutation. +func (m *UIDValidityMutation) ClearedFields() []string { + return nil +} + +// FieldCleared returns a boolean indicating if a field with the given name was +// cleared in this mutation. +func (m *UIDValidityMutation) FieldCleared(name string) bool { + _, ok := m.clearedFields[name] + return ok +} + +// ClearField clears the value of the field with the given name. It returns an +// error if the field is not defined in the schema. +func (m *UIDValidityMutation) ClearField(name string) error { + return fmt.Errorf("unknown UIDValidity nullable field %s", name) +} + +// ResetField resets all changes in the mutation for the field with the given name. +// It returns an error if the field is not defined in the schema. +func (m *UIDValidityMutation) ResetField(name string) error { + switch name { + case uidvalidity.FieldUIDValidity: + m.ResetUIDValidity() + return nil + } + return fmt.Errorf("unknown UIDValidity field %s", name) +} + +// AddedEdges returns all edge names that were set/added in this mutation. +func (m *UIDValidityMutation) AddedEdges() []string { + edges := make([]string, 0, 0) + return edges +} + +// AddedIDs returns all IDs (to other nodes) that were added for the given edge +// name in this mutation. +func (m *UIDValidityMutation) AddedIDs(name string) []ent.Value { + return nil +} + +// RemovedEdges returns all edge names that were removed in this mutation. +func (m *UIDValidityMutation) RemovedEdges() []string { + edges := make([]string, 0, 0) + return edges +} + +// RemovedIDs returns all IDs (to other nodes) that were removed for the edge with +// the given name in this mutation. +func (m *UIDValidityMutation) RemovedIDs(name string) []ent.Value { + return nil +} + +// ClearedEdges returns all edge names that were cleared in this mutation. +func (m *UIDValidityMutation) ClearedEdges() []string { + edges := make([]string, 0, 0) + return edges +} + +// EdgeCleared returns a boolean which indicates if the edge with the given name +// was cleared in this mutation. +func (m *UIDValidityMutation) EdgeCleared(name string) bool { + return false +} + +// ClearEdge clears the value of the edge with the given name. It returns an error +// if that edge is not defined in the schema. +func (m *UIDValidityMutation) ClearEdge(name string) error { + return fmt.Errorf("unknown UIDValidity unique edge %s", name) +} + +// ResetEdge resets all changes to the edge with the given name in this mutation. +// It returns an error if the edge is not defined in the schema. +func (m *UIDValidityMutation) ResetEdge(name string) error { + return fmt.Errorf("unknown UIDValidity edge %s", name) +} diff --git a/internal/db/ent/predicate/predicate.go b/internal/db/ent/predicate/predicate.go index cddae75f..de54cf1c 100644 --- a/internal/db/ent/predicate/predicate.go +++ b/internal/db/ent/predicate/predicate.go @@ -26,3 +26,6 @@ type MessageFlag func(*sql.Selector) // UID is the predicate function for uid builders. type UID func(*sql.Selector) + +// UIDValidity is the predicate function for uidvalidity builders. +type UIDValidity func(*sql.Selector) diff --git a/internal/db/ent/schema/uidvalidity.go b/internal/db/ent/schema/uidvalidity.go new file mode 100644 index 00000000..46997452 --- /dev/null +++ b/internal/db/ent/schema/uidvalidity.go @@ -0,0 +1,25 @@ +package schema + +import ( + "entgo.io/ent" + "entgo.io/ent/schema/field" + "github.com/ProtonMail/gluon/imap" +) + +// UIDValidity holds the current global UIDVALITY value for creating new +// mailbox. +type UIDValidity struct { + ent.Schema +} + +// Fields of the UIDValidity. +func (UIDValidity) Fields() []ent.Field { + return []ent.Field{ + field.Uint32("UIDValidity").GoType(imap.UID(0)), + } +} + +// Edges of the User. +func (UIDValidity) Edges() []ent.Edge { + return nil +} diff --git a/internal/db/ent/tx.go b/internal/db/ent/tx.go index a9a1e170..0b271fcf 100644 --- a/internal/db/ent/tx.go +++ b/internal/db/ent/tx.go @@ -26,6 +26,8 @@ type Tx struct { MessageFlag *MessageFlagClient // UID is the client for interacting with the UID builders. UID *UIDClient + // UIDValidity is the client for interacting with the UIDValidity builders. + UIDValidity *UIDValidityClient // lazily loaded. client *Client @@ -168,6 +170,7 @@ func (tx *Tx) init() { tx.Message = NewMessageClient(tx.config) tx.MessageFlag = NewMessageFlagClient(tx.config) tx.UID = NewUIDClient(tx.config) + tx.UIDValidity = NewUIDValidityClient(tx.config) } // txDriver wraps the given dialect.Tx with a nop dialect.Driver implementation. diff --git a/internal/db/ent/uidvalidity.go b/internal/db/ent/uidvalidity.go new file mode 100644 index 00000000..ac2a285c --- /dev/null +++ b/internal/db/ent/uidvalidity.go @@ -0,0 +1,98 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "fmt" + "strings" + + "entgo.io/ent/dialect/sql" + "github.com/ProtonMail/gluon/imap" + "github.com/ProtonMail/gluon/internal/db/ent/uidvalidity" +) + +// UIDValidity is the model entity for the UIDValidity schema. +type UIDValidity struct { + config `json:"-"` + // ID of the ent. + ID int `json:"id,omitempty"` + // UIDValidity holds the value of the "UIDValidity" field. + UIDValidity imap.UID `json:"UIDValidity,omitempty"` +} + +// scanValues returns the types for scanning values from sql.Rows. +func (*UIDValidity) scanValues(columns []string) ([]interface{}, error) { + values := make([]interface{}, len(columns)) + for i := range columns { + switch columns[i] { + case uidvalidity.FieldID, uidvalidity.FieldUIDValidity: + values[i] = new(sql.NullInt64) + default: + return nil, fmt.Errorf("unexpected column %q for type UIDValidity", columns[i]) + } + } + return values, nil +} + +// assignValues assigns the values that were returned from sql.Rows (after scanning) +// to the UIDValidity fields. +func (uv *UIDValidity) assignValues(columns []string, values []interface{}) error { + if m, n := len(values), len(columns); m < n { + return fmt.Errorf("mismatch number of scan values: %d != %d", m, n) + } + for i := range columns { + switch columns[i] { + case uidvalidity.FieldID: + value, ok := values[i].(*sql.NullInt64) + if !ok { + return fmt.Errorf("unexpected type %T for field id", value) + } + uv.ID = int(value.Int64) + case uidvalidity.FieldUIDValidity: + if value, ok := values[i].(*sql.NullInt64); !ok { + return fmt.Errorf("unexpected type %T for field UIDValidity", values[i]) + } else if value.Valid { + uv.UIDValidity = imap.UID(value.Int64) + } + } + } + return nil +} + +// Update returns a builder for updating this UIDValidity. +// Note that you need to call UIDValidity.Unwrap() before calling this method if this UIDValidity +// was returned from a transaction, and the transaction was committed or rolled back. +func (uv *UIDValidity) Update() *UIDValidityUpdateOne { + return (&UIDValidityClient{config: uv.config}).UpdateOne(uv) +} + +// Unwrap unwraps the UIDValidity entity that was returned from a transaction after it was closed, +// so that all future queries will be executed through the driver which created the transaction. +func (uv *UIDValidity) Unwrap() *UIDValidity { + _tx, ok := uv.config.driver.(*txDriver) + if !ok { + panic("ent: UIDValidity is not a transactional entity") + } + uv.config.driver = _tx.drv + return uv +} + +// String implements the fmt.Stringer. +func (uv *UIDValidity) String() string { + var builder strings.Builder + builder.WriteString("UIDValidity(") + builder.WriteString(fmt.Sprintf("id=%v, ", uv.ID)) + builder.WriteString("UIDValidity=") + builder.WriteString(fmt.Sprintf("%v", uv.UIDValidity)) + builder.WriteByte(')') + return builder.String() +} + +// UIDValidities is a parsable slice of UIDValidity. +type UIDValidities []*UIDValidity + +func (uv UIDValidities) config(cfg config) { + for _i := range uv { + uv[_i].config = cfg + } +} diff --git a/internal/db/ent/uidvalidity/uidvalidity.go b/internal/db/ent/uidvalidity/uidvalidity.go new file mode 100644 index 00000000..3660abdb --- /dev/null +++ b/internal/db/ent/uidvalidity/uidvalidity.go @@ -0,0 +1,30 @@ +// Code generated by ent, DO NOT EDIT. + +package uidvalidity + +const ( + // Label holds the string label denoting the uidvalidity type in the database. + Label = "uid_validity" + // FieldID holds the string denoting the id field in the database. + FieldID = "id" + // FieldUIDValidity holds the string denoting the uidvalidity field in the database. + FieldUIDValidity = "uid_validity" + // Table holds the table name of the uidvalidity in the database. + Table = "uid_validities" +) + +// Columns holds all SQL columns for uidvalidity fields. +var Columns = []string{ + FieldID, + FieldUIDValidity, +} + +// ValidColumn reports if the column name is valid (part of the table columns). +func ValidColumn(column string) bool { + for i := range Columns { + if column == Columns[i] { + return true + } + } + return false +} diff --git a/internal/db/ent/uidvalidity/where.go b/internal/db/ent/uidvalidity/where.go new file mode 100644 index 00000000..b83eacb8 --- /dev/null +++ b/internal/db/ent/uidvalidity/where.go @@ -0,0 +1,190 @@ +// Code generated by ent, DO NOT EDIT. + +package uidvalidity + +import ( + "entgo.io/ent/dialect/sql" + "github.com/ProtonMail/gluon/imap" + "github.com/ProtonMail/gluon/internal/db/ent/predicate" +) + +// ID filters vertices based on their ID field. +func ID(id int) predicate.UIDValidity { + return predicate.UIDValidity(func(s *sql.Selector) { + s.Where(sql.EQ(s.C(FieldID), id)) + }) +} + +// IDEQ applies the EQ predicate on the ID field. +func IDEQ(id int) predicate.UIDValidity { + return predicate.UIDValidity(func(s *sql.Selector) { + s.Where(sql.EQ(s.C(FieldID), id)) + }) +} + +// IDNEQ applies the NEQ predicate on the ID field. +func IDNEQ(id int) predicate.UIDValidity { + return predicate.UIDValidity(func(s *sql.Selector) { + s.Where(sql.NEQ(s.C(FieldID), id)) + }) +} + +// IDIn applies the In predicate on the ID field. +func IDIn(ids ...int) predicate.UIDValidity { + return predicate.UIDValidity(func(s *sql.Selector) { + v := make([]interface{}, len(ids)) + for i := range v { + v[i] = ids[i] + } + s.Where(sql.In(s.C(FieldID), v...)) + }) +} + +// IDNotIn applies the NotIn predicate on the ID field. +func IDNotIn(ids ...int) predicate.UIDValidity { + return predicate.UIDValidity(func(s *sql.Selector) { + v := make([]interface{}, len(ids)) + for i := range v { + v[i] = ids[i] + } + s.Where(sql.NotIn(s.C(FieldID), v...)) + }) +} + +// IDGT applies the GT predicate on the ID field. +func IDGT(id int) predicate.UIDValidity { + return predicate.UIDValidity(func(s *sql.Selector) { + s.Where(sql.GT(s.C(FieldID), id)) + }) +} + +// IDGTE applies the GTE predicate on the ID field. +func IDGTE(id int) predicate.UIDValidity { + return predicate.UIDValidity(func(s *sql.Selector) { + s.Where(sql.GTE(s.C(FieldID), id)) + }) +} + +// IDLT applies the LT predicate on the ID field. +func IDLT(id int) predicate.UIDValidity { + return predicate.UIDValidity(func(s *sql.Selector) { + s.Where(sql.LT(s.C(FieldID), id)) + }) +} + +// IDLTE applies the LTE predicate on the ID field. +func IDLTE(id int) predicate.UIDValidity { + return predicate.UIDValidity(func(s *sql.Selector) { + s.Where(sql.LTE(s.C(FieldID), id)) + }) +} + +// UIDValidity applies equality check predicate on the "UIDValidity" field. It's identical to UIDValidityEQ. +func UIDValidity(v imap.UID) predicate.UIDValidity { + vc := uint32(v) + return predicate.UIDValidity(func(s *sql.Selector) { + s.Where(sql.EQ(s.C(FieldUIDValidity), vc)) + }) +} + +// UIDValidityEQ applies the EQ predicate on the "UIDValidity" field. +func UIDValidityEQ(v imap.UID) predicate.UIDValidity { + vc := uint32(v) + return predicate.UIDValidity(func(s *sql.Selector) { + s.Where(sql.EQ(s.C(FieldUIDValidity), vc)) + }) +} + +// UIDValidityNEQ applies the NEQ predicate on the "UIDValidity" field. +func UIDValidityNEQ(v imap.UID) predicate.UIDValidity { + vc := uint32(v) + return predicate.UIDValidity(func(s *sql.Selector) { + s.Where(sql.NEQ(s.C(FieldUIDValidity), vc)) + }) +} + +// UIDValidityIn applies the In predicate on the "UIDValidity" field. +func UIDValidityIn(vs ...imap.UID) predicate.UIDValidity { + v := make([]interface{}, len(vs)) + for i := range v { + v[i] = uint32(vs[i]) + } + return predicate.UIDValidity(func(s *sql.Selector) { + s.Where(sql.In(s.C(FieldUIDValidity), v...)) + }) +} + +// UIDValidityNotIn applies the NotIn predicate on the "UIDValidity" field. +func UIDValidityNotIn(vs ...imap.UID) predicate.UIDValidity { + v := make([]interface{}, len(vs)) + for i := range v { + v[i] = uint32(vs[i]) + } + return predicate.UIDValidity(func(s *sql.Selector) { + s.Where(sql.NotIn(s.C(FieldUIDValidity), v...)) + }) +} + +// UIDValidityGT applies the GT predicate on the "UIDValidity" field. +func UIDValidityGT(v imap.UID) predicate.UIDValidity { + vc := uint32(v) + return predicate.UIDValidity(func(s *sql.Selector) { + s.Where(sql.GT(s.C(FieldUIDValidity), vc)) + }) +} + +// UIDValidityGTE applies the GTE predicate on the "UIDValidity" field. +func UIDValidityGTE(v imap.UID) predicate.UIDValidity { + vc := uint32(v) + return predicate.UIDValidity(func(s *sql.Selector) { + s.Where(sql.GTE(s.C(FieldUIDValidity), vc)) + }) +} + +// UIDValidityLT applies the LT predicate on the "UIDValidity" field. +func UIDValidityLT(v imap.UID) predicate.UIDValidity { + vc := uint32(v) + return predicate.UIDValidity(func(s *sql.Selector) { + s.Where(sql.LT(s.C(FieldUIDValidity), vc)) + }) +} + +// UIDValidityLTE applies the LTE predicate on the "UIDValidity" field. +func UIDValidityLTE(v imap.UID) predicate.UIDValidity { + vc := uint32(v) + return predicate.UIDValidity(func(s *sql.Selector) { + s.Where(sql.LTE(s.C(FieldUIDValidity), vc)) + }) +} + +// And groups predicates with the AND operator between them. +func And(predicates ...predicate.UIDValidity) predicate.UIDValidity { + return predicate.UIDValidity(func(s *sql.Selector) { + s1 := s.Clone().SetP(nil) + for _, p := range predicates { + p(s1) + } + s.Where(s1.P()) + }) +} + +// Or groups predicates with the OR operator between them. +func Or(predicates ...predicate.UIDValidity) predicate.UIDValidity { + return predicate.UIDValidity(func(s *sql.Selector) { + s1 := s.Clone().SetP(nil) + for i, p := range predicates { + if i > 0 { + s1.Or() + } + p(s1) + } + s.Where(s1.P()) + }) +} + +// Not applies the not operator on the given predicate. +func Not(p predicate.UIDValidity) predicate.UIDValidity { + return predicate.UIDValidity(func(s *sql.Selector) { + p(s.Not()) + }) +} diff --git a/internal/db/ent/uidvalidity_create.go b/internal/db/ent/uidvalidity_create.go new file mode 100644 index 00000000..316c8f15 --- /dev/null +++ b/internal/db/ent/uidvalidity_create.go @@ -0,0 +1,227 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "context" + "errors" + "fmt" + + "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" + "github.com/ProtonMail/gluon/imap" + "github.com/ProtonMail/gluon/internal/db/ent/uidvalidity" +) + +// UIDValidityCreate is the builder for creating a UIDValidity entity. +type UIDValidityCreate struct { + config + mutation *UIDValidityMutation + hooks []Hook +} + +// SetUIDValidity sets the "UIDValidity" field. +func (uvc *UIDValidityCreate) SetUIDValidity(i imap.UID) *UIDValidityCreate { + uvc.mutation.SetUIDValidity(i) + return uvc +} + +// Mutation returns the UIDValidityMutation object of the builder. +func (uvc *UIDValidityCreate) Mutation() *UIDValidityMutation { + return uvc.mutation +} + +// Save creates the UIDValidity in the database. +func (uvc *UIDValidityCreate) Save(ctx context.Context) (*UIDValidity, error) { + var ( + err error + node *UIDValidity + ) + if len(uvc.hooks) == 0 { + if err = uvc.check(); err != nil { + return nil, err + } + node, err = uvc.sqlSave(ctx) + } else { + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutation, ok := m.(*UIDValidityMutation) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + if err = uvc.check(); err != nil { + return nil, err + } + uvc.mutation = mutation + if node, err = uvc.sqlSave(ctx); err != nil { + return nil, err + } + mutation.id = &node.ID + mutation.done = true + return node, err + }) + for i := len(uvc.hooks) - 1; i >= 0; i-- { + if uvc.hooks[i] == nil { + return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = uvc.hooks[i](mut) + } + v, err := mut.Mutate(ctx, uvc.mutation) + if err != nil { + return nil, err + } + nv, ok := v.(*UIDValidity) + if !ok { + return nil, fmt.Errorf("unexpected node type %T returned from UIDValidityMutation", v) + } + node = nv + } + return node, err +} + +// SaveX calls Save and panics if Save returns an error. +func (uvc *UIDValidityCreate) SaveX(ctx context.Context) *UIDValidity { + v, err := uvc.Save(ctx) + if err != nil { + panic(err) + } + return v +} + +// Exec executes the query. +func (uvc *UIDValidityCreate) Exec(ctx context.Context) error { + _, err := uvc.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (uvc *UIDValidityCreate) ExecX(ctx context.Context) { + if err := uvc.Exec(ctx); err != nil { + panic(err) + } +} + +// check runs all checks and user-defined validators on the builder. +func (uvc *UIDValidityCreate) check() error { + if _, ok := uvc.mutation.UIDValidity(); !ok { + return &ValidationError{Name: "UIDValidity", err: errors.New(`ent: missing required field "UIDValidity.UIDValidity"`)} + } + return nil +} + +func (uvc *UIDValidityCreate) sqlSave(ctx context.Context) (*UIDValidity, error) { + _node, _spec := uvc.createSpec() + if err := sqlgraph.CreateNode(ctx, uvc.driver, _spec); err != nil { + if sqlgraph.IsConstraintError(err) { + err = &ConstraintError{msg: err.Error(), wrap: err} + } + return nil, err + } + id := _spec.ID.Value.(int64) + _node.ID = int(id) + return _node, nil +} + +func (uvc *UIDValidityCreate) createSpec() (*UIDValidity, *sqlgraph.CreateSpec) { + var ( + _node = &UIDValidity{config: uvc.config} + _spec = &sqlgraph.CreateSpec{ + Table: uidvalidity.Table, + ID: &sqlgraph.FieldSpec{ + Type: field.TypeInt, + Column: uidvalidity.FieldID, + }, + } + ) + if value, ok := uvc.mutation.UIDValidity(); ok { + _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ + Type: field.TypeUint32, + Value: value, + Column: uidvalidity.FieldUIDValidity, + }) + _node.UIDValidity = value + } + return _node, _spec +} + +// UIDValidityCreateBulk is the builder for creating many UIDValidity entities in bulk. +type UIDValidityCreateBulk struct { + config + builders []*UIDValidityCreate +} + +// Save creates the UIDValidity entities in the database. +func (uvcb *UIDValidityCreateBulk) Save(ctx context.Context) ([]*UIDValidity, error) { + specs := make([]*sqlgraph.CreateSpec, len(uvcb.builders)) + nodes := make([]*UIDValidity, len(uvcb.builders)) + mutators := make([]Mutator, len(uvcb.builders)) + for i := range uvcb.builders { + func(i int, root context.Context) { + builder := uvcb.builders[i] + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutation, ok := m.(*UIDValidityMutation) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + if err := builder.check(); err != nil { + return nil, err + } + builder.mutation = mutation + nodes[i], specs[i] = builder.createSpec() + var err error + if i < len(mutators)-1 { + _, err = mutators[i+1].Mutate(root, uvcb.builders[i+1].mutation) + } else { + spec := &sqlgraph.BatchCreateSpec{Nodes: specs} + // Invoke the actual operation on the latest mutation in the chain. + if err = sqlgraph.BatchCreate(ctx, uvcb.driver, spec); err != nil { + if sqlgraph.IsConstraintError(err) { + err = &ConstraintError{msg: err.Error(), wrap: err} + } + } + } + if err != nil { + return nil, err + } + mutation.id = &nodes[i].ID + if specs[i].ID.Value != nil { + id := specs[i].ID.Value.(int64) + nodes[i].ID = int(id) + } + mutation.done = true + return nodes[i], nil + }) + for i := len(builder.hooks) - 1; i >= 0; i-- { + mut = builder.hooks[i](mut) + } + mutators[i] = mut + }(i, ctx) + } + if len(mutators) > 0 { + if _, err := mutators[0].Mutate(ctx, uvcb.builders[0].mutation); err != nil { + return nil, err + } + } + return nodes, nil +} + +// SaveX is like Save, but panics if an error occurs. +func (uvcb *UIDValidityCreateBulk) SaveX(ctx context.Context) []*UIDValidity { + v, err := uvcb.Save(ctx) + if err != nil { + panic(err) + } + return v +} + +// Exec executes the query. +func (uvcb *UIDValidityCreateBulk) Exec(ctx context.Context) error { + _, err := uvcb.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (uvcb *UIDValidityCreateBulk) ExecX(ctx context.Context) { + if err := uvcb.Exec(ctx); err != nil { + panic(err) + } +} diff --git a/internal/db/ent/uidvalidity_delete.go b/internal/db/ent/uidvalidity_delete.go new file mode 100644 index 00000000..6383ea6f --- /dev/null +++ b/internal/db/ent/uidvalidity_delete.go @@ -0,0 +1,115 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "context" + "fmt" + + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" + "github.com/ProtonMail/gluon/internal/db/ent/predicate" + "github.com/ProtonMail/gluon/internal/db/ent/uidvalidity" +) + +// UIDValidityDelete is the builder for deleting a UIDValidity entity. +type UIDValidityDelete struct { + config + hooks []Hook + mutation *UIDValidityMutation +} + +// Where appends a list predicates to the UIDValidityDelete builder. +func (uvd *UIDValidityDelete) Where(ps ...predicate.UIDValidity) *UIDValidityDelete { + uvd.mutation.Where(ps...) + return uvd +} + +// Exec executes the deletion query and returns how many vertices were deleted. +func (uvd *UIDValidityDelete) Exec(ctx context.Context) (int, error) { + var ( + err error + affected int + ) + if len(uvd.hooks) == 0 { + affected, err = uvd.sqlExec(ctx) + } else { + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutation, ok := m.(*UIDValidityMutation) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + uvd.mutation = mutation + affected, err = uvd.sqlExec(ctx) + mutation.done = true + return affected, err + }) + for i := len(uvd.hooks) - 1; i >= 0; i-- { + if uvd.hooks[i] == nil { + return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = uvd.hooks[i](mut) + } + if _, err := mut.Mutate(ctx, uvd.mutation); err != nil { + return 0, err + } + } + return affected, err +} + +// ExecX is like Exec, but panics if an error occurs. +func (uvd *UIDValidityDelete) ExecX(ctx context.Context) int { + n, err := uvd.Exec(ctx) + if err != nil { + panic(err) + } + return n +} + +func (uvd *UIDValidityDelete) sqlExec(ctx context.Context) (int, error) { + _spec := &sqlgraph.DeleteSpec{ + Node: &sqlgraph.NodeSpec{ + Table: uidvalidity.Table, + ID: &sqlgraph.FieldSpec{ + Type: field.TypeInt, + Column: uidvalidity.FieldID, + }, + }, + } + if ps := uvd.mutation.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + affected, err := sqlgraph.DeleteNodes(ctx, uvd.driver, _spec) + if err != nil && sqlgraph.IsConstraintError(err) { + err = &ConstraintError{msg: err.Error(), wrap: err} + } + return affected, err +} + +// UIDValidityDeleteOne is the builder for deleting a single UIDValidity entity. +type UIDValidityDeleteOne struct { + uvd *UIDValidityDelete +} + +// Exec executes the deletion query. +func (uvdo *UIDValidityDeleteOne) Exec(ctx context.Context) error { + n, err := uvdo.uvd.Exec(ctx) + switch { + case err != nil: + return err + case n == 0: + return &NotFoundError{uidvalidity.Label} + default: + return nil + } +} + +// ExecX is like Exec, but panics if an error occurs. +func (uvdo *UIDValidityDeleteOne) ExecX(ctx context.Context) { + uvdo.uvd.ExecX(ctx) +} diff --git a/internal/db/ent/uidvalidity_query.go b/internal/db/ent/uidvalidity_query.go new file mode 100644 index 00000000..03bb8e5b --- /dev/null +++ b/internal/db/ent/uidvalidity_query.go @@ -0,0 +1,528 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "context" + "fmt" + "math" + + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" + "github.com/ProtonMail/gluon/internal/db/ent/predicate" + "github.com/ProtonMail/gluon/internal/db/ent/uidvalidity" +) + +// UIDValidityQuery is the builder for querying UIDValidity entities. +type UIDValidityQuery struct { + config + limit *int + offset *int + unique *bool + order []OrderFunc + fields []string + predicates []predicate.UIDValidity + // intermediate query (i.e. traversal path). + sql *sql.Selector + path func(context.Context) (*sql.Selector, error) +} + +// Where adds a new predicate for the UIDValidityQuery builder. +func (uvq *UIDValidityQuery) Where(ps ...predicate.UIDValidity) *UIDValidityQuery { + uvq.predicates = append(uvq.predicates, ps...) + return uvq +} + +// Limit adds a limit step to the query. +func (uvq *UIDValidityQuery) Limit(limit int) *UIDValidityQuery { + uvq.limit = &limit + return uvq +} + +// Offset adds an offset step to the query. +func (uvq *UIDValidityQuery) Offset(offset int) *UIDValidityQuery { + uvq.offset = &offset + return uvq +} + +// Unique configures the query builder to filter duplicate records on query. +// By default, unique is set to true, and can be disabled using this method. +func (uvq *UIDValidityQuery) Unique(unique bool) *UIDValidityQuery { + uvq.unique = &unique + return uvq +} + +// Order adds an order step to the query. +func (uvq *UIDValidityQuery) Order(o ...OrderFunc) *UIDValidityQuery { + uvq.order = append(uvq.order, o...) + return uvq +} + +// First returns the first UIDValidity entity from the query. +// Returns a *NotFoundError when no UIDValidity was found. +func (uvq *UIDValidityQuery) First(ctx context.Context) (*UIDValidity, error) { + nodes, err := uvq.Limit(1).All(ctx) + if err != nil { + return nil, err + } + if len(nodes) == 0 { + return nil, &NotFoundError{uidvalidity.Label} + } + return nodes[0], nil +} + +// FirstX is like First, but panics if an error occurs. +func (uvq *UIDValidityQuery) FirstX(ctx context.Context) *UIDValidity { + node, err := uvq.First(ctx) + if err != nil && !IsNotFound(err) { + panic(err) + } + return node +} + +// FirstID returns the first UIDValidity ID from the query. +// Returns a *NotFoundError when no UIDValidity ID was found. +func (uvq *UIDValidityQuery) FirstID(ctx context.Context) (id int, err error) { + var ids []int + if ids, err = uvq.Limit(1).IDs(ctx); err != nil { + return + } + if len(ids) == 0 { + err = &NotFoundError{uidvalidity.Label} + return + } + return ids[0], nil +} + +// FirstIDX is like FirstID, but panics if an error occurs. +func (uvq *UIDValidityQuery) FirstIDX(ctx context.Context) int { + id, err := uvq.FirstID(ctx) + if err != nil && !IsNotFound(err) { + panic(err) + } + return id +} + +// Only returns a single UIDValidity entity found by the query, ensuring it only returns one. +// Returns a *NotSingularError when more than one UIDValidity entity is found. +// Returns a *NotFoundError when no UIDValidity entities are found. +func (uvq *UIDValidityQuery) Only(ctx context.Context) (*UIDValidity, error) { + nodes, err := uvq.Limit(2).All(ctx) + if err != nil { + return nil, err + } + switch len(nodes) { + case 1: + return nodes[0], nil + case 0: + return nil, &NotFoundError{uidvalidity.Label} + default: + return nil, &NotSingularError{uidvalidity.Label} + } +} + +// OnlyX is like Only, but panics if an error occurs. +func (uvq *UIDValidityQuery) OnlyX(ctx context.Context) *UIDValidity { + node, err := uvq.Only(ctx) + if err != nil { + panic(err) + } + return node +} + +// OnlyID is like Only, but returns the only UIDValidity ID in the query. +// Returns a *NotSingularError when more than one UIDValidity ID is found. +// Returns a *NotFoundError when no entities are found. +func (uvq *UIDValidityQuery) OnlyID(ctx context.Context) (id int, err error) { + var ids []int + if ids, err = uvq.Limit(2).IDs(ctx); err != nil { + return + } + switch len(ids) { + case 1: + id = ids[0] + case 0: + err = &NotFoundError{uidvalidity.Label} + default: + err = &NotSingularError{uidvalidity.Label} + } + return +} + +// OnlyIDX is like OnlyID, but panics if an error occurs. +func (uvq *UIDValidityQuery) OnlyIDX(ctx context.Context) int { + id, err := uvq.OnlyID(ctx) + if err != nil { + panic(err) + } + return id +} + +// All executes the query and returns a list of UIDValidities. +func (uvq *UIDValidityQuery) All(ctx context.Context) ([]*UIDValidity, error) { + if err := uvq.prepareQuery(ctx); err != nil { + return nil, err + } + return uvq.sqlAll(ctx) +} + +// AllX is like All, but panics if an error occurs. +func (uvq *UIDValidityQuery) AllX(ctx context.Context) []*UIDValidity { + nodes, err := uvq.All(ctx) + if err != nil { + panic(err) + } + return nodes +} + +// IDs executes the query and returns a list of UIDValidity IDs. +func (uvq *UIDValidityQuery) IDs(ctx context.Context) ([]int, error) { + var ids []int + if err := uvq.Select(uidvalidity.FieldID).Scan(ctx, &ids); err != nil { + return nil, err + } + return ids, nil +} + +// IDsX is like IDs, but panics if an error occurs. +func (uvq *UIDValidityQuery) IDsX(ctx context.Context) []int { + ids, err := uvq.IDs(ctx) + if err != nil { + panic(err) + } + return ids +} + +// Count returns the count of the given query. +func (uvq *UIDValidityQuery) Count(ctx context.Context) (int, error) { + if err := uvq.prepareQuery(ctx); err != nil { + return 0, err + } + return uvq.sqlCount(ctx) +} + +// CountX is like Count, but panics if an error occurs. +func (uvq *UIDValidityQuery) CountX(ctx context.Context) int { + count, err := uvq.Count(ctx) + if err != nil { + panic(err) + } + return count +} + +// Exist returns true if the query has elements in the graph. +func (uvq *UIDValidityQuery) Exist(ctx context.Context) (bool, error) { + if err := uvq.prepareQuery(ctx); err != nil { + return false, err + } + return uvq.sqlExist(ctx) +} + +// ExistX is like Exist, but panics if an error occurs. +func (uvq *UIDValidityQuery) ExistX(ctx context.Context) bool { + exist, err := uvq.Exist(ctx) + if err != nil { + panic(err) + } + return exist +} + +// Clone returns a duplicate of the UIDValidityQuery builder, including all associated steps. It can be +// used to prepare common query builders and use them differently after the clone is made. +func (uvq *UIDValidityQuery) Clone() *UIDValidityQuery { + if uvq == nil { + return nil + } + return &UIDValidityQuery{ + config: uvq.config, + limit: uvq.limit, + offset: uvq.offset, + order: append([]OrderFunc{}, uvq.order...), + predicates: append([]predicate.UIDValidity{}, uvq.predicates...), + // clone intermediate query. + sql: uvq.sql.Clone(), + path: uvq.path, + unique: uvq.unique, + } +} + +// GroupBy is used to group vertices by one or more fields/columns. +// It is often used with aggregate functions, like: count, max, mean, min, sum. +// +// Example: +// +// var v []struct { +// UIDValidity imap.UID `json:"UIDValidity,omitempty"` +// Count int `json:"count,omitempty"` +// } +// +// client.UIDValidity.Query(). +// GroupBy(uidvalidity.FieldUIDValidity). +// Aggregate(ent.Count()). +// Scan(ctx, &v) +// +func (uvq *UIDValidityQuery) GroupBy(field string, fields ...string) *UIDValidityGroupBy { + grbuild := &UIDValidityGroupBy{config: uvq.config} + grbuild.fields = append([]string{field}, fields...) + grbuild.path = func(ctx context.Context) (prev *sql.Selector, err error) { + if err := uvq.prepareQuery(ctx); err != nil { + return nil, err + } + return uvq.sqlQuery(ctx), nil + } + grbuild.label = uidvalidity.Label + grbuild.flds, grbuild.scan = &grbuild.fields, grbuild.Scan + return grbuild +} + +// Select allows the selection one or more fields/columns for the given query, +// instead of selecting all fields in the entity. +// +// Example: +// +// var v []struct { +// UIDValidity imap.UID `json:"UIDValidity,omitempty"` +// } +// +// client.UIDValidity.Query(). +// Select(uidvalidity.FieldUIDValidity). +// Scan(ctx, &v) +// +func (uvq *UIDValidityQuery) Select(fields ...string) *UIDValiditySelect { + uvq.fields = append(uvq.fields, fields...) + selbuild := &UIDValiditySelect{UIDValidityQuery: uvq} + selbuild.label = uidvalidity.Label + selbuild.flds, selbuild.scan = &uvq.fields, selbuild.Scan + return selbuild +} + +func (uvq *UIDValidityQuery) prepareQuery(ctx context.Context) error { + for _, f := range uvq.fields { + if !uidvalidity.ValidColumn(f) { + return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} + } + } + if uvq.path != nil { + prev, err := uvq.path(ctx) + if err != nil { + return err + } + uvq.sql = prev + } + return nil +} + +func (uvq *UIDValidityQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*UIDValidity, error) { + var ( + nodes = []*UIDValidity{} + _spec = uvq.querySpec() + ) + _spec.ScanValues = func(columns []string) ([]interface{}, error) { + return (*UIDValidity).scanValues(nil, columns) + } + _spec.Assign = func(columns []string, values []interface{}) error { + node := &UIDValidity{config: uvq.config} + nodes = append(nodes, node) + return node.assignValues(columns, values) + } + for i := range hooks { + hooks[i](ctx, _spec) + } + if err := sqlgraph.QueryNodes(ctx, uvq.driver, _spec); err != nil { + return nil, err + } + if len(nodes) == 0 { + return nodes, nil + } + return nodes, nil +} + +func (uvq *UIDValidityQuery) sqlCount(ctx context.Context) (int, error) { + _spec := uvq.querySpec() + _spec.Node.Columns = uvq.fields + if len(uvq.fields) > 0 { + _spec.Unique = uvq.unique != nil && *uvq.unique + } + return sqlgraph.CountNodes(ctx, uvq.driver, _spec) +} + +func (uvq *UIDValidityQuery) sqlExist(ctx context.Context) (bool, error) { + n, err := uvq.sqlCount(ctx) + if err != nil { + return false, fmt.Errorf("ent: check existence: %w", err) + } + return n > 0, nil +} + +func (uvq *UIDValidityQuery) querySpec() *sqlgraph.QuerySpec { + _spec := &sqlgraph.QuerySpec{ + Node: &sqlgraph.NodeSpec{ + Table: uidvalidity.Table, + Columns: uidvalidity.Columns, + ID: &sqlgraph.FieldSpec{ + Type: field.TypeInt, + Column: uidvalidity.FieldID, + }, + }, + From: uvq.sql, + Unique: true, + } + if unique := uvq.unique; unique != nil { + _spec.Unique = *unique + } + if fields := uvq.fields; len(fields) > 0 { + _spec.Node.Columns = make([]string, 0, len(fields)) + _spec.Node.Columns = append(_spec.Node.Columns, uidvalidity.FieldID) + for i := range fields { + if fields[i] != uidvalidity.FieldID { + _spec.Node.Columns = append(_spec.Node.Columns, fields[i]) + } + } + } + if ps := uvq.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + if limit := uvq.limit; limit != nil { + _spec.Limit = *limit + } + if offset := uvq.offset; offset != nil { + _spec.Offset = *offset + } + if ps := uvq.order; len(ps) > 0 { + _spec.Order = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + return _spec +} + +func (uvq *UIDValidityQuery) sqlQuery(ctx context.Context) *sql.Selector { + builder := sql.Dialect(uvq.driver.Dialect()) + t1 := builder.Table(uidvalidity.Table) + columns := uvq.fields + if len(columns) == 0 { + columns = uidvalidity.Columns + } + selector := builder.Select(t1.Columns(columns...)...).From(t1) + if uvq.sql != nil { + selector = uvq.sql + selector.Select(selector.Columns(columns...)...) + } + if uvq.unique != nil && *uvq.unique { + selector.Distinct() + } + for _, p := range uvq.predicates { + p(selector) + } + for _, p := range uvq.order { + p(selector) + } + if offset := uvq.offset; offset != nil { + // limit is mandatory for offset clause. We start + // with default value, and override it below if needed. + selector.Offset(*offset).Limit(math.MaxInt32) + } + if limit := uvq.limit; limit != nil { + selector.Limit(*limit) + } + return selector +} + +// UIDValidityGroupBy is the group-by builder for UIDValidity entities. +type UIDValidityGroupBy struct { + config + selector + fields []string + fns []AggregateFunc + // intermediate query (i.e. traversal path). + sql *sql.Selector + path func(context.Context) (*sql.Selector, error) +} + +// Aggregate adds the given aggregation functions to the group-by query. +func (uvgb *UIDValidityGroupBy) Aggregate(fns ...AggregateFunc) *UIDValidityGroupBy { + uvgb.fns = append(uvgb.fns, fns...) + return uvgb +} + +// Scan applies the group-by query and scans the result into the given value. +func (uvgb *UIDValidityGroupBy) Scan(ctx context.Context, v interface{}) error { + query, err := uvgb.path(ctx) + if err != nil { + return err + } + uvgb.sql = query + return uvgb.sqlScan(ctx, v) +} + +func (uvgb *UIDValidityGroupBy) sqlScan(ctx context.Context, v interface{}) error { + for _, f := range uvgb.fields { + if !uidvalidity.ValidColumn(f) { + return &ValidationError{Name: f, err: fmt.Errorf("invalid field %q for group-by", f)} + } + } + selector := uvgb.sqlQuery() + if err := selector.Err(); err != nil { + return err + } + rows := &sql.Rows{} + query, args := selector.Query() + if err := uvgb.driver.Query(ctx, query, args, rows); err != nil { + return err + } + defer rows.Close() + return sql.ScanSlice(rows, v) +} + +func (uvgb *UIDValidityGroupBy) sqlQuery() *sql.Selector { + selector := uvgb.sql.Select() + aggregation := make([]string, 0, len(uvgb.fns)) + for _, fn := range uvgb.fns { + aggregation = append(aggregation, fn(selector)) + } + // If no columns were selected in a custom aggregation function, the default + // selection is the fields used for "group-by", and the aggregation functions. + if len(selector.SelectedColumns()) == 0 { + columns := make([]string, 0, len(uvgb.fields)+len(uvgb.fns)) + for _, f := range uvgb.fields { + columns = append(columns, selector.C(f)) + } + columns = append(columns, aggregation...) + selector.Select(columns...) + } + return selector.GroupBy(selector.Columns(uvgb.fields...)...) +} + +// UIDValiditySelect is the builder for selecting fields of UIDValidity entities. +type UIDValiditySelect struct { + *UIDValidityQuery + selector + // intermediate query (i.e. traversal path). + sql *sql.Selector +} + +// Scan applies the selector query and scans the result into the given value. +func (uvs *UIDValiditySelect) Scan(ctx context.Context, v interface{}) error { + if err := uvs.prepareQuery(ctx); err != nil { + return err + } + uvs.sql = uvs.UIDValidityQuery.sqlQuery(ctx) + return uvs.sqlScan(ctx, v) +} + +func (uvs *UIDValiditySelect) sqlScan(ctx context.Context, v interface{}) error { + rows := &sql.Rows{} + query, args := uvs.sql.Query() + if err := uvs.driver.Query(ctx, query, args, rows); err != nil { + return err + } + defer rows.Close() + return sql.ScanSlice(rows, v) +} diff --git a/internal/db/ent/uidvalidity_update.go b/internal/db/ent/uidvalidity_update.go new file mode 100644 index 00000000..2f44413f --- /dev/null +++ b/internal/db/ent/uidvalidity_update.go @@ -0,0 +1,300 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "context" + "errors" + "fmt" + + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" + "github.com/ProtonMail/gluon/imap" + "github.com/ProtonMail/gluon/internal/db/ent/predicate" + "github.com/ProtonMail/gluon/internal/db/ent/uidvalidity" +) + +// UIDValidityUpdate is the builder for updating UIDValidity entities. +type UIDValidityUpdate struct { + config + hooks []Hook + mutation *UIDValidityMutation +} + +// Where appends a list predicates to the UIDValidityUpdate builder. +func (uvu *UIDValidityUpdate) Where(ps ...predicate.UIDValidity) *UIDValidityUpdate { + uvu.mutation.Where(ps...) + return uvu +} + +// SetUIDValidity sets the "UIDValidity" field. +func (uvu *UIDValidityUpdate) SetUIDValidity(i imap.UID) *UIDValidityUpdate { + uvu.mutation.ResetUIDValidity() + uvu.mutation.SetUIDValidity(i) + return uvu +} + +// AddUIDValidity adds i to the "UIDValidity" field. +func (uvu *UIDValidityUpdate) AddUIDValidity(i imap.UID) *UIDValidityUpdate { + uvu.mutation.AddUIDValidity(i) + return uvu +} + +// Mutation returns the UIDValidityMutation object of the builder. +func (uvu *UIDValidityUpdate) Mutation() *UIDValidityMutation { + return uvu.mutation +} + +// Save executes the query and returns the number of nodes affected by the update operation. +func (uvu *UIDValidityUpdate) Save(ctx context.Context) (int, error) { + var ( + err error + affected int + ) + if len(uvu.hooks) == 0 { + affected, err = uvu.sqlSave(ctx) + } else { + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutation, ok := m.(*UIDValidityMutation) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + uvu.mutation = mutation + affected, err = uvu.sqlSave(ctx) + mutation.done = true + return affected, err + }) + for i := len(uvu.hooks) - 1; i >= 0; i-- { + if uvu.hooks[i] == nil { + return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = uvu.hooks[i](mut) + } + if _, err := mut.Mutate(ctx, uvu.mutation); err != nil { + return 0, err + } + } + return affected, err +} + +// SaveX is like Save, but panics if an error occurs. +func (uvu *UIDValidityUpdate) SaveX(ctx context.Context) int { + affected, err := uvu.Save(ctx) + if err != nil { + panic(err) + } + return affected +} + +// Exec executes the query. +func (uvu *UIDValidityUpdate) Exec(ctx context.Context) error { + _, err := uvu.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (uvu *UIDValidityUpdate) ExecX(ctx context.Context) { + if err := uvu.Exec(ctx); err != nil { + panic(err) + } +} + +func (uvu *UIDValidityUpdate) sqlSave(ctx context.Context) (n int, err error) { + _spec := &sqlgraph.UpdateSpec{ + Node: &sqlgraph.NodeSpec{ + Table: uidvalidity.Table, + Columns: uidvalidity.Columns, + ID: &sqlgraph.FieldSpec{ + Type: field.TypeInt, + Column: uidvalidity.FieldID, + }, + }, + } + if ps := uvu.mutation.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + if value, ok := uvu.mutation.UIDValidity(); ok { + _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ + Type: field.TypeUint32, + Value: value, + Column: uidvalidity.FieldUIDValidity, + }) + } + if value, ok := uvu.mutation.AddedUIDValidity(); ok { + _spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{ + Type: field.TypeUint32, + Value: value, + Column: uidvalidity.FieldUIDValidity, + }) + } + if n, err = sqlgraph.UpdateNodes(ctx, uvu.driver, _spec); err != nil { + if _, ok := err.(*sqlgraph.NotFoundError); ok { + err = &NotFoundError{uidvalidity.Label} + } else if sqlgraph.IsConstraintError(err) { + err = &ConstraintError{msg: err.Error(), wrap: err} + } + return 0, err + } + return n, nil +} + +// UIDValidityUpdateOne is the builder for updating a single UIDValidity entity. +type UIDValidityUpdateOne struct { + config + fields []string + hooks []Hook + mutation *UIDValidityMutation +} + +// SetUIDValidity sets the "UIDValidity" field. +func (uvuo *UIDValidityUpdateOne) SetUIDValidity(i imap.UID) *UIDValidityUpdateOne { + uvuo.mutation.ResetUIDValidity() + uvuo.mutation.SetUIDValidity(i) + return uvuo +} + +// AddUIDValidity adds i to the "UIDValidity" field. +func (uvuo *UIDValidityUpdateOne) AddUIDValidity(i imap.UID) *UIDValidityUpdateOne { + uvuo.mutation.AddUIDValidity(i) + return uvuo +} + +// Mutation returns the UIDValidityMutation object of the builder. +func (uvuo *UIDValidityUpdateOne) Mutation() *UIDValidityMutation { + return uvuo.mutation +} + +// Select allows selecting one or more fields (columns) of the returned entity. +// The default is selecting all fields defined in the entity schema. +func (uvuo *UIDValidityUpdateOne) Select(field string, fields ...string) *UIDValidityUpdateOne { + uvuo.fields = append([]string{field}, fields...) + return uvuo +} + +// Save executes the query and returns the updated UIDValidity entity. +func (uvuo *UIDValidityUpdateOne) Save(ctx context.Context) (*UIDValidity, error) { + var ( + err error + node *UIDValidity + ) + if len(uvuo.hooks) == 0 { + node, err = uvuo.sqlSave(ctx) + } else { + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutation, ok := m.(*UIDValidityMutation) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + uvuo.mutation = mutation + node, err = uvuo.sqlSave(ctx) + mutation.done = true + return node, err + }) + for i := len(uvuo.hooks) - 1; i >= 0; i-- { + if uvuo.hooks[i] == nil { + return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = uvuo.hooks[i](mut) + } + v, err := mut.Mutate(ctx, uvuo.mutation) + if err != nil { + return nil, err + } + nv, ok := v.(*UIDValidity) + if !ok { + return nil, fmt.Errorf("unexpected node type %T returned from UIDValidityMutation", v) + } + node = nv + } + return node, err +} + +// SaveX is like Save, but panics if an error occurs. +func (uvuo *UIDValidityUpdateOne) SaveX(ctx context.Context) *UIDValidity { + node, err := uvuo.Save(ctx) + if err != nil { + panic(err) + } + return node +} + +// Exec executes the query on the entity. +func (uvuo *UIDValidityUpdateOne) Exec(ctx context.Context) error { + _, err := uvuo.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (uvuo *UIDValidityUpdateOne) ExecX(ctx context.Context) { + if err := uvuo.Exec(ctx); err != nil { + panic(err) + } +} + +func (uvuo *UIDValidityUpdateOne) sqlSave(ctx context.Context) (_node *UIDValidity, err error) { + _spec := &sqlgraph.UpdateSpec{ + Node: &sqlgraph.NodeSpec{ + Table: uidvalidity.Table, + Columns: uidvalidity.Columns, + ID: &sqlgraph.FieldSpec{ + Type: field.TypeInt, + Column: uidvalidity.FieldID, + }, + }, + } + id, ok := uvuo.mutation.ID() + if !ok { + return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "UIDValidity.id" for update`)} + } + _spec.Node.ID.Value = id + if fields := uvuo.fields; len(fields) > 0 { + _spec.Node.Columns = make([]string, 0, len(fields)) + _spec.Node.Columns = append(_spec.Node.Columns, uidvalidity.FieldID) + for _, f := range fields { + if !uidvalidity.ValidColumn(f) { + return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} + } + if f != uidvalidity.FieldID { + _spec.Node.Columns = append(_spec.Node.Columns, f) + } + } + } + if ps := uvuo.mutation.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + if value, ok := uvuo.mutation.UIDValidity(); ok { + _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ + Type: field.TypeUint32, + Value: value, + Column: uidvalidity.FieldUIDValidity, + }) + } + if value, ok := uvuo.mutation.AddedUIDValidity(); ok { + _spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{ + Type: field.TypeUint32, + Value: value, + Column: uidvalidity.FieldUIDValidity, + }) + } + _node = &UIDValidity{config: uvuo.config} + _spec.Assign = _node.assignValues + _spec.ScanValues = _node.scanValues + if err = sqlgraph.UpdateNode(ctx, uvuo.driver, _spec); err != nil { + if _, ok := err.(*sqlgraph.NotFoundError); ok { + err = &NotFoundError{uidvalidity.Label} + } else if sqlgraph.IsConstraintError(err) { + err = &ConstraintError{msg: err.Error(), wrap: err} + } + return nil, err + } + return _node, nil +} diff --git a/internal/db/mailbox.go b/internal/db/mailbox.go index 14130414..a7a614af 100644 --- a/internal/db/mailbox.go +++ b/internal/db/mailbox.go @@ -12,6 +12,7 @@ import ( "github.com/ProtonMail/gluon/internal/db/ent/message" "github.com/ProtonMail/gluon/internal/db/ent/messageflag" "github.com/ProtonMail/gluon/internal/db/ent/uid" + "github.com/ProtonMail/gluon/internal/db/ent/uidvalidity" "github.com/ProtonMail/gluon/internal/ids" "github.com/bradenaw/juniper/xslices" ) @@ -36,6 +37,13 @@ func CreateMailbox(ctx context.Context, tx *ent.Tx, labelID imap.LabelID, name s create = create.SetRemoteID(labelID) } + globalUIDValidity, err := getGlobalUIDValidity(ctx, tx) + if err != nil { + return nil, err + } + + create.SetUIDValidity(globalUIDValidity) + return create.Save(ctx) } @@ -63,6 +71,19 @@ func RenameMailboxWithRemoteID(ctx context.Context, tx *ent.Tx, mboxID imap.Labe } func DeleteMailboxWithRemoteID(ctx context.Context, tx *ent.Tx, mboxID imap.LabelID) error { + // get uid validity + mbox, err := tx.Mailbox.Query(). + Where(mailbox.RemoteID(mboxID)). + Select(mailbox.FieldUIDValidity). + Only(ctx) + if err != nil { + return err + } + + if err := updateGlobalUIDValidity(ctx, tx, mbox.UIDValidity); err != nil { + return err + } + if _, err := tx.Mailbox.Delete(). Where(mailbox.RemoteID(mboxID)). Exec(ctx); err != nil { @@ -281,3 +302,43 @@ func FilterMailboxContains(ctx context.Context, client *ent.Client, mboxID imap. return result, nil } + +func getGlobalUIDValidity(ctx context.Context, tx *ent.Tx) (imap.UID, error) { + globalUIDValidity, err := tx.UIDValidity.Query(). + Select(uidvalidity.FieldUIDValidity). + Only(ctx) + + if ent.IsNotFound(err) { + _, err := tx.UIDValidity.Create(). + SetUIDValidity(mailbox.DefaultUIDValidity). + Save(ctx) + if err != nil { + return 0, err + } + + return mailbox.DefaultUIDValidity, nil + } + + if err != nil { + return 0, err + } + + return globalUIDValidity.UIDValidity, nil +} + +func updateGlobalUIDValidity(ctx context.Context, tx *ent.Tx, deletedUIDValidity imap.UID) error { + globalUIDValidity, err := getGlobalUIDValidity(ctx, tx) + if err != nil { + return err + } + + if deletedUIDValidity < globalUIDValidity { + return nil + } + + _, err = tx.UIDValidity.Update(). + SetUIDValidity(deletedUIDValidity.Add(1)). + Save(ctx) + + return err +} diff --git a/tests/create_test.go b/tests/create_test.go index 7b0e6a72..9ebd456d 100644 --- a/tests/create_test.go +++ b/tests/create_test.go @@ -56,31 +56,43 @@ func TestCreatePreviousLevelHierarchyIfNonExisting(t *testing.T) { }) } -// TODO: GOMSRV-51. -func _TestEnsureNewMailboxWithDeletedNameHasGreaterId(t *testing.T) { +func TestEnsureNewMailboxWithDeletedNameHasGreaterId(t *testing.T) { runOneToOneTestClientWithAuth(t, defaultServerOptions(t), func(client *client.Client, _ *testSession) { - const ( - inboxName = "Folder" - ) - var firstId uint32 - var secondId uint32 + var oldValidity uint32 + var newValidity uint32 + { // create Folder inbox, get id and delete - require.NoError(t, client.Create(inboxName)) - mailboxStatus, err := client.Select(inboxName, true) + require.NoError(t, client.Create("mbox1")) + mailboxStatus, err := client.Select("mbox1", true) require.NoError(t, err) - firstId = mailboxStatus.UidValidity + oldValidity = mailboxStatus.UidValidity require.NoError(t, client.Unselect()) // Destroy Folder inbox - require.NoError(t, client.Delete(inboxName)) + require.NoError(t, client.Delete("mbox1")) + require.NoError(t, client.Create("mbox2")) } + { // re-create Folder inbox - require.NoError(t, client.Create(inboxName)) - mailboxStatus, err := client.Select(inboxName, true) + require.NoError(t, client.Create("mbox1")) + mailboxStatus, err := client.Select("mbox1", true) + require.NoError(t, err) + newValidity = mailboxStatus.UidValidity + require.Greater(t, newValidity, oldValidity) + oldValidity = newValidity + } + + { + require.NoError(t, client.Unselect()) + require.NoError(t, client.Delete("mbox1")) + require.NoError(t, client.Delete("mbox2")) + require.NoError(t, client.Create("mbox2")) + mailboxStatus, err := client.Select("mbox2", true) require.NoError(t, err) - secondId = mailboxStatus.UidValidity + newValidity = mailboxStatus.UidValidity + require.Greater(t, newValidity, oldValidity) } - require.NotEqual(t, secondId, firstId) + }) }