completely fresh database ✨
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
const { Client } = require("pg");
|
||||
const crypto = require("crypto");
|
||||
const { callback } = require("pg/lib/native/query");
|
||||
require("dotenv").config();
|
||||
const client = new Client({
|
||||
user: process.env.PG_USER,
|
||||
@@ -13,8 +14,14 @@ client
|
||||
.connect()
|
||||
.then(() => {
|
||||
createTables()
|
||||
.then(() => console.log("Tables created successfully"))
|
||||
.catch((e) => console.error("Failed to create tables ", e));
|
||||
.then(() => {
|
||||
client.end();
|
||||
console.log("Tables created successfully");
|
||||
})
|
||||
.catch((e) => {
|
||||
client.end();
|
||||
console.error("Failed to create tables ", e);
|
||||
});
|
||||
console.log(
|
||||
`Successfully connected to database: ${process.env.PG_DATABASE}`,
|
||||
);
|
||||
@@ -28,47 +35,85 @@ client
|
||||
// Creating database tables
|
||||
async function createTables() {
|
||||
try {
|
||||
// Create Accounts Table
|
||||
await client.query(`
|
||||
CREATE TABLE IF NOT EXISTS accounts (
|
||||
username VARCHAR(20) NOT NULL UNIQUE,
|
||||
password VARCHAR(128) NOT NULL,
|
||||
user_id VARCHAR(128) PRIMARY KEY,
|
||||
created_at TIMESTAMPTZ DEFAULT NOW()
|
||||
CREATE TABLE IF NOT EXISTS Accounts (
|
||||
user_id SERIAL PRIMARY KEY,
|
||||
username VARCHAR(255) UNIQUE NOT NULL,
|
||||
email VARCHAR(255) UNIQUE NOT NULL,
|
||||
password_hash VARCHAR(255) NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_username ON Accounts (username);
|
||||
CREATE INDEX IF NOT EXISTS idx_email ON Accounts (email);
|
||||
`);
|
||||
} catch (e) {
|
||||
console.error("Failed to create accounts table ", e);
|
||||
console.error("Failed to create Accounts table: ", e);
|
||||
}
|
||||
|
||||
try {
|
||||
// Create Conversations Table
|
||||
await client.query(`
|
||||
CREATE TABLE IF NOT EXISTS messages (
|
||||
sender VARCHAR(128) NOT NULL,
|
||||
recipient VARCHAR(128) NOT NULL,
|
||||
message TEXT NOT NULL,
|
||||
timestamp TIMESTAMPTZ DEFAULT NOW(),
|
||||
message_id SERIAL PRIMARY KEY,
|
||||
attachment_url TEXT,
|
||||
UNIQUE (sender, recipient, message_id)
|
||||
CREATE TABLE IF NOT EXISTS Conversations (
|
||||
conversation_id SERIAL PRIMARY KEY,
|
||||
conversation_type VARCHAR(10) NOT NULL CHECK (conversation_type IN ('direct', 'group')),
|
||||
name VARCHAR(255),
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_messages_conversation
|
||||
ON messages (sender, recipient, message_id ASC);
|
||||
CREATE INDEX IF NOT EXISTS idx_conversation_type ON Conversations (conversation_type);
|
||||
`);
|
||||
} catch (e) {
|
||||
console.error("Failed to create messages table ", e);
|
||||
console.error("Failed to create Conversations table: ", e);
|
||||
}
|
||||
|
||||
try {
|
||||
// Create Messages Table
|
||||
await client.query(`
|
||||
CREATE TABLE IF NOT EXISTS contacts (
|
||||
username VARCHAR(20) NOT NULL,
|
||||
usernameContact VARCHAR(20) NOT NULL,
|
||||
CREATE TABLE IF NOT EXISTS Messages (
|
||||
message_id SERIAL PRIMARY KEY,
|
||||
conversation_id INT REFERENCES Conversations(conversation_id) ON DELETE CASCADE,
|
||||
sender_id INT REFERENCES Accounts(user_id) ON DELETE CASCADE,
|
||||
content TEXT NOT NULL,
|
||||
sent_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_messages_conversation_id ON Messages (conversation_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_messages_sender_id ON Messages (sender_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_messages_conversation_sent_at ON Messages (conversation_id, sent_at);
|
||||
`);
|
||||
} catch (e) {
|
||||
console.error("Failed to create Messages table: ", e);
|
||||
}
|
||||
|
||||
try {
|
||||
// Create Memberships Table
|
||||
await client.query(`
|
||||
CREATE TABLE IF NOT EXISTS Memberships (
|
||||
conversation_id INT REFERENCES Conversations(conversation_id) ON DELETE CASCADE,
|
||||
user_id INT REFERENCES Accounts(user_id) ON DELETE CASCADE,
|
||||
joined_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (conversation_id, user_id)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_memberships_conversation_id ON Memberships (conversation_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_memberships_user_id ON Memberships (user_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_memberships_conversation_joined_at ON Memberships (conversation_id, joined_at);
|
||||
`);
|
||||
} catch (e) {
|
||||
console.error("Failed to create Memberships table: ", e);
|
||||
}
|
||||
|
||||
try {
|
||||
// Create Contacts Table
|
||||
await client.query(`
|
||||
CREATE TABLE IF NOT EXISTS Contacts (
|
||||
username VARCHAR(255) NOT NULL,
|
||||
usernameContact VARCHAR(255) NOT NULL,
|
||||
read BOOLEAN NOT NULL,
|
||||
lastActive VARCHAR(100) NOT NULL,
|
||||
lastActive TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
CONSTRAINT unique_username_contact UNIQUE (username, usernameContact)
|
||||
);
|
||||
`);
|
||||
} catch (e) {
|
||||
console.error("Failed to create messages table ", e);
|
||||
console.error("Failed to create Contacts table: ", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user