willow/db/sql/schema.sql

48 lines
1.5 KiB
MySQL
Raw Normal View History

-- SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
--
-- SPDX-License-Identifier: CC0-1.0
-- Create table of users with username, password hash, salt, and creation
-- timestamp
CREATE TABLE users
(
username TEXT NOT NULL PRIMARY KEY,
hash TEXT NOT NULL,
salt TEXT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
-- Create table of sessions with session GUID, username, and timestamp of when
-- the session was created
CREATE TABLE sessions
(
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 984d44775bcd1331eb73e4a7812d768fd8c485bb are fine and new databases should be initialised correctly, but databases created between commit 984d44775bcd1331eb73e4a7812d768fd8c485bb 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
2023-10-30 22:41:48 +00:00
token TEXT NOT NULL PRIMARY KEY,
username TEXT NOT NULL,
expires TIMESTAMP NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
-- Create table of tracked projects with URL, name, forge, running version, and
-- timestamp of when the project was added
CREATE TABLE projects
(
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 984d44775bcd1331eb73e4a7812d768fd8c485bb are fine and new databases should be initialised correctly, but databases created between commit 984d44775bcd1331eb73e4a7812d768fd8c485bb 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
2023-10-30 22:41:48 +00:00
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
);
-- Create table of project releases with the project URL and the release tags,
-- contents, URLs, and dates
CREATE TABLE releases
(
id TEXT NOT NULL PRIMARY KEY,
project_url TEXT NOT NULL,
release_url TEXT NOT NULL,
tag TEXT NOT NULL,
content TEXT NOT NULL,
date TIMESTAMP NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
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 984d44775bcd1331eb73e4a7812d768fd8c485bb are fine and new databases should be initialised correctly, but databases created between commit 984d44775bcd1331eb73e4a7812d768fd8c485bb 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
2023-10-30 22:41:48 +00:00
);