Skip to content

Commit 9f882be

Browse files
committed
Fix D1 database configuration for Cloudflare deployment
- Update wrangler.toml with correct database ID - Add migrations directory configuration - Create initial schema migration file - Apply migrations to both local and remote D1 databases - Successfully deploy to Cloudflare Workers
1 parent c1cec4b commit 9f882be

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

migrations/0001_initial_schema.sql

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
-- Initial schema migration for Comprehendo D1 database
2+
-- This migration creates all the required tables for the application
3+
4+
CREATE TABLE IF NOT EXISTS quiz (
5+
id INTEGER PRIMARY KEY AUTOINCREMENT,
6+
language TEXT NOT NULL,
7+
level TEXT NOT NULL,
8+
content TEXT NOT NULL,
9+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
10+
question_language TEXT,
11+
user_id INTEGER,
12+
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL
13+
);
14+
15+
CREATE TABLE IF NOT EXISTS users (
16+
id INTEGER PRIMARY KEY AUTOINCREMENT,
17+
provider_id TEXT NOT NULL,
18+
provider TEXT NOT NULL,
19+
name TEXT,
20+
email TEXT,
21+
image TEXT,
22+
first_login TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
23+
last_login TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
24+
language TEXT DEFAULT 'en',
25+
UNIQUE(provider_id, provider)
26+
);
27+
28+
CREATE TABLE IF NOT EXISTS user_language_progress (
29+
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
30+
language_code TEXT NOT NULL,
31+
cefr_level TEXT NOT NULL DEFAULT 'A1',
32+
correct_streak INTEGER NOT NULL DEFAULT 0,
33+
last_practiced TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
34+
PRIMARY KEY (user_id, language_code)
35+
);
36+
37+
CREATE TABLE IF NOT EXISTS question_feedback (
38+
id INTEGER PRIMARY KEY AUTOINCREMENT,
39+
quiz_id INTEGER NOT NULL,
40+
user_id INTEGER NOT NULL,
41+
is_good INTEGER NOT NULL,
42+
user_answer TEXT,
43+
is_correct INTEGER,
44+
submitted_at DATETIME DEFAULT CURRENT_TIMESTAMP,
45+
FOREIGN KEY (quiz_id) REFERENCES quiz (id) ON DELETE CASCADE,
46+
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE
47+
);
48+
49+
CREATE TABLE IF NOT EXISTS rate_limits (
50+
ip_address TEXT PRIMARY KEY,
51+
request_count INTEGER NOT NULL DEFAULT 1,
52+
window_start_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
53+
);
54+
55+
CREATE TABLE IF NOT EXISTS translation_cache (
56+
id INTEGER PRIMARY KEY AUTOINCREMENT,
57+
source_word TEXT NOT NULL,
58+
source_language TEXT NOT NULL,
59+
target_language TEXT NOT NULL,
60+
translated_text TEXT NOT NULL,
61+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
62+
UNIQUE(source_word, source_language, target_language)
63+
);
64+
65+
CREATE TABLE IF NOT EXISTS ai_api_usage (
66+
id INTEGER PRIMARY KEY AUTOINCREMENT,
67+
date TEXT NOT NULL,
68+
request_count INTEGER NOT NULL DEFAULT 0,
69+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
70+
UNIQUE(date)
71+
);
72+
73+
-- Create indexes for better performance
74+
CREATE INDEX IF NOT EXISTS idx_quiz_created_at ON quiz(created_at DESC);
75+
CREATE INDEX IF NOT EXISTS idx_users_last_login ON users(last_login DESC);
76+
CREATE INDEX IF NOT EXISTS idx_user_language_progress_last_practiced ON user_language_progress(last_practiced DESC);
77+
CREATE INDEX IF NOT EXISTS idx_question_feedback_quiz_id ON question_feedback (quiz_id);
78+
CREATE INDEX IF NOT EXISTS idx_question_feedback_user_id ON question_feedback (user_id);
79+
CREATE INDEX IF NOT EXISTS idx_translation_cache_lookup ON translation_cache(source_word, source_language, target_language);
80+
CREATE INDEX IF NOT EXISTS idx_ai_api_usage_date ON ai_api_usage(date);

wrangler.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ directory = ".open-next/assets"
1010
[[d1_databases]]
1111
binding = "DB"
1212
database_name = "comprehendo-db"
13-
# TODO: Replace the placeholder below with your actual database ID after creation
14-
database_id = "<YOUR_DATABASE_ID_HERE>"
13+
database_id = "a3e39277-f1f5-4c99-bee5-b41a20e01afa"
14+
migrations_dir = "migrations"
1515

1616
[vars]
1717
NODE_ENV = "production"

0 commit comments

Comments
 (0)