Skip to content
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ WORKDIR /app

# Install system dependencies for psycopg2
RUN apt-get update && apt-get install -y \
gcc libpq-dev && \
gcc libpq-dev postgresql-client && \
rm -rf /var/lib/apt/lists/*

# Install Python dependencies
Expand All @@ -14,6 +14,3 @@ RUN pip install --no-cache-dir -r requirements.txt

# Copy source code
COPY . .

CMD ["python", "app.py"]

6 changes: 1 addition & 5 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: "3.9"

services:
db:
image: postgres:16
Expand All @@ -23,9 +21,7 @@ services:
DATABASE_URL: postgres://postgres:postgres@db:5432/mydb
volumes:
- .:/app
- ./src/sql:/docker-entrypoint-initb.d
command: ["python", "src/scripts/app.py"]
command: ["sh", "-c", "until pg_isready -h db -p 5432; do sleep 1; done && python src/scripts/app.py"]

volumes:
db_data:

59 changes: 59 additions & 0 deletions src/scripts/app.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,74 @@
from pathlib import Path
import os
import psycopg2

DATABASE_URL = os.getenv("DATABASE_URL", "postgres://postgres:postgres@db:5432/mydb")

# Create Table SQL strings
table_members = Path("/app/src/sql/members/table_members.sql").read_text()
table_weekly_reports = Path("/app/src/sql/weekly_reports/table_weekly_reports.sql").read_text()
table_teams = Path("/app/src/sql/teams/table_teams.sql").read_text()
table_supplies = Path("/app/src/sql/supplies/table_supplies.sql").read_text()
table_orders = Path("/app/src/sql/orders/table_orders.sql").read_text()
# Insert SQL strings
insert_member = Path("/app/src/sql/members/insert_member.sql").read_text()

# Make table, log if success or failure
def make_table(cur, table_sql, table_name):
try:
cur.execute(table_sql)
except Exception as e:
print(f"Make {table_name} table failed.", e)
else:
print(f"Make {table_name} table succeeded.")

try:
conn = psycopg2.connect(DATABASE_URL)
cur = conn.cursor()
cur.execute("SELECT version();")
print("Postgres version:", cur.fetchone())

# Drop old tables for testing purposes
cur.execute("DROP TABLE IF EXISTS members CASCADE;")
cur.execute("DROP TABLE IF EXISTS weekly_reports;")
cur.execute("DROP TABLE IF EXISTS teams;")
cur.execute("DROP TABLE IF EXISTS supplies;")
cur.execute("DROP TABLE IF EXISTS orders;")
conn.commit()

# Make SQL Tables
make_table(cur, table_members, "members")
make_table(cur, table_weekly_reports, "weekly_reports")
make_table(cur, table_teams, "teams")
make_table(cur, table_supplies, "supplies")
make_table(cur, table_orders, "orders")

# Insert a test member
try:
cur.execute(insert_member, {
"first": "Albert",
"last": "Gator",
"ufid": "12345678",
"email": "[email protected]",
"phone": "352-201-0001",
"team": "Mechanical",
"discord":"AlbertDiscord",
"github": "AlbertGithub",
"grad": "2026-05-01",
"join": "2024-09-01",
"leader": False,
})
except Exception as e:
print("Insert member failed:", e)
else:
print("Insert member succeeded.")

# Finish up
print("Connection committing and then closing.")
conn.commit()
cur.close()
conn.close()

except Exception as e:
print("Database connection failed:", e)

3 changes: 3 additions & 0 deletions src/sql/members/insert_member.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- Inserts into members table and returns the uf_id of the new member
INSERT INTO members(first_name,last_name,uf_id,uf_email,phone_number,team,discord,github,grad_date,join_date,is_leader)
VALUES (%(first)s,%(last)s,%(ufid)s,%(email)s,%(phone)s,%(team)s,%(discord)s,%(github)s,%(grad)s,%(join)s,%(leader)s)
13 changes: 13 additions & 0 deletions src/sql/members/table_members.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CREATE TABLE members (
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
uf_id CHAR(8) NOT NULL CHECK (uf_id ~ '^[0-9]{8}$') PRIMARY KEY,
uf_email VARCHAR(150) NOT NULL UNIQUE,
phone_number VARCHAR(50),
team VARCHAR(50) REFERENCES teams(name),
discord VARCHAR(150) NOT NULL UNIQUE,
github VARCHAR(150) NOT NULL UNIQUE,
grad_date DATE,
join_date DATE,
is_leader BOOLEAN DEFAULT FALSE
);
11 changes: 11 additions & 0 deletions src/sql/orders/table_orders.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE TABLE IF NOT EXISTS orders (
order_id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
item_name VARCHAR(50),
count INT,
company VARCHAR(50),
item_description VARCHAR(200),
cost_estimate INT,
purchase_link VARCHAR(200),
requester_id CHAR(8) REFERENCES members(uf_id),
leader_id CHAR(8) REFERENCES members(uf_id)
);
6 changes: 6 additions & 0 deletions src/sql/supplies/table_supplies.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE supplies (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name VARCHAR(200) NOT NULL,
amount INTEGER NOT NULL DEFAULT 0,
last_order_date DATE
);
17 changes: 0 additions & 17 deletions src/sql/table.sql

This file was deleted.

9 changes: 9 additions & 0 deletions src/sql/teams/table_teams.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE teams (
name VARCHAR(50) CHECK (name IN ('Software', 'Electrical', 'Mechanical')) PRIMARY KEY
);

INSERT INTO teams (name)
VALUES
('Software'),
('Electrical'),
('Mechanical');
4 changes: 4 additions & 0 deletions src/sql/weekly_reports/insert_weekly_report.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Inserts into members table and returns the uf_id of the new member
INSERT INTO weekly_reports(uf_id, report_date, progress_rating)
VALUES (%(uf_id)s,%(report_date)s,%(progress_rating)s)
RETURNING uf_id;
6 changes: 6 additions & 0 deletions src/sql/weekly_reports/table_weekly_reports.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE weekly_reports (
uf_id CHAR(8) REFERENCES members(uf_id),
report_date DATE NOT NULL,
progress_rating VARCHAR(50) CHECK (progress_rating IN ('Red', 'Yellow', 'Green')),
PRIMARY KEY (uf_id, report_date)
);