Skip to content

Commit

Permalink
Convert to PostgreSQL
Browse files Browse the repository at this point in the history
  • Loading branch information
Astralchroma committed May 18, 2024
1 parent 0c21bb7 commit 8ccac42
Show file tree
Hide file tree
Showing 16 changed files with 217 additions and 224 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DATABASE_URL=sqlite://.sqlx-check.db?mode=rwc
DATABASE_URL=postgres://postgres@[::]:5432/postgres
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.sql linguist-detectable
api_documentation.md linguist-detectable
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/.idea
/target

/.sqlx-check.db
/Cargo.lock
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ axum = { version = "0.7", default-features = false, features = ["http1", "json",
[dependencies.sqlx]
version = "0.7"
default-features = false
features = ["chrono", "macros", "migrate", "runtime-tokio", "sqlite", "tls-none", "uuid"]
features = ["chrono", "macros", "migrate", "postgres", "runtime-tokio", "tls-none", "uuid"]
90 changes: 90 additions & 0 deletions migrations/1_Initial.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
CREATE TABLE players (
uuid UUID
PRIMARY KEY,
username VARCHAR(16)
NOT NULL
UNIQUE,

registered TIMESTAMP
NOT NULL
DEFAULT 'now',
last_online TIMESTAMP
NOT NULL
DEFAULT 'now',

show_registered BOOLEAN
NOT NULL
DEFAULT true,
show_status BOOLEAN
NOT NULL
DEFAULT true,
retain_usernames BOOLEAN
NOT NULL
DEFAULT true
);

CREATE TABLE previous_usernames (
player UUID
NOT NULL,
username VARCHAR(16)
NOT NULL,
public BOOLEAN
NOT NULL
DEFAULT TRUE,

FOREIGN KEY (player) REFERENCES players(uuid) ON DELETE CASCADE
);

CREATE TABLE tokens (
token BYTEA
PRIMARY KEY,
player UUID,

created TIMESTAMP
NOT NULL
CHECK (used >= created)
DEFAULT 'now',
used TIMESTAMP
NOT NULL
CHECK (used >= created)
DEFAULT 'now',

revoked BOOLEAN
NOT NULL
DEFAULT false,
expired BOOLEAN
NOT NULL
GENERATED ALWAYS AS (used - created > '1 day') STORED,
valid BOOLEAN
NOT NULL
GENERATED ALWAYS AS (player IS NOT NULL AND NOT revoked AND NOT used - created > '1 day') STORED,

FOREIGN KEY (player) REFERENCES players(uuid) ON DELETE SET NULL
);

CREATE TABLE channels (
id BIGINT
PRIMARY KEY,
name VARCHAR(32)
NOT NULL
UNIQUE,
owner UUID
NOT NULL,

created TIMESTAMP
NOT NULL
DEFAULT 'now',
last_updated TIMESTAMP
NOT NULL
DEFAULT 'now',
last_message TIMESTAMP
NOT NULL
DEFAULT 'now',

persistence SMALLINT
NOT NULL,
persistence_count INT,
persistence_duration_seconds INT,

FOREIGN KEY (owner) REFERENCES players(uuid) ON DELETE CASCADE
);
60 changes: 0 additions & 60 deletions migrations/1_Users_and_Authentication.sql

This file was deleted.

3 changes: 0 additions & 3 deletions migrations/2_Configure_Username_Retention.sql

This file was deleted.

1 change: 0 additions & 1 deletion migrations/3_Rename_show_to_public.sql

This file was deleted.

26 changes: 0 additions & 26 deletions migrations/4_Channels.sql

This file was deleted.

2 changes: 0 additions & 2 deletions migrations/5_Rename_status_columns.sql

This file was deleted.

106 changes: 53 additions & 53 deletions schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,95 +2,95 @@
-- So here is a combination of those migrations for convenience, this should be kept up to date.
-- This should not be actually used for a database, this is just a programmer reference.

-- Currently in line with: `5_Rename_status_columns.sql`
-- Currently in line with: `migrations/1_Initial.sql`

CREATE TABLE players (
uuid UUID
PRIMARY KEY,
username VARCHAR(16)
NOT NULL
UNIQUE,

registered TIMESTAMP
NOT NULL
DEFAULT 'now',
last_online TIMESTAMP
NOT NULL
DEFAULT 'now',

CREATE TABLE users (
uuid BLOB
PRIMARY KEY
NOT NULL
UNIQUE,
username VARCHAR(16) COLLATE NOCASE
NOT NULL
UNIQUE,
registered DATETIME
NOT NULL
DEFAULT CURRENT_TIMESTAMP,
show_registered BOOLEAN
NOT NULL
DEFAULT TRUE,
last_online DATETIME
NOT NULL
DEFAULT CURRENT_TIMESTAMP,
DEFAULT true,
show_status BOOLEAN
NOT NULL
DEFAULT TRUE,
DEFAULT true,
retain_usernames BOOLEAN
NOT NULL
DEFAULT TRUE
DEFAULT true
);

CREATE TABLE old_usernames (
user BLOB
CREATE TABLE previous_usernames (
player UUID
NOT NULL,
username VARCHAR(16) COLLATE NOCASE
username VARCHAR(16)
NOT NULL,
public BOOLEAN
NOT NULL
DEFAULT TRUE,

FOREIGN KEY (user) REFERENCES users(uuid) ON DELETE CASCADE
FOREIGN KEY (player) REFERENCES players(uuid) ON DELETE CASCADE
);

CREATE TABLE tokens (
token BLOB
PRIMARY KEY
NOT NULL
UNIQUE,
created DATETIME
token BYTEA
PRIMARY KEY,
player UUID,

created TIMESTAMP
NOT NULL
CHECK (used >= created)
DEFAULT CURRENT_TIMESTAMP,
used DATETIME
DEFAULT 'now',
used TIMESTAMP
NOT NULL
CHECK (used >= created)
DEFAULT CURRENT_TIMESTAMP,
DEFAULT 'now',

revoked BOOLEAN
NOT NULL
DEFAULT FALSE,
DEFAULT false,
expired BOOLEAN
NOT NULL
GENERATED ALWAYS AS (unixepoch(used) - unixepoch(created) > 60 * 60 * 24),
GENERATED ALWAYS AS (used - created > '1 day') STORED,
valid BOOLEAN
NOT NULL
GENERATED ALWAYS AS (user IS NOT NULL AND NOT revoked AND NOT expired),
user BLOB,
GENERATED ALWAYS AS (player IS NOT NULL AND NOT revoked AND NOT used - created > '1 day') STORED,

FOREIGN KEY (user) REFERENCES users(uuid) ON DELETE SET NULL
FOREIGN KEY (player) REFERENCES players(uuid) ON DELETE SET NULL
);

CREATE TABLE channels (
id UNSIGNED BIGINT
PRIMARY KEY
NOT NULL
UNIQUE,
name VARCHAR(32) COLLATE NOCASE
id BIGINT
PRIMARY KEY,
name VARCHAR(32)
NOT NULL
UNIQUE,
owner BLOB
owner UUID
NOT NULL,
created DATETIME

created TIMESTAMP
NOT NULL
DEFAULT 'now',
last_updated TIMESTAMP
NOT NULL
DEFAULT 'now',
last_message TIMESTAMP
NOT NULL
DEFAULT CURRENT_TIMESTAMP,
last_updated DATETIME
NOT NULL
DEFAULT CURRENT_TIMESTAMP,
last_active DATETIME
NOT NULL
DEFAULT CURRENT_TIMESTAMP,
persistence UNSIGNED TINYINT
DEFAULT 'now',

persistence SMALLINT
NOT NULL,
persistence_count UNSIGNED INT,
persistence_duration_seconds UNSIGNED INT,
persistence_count INT,
persistence_duration_seconds INT,

FOREIGN KEY (owner) REFERENCES users (uuid) ON DELETE CASCADE
FOREIGN KEY (owner) REFERENCES players(uuid) ON DELETE CASCADE
);
Loading

0 comments on commit 8ccac42

Please sign in to comment.