From 8fb5039da53ac658fa279ae45ae87546833eb0f4 Mon Sep 17 00:00:00 2001 From: Hayden <64056131+hay-kot@users.noreply.github.com> Date: Sat, 24 Sep 2022 15:11:11 -0800 Subject: [PATCH] bad migration example --- backend/ent/item.go | 13 +- backend/ent/item/item.go | 5 + backend/ent/item/where.go | 106 +++++++++++++ backend/ent/item_create.go | 29 ++++ backend/ent/item_update.go | 42 +++++ backend/ent/migrate/schema.go | 5 +- backend/ent/mutation.go | 144 ++++++++++++------ backend/ent/runtime.go | 4 + backend/ent/schema/item.go | 3 + backend/internal/config/conf_database.go | 4 +- .../migrations/20220924230033_init.sql | 40 +++++ .../migrations/20220924230050_init.sql | 20 +++ .../internal/migrations/migrations/atlas.sum | 3 + 13 files changed, 368 insertions(+), 50 deletions(-) create mode 100644 backend/internal/migrations/migrations/20220924230033_init.sql create mode 100644 backend/internal/migrations/migrations/20220924230050_init.sql create mode 100644 backend/internal/migrations/migrations/atlas.sum diff --git a/backend/ent/item.go b/backend/ent/item.go index b58cbd2..e663887 100644 --- a/backend/ent/item.go +++ b/backend/ent/item.go @@ -61,6 +61,8 @@ type Item struct { SoldPrice float64 `json:"sold_price,omitempty"` // SoldNotes holds the value of the "sold_notes" field. SoldNotes string `json:"sold_notes,omitempty"` + // TestMigrationField holds the value of the "test_migration_field" field. + TestMigrationField string `json:"test_migration_field,omitempty"` // Edges holds the relations/edges for other nodes in the graph. // The values are being populated by the ItemQuery when eager-loading is set. Edges ItemEdges `json:"edges"` @@ -149,7 +151,7 @@ func (*Item) scanValues(columns []string) ([]interface{}, error) { values[i] = new(sql.NullFloat64) case item.FieldQuantity: values[i] = new(sql.NullInt64) - case item.FieldName, item.FieldDescription, item.FieldImportRef, item.FieldNotes, item.FieldSerialNumber, item.FieldModelNumber, item.FieldManufacturer, item.FieldWarrantyDetails, item.FieldPurchaseFrom, item.FieldSoldTo, item.FieldSoldNotes: + case item.FieldName, item.FieldDescription, item.FieldImportRef, item.FieldNotes, item.FieldSerialNumber, item.FieldModelNumber, item.FieldManufacturer, item.FieldWarrantyDetails, item.FieldPurchaseFrom, item.FieldSoldTo, item.FieldSoldNotes, item.FieldTestMigrationField: values[i] = new(sql.NullString) case item.FieldCreatedAt, item.FieldUpdatedAt, item.FieldWarrantyExpires, item.FieldPurchaseTime, item.FieldSoldTime: values[i] = new(sql.NullTime) @@ -306,6 +308,12 @@ func (i *Item) assignValues(columns []string, values []interface{}) error { } else if value.Valid { i.SoldNotes = value.String } + case item.FieldTestMigrationField: + if value, ok := values[j].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field test_migration_field", values[j]) + } else if value.Valid { + i.TestMigrationField = value.String + } case item.ForeignKeys[0]: if value, ok := values[j].(*sql.NullScanner); !ok { return fmt.Errorf("unexpected type %T for field group_items", values[j]) @@ -435,6 +443,9 @@ func (i *Item) String() string { builder.WriteString(", ") builder.WriteString("sold_notes=") builder.WriteString(i.SoldNotes) + builder.WriteString(", ") + builder.WriteString("test_migration_field=") + builder.WriteString(i.TestMigrationField) builder.WriteByte(')') return builder.String() } diff --git a/backend/ent/item/item.go b/backend/ent/item/item.go index ca05a2e..aa3b60b 100644 --- a/backend/ent/item/item.go +++ b/backend/ent/item/item.go @@ -55,6 +55,8 @@ const ( FieldSoldPrice = "sold_price" // FieldSoldNotes holds the string denoting the sold_notes field in the database. FieldSoldNotes = "sold_notes" + // FieldTestMigrationField holds the string denoting the test_migration_field field in the database. + FieldTestMigrationField = "test_migration_field" // EdgeGroup holds the string denoting the group edge name in mutations. EdgeGroup = "group" // EdgeLocation holds the string denoting the location edge name in mutations. @@ -126,6 +128,7 @@ var Columns = []string{ FieldSoldTo, FieldSoldPrice, FieldSoldNotes, + FieldTestMigrationField, } // ForeignKeys holds the SQL foreign-keys that are owned by the "items" @@ -191,6 +194,8 @@ var ( DefaultSoldPrice float64 // SoldNotesValidator is a validator for the "sold_notes" field. It is called by the builders before save. SoldNotesValidator func(string) error + // DefaultTestMigrationField holds the default value on creation for the "test_migration_field" field. + DefaultTestMigrationField string // DefaultID holds the default value on creation for the "id" field. DefaultID func() uuid.UUID ) diff --git a/backend/ent/item/where.go b/backend/ent/item/where.go index 6cec831..47b455b 100644 --- a/backend/ent/item/where.go +++ b/backend/ent/item/where.go @@ -229,6 +229,13 @@ func SoldNotes(v string) predicate.Item { }) } +// TestMigrationField applies equality check predicate on the "test_migration_field" field. It's identical to TestMigrationFieldEQ. +func TestMigrationField(v string) predicate.Item { + return predicate.Item(func(s *sql.Selector) { + s.Where(sql.EQ(s.C(FieldTestMigrationField), v)) + }) +} + // CreatedAtEQ applies the EQ predicate on the "created_at" field. func CreatedAtEQ(v time.Time) predicate.Item { return predicate.Item(func(s *sql.Selector) { @@ -2040,6 +2047,105 @@ func SoldNotesContainsFold(v string) predicate.Item { }) } +// TestMigrationFieldEQ applies the EQ predicate on the "test_migration_field" field. +func TestMigrationFieldEQ(v string) predicate.Item { + return predicate.Item(func(s *sql.Selector) { + s.Where(sql.EQ(s.C(FieldTestMigrationField), v)) + }) +} + +// TestMigrationFieldNEQ applies the NEQ predicate on the "test_migration_field" field. +func TestMigrationFieldNEQ(v string) predicate.Item { + return predicate.Item(func(s *sql.Selector) { + s.Where(sql.NEQ(s.C(FieldTestMigrationField), v)) + }) +} + +// TestMigrationFieldIn applies the In predicate on the "test_migration_field" field. +func TestMigrationFieldIn(vs ...string) predicate.Item { + v := make([]interface{}, len(vs)) + for i := range v { + v[i] = vs[i] + } + return predicate.Item(func(s *sql.Selector) { + s.Where(sql.In(s.C(FieldTestMigrationField), v...)) + }) +} + +// TestMigrationFieldNotIn applies the NotIn predicate on the "test_migration_field" field. +func TestMigrationFieldNotIn(vs ...string) predicate.Item { + v := make([]interface{}, len(vs)) + for i := range v { + v[i] = vs[i] + } + return predicate.Item(func(s *sql.Selector) { + s.Where(sql.NotIn(s.C(FieldTestMigrationField), v...)) + }) +} + +// TestMigrationFieldGT applies the GT predicate on the "test_migration_field" field. +func TestMigrationFieldGT(v string) predicate.Item { + return predicate.Item(func(s *sql.Selector) { + s.Where(sql.GT(s.C(FieldTestMigrationField), v)) + }) +} + +// TestMigrationFieldGTE applies the GTE predicate on the "test_migration_field" field. +func TestMigrationFieldGTE(v string) predicate.Item { + return predicate.Item(func(s *sql.Selector) { + s.Where(sql.GTE(s.C(FieldTestMigrationField), v)) + }) +} + +// TestMigrationFieldLT applies the LT predicate on the "test_migration_field" field. +func TestMigrationFieldLT(v string) predicate.Item { + return predicate.Item(func(s *sql.Selector) { + s.Where(sql.LT(s.C(FieldTestMigrationField), v)) + }) +} + +// TestMigrationFieldLTE applies the LTE predicate on the "test_migration_field" field. +func TestMigrationFieldLTE(v string) predicate.Item { + return predicate.Item(func(s *sql.Selector) { + s.Where(sql.LTE(s.C(FieldTestMigrationField), v)) + }) +} + +// TestMigrationFieldContains applies the Contains predicate on the "test_migration_field" field. +func TestMigrationFieldContains(v string) predicate.Item { + return predicate.Item(func(s *sql.Selector) { + s.Where(sql.Contains(s.C(FieldTestMigrationField), v)) + }) +} + +// TestMigrationFieldHasPrefix applies the HasPrefix predicate on the "test_migration_field" field. +func TestMigrationFieldHasPrefix(v string) predicate.Item { + return predicate.Item(func(s *sql.Selector) { + s.Where(sql.HasPrefix(s.C(FieldTestMigrationField), v)) + }) +} + +// TestMigrationFieldHasSuffix applies the HasSuffix predicate on the "test_migration_field" field. +func TestMigrationFieldHasSuffix(v string) predicate.Item { + return predicate.Item(func(s *sql.Selector) { + s.Where(sql.HasSuffix(s.C(FieldTestMigrationField), v)) + }) +} + +// TestMigrationFieldEqualFold applies the EqualFold predicate on the "test_migration_field" field. +func TestMigrationFieldEqualFold(v string) predicate.Item { + return predicate.Item(func(s *sql.Selector) { + s.Where(sql.EqualFold(s.C(FieldTestMigrationField), v)) + }) +} + +// TestMigrationFieldContainsFold applies the ContainsFold predicate on the "test_migration_field" field. +func TestMigrationFieldContainsFold(v string) predicate.Item { + return predicate.Item(func(s *sql.Selector) { + s.Where(sql.ContainsFold(s.C(FieldTestMigrationField), v)) + }) +} + // HasGroup applies the HasEdge predicate on the "group" edge. func HasGroup() predicate.Item { return predicate.Item(func(s *sql.Selector) { diff --git a/backend/ent/item_create.go b/backend/ent/item_create.go index 1ad1837..4788bb8 100644 --- a/backend/ent/item_create.go +++ b/backend/ent/item_create.go @@ -312,6 +312,20 @@ func (ic *ItemCreate) SetNillableSoldNotes(s *string) *ItemCreate { return ic } +// SetTestMigrationField sets the "test_migration_field" field. +func (ic *ItemCreate) SetTestMigrationField(s string) *ItemCreate { + ic.mutation.SetTestMigrationField(s) + return ic +} + +// SetNillableTestMigrationField sets the "test_migration_field" field if the given value is not nil. +func (ic *ItemCreate) SetNillableTestMigrationField(s *string) *ItemCreate { + if s != nil { + ic.SetTestMigrationField(*s) + } + return ic +} + // SetID sets the "id" field. func (ic *ItemCreate) SetID(u uuid.UUID) *ItemCreate { ic.mutation.SetID(u) @@ -506,6 +520,10 @@ func (ic *ItemCreate) defaults() { v := item.DefaultSoldPrice ic.mutation.SetSoldPrice(v) } + if _, ok := ic.mutation.TestMigrationField(); !ok { + v := item.DefaultTestMigrationField + ic.mutation.SetTestMigrationField(v) + } if _, ok := ic.mutation.ID(); !ok { v := item.DefaultID() ic.mutation.SetID(v) @@ -583,6 +601,9 @@ func (ic *ItemCreate) check() error { return &ValidationError{Name: "sold_notes", err: fmt.Errorf(`ent: validator failed for field "Item.sold_notes": %w`, err)} } } + if _, ok := ic.mutation.TestMigrationField(); !ok { + return &ValidationError{Name: "test_migration_field", err: errors.New(`ent: missing required field "Item.test_migration_field"`)} + } if _, ok := ic.mutation.GroupID(); !ok { return &ValidationError{Name: "group", err: errors.New(`ent: missing required edge "Item.group"`)} } @@ -790,6 +811,14 @@ func (ic *ItemCreate) createSpec() (*Item, *sqlgraph.CreateSpec) { }) _node.SoldNotes = value } + if value, ok := ic.mutation.TestMigrationField(); ok { + _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ + Type: field.TypeString, + Value: value, + Column: item.FieldTestMigrationField, + }) + _node.TestMigrationField = value + } if nodes := ic.mutation.GroupIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, diff --git a/backend/ent/item_update.go b/backend/ent/item_update.go index ef31b2a..76cacfd 100644 --- a/backend/ent/item_update.go +++ b/backend/ent/item_update.go @@ -377,6 +377,20 @@ func (iu *ItemUpdate) ClearSoldNotes() *ItemUpdate { return iu } +// SetTestMigrationField sets the "test_migration_field" field. +func (iu *ItemUpdate) SetTestMigrationField(s string) *ItemUpdate { + iu.mutation.SetTestMigrationField(s) + return iu +} + +// SetNillableTestMigrationField sets the "test_migration_field" field if the given value is not nil. +func (iu *ItemUpdate) SetNillableTestMigrationField(s *string) *ItemUpdate { + if s != nil { + iu.SetTestMigrationField(*s) + } + return iu +} + // SetGroupID sets the "group" edge to the Group entity by ID. func (iu *ItemUpdate) SetGroupID(id uuid.UUID) *ItemUpdate { iu.mutation.SetGroupID(id) @@ -899,6 +913,13 @@ func (iu *ItemUpdate) sqlSave(ctx context.Context) (n int, err error) { Column: item.FieldSoldNotes, }) } + if value, ok := iu.mutation.TestMigrationField(); ok { + _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ + Type: field.TypeString, + Value: value, + Column: item.FieldTestMigrationField, + }) + } if iu.mutation.GroupCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, @@ -1493,6 +1514,20 @@ func (iuo *ItemUpdateOne) ClearSoldNotes() *ItemUpdateOne { return iuo } +// SetTestMigrationField sets the "test_migration_field" field. +func (iuo *ItemUpdateOne) SetTestMigrationField(s string) *ItemUpdateOne { + iuo.mutation.SetTestMigrationField(s) + return iuo +} + +// SetNillableTestMigrationField sets the "test_migration_field" field if the given value is not nil. +func (iuo *ItemUpdateOne) SetNillableTestMigrationField(s *string) *ItemUpdateOne { + if s != nil { + iuo.SetTestMigrationField(*s) + } + return iuo +} + // SetGroupID sets the "group" edge to the Group entity by ID. func (iuo *ItemUpdateOne) SetGroupID(id uuid.UUID) *ItemUpdateOne { iuo.mutation.SetGroupID(id) @@ -2045,6 +2080,13 @@ func (iuo *ItemUpdateOne) sqlSave(ctx context.Context) (_node *Item, err error) Column: item.FieldSoldNotes, }) } + if value, ok := iuo.mutation.TestMigrationField(); ok { + _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ + Type: field.TypeString, + Value: value, + Column: item.FieldTestMigrationField, + }) + } if iuo.mutation.GroupCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, diff --git a/backend/ent/migrate/schema.go b/backend/ent/migrate/schema.go index 7b00b01..3b32bfb 100644 --- a/backend/ent/migrate/schema.go +++ b/backend/ent/migrate/schema.go @@ -159,6 +159,7 @@ var ( {Name: "sold_to", Type: field.TypeString, Nullable: true}, {Name: "sold_price", Type: field.TypeFloat64, Default: 0}, {Name: "sold_notes", Type: field.TypeString, Nullable: true, Size: 1000}, + {Name: "test_migration_field", Type: field.TypeString, Default: "test-migration-value"}, {Name: "group_items", Type: field.TypeUUID}, {Name: "location_items", Type: field.TypeUUID, Nullable: true}, } @@ -170,13 +171,13 @@ var ( ForeignKeys: []*schema.ForeignKey{ { Symbol: "items_groups_items", - Columns: []*schema.Column{ItemsColumns[22]}, + Columns: []*schema.Column{ItemsColumns[23]}, RefColumns: []*schema.Column{GroupsColumns[0]}, OnDelete: schema.Cascade, }, { Symbol: "items_locations_items", - Columns: []*schema.Column{ItemsColumns[23]}, + Columns: []*schema.Column{ItemsColumns[24]}, RefColumns: []*schema.Column{LocationsColumns[0]}, OnDelete: schema.Cascade, }, diff --git a/backend/ent/mutation.go b/backend/ent/mutation.go index bbd71a5..57a9ed4 100644 --- a/backend/ent/mutation.go +++ b/backend/ent/mutation.go @@ -3406,50 +3406,51 @@ func (m *GroupMutation) ResetEdge(name string) error { // ItemMutation represents an operation that mutates the Item nodes in the graph. type ItemMutation struct { config - op Op - typ string - id *uuid.UUID - created_at *time.Time - updated_at *time.Time - name *string - description *string - import_ref *string - notes *string - quantity *int - addquantity *int - insured *bool - serial_number *string - model_number *string - manufacturer *string - lifetime_warranty *bool - warranty_expires *time.Time - warranty_details *string - purchase_time *time.Time - purchase_from *string - purchase_price *float64 - addpurchase_price *float64 - sold_time *time.Time - sold_to *string - sold_price *float64 - addsold_price *float64 - sold_notes *string - clearedFields map[string]struct{} - group *uuid.UUID - clearedgroup bool - location *uuid.UUID - clearedlocation bool - fields map[uuid.UUID]struct{} - removedfields map[uuid.UUID]struct{} - clearedfields bool - label map[uuid.UUID]struct{} - removedlabel map[uuid.UUID]struct{} - clearedlabel bool - attachments map[uuid.UUID]struct{} - removedattachments map[uuid.UUID]struct{} - clearedattachments bool - done bool - oldValue func(context.Context) (*Item, error) - predicates []predicate.Item + op Op + typ string + id *uuid.UUID + created_at *time.Time + updated_at *time.Time + name *string + description *string + import_ref *string + notes *string + quantity *int + addquantity *int + insured *bool + serial_number *string + model_number *string + manufacturer *string + lifetime_warranty *bool + warranty_expires *time.Time + warranty_details *string + purchase_time *time.Time + purchase_from *string + purchase_price *float64 + addpurchase_price *float64 + sold_time *time.Time + sold_to *string + sold_price *float64 + addsold_price *float64 + sold_notes *string + test_migration_field *string + clearedFields map[string]struct{} + group *uuid.UUID + clearedgroup bool + location *uuid.UUID + clearedlocation bool + fields map[uuid.UUID]struct{} + removedfields map[uuid.UUID]struct{} + clearedfields bool + label map[uuid.UUID]struct{} + removedlabel map[uuid.UUID]struct{} + clearedlabel bool + attachments map[uuid.UUID]struct{} + removedattachments map[uuid.UUID]struct{} + clearedattachments bool + done bool + oldValue func(context.Context) (*Item, error) + predicates []predicate.Item } var _ ent.Mutation = (*ItemMutation)(nil) @@ -4541,6 +4542,42 @@ func (m *ItemMutation) ResetSoldNotes() { delete(m.clearedFields, item.FieldSoldNotes) } +// SetTestMigrationField sets the "test_migration_field" field. +func (m *ItemMutation) SetTestMigrationField(s string) { + m.test_migration_field = &s +} + +// TestMigrationField returns the value of the "test_migration_field" field in the mutation. +func (m *ItemMutation) TestMigrationField() (r string, exists bool) { + v := m.test_migration_field + if v == nil { + return + } + return *v, true +} + +// OldTestMigrationField returns the old "test_migration_field" field's value of the Item entity. +// If the Item 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 *ItemMutation) OldTestMigrationField(ctx context.Context) (v string, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldTestMigrationField is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldTestMigrationField requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldTestMigrationField: %w", err) + } + return oldValue.TestMigrationField, nil +} + +// ResetTestMigrationField resets all changes to the "test_migration_field" field. +func (m *ItemMutation) ResetTestMigrationField() { + m.test_migration_field = nil +} + // SetGroupID sets the "group" edge to the Group entity by id. func (m *ItemMutation) SetGroupID(id uuid.UUID) { m.group = &id @@ -4800,7 +4837,7 @@ func (m *ItemMutation) Type() string { // order to get all numeric fields that were incremented/decremented, call // AddedFields(). func (m *ItemMutation) Fields() []string { - fields := make([]string, 0, 21) + fields := make([]string, 0, 22) if m.created_at != nil { fields = append(fields, item.FieldCreatedAt) } @@ -4864,6 +4901,9 @@ func (m *ItemMutation) Fields() []string { if m.sold_notes != nil { fields = append(fields, item.FieldSoldNotes) } + if m.test_migration_field != nil { + fields = append(fields, item.FieldTestMigrationField) + } return fields } @@ -4914,6 +4954,8 @@ func (m *ItemMutation) Field(name string) (ent.Value, bool) { return m.SoldPrice() case item.FieldSoldNotes: return m.SoldNotes() + case item.FieldTestMigrationField: + return m.TestMigrationField() } return nil, false } @@ -4965,6 +5007,8 @@ func (m *ItemMutation) OldField(ctx context.Context, name string) (ent.Value, er return m.OldSoldPrice(ctx) case item.FieldSoldNotes: return m.OldSoldNotes(ctx) + case item.FieldTestMigrationField: + return m.OldTestMigrationField(ctx) } return nil, fmt.Errorf("unknown Item field %s", name) } @@ -5121,6 +5165,13 @@ func (m *ItemMutation) SetField(name string, value ent.Value) error { } m.SetSoldNotes(v) return nil + case item.FieldTestMigrationField: + v, ok := value.(string) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetTestMigrationField(v) + return nil } return fmt.Errorf("unknown Item field %s", name) } @@ -5353,6 +5404,9 @@ func (m *ItemMutation) ResetField(name string) error { case item.FieldSoldNotes: m.ResetSoldNotes() return nil + case item.FieldTestMigrationField: + m.ResetTestMigrationField() + return nil } return fmt.Errorf("unknown Item field %s", name) } diff --git a/backend/ent/runtime.go b/backend/ent/runtime.go index 6414b69..a4b9257 100644 --- a/backend/ent/runtime.go +++ b/backend/ent/runtime.go @@ -275,6 +275,10 @@ func init() { itemDescSoldNotes := itemFields[16].Descriptor() // item.SoldNotesValidator is a validator for the "sold_notes" field. It is called by the builders before save. item.SoldNotesValidator = itemDescSoldNotes.Validators[0].(func(string) error) + // itemDescTestMigrationField is the schema descriptor for test_migration_field field. + itemDescTestMigrationField := itemFields[17].Descriptor() + // item.DefaultTestMigrationField holds the default value on creation for the test_migration_field field. + item.DefaultTestMigrationField = itemDescTestMigrationField.Default.(string) // itemDescID is the schema descriptor for id field. itemDescID := itemMixinFields0[0].Descriptor() // item.DefaultID holds the default value on creation for the id field. diff --git a/backend/ent/schema/item.go b/backend/ent/schema/item.go index 9728fd8..e9b62db 100644 --- a/backend/ent/schema/item.go +++ b/backend/ent/schema/item.go @@ -88,6 +88,9 @@ func (Item) Fields() []ent.Field { field.String("sold_notes"). MaxLen(1000). Optional(), + + field.String("test_migration_field"). + Default("test-migration-value"), } } diff --git a/backend/internal/config/conf_database.go b/backend/internal/config/conf_database.go index 047d5d2..69a67b9 100644 --- a/backend/internal/config/conf_database.go +++ b/backend/internal/config/conf_database.go @@ -6,6 +6,6 @@ const ( type Storage struct { // Data is the path to the root directory - Data string `yaml:"data" conf:"default:./homebox-data"` - SqliteUrl string `yaml:"sqlite-url" conf:"default:./homebox-data/homebox.db?_fk=1"` + Data string `yaml:"data" conf:"default:./.data"` + SqliteUrl string `yaml:"sqlite-url" conf:"default:./.data/homebox.db?_fk=1"` } diff --git a/backend/internal/migrations/migrations/20220924230033_init.sql b/backend/internal/migrations/migrations/20220924230033_init.sql new file mode 100644 index 0000000..579d559 --- /dev/null +++ b/backend/internal/migrations/migrations/20220924230033_init.sql @@ -0,0 +1,40 @@ +-- create "attachments" table +CREATE TABLE `attachments` (`id` uuid NOT NULL, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL, `type` text NOT NULL DEFAULT 'attachment', `document_attachments` uuid NOT NULL, `item_attachments` uuid NOT NULL, PRIMARY KEY (`id`), CONSTRAINT `attachments_documents_attachments` FOREIGN KEY (`document_attachments`) REFERENCES `documents` (`id`) ON DELETE CASCADE, CONSTRAINT `attachments_items_attachments` FOREIGN KEY (`item_attachments`) REFERENCES `items` (`id`) ON DELETE CASCADE); +-- create "auth_tokens" table +CREATE TABLE `auth_tokens` (`id` uuid NOT NULL, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL, `token` blob NOT NULL, `expires_at` datetime NOT NULL, `user_auth_tokens` uuid NULL, PRIMARY KEY (`id`), CONSTRAINT `auth_tokens_users_auth_tokens` FOREIGN KEY (`user_auth_tokens`) REFERENCES `users` (`id`) ON DELETE CASCADE); +-- create index "auth_tokens_token_key" to table: "auth_tokens" +CREATE UNIQUE INDEX `auth_tokens_token_key` ON `auth_tokens` (`token`); +-- create index "authtokens_token" to table: "auth_tokens" +CREATE INDEX `authtokens_token` ON `auth_tokens` (`token`); +-- create "documents" table +CREATE TABLE `documents` (`id` uuid NOT NULL, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL, `title` text NOT NULL, `path` text NOT NULL, `group_documents` uuid NOT NULL, PRIMARY KEY (`id`), CONSTRAINT `documents_groups_documents` FOREIGN KEY (`group_documents`) REFERENCES `groups` (`id`) ON DELETE CASCADE); +-- create "document_tokens" table +CREATE TABLE `document_tokens` (`id` uuid NOT NULL, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL, `token` blob NOT NULL, `uses` integer NOT NULL DEFAULT 1, `expires_at` datetime NOT NULL, `document_document_tokens` uuid NULL, PRIMARY KEY (`id`), CONSTRAINT `document_tokens_documents_document_tokens` FOREIGN KEY (`document_document_tokens`) REFERENCES `documents` (`id`) ON DELETE CASCADE); +-- create index "document_tokens_token_key" to table: "document_tokens" +CREATE UNIQUE INDEX `document_tokens_token_key` ON `document_tokens` (`token`); +-- create index "documenttoken_token" to table: "document_tokens" +CREATE INDEX `documenttoken_token` ON `document_tokens` (`token`); +-- create "groups" table +CREATE TABLE `groups` (`id` uuid NOT NULL, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL, `name` text NOT NULL, `currency` text NOT NULL DEFAULT 'usd', PRIMARY KEY (`id`)); +-- create "items" table +CREATE TABLE `items` (`id` uuid NOT NULL, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL, `name` text NOT NULL, `description` text NULL, `import_ref` text NULL, `notes` text NULL, `quantity` integer NOT NULL DEFAULT 1, `insured` bool NOT NULL DEFAULT false, `serial_number` text NULL, `model_number` text NULL, `manufacturer` text NULL, `lifetime_warranty` bool NOT NULL DEFAULT false, `warranty_expires` datetime NULL, `warranty_details` text NULL, `purchase_time` datetime NULL, `purchase_from` text NULL, `purchase_price` real NOT NULL DEFAULT 0, `sold_time` datetime NULL, `sold_to` text NULL, `sold_price` real NOT NULL DEFAULT 0, `sold_notes` text NULL, `group_items` uuid NOT NULL, `location_items` uuid NULL, PRIMARY KEY (`id`), CONSTRAINT `items_groups_items` FOREIGN KEY (`group_items`) REFERENCES `groups` (`id`) ON DELETE CASCADE, CONSTRAINT `items_locations_items` FOREIGN KEY (`location_items`) REFERENCES `locations` (`id`) ON DELETE CASCADE); +-- create index "item_name" to table: "items" +CREATE INDEX `item_name` ON `items` (`name`); +-- create index "item_manufacturer" to table: "items" +CREATE INDEX `item_manufacturer` ON `items` (`manufacturer`); +-- create index "item_model_number" to table: "items" +CREATE INDEX `item_model_number` ON `items` (`model_number`); +-- create index "item_serial_number" to table: "items" +CREATE INDEX `item_serial_number` ON `items` (`serial_number`); +-- create "item_fields" table +CREATE TABLE `item_fields` (`id` uuid NOT NULL, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL, `name` text NOT NULL, `description` text NULL, `type` text NOT NULL, `text_value` text NULL, `number_value` integer NULL, `boolean_value` bool NOT NULL DEFAULT false, `time_value` datetime NOT NULL, `item_fields` uuid NULL, PRIMARY KEY (`id`), CONSTRAINT `item_fields_items_fields` FOREIGN KEY (`item_fields`) REFERENCES `items` (`id`) ON DELETE CASCADE); +-- create "labels" table +CREATE TABLE `labels` (`id` uuid NOT NULL, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL, `name` text NOT NULL, `description` text NULL, `color` text NULL, `group_labels` uuid NOT NULL, PRIMARY KEY (`id`), CONSTRAINT `labels_groups_labels` FOREIGN KEY (`group_labels`) REFERENCES `groups` (`id`) ON DELETE CASCADE); +-- create "locations" table +CREATE TABLE `locations` (`id` uuid NOT NULL, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL, `name` text NOT NULL, `description` text NULL, `group_locations` uuid NOT NULL, PRIMARY KEY (`id`), CONSTRAINT `locations_groups_locations` FOREIGN KEY (`group_locations`) REFERENCES `groups` (`id`) ON DELETE CASCADE); +-- create "users" table +CREATE TABLE `users` (`id` uuid NOT NULL, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL, `name` text NOT NULL, `email` text NOT NULL, `password` text NOT NULL, `is_superuser` bool NOT NULL DEFAULT false, `group_users` uuid NOT NULL, PRIMARY KEY (`id`), CONSTRAINT `users_groups_users` FOREIGN KEY (`group_users`) REFERENCES `groups` (`id`) ON DELETE CASCADE); +-- create index "users_email_key" to table: "users" +CREATE UNIQUE INDEX `users_email_key` ON `users` (`email`); +-- create "label_items" table +CREATE TABLE `label_items` (`label_id` uuid NOT NULL, `item_id` uuid NOT NULL, PRIMARY KEY (`label_id`, `item_id`), CONSTRAINT `label_items_label_id` FOREIGN KEY (`label_id`) REFERENCES `labels` (`id`) ON DELETE CASCADE, CONSTRAINT `label_items_item_id` FOREIGN KEY (`item_id`) REFERENCES `items` (`id`) ON DELETE CASCADE); diff --git a/backend/internal/migrations/migrations/20220924230050_init.sql b/backend/internal/migrations/migrations/20220924230050_init.sql new file mode 100644 index 0000000..e4baa0b --- /dev/null +++ b/backend/internal/migrations/migrations/20220924230050_init.sql @@ -0,0 +1,20 @@ +-- disable the enforcement of foreign-keys constraints +PRAGMA foreign_keys = off; +-- create "new_items" table +CREATE TABLE `new_items` (`id` uuid NOT NULL, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL, `name` text NOT NULL, `description` text NULL, `import_ref` text NULL, `notes` text NULL, `quantity` integer NOT NULL DEFAULT 1, `insured` bool NOT NULL DEFAULT false, `serial_number` text NULL, `model_number` text NULL, `manufacturer` text NULL, `lifetime_warranty` bool NOT NULL DEFAULT false, `warranty_expires` datetime NULL, `warranty_details` text NULL, `purchase_time` datetime NULL, `purchase_from` text NULL, `purchase_price` real NOT NULL DEFAULT 0, `sold_time` datetime NULL, `sold_to` text NULL, `sold_price` real NOT NULL DEFAULT 0, `sold_notes` text NULL, `test_migration_field` text NOT NULL DEFAULT 'test-migration-value', `group_items` uuid NOT NULL, `location_items` uuid NULL, PRIMARY KEY (`id`), CONSTRAINT `items_groups_items` FOREIGN KEY (`group_items`) REFERENCES `groups` (`id`) ON DELETE CASCADE, CONSTRAINT `items_locations_items` FOREIGN KEY (`location_items`) REFERENCES `locations` (`id`) ON DELETE CASCADE); +-- copy rows from old table "items" to new temporary table "new_items" +INSERT INTO `new_items` (`id`, `created_at`, `updated_at`, `name`, `description`, `import_ref`, `notes`, `quantity`, `insured`, `serial_number`, `model_number`, `manufacturer`, `lifetime_warranty`, `warranty_expires`, `warranty_details`, `purchase_time`, `purchase_from`, `purchase_price`, `sold_time`, `sold_to`, `sold_price`, `sold_notes`, `group_items`, `location_items`) SELECT `id`, `created_at`, `updated_at`, `name`, `description`, `import_ref`, `notes`, `quantity`, `insured`, `serial_number`, `model_number`, `manufacturer`, `lifetime_warranty`, `warranty_expires`, `warranty_details`, `purchase_time`, `purchase_from`, `purchase_price`, `sold_time`, `sold_to`, `sold_price`, `sold_notes`, `group_items`, `location_items` FROM `items`; +-- drop "items" table after copying rows +DROP TABLE `items`; +-- rename temporary table "new_items" to "items" +ALTER TABLE `new_items` RENAME TO `items`; +-- create index "item_name" to table: "items" +CREATE INDEX `item_name` ON `items` (`name`); +-- create index "item_manufacturer" to table: "items" +CREATE INDEX `item_manufacturer` ON `items` (`manufacturer`); +-- create index "item_model_number" to table: "items" +CREATE INDEX `item_model_number` ON `items` (`model_number`); +-- create index "item_serial_number" to table: "items" +CREATE INDEX `item_serial_number` ON `items` (`serial_number`); +-- enable back the enforcement of foreign-keys constraints +PRAGMA foreign_keys = on; diff --git a/backend/internal/migrations/migrations/atlas.sum b/backend/internal/migrations/migrations/atlas.sum new file mode 100644 index 0000000..1993f65 --- /dev/null +++ b/backend/internal/migrations/migrations/atlas.sum @@ -0,0 +1,3 @@ +h1:iT4/mowRSTJS2cBVne40fhGw9l4NVFeb6LBsXO+WSWA= +20220924230033_init.sql h1:NIjwWnwupwH/EyZC5tZnA/dmff1vMhG2I3EiVQQUNbA= +20220924230050_init.sql h1:PRr2ZtlBT9kaYk+0JjvKE5FuPe7pK8U0cXGRjbxeaYE=