交付双端基础、实时联系人和隔离浏览器
This commit is contained in:
1 parent
43687d5e7e
commit
67fef3ff87
59 files changed
+5094
-1
No files matched your search
@@ -0,0 +1,27 @@
|
||||
CREATE TABLE users (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
username text UNIQUE NOT NULL CHECK (username ~ '^[a-z0-9_]{3,32}$'),
|
||||
password_hash text NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE TABLE devices (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
public_key bytea NOT NULL CHECK (octet_length(public_key) = 32),
|
||||
name text NOT NULL,
|
||||
platform text NOT NULL CHECK (platform IN ('web', 'windows')),
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
last_active_at timestamptz NOT NULL DEFAULT now(),
|
||||
revoked_at timestamptz,
|
||||
UNIQUE (user_id, public_key)
|
||||
);
|
||||
|
||||
CREATE TABLE sessions (
|
||||
token_hash bytea PRIMARY KEY CHECK (octet_length(token_hash) = 32),
|
||||
device_id uuid NOT NULL REFERENCES devices(id) ON DELETE CASCADE,
|
||||
expires_at timestamptz NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX sessions_device ON sessions(device_id);
|
||||
CREATE INDEX sessions_expiry ON sessions(expires_at);
|
||||
@@ -0,0 +1,28 @@
|
||||
CREATE TABLE contact_relationships (
|
||||
first_user uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
second_user uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
requested_by uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
status text NOT NULL CHECK (status IN ('pending','accepted')),
|
||||
updated_at timestamptz NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY (first_user, second_user),
|
||||
CHECK (first_user < second_user),
|
||||
CHECK (requested_by IN (first_user, second_user))
|
||||
);
|
||||
|
||||
CREATE INDEX contact_relationships_second_user ON contact_relationships(second_user);
|
||||
|
||||
CREATE TABLE contact_blocks (
|
||||
owner_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
blocked_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
PRIMARY KEY (owner_id, blocked_id),
|
||||
CHECK (owner_id <> blocked_id)
|
||||
);
|
||||
|
||||
CREATE TABLE contact_preferences (
|
||||
owner_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
contact_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
alias text NOT NULL,
|
||||
category text NOT NULL,
|
||||
PRIMARY KEY (owner_id, contact_id),
|
||||
CHECK (owner_id <> contact_id)
|
||||
);
|
||||
@@ -0,0 +1,45 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
_ "embed"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
//go:embed 001_identity.sql
|
||||
var identityMigration string
|
||||
|
||||
//go:embed 002_contacts.sql
|
||||
var contactsMigration string
|
||||
|
||||
func Migrate(ctx context.Context, pool *pgxpool.Pool) error {
|
||||
tx, err := pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
if _, err = tx.Exec(ctx, "SELECT pg_advisory_xact_lock(749109272)"); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, "CREATE TABLE IF NOT EXISTS schema_migrations (version integer PRIMARY KEY)"); err != nil {
|
||||
return err
|
||||
}
|
||||
for index, migration := range []string{identityMigration, contactsMigration} {
|
||||
version := index + 1
|
||||
var applied bool
|
||||
if err = tx.QueryRow(ctx, "SELECT EXISTS (SELECT 1 FROM schema_migrations WHERE version=$1)", version).Scan(&applied); err != nil {
|
||||
return err
|
||||
}
|
||||
if applied {
|
||||
continue
|
||||
}
|
||||
if _, err = tx.Exec(ctx, migration); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, "INSERT INTO schema_migrations VALUES ($1)", version); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
Reference in new issue
Block a user