bmc_hub/migrations/175_supplier_invoice_attachments.sql
Christian 8e8616c835 feat: Enhance vendor and customer linking functionality
- Added endpoints to link and unlink customers to vendors, including validation for relationship types.
- Implemented a UI for managing linked customers in the vendor detail view.
- Introduced a search feature for customers when linking to vendors.
- Updated database schema to support customer-vendor relationships with necessary constraints and indices.
- Added migration scripts for new tables and fields related to supplier invoices and customer-vendor links.
- Modified bottom bar visibility in the frontend for improved user experience.
2026-04-15 09:34:26 +02:00

49 lines
2.0 KiB
SQL

-- Migration 175: Supplier invoice attachments
-- Created: 2026-04-13
CREATE TABLE IF NOT EXISTS supplier_invoice_attachments (
id BIGSERIAL PRIMARY KEY,
supplier_invoice_id INTEGER NOT NULL REFERENCES supplier_invoices(id) ON DELETE CASCADE,
source_type VARCHAR(30) NOT NULL DEFAULT 'upload',
source_email_id INTEGER REFERENCES email_messages(id) ON DELETE SET NULL,
source_email_attachment_id INTEGER REFERENCES email_attachments(id) ON DELETE SET NULL,
filename VARCHAR(255) NOT NULL,
mime_type VARCHAR(120),
file_path TEXT,
size_bytes BIGINT,
notes TEXT,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM pg_constraint
WHERE conname = 'chk_supplier_invoice_attachments_source_type'
) THEN
ALTER TABLE supplier_invoice_attachments
ADD CONSTRAINT chk_supplier_invoice_attachments_source_type
CHECK (source_type IN ('email', 'upload', 'manual'));
END IF;
END $$;
CREATE INDEX IF NOT EXISTS idx_supplier_invoice_attachments_supplier_invoice_id
ON supplier_invoice_attachments(supplier_invoice_id);
CREATE INDEX IF NOT EXISTS idx_supplier_invoice_attachments_source_email_id
ON supplier_invoice_attachments(source_email_id)
WHERE source_email_id IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_supplier_invoice_attachments_source_email_attachment_id
ON supplier_invoice_attachments(source_email_attachment_id)
WHERE source_email_attachment_id IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_supplier_invoice_attachments_source_type
ON supplier_invoice_attachments(source_type);
COMMENT ON TABLE supplier_invoice_attachments IS 'Attachments linked to supplier invoices';
COMMENT ON COLUMN supplier_invoice_attachments.source_type IS 'Attachment source: email, upload, or manual';
COMMENT ON COLUMN supplier_invoice_attachments.source_email_id IS 'Source email id when attachment came from email';
COMMENT ON COLUMN supplier_invoice_attachments.source_email_attachment_id IS 'Source email attachment id when available';