Refactor the database schema to better model the data relationships: Schema changes: - Removed cards.card_type (redundant, identical to card_number) - Removed transactions.card_id (unnecessary indirection) - Added transactions.card_number (stores card number for all transactions) - Made cards.customer_id NOT NULL (every card must belong to a customer) - Made transactions.customer_id nullable (NULL for anonymized transactions) Import logic changes: - Only create cards for known customers (transactions with customer_number) - Store card_number for ALL transactions (including anonymized) - Skip cards/customer creation for anonymized transactions Additional changes: - Add 'db reset' command to drop and recreate database - Update migration file with new schema This simplifies queries and better reflects the data model: - Cards table: authoritative mapping of card_number -> customer_id - Transactions table: stores all raw data including anonymized cards - Customer relationship via JOIN on card_number for known customers
47 lines
1.9 KiB
SQL
47 lines
1.9 KiB
SQL
-- Schema for rusty_petroleum
|
|
-- Run after creating the database
|
|
|
|
CREATE TABLE IF NOT EXISTS customers (
|
|
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
|
customer_number VARCHAR(50) NOT NULL UNIQUE,
|
|
card_report_group TINYINT UNSIGNED NOT NULL DEFAULT 0,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
|
INDEX idx_customer_number (customer_number)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
|
|
|
CREATE TABLE IF NOT EXISTS cards (
|
|
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
|
card_number VARCHAR(50) NOT NULL UNIQUE,
|
|
customer_id INT UNSIGNED NOT NULL,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
|
INDEX idx_card_number (card_number),
|
|
INDEX idx_customer_id (customer_id),
|
|
FOREIGN KEY (customer_id) REFERENCES customers(id)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
|
|
|
CREATE TABLE IF NOT EXISTS transactions (
|
|
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
|
transaction_date DATETIME NOT NULL,
|
|
batch_number VARCHAR(20) NOT NULL,
|
|
amount DECIMAL(10,2) NOT NULL,
|
|
volume DECIMAL(10,3) NOT NULL,
|
|
price DECIMAL(8,4) NOT NULL,
|
|
quality_code INT NOT NULL,
|
|
quality_name VARCHAR(50) NOT NULL,
|
|
card_number VARCHAR(50) NOT NULL,
|
|
station VARCHAR(20) NOT NULL,
|
|
terminal VARCHAR(10) NOT NULL,
|
|
pump VARCHAR(10) NOT NULL,
|
|
receipt VARCHAR(20) NOT NULL,
|
|
control_number VARCHAR(20),
|
|
customer_id INT UNSIGNED NULL,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
INDEX idx_transaction_date (transaction_date),
|
|
INDEX idx_batch_number (batch_number),
|
|
INDEX idx_customer_id (customer_id),
|
|
INDEX idx_card_number (card_number),
|
|
INDEX idx_station (station)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|