Files
test-glm47/ai_coder_1/subtasks/Decomp-001-01.md
T
2026-02-04 16:54:36 +10:00

84 lines
2.7 KiB
Markdown

# Subtask: Database Schema Design
**Parent Task**: Decomp/001
**Subtask ID**: Decomp/001-01
**Dependencies**: None
## Description
Design and implement the database schema for URL storage, including tables for URLs, analytics, and custom aliases. The schema must support 10M URLs with efficient lookups and indexing.
## Requirements
- Support storage of original URLs, short codes, and metadata
- Track custom aliases (user-defined short codes)
- Store creation timestamps and TTL (expiration)
- Enable efficient lookup by short code (O(log n) or better)
- Support duplicate URL detection (same long URL → same short code)
- Handle index size for 10M URLs
## Technical Specifications
**Tables**:
```sql
-- URLs table (primary storage)
CREATE TABLE urls (
id BIGSERIAL PRIMARY KEY,
short_code VARCHAR(8) UNIQUE NOT NULL,
original_url TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
expires_at TIMESTAMP WITH TIME ZONE,
is_custom BOOLEAN DEFAULT FALSE,
checksum VARCHAR(64), -- SHA-256 of original_url for duplicate detection
INDEX idx_short_code (short_code),
INDEX idx_checksum (checksum),
INDEX idx_expires_at (expires_at) FOR cleanup
);
-- Analytics table (click tracking)
CREATE TABLE analytics (
id BIGSERIAL PRIMARY KEY,
url_id BIGINT REFERENCES urls(id) ON DELETE CASCADE,
clicked_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
user_agent TEXT,
ip_address INET,
country_code CHAR(2),
city VARCHAR(100),
referrer TEXT,
INDEX idx_url_id_clicked (url_id, clicked_at)
);
```
**Algorithm choices**:
- SHA-256 for URL checksum (duplicate detection)
- B-tree index on short_code for point lookups
- Composite index on (url_id, clicked_at) for analytics queries
**Data structures**:
- PostgreSQL (chosen over NoSQL for ACID guarantees and relational analytics)
- Partitioning strategy: hash partition by short_code for horizontal scaling
## Acceptance Criteria
- [ ] SQL schema file created with all tables and indexes
- [ ] Schema supports 10M URLs with <50ms lookup time
- [ ] Duplicate URL detection enabled via checksum index
- [ ] Foreign key constraints defined (analytics → URLs)
- [ ] Migration script provided for schema initialization
## Notes
**Trade-offs**:
- PostgreSQL over MongoDB: stronger consistency for redirects, better for analytics
- Hash partitioning chosen over range: even distribution of short codes
**Edge cases**:
- Very long URLs (>2000 chars): TEXT type supports unlimited length
- Unicode URLs: store as-is, encode/decode at application layer
- Expired URLs: soft delete (mark expired) + async cleanup job
**Alternatives considered**:
- NoSQL (MongoDB): rejected due to weaker consistency guarantees
- Separate deduplication table: rejected, checksum column sufficient