-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.sql
More file actions
54 lines (48 loc) · 1.9 KB
/
init.sql
File metadata and controls
54 lines (48 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
-- Initialize database schema for Saga Pattern demo
USE saga_db;
-- Orders table
CREATE TABLE IF NOT EXISTS orders (
order_id VARCHAR(50) PRIMARY KEY,
customer_id VARCHAR(50) NOT NULL,
product_id VARCHAR(50) NOT NULL,
quantity INT NOT NULL,
total_amount DECIMAL(10, 2) NOT NULL,
status ENUM('PENDING', 'CONFIRMED', 'FAILED') DEFAULT 'PENDING',
failure_reason VARCHAR(255),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
-- Inventory table
CREATE TABLE IF NOT EXISTS inventory (
product_id VARCHAR(50) PRIMARY KEY,
product_name VARCHAR(100) NOT NULL,
quantity INT NOT NULL DEFAULT 0,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
-- Inventory reservations table
CREATE TABLE IF NOT EXISTS inventory_reservations (
reservation_id VARCHAR(50) PRIMARY KEY,
order_id VARCHAR(50) NOT NULL,
product_id VARCHAR(50) NOT NULL,
quantity INT NOT NULL,
status ENUM('RESERVED', 'RELEASED', 'CONFIRMED') DEFAULT 'RESERVED',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (product_id) REFERENCES inventory(product_id)
);
-- Payments table
CREATE TABLE IF NOT EXISTS payments (
payment_id VARCHAR(50) PRIMARY KEY,
order_id VARCHAR(50) NOT NULL,
amount DECIMAL(10, 2) NOT NULL,
status ENUM('PENDING', 'COMPLETED', 'FAILED') DEFAULT 'PENDING',
failure_reason VARCHAR(255),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
-- Insert initial inventory
INSERT INTO inventory (product_id, product_name, quantity) VALUES
('PROD-001', 'Laptop', 100),
('PROD-002', 'Phone', 50),
('PROD-003', 'Tablet', 0)
ON DUPLICATE KEY UPDATE product_name = VALUES(product_name);