Files
cov-to-ics/db/init.py
2026-01-29 20:51:14 +00:00

82 lines
2.2 KiB
Python

import sqlite3
import uuid
import os
os.makedirs("data", exist_ok=True)
# Connect to SQLite database (creates file if not exists)
conn = sqlite3.connect("data/sqlite.db")
cursor = conn.cursor()
# Enable foreign key support
cursor.execute("PRAGMA foreign_keys = ON;")
# Create User table
cursor.execute("""
CREATE TABLE IF NOT EXISTS user (
id TEXT PRIMARY KEY,
username TEXT UNIQUE NOT NULL
);
""")
# Create Tokens table
cursor.execute("""
CREATE TABLE IF NOT EXISTS token (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY(user_id) REFERENCES user(id) ON DELETE CASCADE
);
""")
# Create Calendar table
cursor.execute("""
CREATE TABLE IF NOT EXISTS calendar (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
name TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY(user_id) REFERENCES user(id) ON DELETE CASCADE
);
""")
# Create Event table
cursor.execute("""
CREATE TABLE IF NOT EXISTS event (
id TEXT PRIMARY KEY,
calendar_id TEXT NOT NULL,
title TEXT NOT NULL,
description TEXT,
start_time DATETIME NOT NULL,
end_time DATETIME NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY(calendar_id) REFERENCES calendar(id) ON DELETE CASCADE
);
""")
conn.commit()
print("Database and tables created successfully!")
# Example: insert a user with a random UUID
user_id = str(uuid.uuid4())
cursor.execute("INSERT INTO user (id, username) VALUES (?, ?)", (user_id, "alice"))
# Example: insert a token for the user
token_id = str(uuid.uuid4())
cursor.execute("INSERT INTO token (id, user_id) VALUES (?, ?)", (token_id, user_id))
# Example: insert a calendar for the user
calendar_id = str(uuid.uuid4())
cursor.execute("INSERT INTO calendar (id, user_id, name) VALUES (?, ?, ?)",
(calendar_id, user_id, "Work Calendar"))
# Example: insert an event for the calendar
event_id = str(uuid.uuid4())
cursor.execute("""
INSERT INTO event (id, calendar_id, title, description, start_time, end_time)
VALUES (?, ?, ?, ?, ?, ?)
""", (event_id, calendar_id, "Meeting", "Project sync-up", "2026-01-23 10:00", "2026-01-23 11:00"))
conn.commit()
conn.close()