correct the database creation script for the expected fields.

This commit is contained in:
Brian O'Reilly 2025-12-07 17:22:14 -05:00 committed by Brian O'Reilly
parent c9f6cb2aa7
commit 4ca6570d5e
2 changed files with 40 additions and 38 deletions

View File

@ -42,7 +42,7 @@ services:
- "5432:5432"
volumes:
- postgres-data:/var/lib/postgresql/data
# - ./init-db.sql:/docker-entrypoint-initdb.d/init-db.sql:ro
- ./init-db.sql:/docker-entrypoint-initdb.d/init-db.sql:ro
restart: unless-stopped
networks:
- asteroid-network

View File

@ -5,34 +5,36 @@
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- Users table
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
CREATE TABLE IF NOT EXISTS "USERS" (
_id SERIAL PRIMARY KEY,
username VARCHAR(255) UNIQUE NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
password_hash TEXT NOT NULL,
"password-hash" TEXT NOT NULL,
role VARCHAR(50) DEFAULT 'listener',
active BOOLEAN DEFAULT true,
created_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
last_login TIMESTAMP,
active integer DEFAULT 1,
-- "created-date" integer DEFAULT CURRENT_TIMESTAMP,
"created-date" integer,
"last-login" integer,
CONSTRAINT valid_role CHECK (role IN ('listener', 'dj', 'admin'))
);
-- Create index on username and email for faster lookups
CREATE INDEX idx_users_username ON users(username);
CREATE INDEX idx_users_email ON users(email);
CREATE INDEX idx_users_username ON "USERS"(username);
CREATE INDEX idx_users_email ON "USERS"(email);
-- Tracks table
CREATE TABLE IF NOT EXISTS tracks (
id SERIAL PRIMARY KEY,
_id SERIAL PRIMARY KEY,
title VARCHAR(500) NOT NULL,
artist VARCHAR(500),
album VARCHAR(500),
duration INTEGER DEFAULT 0,
format VARCHAR(50),
file_path TEXT NOT NULL UNIQUE,
play_count INTEGER DEFAULT 0,
added_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
last_played TIMESTAMP
bitrate integer,
"file-path" TEXT NOT NULL UNIQUE,
"play-count" INTEGER DEFAULT 0,
"added-date" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
"last-played" TIMESTAMP
);
-- Create indexes for common queries
@ -42,25 +44,25 @@ CREATE INDEX idx_tracks_title ON tracks(title);
-- Playlists table
CREATE TABLE IF NOT EXISTS playlists (
id SERIAL PRIMARY KEY,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
_id SERIAL PRIMARY KEY,
"user-id" INTEGER NOT NULL REFERENCES "USERS"(_id) ON DELETE CASCADE,
name VARCHAR(255) NOT NULL,
description TEXT,
created_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
modified_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
"created-date" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
"modified-date" TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Create index on user_id for faster user playlist lookups
CREATE INDEX idx_playlists_user_id ON playlists(user_id);
CREATE INDEX idx_playlists_user_id ON playlists("user-id");
-- Playlist tracks junction table (many-to-many relationship)
CREATE TABLE IF NOT EXISTS playlist_tracks (
id SERIAL PRIMARY KEY,
playlist_id INTEGER NOT NULL REFERENCES playlists(id) ON DELETE CASCADE,
track_id INTEGER NOT NULL REFERENCES tracks(id) ON DELETE CASCADE,
_id SERIAL PRIMARY KEY,
playlist_id INTEGER NOT NULL REFERENCES playlists(_id) ON DELETE CASCADE,
track_id INTEGER NOT NULL REFERENCES tracks(_id) ON DELETE CASCADE,
position INTEGER NOT NULL,
added_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UNIQUE(playlist_id, track_id, position)
UNIQUE(playlist_id, track_id, "position")
);
-- Create indexes for playlist track queries
@ -68,28 +70,28 @@ CREATE INDEX idx_playlist_tracks_playlist_id ON playlist_tracks(playlist_id);
CREATE INDEX idx_playlist_tracks_track_id ON playlist_tracks(track_id);
-- Sessions table (for Radiance session management)
CREATE TABLE IF NOT EXISTS sessions (
id VARCHAR(255) PRIMARY KEY,
user_id INTEGER REFERENCES users(id) ON DELETE CASCADE,
data JSONB,
created_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
expires_at TIMESTAMP NOT NULL
);
-- CREATE TABLE IF NOT EXISTS sessions (
-- _id VARCHAR(255) PRIMARY KEY,
-- "user_id INTEGER REFERENCES users(id) ON DELETE CASCADE,
-- data JSONB,
-- created_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
-- expires_at TIMESTAMP NOT NULL
-- );
-- Create index on user_id and expires_at
CREATE INDEX idx_sessions_user_id ON sessions(user_id);
CREATE INDEX idx_sessions_expires_at ON sessions(expires_at);
-- CREATE INDEX idx_sessions_user_id ON sessions(user_id);
-- CREATE INDEX idx_sessions_expires_at ON sessions(expires_at);
-- Create default admin user (password: admin - CHANGE THIS!)
-- Password hash for 'admin' using bcrypt
-- INSERT INTO users (username, email, password_hash, role, active)
-- VALUES ('admin', 'admin@asteroid.radio', '$2a$12$LQv3c1yqBWVHxkd0LHAkCOYz6TtxMQJqhN8/LewY5GyYqYqYqYqYq', 'admin', true)
-- ON CONFLICT (username) DO NOTHING;
INSERT INTO "USERS" (username, email, "password-hash", role, active)
-- VALUES ('admin', 'admin@asteroid.radio', '$2a$12$LQv3c1yqBWVHxkd0LHAkCOYz6TtxMQJqhN8/LewY5GyYqYqYqYqYq', 'admin', 1)
VALUES ('admin', 'admin@asteroid.radio','8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918', 'admin', 1)
ON CONFLICT (username) DO NOTHING;
-- Create a test listener user
-- INSERT INTO users (username, email, password_hash, role, active)
-- VALUES ('listener', 'listener@asteroid.radio', '$2a$12$LQv3c1yqBWVHxkd0LHAkCOYz6TtxMQJqhN8/LewY5GyYqYqYqYqYq', 'listener', true)
-- ON CONFLICT (username) DO NOTHING;
INSERT INTO "USERS" (username, email, "password-hash", role, active)
VALUES ('listener', 'listener@asteroid.radio', '$2a$12$LQv3c1yqBWVHxkd0LHAkCOYz6TtxMQJqhN8/LewY5GyYqYqYqYqYq', 'listener', 1);
-- Grant necessary permissions
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO asteroid;