Simplify database schema: remove card_type, card_id, add card_number
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
This commit is contained in:
@@ -13,13 +13,12 @@ CREATE TABLE IF NOT EXISTS customers (
|
||||
CREATE TABLE IF NOT EXISTS cards (
|
||||
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
||||
card_number VARCHAR(50) NOT NULL UNIQUE,
|
||||
card_type VARCHAR(50),
|
||||
customer_id INT UNSIGNED NULL,
|
||||
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) ON DELETE SET NULL
|
||||
FOREIGN KEY (customer_id) REFERENCES customers(id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS transactions (
|
||||
@@ -31,19 +30,17 @@ CREATE TABLE IF NOT EXISTS transactions (
|
||||
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),
|
||||
card_id INT UNSIGNED NOT NULL,
|
||||
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_card_id (card_id),
|
||||
INDEX idx_customer_id (customer_id),
|
||||
INDEX idx_station (station),
|
||||
FOREIGN KEY (card_id) REFERENCES cards(id),
|
||||
FOREIGN KEY (customer_id) REFERENCES customers(id) ON DELETE SET NULL
|
||||
INDEX idx_card_number (card_number),
|
||||
INDEX idx_station (station)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||
|
||||
Reference in New Issue
Block a user