BREAKING-ish: SQL schema correction

While refactoring the schema yesterday, I forgot to add primary keys
back to the sessions and projects tables. Databases existing before
commit 984d44775b are fine and new
databases should be initialised correctly, but databases created between
commit 984d44775b and this one require
either SQL migrations or deleting willow.sqlite (which will remove all
users, projects, login sessions, etc.).

For the migrations, open the database with `sqlite3 willow.sqlite` and
paste the SQL statements below.

```
ALTER TABLE sessions RENAME TO sessions_bak;
CREATE TABLE sessions
(
    token      TEXT      NOT NULL PRIMARY KEY,
    username   TEXT      NOT NULL,
    expires    TIMESTAMP NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO sessions (token, username, expires, created_at)
    SELECT token, username, expires, created_at FROM sessions_bak;
ALTER TABLE projects RENAME TO projects_bak;
CREATE TABLE projects
(
    url        TEXT      NOT NULL PRIMARY KEY,
    name       TEXT      NOT NULL,
    forge      TEXT      NOT NULL,
    version    TEXT      NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO projects (url, name, forge, version, created_at)
    SELECT url, name, forge, version, created_at FROM projects_bak;
```

Assuming the statements execute without error, Willow starts up
correctly, and you can log in, you can safely drop open the database
again and drop the projects_bak and sessions_bak tables with

```
DROP TABLE projects_bak;
DROP TABLE sessions_bak;
```

---

References: https://todo.sr.ht/~amolith/willow/14
This commit is contained in:
Amolith 2023-10-30 18:41:48 -04:00
parent 181509faa8
commit aa29366696
Signed by: Amolith
GPG Key ID: 8AE30347CE28D101
1 changed files with 3 additions and 3 deletions

View File

@ -16,7 +16,7 @@ CREATE TABLE users
-- the session was created -- the session was created
CREATE TABLE sessions CREATE TABLE sessions
( (
token TEXT NOT NULL, token TEXT NOT NULL PRIMARY KEY,
username TEXT NOT NULL, username TEXT NOT NULL,
expires TIMESTAMP NOT NULL, expires TIMESTAMP NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
@ -26,7 +26,7 @@ CREATE TABLE sessions
-- timestamp of when the project was added -- timestamp of when the project was added
CREATE TABLE projects CREATE TABLE projects
( (
url TEXT NOT NULL, url TEXT NOT NULL PRIMARY KEY,
name TEXT NOT NULL, name TEXT NOT NULL,
forge TEXT NOT NULL, forge TEXT NOT NULL,
version TEXT NOT NULL, version TEXT NOT NULL,
@ -44,4 +44,4 @@ CREATE TABLE releases
content TEXT NOT NULL, content TEXT NOT NULL,
date TIMESTAMP NOT NULL, date TIMESTAMP NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
); );