Skip to content

Commit

Permalink
update attachment structure
Browse files Browse the repository at this point in the history
  • Loading branch information
vsimakhin committed Jan 17, 2025
1 parent 05a24d5 commit 8388859
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 23 deletions.
14 changes: 1 addition & 13 deletions internal/driver/db_structure.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package driver

var (
schemaVersion = "3.0.3"
schemaVersion = "3.0.4"

UUID = ColumnType{SQLite: "TEXT", MySQL: "VARCHAR(36)"}
DateTime = ColumnType{SQLite: "TEXT", MySQL: "VARCHAR(32)"}
Expand Down Expand Up @@ -89,7 +89,6 @@ var licensingTable = NewTable("licensing", "uuid", UUID,
var attachmentsTable = NewTable("attachments", "uuid", UUID,
[]Column{
{Name: "record_id", Type: UUID},
{Name: "description", Type: BigText, Properties: "NOT NULL DEFAULT ''"},
{Name: "document_name", Type: BigText},
{Name: "document", Type: Blob},
})
Expand Down Expand Up @@ -141,14 +140,3 @@ var airportsView = NewView("airports_view",
`,
},
)

var attachmentsView = NewView("attachments_view",
SQLQuery{
SQLite: `
SELECT uuid, record_id, IFNULL(description, "") AS description, document_name, document FROM attachments;
`,
MySQL: `
SELECT uuid, record_id, IFNULL(description, "") AS description, document_name, document FROM attachments;
`,
},
)
2 changes: 1 addition & 1 deletion internal/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func validateDB(db *sql.DB, engine string) error {
}

// check views
views := []*View{logbookView, airportsView, attachmentsView}
views := []*View{logbookView, airportsView}
for _, view := range views {
if err := view.initView(db, engine); err != nil {
return err
Expand Down
16 changes: 8 additions & 8 deletions internal/models/attachments.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func (m *DBModel) GetAttachments(recordID string) (attachments []Attachment, err
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

query := "SELECT uuid, record_id, description, document_name FROM attachments_view WHERE record_id = ?"
query := "SELECT uuid, record_id, document_name FROM attachments WHERE record_id = ?"
rows, err := m.DB.QueryContext(ctx, query, recordID)
if err != nil {
return attachments, err
Expand All @@ -19,7 +19,7 @@ func (m *DBModel) GetAttachments(recordID string) (attachments []Attachment, err

for rows.Next() {
var att Attachment
if err = rows.Scan(&att.UUID, &att.RecordID, &att.Description, &att.DocumentName); err != nil {
if err = rows.Scan(&att.UUID, &att.RecordID, &att.DocumentName); err != nil {
return attachments, err
}
attachments = append(attachments, att)
Expand All @@ -33,7 +33,7 @@ func (m *DBModel) GetAllAttachments() (attachments []Attachment, err error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

query := "SELECT uuid, record_id, description, document_name FROM attachments_view"
query := "SELECT uuid, record_id, document_name FROM attachments"
rows, err := m.DB.QueryContext(ctx, query)
if err != nil {
return attachments, err
Expand All @@ -42,7 +42,7 @@ func (m *DBModel) GetAllAttachments() (attachments []Attachment, err error) {

for rows.Next() {
var att Attachment
if err = rows.Scan(&att.UUID, &att.RecordID, &att.Description, &att.DocumentName); err != nil {
if err = rows.Scan(&att.UUID, &att.RecordID, &att.DocumentName); err != nil {
return attachments, err
}
attachments = append(attachments, att)
Expand All @@ -56,8 +56,8 @@ func (m *DBModel) InsertAttachmentRecord(att Attachment) (err error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

query := "INSERT INTO attachments (uuid, record_id, description, document_name, document) VALUES (?, ?, ?, ?, ?)"
_, err = m.DB.ExecContext(ctx, query, att.UUID, att.RecordID, att.Description, att.DocumentName, att.Document)
query := "INSERT INTO attachments (uuid, record_id, document_name, document) VALUES (?, ?, ?, ?)"
_, err = m.DB.ExecContext(ctx, query, att.UUID, att.RecordID, att.DocumentName, att.Document)

return err
}
Expand Down Expand Up @@ -100,8 +100,8 @@ func (m *DBModel) GetAttachmentByID(uuid string) (att Attachment, err error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

query := "SELECT uuid, record_id, description, document_name, document FROM attachments_view WHERE uuid = ?"
query := "SELECT uuid, record_id, document_name, document FROM attachments WHERE uuid = ?"
row := m.DB.QueryRowContext(ctx, query, uuid)
err = row.Scan(&att.UUID, &att.RecordID, &att.Description, &att.DocumentName, &att.Document)
err = row.Scan(&att.UUID, &att.RecordID, &att.DocumentName, &att.Document)
return att, err
}
1 change: 0 additions & 1 deletion internal/models/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ type License struct {
type Attachment struct {
UUID string `json:"uuid"`
RecordID string `json:"record_id"`
Description string `json:"description"`
DocumentName string `json:"document_name"`
Document []byte `json:"document"`
}
Expand Down

0 comments on commit 8388859

Please sign in to comment.