Skip to content

Commit

Permalink
Fix Python 3.12 sqlite deprecation warnings; migrate to more correct …
Browse files Browse the repository at this point in the history
…DATETIME sqlite column type.
  • Loading branch information
liffiton committed Jul 26, 2024
1 parent a8f3687 commit e8222af
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 16 deletions.
8 changes: 4 additions & 4 deletions dev/schema_model_evals.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ CREATE TABLE prompt (

CREATE TABLE prompt_set (
id INTEGER PRIMARY KEY AUTOINCREMENT,
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
created DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
query_src_file TEXT NOT NULL,
prompt_func TEXT NOT NULL
);
Expand All @@ -41,7 +41,7 @@ CREATE TABLE response (

CREATE TABLE response_set (
id INTEGER PRIMARY KEY AUTOINCREMENT,
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
created DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
prompt_set_id INTEGER NOT NULL,
model TEXT NOT NULL,
FOREIGN KEY(prompt_set_id) REFERENCES prompt_set(id)
Expand All @@ -50,7 +50,7 @@ CREATE TABLE response_set (

CREATE TABLE eval_prompt (
id INTEGER PRIMARY KEY AUTOINCREMENT,
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
created DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
sys_prompt TEXT NOT NULL UNIQUE
);

Expand All @@ -65,7 +65,7 @@ CREATE TABLE eval (

CREATE TABLE eval_set (
id INTEGER PRIMARY KEY AUTOINCREMENT,
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
created DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
response_set_id INTEGER NOT NULL,
eval_prompt_id INTEGER NOT NULL,
model TEXT NOT NULL,
Expand Down
4 changes: 2 additions & 2 deletions src/codehelp/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ PRAGMA foreign_keys = ON;
DROP TABLE IF EXISTS queries;
CREATE TABLE queries (
id INTEGER PRIMARY KEY AUTOINCREMENT,
query_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
query_time DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
context_name TEXT,
context_string_id INTEGER,
code TEXT,
Expand All @@ -32,7 +32,7 @@ CREATE INDEX queries_by_role ON queries(role_id);
DROP TABLE IF EXISTS chats;
CREATE TABLE chats (
id INTEGER PRIMARY KEY AUTOINCREMENT,
chat_started TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
chat_started DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
topic TEXT NOT NULL,
context_name TEXT,
context_string_id INTEGER,
Expand Down
26 changes: 26 additions & 0 deletions src/gened/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import sqlite3
import string
from collections.abc import Callable
from datetime import date, datetime
from getpass import getpass
from importlib import resources
from pathlib import Path
Expand All @@ -19,6 +20,31 @@
AUTH_PROVIDER_LOCAL = 1


# https://docs.python.org/3/library/sqlite3.html#sqlite3-adapter-converter-recipes
# Register adapters going from date/datetime to text (going into SQLite).
def adapt_date_iso(val: date) -> str:
"""Adapt datetime.date to ISO 8601 date."""
return val.isoformat()

def adapt_datetime_iso(val: datetime) -> str:
"""Adapt datetime.datetime to timezone-naive ISO 8601 date."""
return val.isoformat()

# And converters for coming from SQLite and converting back to date/datetime objects.
def convert_date(val: bytes) -> date:
"""Convert ISO 8601 date to datetime.date object."""
return date.fromisoformat(val.decode())

def convert_datetime(val: bytes) -> datetime:
"""Convert ISO 8601 datetime to datetime.datetime object."""
return datetime.fromisoformat(val.decode())

sqlite3.register_adapter(date, adapt_date_iso)
sqlite3.register_adapter(datetime, adapt_datetime_iso)
sqlite3.register_converter("date", convert_date)
sqlite3.register_converter("datetime", convert_datetime)


def get_db() -> sqlite3.Connection:
if 'db' not in g:
g.db = sqlite3.connect(
Expand Down
17 changes: 17 additions & 0 deletions src/gened/migrations/20240726--change-timestamp-to-datetime.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- SPDX-FileCopyrightText: 2024 Mark Liffiton <[email protected]>
--
-- SPDX-License-Identifier: AGPL-3.0-only

PRAGMA foreign_keys = OFF;

BEGIN;

-- Somewhat dangerous hack to switch column types given no ALTER COLUMN ability in SQLite
-- But the alternative is recreate, copy, and drop every table, keeping track of indexes, etc.
PRAGMA writable_schema = 1;
UPDATE SQLITE_MASTER SET SQL = REPLACE(SQL, ' TIMESTAMP ', ' DATETIME ');
PRAGMA writable_schema = 0;

COMMIT;

PRAGMA foreign_keys = ON;
18 changes: 9 additions & 9 deletions src/gened/schema_common.sql
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ CREATE TABLE consumers (
lti_secret TEXT,
openai_key TEXT,
model_id INTEGER NOT NULL DEFAULT 1, -- gpt-3.5
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
created DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY(model_id) REFERENCES models(id)
);

Expand Down Expand Up @@ -62,7 +62,7 @@ CREATE TABLE users (
is_admin BOOLEAN NOT NULL CHECK (is_admin IN (0,1)) DEFAULT 0,
is_tester BOOLEAN NOT NULL CHECK (is_tester IN (0,1)) DEFAULT 0,
query_tokens INTEGER NOT NULL DEFAULT 0, -- number of tokens left for making queries - 0 means cut off
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
created DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY(auth_provider) REFERENCES auth_providers(id)
);

Expand All @@ -71,15 +71,15 @@ CREATE TABLE auth_local (
user_id INTEGER PRIMARY KEY,
username TEXT NOT NULL,
password TEXT NOT NULL,
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
created DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY(user_id) REFERENCES users(id)
);

CREATE TABLE auth_external (
user_id INTEGER PRIMARY KEY,
auth_provider INTEGER NOT NULL,
ext_id TEXT NOT NULL, -- the primary, unique ID used by the external provider
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
created DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY(user_id) REFERENCES users(id),
FOREIGN KEY(auth_provider) REFERENCES auth_providers(id)
);
Expand All @@ -93,15 +93,15 @@ CREATE TABLE classes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
enabled BOOLEAN NOT NULL CHECK (enabled IN (0,1)) DEFAULT 1,
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP
created DATETIME DEFAULT CURRENT_TIMESTAMP
);

-- Classes created/accessed via LTI
CREATE TABLE classes_lti (
class_id INTEGER PRIMARY KEY, -- references classes.id
lti_consumer_id INTEGER NOT NULL, -- references consumers.id
lti_context_id TEXT NOT NULL, -- class ID from the LMS
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
created DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY(class_id) REFERENCES classes(id),
FOREIGN KEY(lti_consumer_id) REFERENCES consumers(id)
);
Expand All @@ -116,7 +116,7 @@ CREATE TABLE classes_user (
link_ident TEXT NOT NULL UNIQUE, -- random (unguessable) identifier used in access/registration link for this class
link_reg_expires DATE NOT NULL, -- registration active for the class link if this date is in the future (anywhere on Earth)
creator_user_id INTEGER NOT NULL, -- references users.id
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
created DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY(class_id) REFERENCES classes(id),
FOREIGN KEY(model_id) REFERENCES models(id),
FOREIGN KEY(creator_user_id) REFERENCES users(id)
Expand All @@ -133,7 +133,7 @@ CREATE TABLE contexts (
class_order INTEGER NOT NULL, -- position within manual ordering of contexts within a class
available DATE NOT NULL, -- date on which this context will be available to students (& mindate=available, maxdate=disabled)
config TEXT NOT NULL DEFAULT "{}", -- JSON containing context config options
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
created DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY(class_id) REFERENCES classes(id)
);
-- names must be unique within a class, and we often look up by class and name
Expand Down Expand Up @@ -165,7 +165,7 @@ CREATE TABLE demo_links (
CREATE TABLE migrations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
filename TEXT NOT NULL UNIQUE,
applied_on TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
applied_on DATETIME DEFAULT CURRENT_TIMESTAMP,
skipped BOOLEAN NOT NULL CHECK (skipped IN (0,1)) DEFAULT 0,
succeeded BOOLEAN NOT NULL CHECK (succeeded IN (0,1)) DEFAULT 0
);
Expand Down
2 changes: 1 addition & 1 deletion src/starburst/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ DROP TABLE IF EXISTS queries;

CREATE TABLE queries (
id INTEGER PRIMARY KEY AUTOINCREMENT,
query_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
query_time DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
assignment TEXT,
topics TEXT,
response_json TEXT,
Expand Down

0 comments on commit e8222af

Please sign in to comment.