Restore accidentally removed files, a few additional import/calculation fixes
This commit is contained in:
29
inventory-server/migrations/001_create_import_sessions.sql
Normal file
29
inventory-server/migrations/001_create_import_sessions.sql
Normal file
@@ -0,0 +1,29 @@
|
||||
-- Migration: Create import_sessions table
|
||||
-- Run this against your PostgreSQL database
|
||||
|
||||
CREATE TABLE IF NOT EXISTS import_sessions (
|
||||
id SERIAL PRIMARY KEY,
|
||||
user_id INTEGER NOT NULL,
|
||||
name VARCHAR(255), -- NULL for unnamed/autosave sessions
|
||||
current_step VARCHAR(50) NOT NULL, -- 'validation' | 'imageUpload'
|
||||
data JSONB NOT NULL, -- Product rows
|
||||
product_images JSONB, -- Image assignments
|
||||
global_selections JSONB, -- Supplier, company, line, subline
|
||||
validation_state JSONB, -- Errors, UPC status, generated item numbers
|
||||
created_at TIMESTAMP DEFAULT NOW(),
|
||||
updated_at TIMESTAMP DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Ensure only one unnamed session per user (autosave slot)
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_unnamed_session_per_user
|
||||
ON import_sessions (user_id)
|
||||
WHERE name IS NULL;
|
||||
|
||||
-- Index for fast user lookups
|
||||
CREATE INDEX IF NOT EXISTS idx_import_sessions_user_id
|
||||
ON import_sessions (user_id);
|
||||
|
||||
-- Add comment for documentation
|
||||
COMMENT ON TABLE import_sessions IS 'Stores in-progress product import sessions for users';
|
||||
COMMENT ON COLUMN import_sessions.name IS 'Session name - NULL indicates the single unnamed/autosave session per user';
|
||||
COMMENT ON COLUMN import_sessions.current_step IS 'Which step the user was on: validation or imageUpload';
|
||||
@@ -0,0 +1,57 @@
|
||||
-- Migration: Make AI prompts extensible with is_singleton column
|
||||
-- Date: 2024-01-19
|
||||
-- Description: Removes hardcoded prompt_type CHECK constraint, adds is_singleton column
|
||||
-- for dynamic uniqueness enforcement, and creates appropriate indexes.
|
||||
|
||||
-- 1. Drop the old CHECK constraints on prompt_type (allows any string value now)
|
||||
ALTER TABLE ai_prompts DROP CONSTRAINT IF EXISTS ai_prompts_prompt_type_check;
|
||||
ALTER TABLE ai_prompts DROP CONSTRAINT IF EXISTS company_required_for_specific;
|
||||
|
||||
-- 2. Add is_singleton column (defaults to true for backwards compatibility)
|
||||
ALTER TABLE ai_prompts ADD COLUMN IF NOT EXISTS is_singleton BOOLEAN NOT NULL DEFAULT true;
|
||||
|
||||
-- 3. Drop ALL old unique constraints and indexes (cleanup)
|
||||
-- Some were created as CONSTRAINTS (via ADD CONSTRAINT), others as standalone indexes
|
||||
-- Must drop constraints first, then remaining standalone indexes
|
||||
|
||||
-- Drop constraints (these also remove their backing indexes)
|
||||
ALTER TABLE ai_prompts DROP CONSTRAINT IF EXISTS unique_company_prompt;
|
||||
ALTER TABLE ai_prompts DROP CONSTRAINT IF EXISTS idx_unique_general_prompt;
|
||||
ALTER TABLE ai_prompts DROP CONSTRAINT IF EXISTS idx_unique_system_prompt;
|
||||
|
||||
-- Drop standalone indexes (IF EXISTS handles cases where they don't exist)
|
||||
DROP INDEX IF EXISTS idx_unique_general_prompt;
|
||||
DROP INDEX IF EXISTS idx_unique_system_prompt;
|
||||
DROP INDEX IF EXISTS idx_unique_name_validation_system;
|
||||
DROP INDEX IF EXISTS idx_unique_name_validation_general;
|
||||
DROP INDEX IF EXISTS idx_unique_description_validation_system;
|
||||
DROP INDEX IF EXISTS idx_unique_description_validation_general;
|
||||
DROP INDEX IF EXISTS idx_unique_sanity_check_system;
|
||||
DROP INDEX IF EXISTS idx_unique_sanity_check_general;
|
||||
DROP INDEX IF EXISTS idx_unique_bulk_validation_system;
|
||||
DROP INDEX IF EXISTS idx_unique_bulk_validation_general;
|
||||
DROP INDEX IF EXISTS idx_unique_name_validation_company;
|
||||
DROP INDEX IF EXISTS idx_unique_description_validation_company;
|
||||
DROP INDEX IF EXISTS idx_unique_bulk_validation_company;
|
||||
|
||||
-- 4. Create new partial unique indexes based on is_singleton
|
||||
-- For singleton types WITHOUT company (only one per prompt_type)
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_singleton_no_company
|
||||
ON ai_prompts (prompt_type)
|
||||
WHERE is_singleton = true AND company IS NULL;
|
||||
|
||||
-- For singleton types WITH company (only one per prompt_type + company combination)
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_singleton_with_company
|
||||
ON ai_prompts (prompt_type, company)
|
||||
WHERE is_singleton = true AND company IS NOT NULL;
|
||||
|
||||
-- 5. Add index for fast lookups by type
|
||||
CREATE INDEX IF NOT EXISTS idx_prompt_type ON ai_prompts (prompt_type);
|
||||
|
||||
-- NOTE: After running this migration, you should:
|
||||
-- 1. Delete existing prompts with old types (general, system, company_specific)
|
||||
-- 2. Create new prompts with the new type naming convention:
|
||||
-- - name_validation_system, name_validation_general, name_validation_company_specific
|
||||
-- - description_validation_system, description_validation_general, description_validation_company_specific
|
||||
-- - sanity_check_system, sanity_check_general
|
||||
-- - bulk_validation_system, bulk_validation_general, bulk_validation_company_specific
|
||||
Reference in New Issue
Block a user