Migrate flight subscription data from Cloudflare KV to Cloudflare D1, ensuring all related operations (fetching, adding, retrieving user flights, scheduling cleanup) are updated to use D1.
CREATE TABLE subscriptions (
telegram_id TEXT,
flight_number TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
auto_cleanup_at DATETIME NULL,
PRIMARY KEY (telegram_id, flight_number)
);
-- Indexes for performance
CREATE INDEX idx_active_subs ON subscriptions(auto_cleanup_at) WHERE auto_cleanup_at IS NULL;
CREATE INDEX idx_cleanup_ready ON subscriptions(auto_cleanup_at) WHERE auto_cleanup_at IS NOT NULL;
CREATE INDEX idx_user_subs ON subscriptions(telegram_id);CREATE TABLE IF NOT EXISTS flights (
id TEXT PRIMARY KEY NOT NULL,
flight_number TEXT NOT NULL,
status TEXT NOT NULL,
scheduled_departure_time INTEGER,
actual_departure_time INTEGER,
scheduled_arrival_time INTEGER,
actual_arrival_time INTEGER,
city TEXT,
airline TEXT,
created_at INTEGER DEFAULT (strftime('%s', 'now')),
updated_at INTEGER DEFAULT (strftime('%s', 'now'))
);
-- Create indexes for efficient querying
CREATE INDEX IF NOT EXISTS idx_flight_number ON flights (flight_number);
CREATE INDEX IF NOT EXISTS idx_status ON flights (status);- Identify current KV usage: Locate all instances where
env.DB(KV) is used for subscription management. - Create D1 database and apply schema: Manually create the D1 database and apply the schema using
wrangler d1 execute <DB_NAME> --file=./schema.sql. (This step is outside the scope of automated changes but needs to be documented). - Update
workers/src/services/tracking.ts:- Modify the
getAllSubscriptionsfunction to query D1. - Update the
addSubscriptionfunction to insert into D1. - Adjust
getUserFlightsto fetch from D1. - Change
scheduleCleanupto update D1.
- Modify the
- Update
workers/src/index.ts:- Ensure the D1 binding (
env.DB) is correctly configured inwrangler.tomland accessible in the worker.
- Ensure the D1 binding (
- Testing: Verify all subscription-related functionalities work correctly with D1.
Migrate historical and current flight data from KV to D1, ensuring all flight tracking and data retrieval operations are updated to use D1.
- Create D1 database and apply schema: The
flightstable schema has been added toworkers/src/schema.sql, including newcityandairlinefields. Apply this schema to your D1 database. - Identify current KV usage for flight data: Locate all instances where KV is used to store and retrieve flight data.
- Migrate existing flight data from KV to D1:
- This step will be handled by a separate migration script.
- Update
workers/src/services/flightData.ts:- Modify functions that fetch or store flight data to use D1 queries instead of KV operations.
- Update
workers/src/services/tracking.ts:- Adjust any functions that rely on flight data to fetch from D1.
- Update
workers/src/handlers/cron.tsandworkers/src/handlers/alerts.ts:- Ensure these handlers use D1 for flight data interactions.
- Update
workers/src/types/index.ts:- Introduce
D1Flightinterface for the D1flightstable schema. - Ensure the existing
Flightinterface accurately reflects the external API response structure.
- Introduce
- Testing: Verify all flight data-related functionalities work correctly with D1.
Remove all gate-related code and Flightradar24 Telegram links from the project.
- Removed 'gate' column from
flightstable inworkers/src/schema.sql(confirmed it was not present). - Removed
gateproperty fromFlightinterface inworkers/src/types/index.ts(confirmedD1Flightdid not have it). - Confirmed no gate-related code in
workers/src/services/flightData.ts. - Removed Flightradar24 Telegram links from
workers/src/utils/formatting.ts. - Confirmed no affected code in
workers/src/handlers/alerts.ts,workers/src/handlers/commands.ts, andworkers/src/handlers/cron.ts. - Formatted modified files (
workers/src/types/index.ts,workers/src/utils/formatting.ts).
Complete removal of all Cloudflare KV references (env.FLIGHT_DATA) and migrate all remaining operations to D1.
-
Added
statustable to D1 schema (workers/src/schema.sql):- Created table to store system-wide key-value pairs
- Stores
latest-arrivals,prev-arrivals,update-counter, andlast-error
-
Updated
workers/src/index.ts:- Removed
FLIGHT_DATA: KVNamespacefromEnvinterface - Replaced KV
get('latest-arrivals')with D1 query tostatustable
- Removed
-
Updated
workers/src/handlers/cron.ts:- Replaced KV operations for
update-counter,latest-arrivals, andlast-error - All operations now use D1
statustable withINSERT OR REPLACEqueries
- Replaced KV operations for
-
Updated
workers/src/handlers/commands.ts:- Replaced all KV
get('latest-arrivals')andget('last-error')calls - All status queries now use D1
statustable
- Replaced all KV
-
Updated
workers/src/handlers/alerts.ts:- Replaced KV operations for
prev-arrivalsand tracking data - Migrated from KV tracking keys to D1
subscriptionstable queries - Simplified alert logic using existing D1 subscription relationships
- Replaced KV operations for
-
Updated
workers/src/services/flightData.ts:- Replaced legacy KV tracking cleanup with D1 subscription cleanup
- Uses
UPDATE subscriptions SET auto_cleanup_atfor completed flights
-
Verified configuration:
workers/wrangler.toml: FLIGHT_DATA KV binding already commented outworkers/worker-configuration.d.ts: Already contains only D1 binding
-
Formatted modified files: All TypeScript files formatted with Prettier
- Before: Mixed KV and D1 usage with complex tracking relationships
- After: Pure D1 implementation using
flights,subscriptions, andstatustables - Benefits: Simplified data model, better consistency, single database system
- Status: All KV references removed, system fully migrated to D1