changed file properties table so "ON DELETE CASCADE" works
This commit is contained in:
		
							parent
							
								
									0eaf4b1fb1
								
							
						
					
					
						commit
						d1423e4af9
					
				
					 6 changed files with 38 additions and 16 deletions
				
			
		| 
						 | 
				
			
			@ -22,15 +22,29 @@ func GetFileByID(fileID string) (sqlc.File, error) {
 | 
			
		|||
	return file, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func DeleteFileByID(fileID string) error {
 | 
			
		||||
	var pgUUID pgtype.UUID
 | 
			
		||||
	err := pgUUID.Scan(fileID)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		slog.Error("Unable to convert string to UUID", "file-uuid", fileID, "error", err)
 | 
			
		||||
	}
 | 
			
		||||
	query := sqlc.New(pool)
 | 
			
		||||
	err = query.DeleteFile(context.Background(), pgUUID)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		slog.Error("Unable to delete file", "file-uuid", fileID, "error", err)
 | 
			
		||||
	}
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func InsertFileProperties(properties sqlc.InsertFilePropertiesParams) error {
 | 
			
		||||
	query := sqlc.New(pool)
 | 
			
		||||
	slog.Debug("InsertFileProperties", "file-uuid", properties.ID.String(), "file-sha256",
 | 
			
		||||
	slog.Debug("InsertFileProperties", "file-uuid", properties.FileID.String(), "file-sha256",
 | 
			
		||||
		hex.EncodeToString(properties.Sha256), "file-md5", hex.EncodeToString(properties.Md5),
 | 
			
		||||
		"file-mime", properties.LibmagicMime.String, "file-extension", properties.LibmagicExtension.String,
 | 
			
		||||
		"file-apple", properties.LibmagicApple.String)
 | 
			
		||||
	err := query.InsertFileProperties(context.Background(), properties)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		slog.Error("Unable to add file properties", "file-uuid", properties.ID.String(), "error", err)
 | 
			
		||||
		slog.Error("Unable to add file properties", "file-uuid", properties.FileID.String(), "error", err)
 | 
			
		||||
	}
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,11 +1,12 @@
 | 
			
		|||
-- name: InsertFileProperties :exec
 | 
			
		||||
INSERT INTO file_properties (
 | 
			
		||||
  id, sha256, md5, libmagic_mime, libmagic_extension, libmagic_apple
 | 
			
		||||
) VALUES ($1, $2, $3, $4, $5, $6);
 | 
			
		||||
  file_id, sha256, md5, libmagic_mime, libmagic_extension, libmagic_apple
 | 
			
		||||
) VALUES ($1, $2, $3, $4, $5, $6)
 | 
			
		||||
;
 | 
			
		||||
 | 
			
		||||
-- name: GetFileProperties :one
 | 
			
		||||
SELECT * FROM file_properties
 | 
			
		||||
WHERE id = $1;
 | 
			
		||||
WHERE file_id = $1;
 | 
			
		||||
 | 
			
		||||
-- name: InsertFileDIEC :exec
 | 
			
		||||
INSERT INTO diec (
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -69,7 +69,8 @@ CREATE TABLE IF NOT EXISTS msoffice (
 | 
			
		|||
 | 
			
		||||
 | 
			
		||||
CREATE TABLE IF NOT EXISTS file_properties (
 | 
			
		||||
  id UUID PRIMARY KEY,
 | 
			
		||||
  id BIGSERIAL PRIMARY KEY,
 | 
			
		||||
  file_id UUID REFERENCES files (id) ON DELETE CASCADE,
 | 
			
		||||
  sha256 BYTEA,
 | 
			
		||||
  md5 BYTEA,
 | 
			
		||||
  libmagic_mime TEXT,
 | 
			
		||||
| 
						 | 
				
			
			@ -91,5 +92,6 @@ CREATE INDEX idx_msoffice_oleid_file_id ON msoffice_oleid (file_id);
 | 
			
		|||
CREATE INDEX idx_msoffice_olevba_file_id ON msoffice_olevba (file_id);
 | 
			
		||||
CREATE INDEX idx_msoffice_mraptor_file_id ON msoffice_mraptor (file_id);
 | 
			
		||||
CREATE INDEX idx_msoffice_results_file_id ON msoffice (file_id);
 | 
			
		||||
CREATE INDEX idx_file_properties_id ON file_properties (id);
 | 
			
		||||
CREATE INDEX idx_file_properties_file_id ON file_properties (file_id);
 | 
			
		||||
CREATE INDEX idx_file_id ON files (id);
 | 
			
		||||
CREATE INDEX idx_yara_results_file_id ON yara_results (file_id);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -28,7 +28,10 @@ func BasicProcessing(job sqlc.ProcessingJob) error {
 | 
			
		|||
	}
 | 
			
		||||
 | 
			
		||||
	fileProperties := sqlc.InsertFilePropertiesParams{}
 | 
			
		||||
	fileProperties.ID = job.FileID
 | 
			
		||||
	fileProperties.LibmagicMime.Valid = true
 | 
			
		||||
	fileProperties.LibmagicApple.Valid = true
 | 
			
		||||
	fileProperties.LibmagicExtension.Valid = true
 | 
			
		||||
	fileProperties.FileID = job.FileID
 | 
			
		||||
	fileProperties.Md5 = md5sum[:]
 | 
			
		||||
	fileProperties.Sha256 = sha256sum[:]
 | 
			
		||||
	fileProperties.LibmagicMime.String = fileCmdResult.MimeType
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -26,7 +26,8 @@ type File struct {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
type FileProperty struct {
 | 
			
		||||
	ID                pgtype.UUID
 | 
			
		||||
	ID                int64
 | 
			
		||||
	FileID            pgtype.UUID
 | 
			
		||||
	Sha256            []byte
 | 
			
		||||
	Md5               []byte
 | 
			
		||||
	LibmagicMime      pgtype.Text
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -12,15 +12,16 @@ import (
 | 
			
		|||
)
 | 
			
		||||
 | 
			
		||||
const getFileProperties = `-- name: GetFileProperties :one
 | 
			
		||||
SELECT id, sha256, md5, libmagic_mime, libmagic_extension, libmagic_apple FROM file_properties
 | 
			
		||||
WHERE id = $1
 | 
			
		||||
SELECT id, file_id, sha256, md5, libmagic_mime, libmagic_extension, libmagic_apple FROM file_properties
 | 
			
		||||
WHERE file_id = $1
 | 
			
		||||
`
 | 
			
		||||
 | 
			
		||||
func (q *Queries) GetFileProperties(ctx context.Context, id pgtype.UUID) (FileProperty, error) {
 | 
			
		||||
	row := q.db.QueryRow(ctx, getFileProperties, id)
 | 
			
		||||
func (q *Queries) GetFileProperties(ctx context.Context, fileID pgtype.UUID) (FileProperty, error) {
 | 
			
		||||
	row := q.db.QueryRow(ctx, getFileProperties, fileID)
 | 
			
		||||
	var i FileProperty
 | 
			
		||||
	err := row.Scan(
 | 
			
		||||
		&i.ID,
 | 
			
		||||
		&i.FileID,
 | 
			
		||||
		&i.Sha256,
 | 
			
		||||
		&i.Md5,
 | 
			
		||||
		&i.LibmagicMime,
 | 
			
		||||
| 
						 | 
				
			
			@ -48,12 +49,12 @@ func (q *Queries) InsertFileDIEC(ctx context.Context, arg InsertFileDIECParams)
 | 
			
		|||
 | 
			
		||||
const insertFileProperties = `-- name: InsertFileProperties :exec
 | 
			
		||||
INSERT INTO file_properties (
 | 
			
		||||
  id, sha256, md5, libmagic_mime, libmagic_extension, libmagic_apple
 | 
			
		||||
  file_id, sha256, md5, libmagic_mime, libmagic_extension, libmagic_apple
 | 
			
		||||
) VALUES ($1, $2, $3, $4, $5, $6)
 | 
			
		||||
`
 | 
			
		||||
 | 
			
		||||
type InsertFilePropertiesParams struct {
 | 
			
		||||
	ID                pgtype.UUID
 | 
			
		||||
	FileID            pgtype.UUID
 | 
			
		||||
	Sha256            []byte
 | 
			
		||||
	Md5               []byte
 | 
			
		||||
	LibmagicMime      pgtype.Text
 | 
			
		||||
| 
						 | 
				
			
			@ -63,7 +64,7 @@ type InsertFilePropertiesParams struct {
 | 
			
		|||
 | 
			
		||||
func (q *Queries) InsertFileProperties(ctx context.Context, arg InsertFilePropertiesParams) error {
 | 
			
		||||
	_, err := q.db.Exec(ctx, insertFileProperties,
 | 
			
		||||
		arg.ID,
 | 
			
		||||
		arg.FileID,
 | 
			
		||||
		arg.Sha256,
 | 
			
		||||
		arg.Md5,
 | 
			
		||||
		arg.LibmagicMime,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue