adjust schema slightly
This commit is contained in:
@@ -2,6 +2,7 @@ package com.ddf.vodsystem.entities;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Data;
|
||||
import org.hibernate.annotations.CreationTimestamp;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
@@ -11,19 +12,20 @@ import java.time.Instant;
|
||||
public class RefreshToken {
|
||||
@Id
|
||||
@Column(name = "id", nullable = false, unique = true)
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "user_id", nullable = false)
|
||||
private User user;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "family_id", nullable = false)
|
||||
private TokenFamily tokenFamily;
|
||||
|
||||
@Column(name = "token_hash", unique = true, nullable = false)
|
||||
@Column(name = "token_hash", unique = true, nullable = false, length = 32)
|
||||
private byte[] tokenHash;
|
||||
|
||||
@Column(name = "created_at", nullable = false)
|
||||
@CreationTimestamp
|
||||
private Instant createdAt;
|
||||
|
||||
@Column(name = "expires_at", nullable = false)
|
||||
private Instant expiresAt;
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.ddf.vodsystem.entities;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Data;
|
||||
import org.hibernate.annotations.CreationTimestamp;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
@@ -11,12 +12,17 @@ import java.time.Instant;
|
||||
public class TokenFamily {
|
||||
@Id
|
||||
@Column(name = "id", nullable = false, unique = true)
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "user_id", nullable = false)
|
||||
private User user;
|
||||
|
||||
@Column(name = "created_at", nullable = false)
|
||||
@CreationTimestamp
|
||||
private Instant createdAt;
|
||||
|
||||
@Column(name = "expires_at", nullable = false)
|
||||
private Instant expiresAt;
|
||||
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
CREATE TABLE IF NOT EXISTS token_families
|
||||
(
|
||||
id BIGSERIAL PRIMARY KEY NOT NULl,
|
||||
user_id BIGSERIAL NOT NULL,
|
||||
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
user_id BIGINT NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
expires_at TIMESTAMPTZ NOT NULL,
|
||||
revoked_at TIMESTAMPTZ,
|
||||
|
||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
||||
revoked_at TIMESTAMPTZ
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_token_families_user_id
|
||||
@@ -13,18 +12,15 @@ CREATE INDEX IF NOT EXISTS idx_token_families_user_id
|
||||
|
||||
CREATE TABLE IF NOT EXISTS refresh_tokens
|
||||
(
|
||||
id BIGSERIAL PRIMARY KEY NOT NULL,
|
||||
user_id BIGSERIAL NOT NULL,
|
||||
family_id BIGSERIAL NOT NULL,
|
||||
token_hash BYTEA NOT NULL,
|
||||
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
family_id BIGINT NOT NULL REFERENCES token_families (id) ON DELETE CASCADE,
|
||||
token_hash BYTEA NOT NULL UNIQUE, -- unique automatically creates a B-tree index
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
expires_at TIMESTAMPTZ NOT NULL,
|
||||
used_at TIMESTAMPTZ,
|
||||
revoked_at TIMESTAMPTZ,
|
||||
|
||||
CONSTRAINT refresh_tokens_hash_len CHECK (octet_length(token_hash) = 32),
|
||||
|
||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (family_id) REFERENCES token_families(id) ON DELETE CASCADE
|
||||
CONSTRAINT refresh_tokens_hash_len CHECK (octet_length(token_hash) = 32)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_refresh_tokens_family_id
|
||||
|
||||
Reference in New Issue
Block a user