Fix up reset scripts again

This commit is contained in:
2025-01-12 00:44:53 -05:00
parent e48911ae24
commit 03ad15c731
4 changed files with 472 additions and 103 deletions

View File

@@ -85,10 +85,6 @@ CREATE TABLE IF NOT EXISTS vendor_metrics (
-- Re-enable foreign key checks
SET FOREIGN_KEY_CHECKS = 1;
-- Create optimized indexes for metrics calculations
CREATE INDEX idx_orders_metrics ON orders (product_id, date, canceled, quantity, price);
CREATE INDEX idx_purchase_orders_metrics ON purchase_orders (product_id, date, status, ordered, received);
-- Create view for inventory health (after all tables are created)
CREATE OR REPLACE VIEW inventory_health AS
SELECT

View File

@@ -1,5 +1,9 @@
-- Create tables if they don't exist
CREATE TABLE IF NOT EXISTS products (
-- Enable strict error reporting
SET sql_mode = 'STRICT_ALL_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ZERO_DATE,NO_ZERO_IN_DATE,NO_ENGINE_SUBSTITUTION';
SET FOREIGN_KEY_CHECKS = 0;
-- Create tables
CREATE TABLE products (
product_id BIGINT NOT NULL,
title VARCHAR(255) NOT NULL,
SKU VARCHAR(50) NOT NULL,
@@ -28,9 +32,29 @@ CREATE TABLE IF NOT EXISTS products (
UNIQUE KEY unique_sku (SKU),
INDEX idx_vendor (vendor),
INDEX idx_brand (brand)
);
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS orders (
-- Create categories table first (referenced by product_categories)
CREATE TABLE categories (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UNIQUE KEY unique_name (name)
) ENGINE=InnoDB;
-- Create product_categories junction table
CREATE TABLE product_categories (
product_id BIGINT NOT NULL,
category_id BIGINT NOT NULL,
PRIMARY KEY (product_id, category_id),
FOREIGN KEY (product_id) REFERENCES products(product_id) ON DELETE CASCADE,
FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE,
INDEX idx_category (category_id),
INDEX idx_product (product_id)
) ENGINE=InnoDB;
-- Create orders table with its indexes
CREATE TABLE orders (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
order_number VARCHAR(50) NOT NULL,
product_id BIGINT NOT NULL,
@@ -55,10 +79,12 @@ CREATE TABLE IF NOT EXISTS orders (
INDEX idx_customer (customer),
INDEX idx_date (date),
INDEX idx_status (status),
INDEX idx_orders_metrics (product_id, date, canceled, quantity, price),
UNIQUE KEY unique_order_product (order_number, product_id)
);
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS purchase_orders (
-- Create purchase_orders table with its indexes
CREATE TABLE purchase_orders (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
po_id VARCHAR(50) NOT NULL,
vendor VARCHAR(100) NOT NULL,
@@ -77,27 +103,11 @@ CREATE TABLE IF NOT EXISTS purchase_orders (
INDEX idx_po_id (po_id),
INDEX idx_vendor (vendor),
INDEX idx_status (status),
INDEX idx_purchase_orders_metrics (product_id, date, status, ordered, received),
UNIQUE KEY unique_po_product (po_id, product_id)
);
) ENGINE=InnoDB;
-- Create categories table
CREATE TABLE IF NOT EXISTS categories (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UNIQUE KEY unique_name (name)
);
-- Create product_categories junction table
CREATE TABLE IF NOT EXISTS product_categories (
product_id BIGINT NOT NULL,
category_id BIGINT NOT NULL,
PRIMARY KEY (product_id, category_id),
FOREIGN KEY (product_id) REFERENCES products(product_id) ON DELETE CASCADE,
FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE,
INDEX idx_category (category_id),
INDEX idx_product (product_id)
);
SET FOREIGN_KEY_CHECKS = 1;
-- Create views for common calculations
CREATE OR REPLACE VIEW product_sales_trends AS