Starting the move to PGSQL

This commit is contained in:
2025-08-31 23:47:19 +01:00
parent 9bf5916938
commit 22b3d810cb
5 changed files with 172 additions and 51 deletions
+56 -51
View File
@@ -2,60 +2,65 @@
-- Check why sqlc doesn't correctly parse all my migrations -- Check why sqlc doesn't correctly parse all my migrations
-- This should only be a workaround :c -- This should only be a workaround :c
CREATE TABLE categories( CREATE TABLE categories (
"CategoryID" integer PRIMARY KEY AUTOINCREMENT, CategoryID SERIAL PRIMARY KEY,
"CategoryName" text NOT NULL CategoryName TEXT NOT NULL
) );
CREATE TABLE "expenseTypes"( CREATE TABLE expenseTypes (
"TypeID" integer PRIMARY KEY AUTOINCREMENT, TypeID SERIAL PRIMARY KEY,
"TypeName" text NOT NULL TypeName TEXT NOT NULL
) );
CREATE TABLE "expenses" ( CREATE TABLE stores (
"ExpID" INTEGER PRIMARY KEY AUTOINCREMENT, StoreID SERIAL PRIMARY KEY,
"Description" TEXT NOT NULL, StoreName TEXT NOT NULL
"Value" TEXT NOT NULL DEFAULT 0, );
"StoreID" int NOT NULL DEFAULT 0,
"CategoryID" int NOT NULL DEFAULT 0,
"OwnerUserID" int NOT NULL DEFAULT 0,
"TypeID" int NOT NULL DEFAULT 0,
"ExpDate" int NOT NULL DEFAULT -1,
"CreationDate" int NOT NULL DEFAULT -1,
"PaidOff" BOOL NOT NULL DEFAULT FALSE,
"SharesEven" BOOL NOT NULL DEFAULT FALSE,
FOREIGN KEY(StoreID) REFERENCES stores(StoreID) ON DELETE RESTRICT,
FOREIGN KEY(CategoryID) REFERENCES categories(CategoryID) ON DELETE RESTRICT,
FOREIGN KEY(OwnerUserID) REFERENCES users(UserID) ON DELETE RESTRICT,
FOREIGN KEY(TypeID) REFERENCES expensetypes(TypeID) ON DELETE RESTRICT
)
CREATE TABLE "expensesPayments" ( CREATE TABLE users (
"ExpPaymID" integer PRIMARY KEY AUTOINCREMENT, UserID SERIAL PRIMARY KEY,
"ExpID" int NOT NULL, UserName TEXT NOT NULL,
"UserID" int NOT NULL, UserPass TEXT NOT NULL
"Payed" TEXT NOT NULL DEFAULT 0, );
FOREIGN KEY(ExpID) REFERENCES expenses(ExpID) ON DELETE RESTRICT,
FOREIGN KEY(UserID) REFERENCES users(UserID) ON DELETE RESTRICT
)
CREATE TABLE "expensesShares" ( CREATE TABLE expenses (
"ExpShareID" integer PRIMARY KEY AUTOINCREMENT, ExpID SERIAL PRIMARY KEY,
"ExpID" int NOT NULL, Description TEXT NOT NULL,
"UserID" int NOT NULL, Value NUMERIC(12,2) NOT NULL DEFAULT 0,
"Share" TEXT NOT NULL DEFAULT 0.5, StoreID INT NOT NULL DEFAULT 0,
"Calculated" TEXT NOT NULL DEFAULT 0, CategoryID INT NOT NULL DEFAULT 0,
FOREIGN KEY(ExpID) REFERENCES expenses(ExpID) ON DELETE RESTRICT, OwnerUserID INT NOT NULL DEFAULT 0,
FOREIGN KEY(UserID) REFERENCES users(UserID) ON DELETE RESTRICT TypeID INT NOT NULL DEFAULT 0,
) ExpDate BIGINT NOT NULL DEFAULT -1,
CreationDate BIGINT NOT NULL DEFAULT -1,
PaidOff BOOLEAN DEFAULT FALSE,
SharesEven BOOLEAN DEFAULT FALSE,
FOREIGN KEY (StoreID) REFERENCES stores(StoreID) ON DELETE RESTRICT,
FOREIGN KEY (CategoryID) REFERENCES categories(CategoryID) ON DELETE RESTRICT,
FOREIGN KEY (OwnerUserID) REFERENCES users(UserID) ON DELETE RESTRICT,
FOREIGN KEY (TypeID) REFERENCES expenseTypes(TypeID) ON DELETE RESTRICT
);
CREATE TABLE stores( CREATE TABLE expensesPayments (
"StoreID" integer PRIMARY KEY AUTOINCREMENT, ExpPaymID SERIAL PRIMARY KEY,
"StoreName" text NOT NULL ExpID INT NOT NULL,
) UserID INT NOT NULL,
Payed NUMERIC(12,2) NOT NULL DEFAULT 0,
FOREIGN KEY (ExpID) REFERENCES expenses(ExpID) ON DELETE RESTRICT,
FOREIGN KEY (UserID) REFERENCES users(UserID) ON DELETE RESTRICT
);
CREATE TABLE users( CREATE TABLE expensesShares (
"UserID" integer PRIMARY KEY AUTOINCREMENT, ExpShareID SERIAL PRIMARY KEY,
"UserName" text NOT NULL, ExpID INT NOT NULL,
"UserPass" text NOT NULL UserID INT NOT NULL,
) Share NUMERIC(6,2) NOT NULL DEFAULT 0.5,
Calculated NUMERIC(12,2) NOT NULL DEFAULT 0,
FOREIGN KEY (ExpID) REFERENCES expenses(ExpID) ON DELETE RESTRICT,
FOREIGN KEY (UserID) REFERENCES users(UserID) ON DELETE RESTRICT
);
CREATE TABLE schema_migrations (
version BIGINT PRIMARY KEY,
dirty BOOLEAN
);
+32
View File
@@ -0,0 +1,32 @@
FROM dir.esilva.org/esilva/esilva-alpine:v1.0.0
# Required ENV variables for pg
ENV LANG=en_GB.utf8
# Downloads the required packages for the webserver
RUN apk --no-cache add su-exec zsh \
postgresql16 \
&& rm -rf /tmp/*
# Directory for the pg socket
RUN mkdir -p /run/postgresql && \
chown -R $UNAME:$UNAME /run/postgresql
RUN mkdir -p /var/lib/postgresql/data && \
chown -R $UNAME:$UNAME /var/lib/postgresql
COPY ./schema.sql /schema.sql
# Expose the pgsql port
EXPOSE 5431
# Copies the start script
COPY ./start.sh /start.sh
USER $UNAME
STOPSIGNAL SIGTERM
ENTRYPOINT [ "/start.sh" ]
+31
View File
@@ -0,0 +1,31 @@
service = expenses-pgdb
version = v0.0.81-alpha
image = $(service)
container = $(image)-container
.PHONY: clean attach-test test
clean:
@docker image inspect $(image) > /dev/null 2>&1 && \
docker image rm $(image) || true
build:
cp ../expenses/db/schema.sql .
docker buildx build \
-t $(image) .
attach:
docker exec -it $(container) zsh
attach-test:
docker exec -it $(container)-dev zsh
remove-dev-container:
docker container rm $(container)-dev --force > /dev/null 2>&1
test: remove-dev-container
docker run -p 5431:5432 \
--name $(container)-dev \
$$(cat .env | sed 's/^/-e /') \
-v ./pgsql-data-dev/:/var/lib/postgresql/data/ \
-it $(image)
+9
View File
@@ -0,0 +1,9 @@
# README
Requires a `.env` file with the following fields:
```
PGDATA=<path-to-dir-where-pg-data-lives>
ExpDB_USER=<user-for-expenses-db>
ExpDB_PASS=<pass-for-expenses-db>
ExpDB_NAME=<name-for-expenses-db>
```
+44
View File
@@ -0,0 +1,44 @@
#!/bin/sh
set -e
if [ -z "$(ls -A "$PGDATA")" ]; then
echo "Creating database with:"
echo "DIR: $PGDATA"
echo "USER: $ExpDB_USER"
echo "PASS: $ExpDB_PASS"
echo "NAME: $ExpDB_NAME"
# Initiate PG data directory
initdb \
--locale=C.UTF-8 \
--encoding=UTF8 \
-D "$PGDATA" \
--data-checksums \
--username="$UNAME"
# Set PG to listen on all addresses
sed -ri "s/^#(listen_addresses\s*=\s*)\S+/\1'*'/" "$PGDATA"/postgresql.conf
# Start PG to add the NC DB
pg_ctl -D "$PGDATA" \
-o "-c listen_addresses=''" \
-w start
# Create the NC database
createUser="CREATE USER $ExpDB_USER WITH PASSWORD '$ExpDB_PASS';"
createDB="CREATE DATABASE $ExpDB_NAME TEMPLATE template0 ENCODING 'UNICODE';"
alterDB="ALTER DATABASE $ExpDB_NAME OWNER TO $ExpDB_USER;"
grantPriv="GRANT ALL PRIVILEGES ON DATABASE $ExpDB_NAME TO $ExpDB_USER;"
psql -d postgres -c "${createUser}"
psql -d postgres -c "${createDB}"
psql -d postgres -c "${alterDB}"
psql -d postgres -c "${grantPriv}"
# Execute the initial schema
psql -U "$ExpDB_USER" -d "$ExpDB_NAME" -a -f /schema.sql
pg_ctl -D "$PGDATA" -m fast -w stop
{ echo; echo "host all all 0.0.0.0/0 md5"; } >> "$PGDATA"/pg_hba.conf
fi
postgres "$@"