Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
70 changes: 70 additions & 0 deletions .github/workflows/backend.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: Backend CI

on:
push:
branches:
- main
paths:
- 'sticker-business/**'
- 'backend/init_tables.sql'
- '.github/workflows/backend.yml'
pull_request:
paths:
- 'sticker-business/**'
- 'backend/init_tables.sql'
- '.github/workflows/backend.yml'

permissions:
contents: read

jobs:
backend:
name: Backend tests with MySQL schema
runs-on: ubuntu-latest
services:
mysql:
image: mysql:8.4
ports:
- 3306:3306
env:
MYSQL_ROOT_PASSWORD: ci-root-password
MYSQL_DATABASE: sticker_business
options: >-
--health-cmd="mysqladmin ping --host=127.0.0.1 --user=root --password=ci-root-password"
--health-interval=10s
--health-timeout=5s
--health-retries=10
--health-start-period=30s
steps:
- name: Check out repository
uses: actions/checkout@v4

- name: Set up Java
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: 25
cache: maven

- name: Install MySQL client
run: |
sudo apt-get update
sudo apt-get install --no-install-recommends -y mysql-client

- name: Initialize MySQL schema
env:
MYSQL_PWD: ci-root-password
run: mysql --host=127.0.0.1 --user=root --database=sticker_business --batch --show-warnings < backend/init_tables.sql

- name: Build and test against MySQL
env:
SPRING_DATASOURCE_URL: jdbc:mysql://127.0.0.1:3306/sticker_business
SPRING_DATASOURCE_USERNAME: root
SPRING_DATASOURCE_PASSWORD: ci-root-password
SPRING_JPA_HIBERNATE_DDL_AUTO: validate
HIBERNATE_TEST_JDBC_URL: jdbc:mysql://127.0.0.1:3306/sticker_business
HIBERNATE_TEST_JDBC_USERNAME: root
HIBERNATE_TEST_JDBC_PASSWORD: ci-root-password
HIBERNATE_TEST_JDBC_DRIVER: com.mysql.cj.jdbc.Driver
run: ./mvnw -B clean verify
working-directory: sticker-business
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ docker compose up --build
- `--build` rebuilds the images so they include your latest code. Docker compiles the backend for you inside a container.
- The **first** build takes several minutes while Docker downloads Java, Node.js, MySQL and all libraries. Later builds are much faster because Docker caches (reuses) those downloads.
- Wait until the logs stop scrolling. The database needs up to ~30 seconds to become healthy before the backend starts.
- On the database's first startup, Docker runs `backend/init_tables.sql` to create the tables. The script does not rerun for an existing database volume.
- Open http://localhost:8080. You should see **Hello Docker World**.
- Open http://localhost:5173. You should see the React starter page.

Expand All @@ -93,10 +94,14 @@ docker compose up --build
**Useful backend commands** (run inside `sticker-business/`):
| Git Bash / macOS / Linux | PowerShell | What it does |
|---|---|---|
| `./mvnw test` | `.\mvnw.cmd test` | Runs the tests. They use a temporary in-memory database (H2), so Docker is not needed. |
| `./mvnw test` | `.\mvnw.cmd test` | Runs the tests against H2 by default; Docker is not needed. Set `SPRING_DATASOURCE_URL`, `SPRING_DATASOURCE_USERNAME`, and `SPRING_DATASOURCE_PASSWORD` to run them against MySQL instead. |
| `./mvnw -Dtest=StickerBusinessApplicationTests#contextLoads test` | `.\mvnw.cmd -Dtest=StickerBusinessApplicationTests#contextLoads test` | Runs only the Spring context test. |
| `./mvnw -Dtest=StickerBusinessApplicationTests#dataSchemaValidates test` | `.\mvnw.cmd -Dtest=StickerBusinessApplicationTests#dataSchemaValidates test` | Runs only the standalone Hibernate schema-validation test. |
| `./mvnw spotless:apply` | `.\mvnw.cmd spotless:apply` | Auto-formats your Java code to the team style. Run it before committing. |
| `./mvnw clean verify` | `.\mvnw.cmd clean verify` | Full check: compiles, runs the tests and the format check. Look for `BUILD SUCCESS`. |

The standalone schema-validation test uses H2 by default. To run both tests against MySQL, start the database with `docker compose up -d database`, then set `SPRING_DATASOURCE_URL`, `SPRING_DATASOURCE_USERNAME`, and `SPRING_DATASOURCE_PASSWORD` for Spring, and set matching `HIBERNATE_TEST_JDBC_URL`, `HIBERNATE_TEST_JDBC_USERNAME`, `HIBERNATE_TEST_JDBC_PASSWORD`, and `HIBERNATE_TEST_JDBC_DRIVER=com.mysql.cj.jdbc.Driver` for the standalone Hibernate factory. Also set `SPRING_JPA_HIBERNATE_DDL_AUTO=validate`. The database initializes from `backend/init_tables.sql` only when its volume is first created.

**Useful frontend commands** (run inside `frontend/`, needs Node.js):
| Command | What it does |
|---|---|
Expand Down
194 changes: 194 additions & 0 deletions backend/init_tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
-- defaults to innoDB in case future changes to mySQL
CREATE TABLE IF NOT EXISTS `account` (
`id` INT NOT NULL AUTO_INCREMENT,
`username` VARCHAR(255),
`password_hash` VARCHAR(255),
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `permission_type` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255),
PRIMARY KEY (`id`),
UNIQUE KEY `UK_PermissionType_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `permission` (
`id` INT NOT NULL AUTO_INCREMENT,
`permission_type_id` INT,
PRIMARY KEY (`id`),
KEY `IDX_Permission_permissionTypeId` (`permission_type_id`),
CONSTRAINT `FK_Permission_permissionType`
FOREIGN KEY (`permission_type_id`) REFERENCES `permission_type` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `user_permission` (
`user_id` INT NOT NULL,
`permission_id` INT NOT NULL,
PRIMARY KEY (`user_id`, `permission_id`),
KEY `IDX_UserPermission_permissionId` (`permission_id`),
CONSTRAINT `FK_UserPermission_user`
FOREIGN KEY (`user_id`) REFERENCES `account` (`id`),
CONSTRAINT `FK_UserPermission_permission`
FOREIGN KEY (`permission_id`) REFERENCES `permission` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `tag` (
`id` INT NOT NULL AUTO_INCREMENT,
`label` VARCHAR(255),
PRIMARY KEY (`id`),
UNIQUE KEY `UK_Tag_label` (`label`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `sticker` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255),
`length` DOUBLE NOT NULL,
`width` DOUBLE NOT NULL,
`desc` VARCHAR(255),
`svg_path` VARCHAR(255),
`active` BIT(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `sticker_sheets` (
`id` INT NOT NULL AUTO_INCREMENT,
`file_path` VARCHAR(255),
`sticker_idsjson` VARCHAR(255),
`sticker_ids` VARCHAR(255),
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `placed_order` (
`id` INT NOT NULL AUTO_INCREMENT,
`customer_id` INT,
`placement_timestamp` DATETIME(6) NOT NULL,
`status` ENUM('ACCEPTED','CANCELLED','CUT','DENIED','IN_PROGRESS','PRINTED','SHIPPED','SUBMITTED'),
`addressjson` VARCHAR(255),
`note` VARCHAR(255),
`address` VARCHAR(255),
PRIMARY KEY (`id`),
KEY `IDX_PlacedOrder_customerId` (`customer_id`),
CONSTRAINT `FK_PlacedOrder_customer`
FOREIGN KEY (`customer_id`) REFERENCES `account` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `order_items` (
`id` INT NOT NULL AUTO_INCREMENT,
`order_id` INT,
`sheet_id` INT,
`quantity` INT NOT NULL,
PRIMARY KEY (`id`),
KEY `IDX_OrderItems_orderId` (`order_id`),
KEY `IDX_OrderItems_sheetId` (`sheet_id`),
CONSTRAINT `FK_OrderItems_order`
FOREIGN KEY (`order_id`) REFERENCES `placed_order` (`id`),
CONSTRAINT `ORDER_ITEMS_STICKER_SHEET_ID_FK`
FOREIGN KEY (`sheet_id`) REFERENCES `sticker_sheets` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `order_items_status` (
`id` INT NOT NULL AUTO_INCREMENT,
`order_items_id` INT,
`complete` BIT(1) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UK_OrderItemsStatus_orderItemsId` (`order_items_id`),
CONSTRAINT `ORDER_ITEMS_STATUS_ORDER_ITEMS_ID_FK`
FOREIGN KEY (`order_items_id`) REFERENCES `order_items` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `order_updates` (
`id` INT NOT NULL AUTO_INCREMENT,
`order_id` INT,
`timestamp` DATETIME(6),
`order_type` TINYINT,
PRIMARY KEY (`id`),
KEY `IDX_OrderUpdates_orderId` (`order_id`),
CONSTRAINT `FK_OrderUpdates_order`
FOREIGN KEY (`order_id`) REFERENCES `placed_order` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `collection` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255),
PRIMARY KEY (`id`),
UNIQUE KEY `UK_Collection_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `collection_items` (
`id` INT NOT NULL AUTO_INCREMENT,
`sticker_id` INT,
`collection_id` INT,
PRIMARY KEY (`id`),
KEY `IDX_CollectionItems_stickerId` (`sticker_id`),
KEY `IDX_CollectionItems_collectionId` (`collection_id`),
CONSTRAINT `COLLECTION_ITEMS_STICKER_ID_FK`
FOREIGN KEY (`sticker_id`) REFERENCES `sticker` (`id`),
CONSTRAINT `COLLECTION_ITEMS_COLLECTION_ID_FK`
FOREIGN KEY (`collection_id`) REFERENCES `collection` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `tag_items` (
`id` INT NOT NULL AUTO_INCREMENT,
`tag_id` INT,
`sticker_id` INT,
PRIMARY KEY (`id`),
KEY `IDX_TagItems_tagId` (`tag_id`),
KEY `IDX_TagItems_stickerId` (`sticker_id`),
CONSTRAINT `TAG_ITEMS_TAG_ID_FK`
FOREIGN KEY (`tag_id`) REFERENCES `tag` (`id`),
CONSTRAINT `TAG_ITEMS_STICKER_ID_FK`
FOREIGN KEY (`sticker_id`) REFERENCES `sticker` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `account_seq` (`next_val` BIGINT);
INSERT INTO `account_seq` (`next_val`) SELECT 1
WHERE NOT EXISTS (SELECT 1 FROM `account_seq`);

CREATE TABLE IF NOT EXISTS `permission_seq` (`next_val` BIGINT);
INSERT INTO `permission_seq` (`next_val`) SELECT 1
WHERE NOT EXISTS (SELECT 1 FROM `permission_seq`);

CREATE TABLE IF NOT EXISTS `permission_type_seq` (`next_val` BIGINT);
INSERT INTO `permission_type_seq` (`next_val`) SELECT 1
WHERE NOT EXISTS (SELECT 1 FROM `permission_type_seq`);

CREATE TABLE IF NOT EXISTS `tag_seq` (`next_val` BIGINT);
INSERT INTO `tag_seq` (`next_val`) SELECT 1
WHERE NOT EXISTS (SELECT 1 FROM `tag_seq`);

CREATE TABLE IF NOT EXISTS `sticker_seq` (`next_val` BIGINT);
INSERT INTO `sticker_seq` (`next_val`) SELECT 1
WHERE NOT EXISTS (SELECT 1 FROM `sticker_seq`);

CREATE TABLE IF NOT EXISTS `sticker_sheets_seq` (`next_val` BIGINT);
INSERT INTO `sticker_sheets_seq` (`next_val`) SELECT 1
WHERE NOT EXISTS (SELECT 1 FROM `sticker_sheets_seq`);

CREATE TABLE IF NOT EXISTS `placed_order_seq` (`next_val` BIGINT);
INSERT INTO `placed_order_seq` (`next_val`) SELECT 1
WHERE NOT EXISTS (SELECT 1 FROM `placed_order_seq`);

CREATE TABLE IF NOT EXISTS `order_items_seq` (`next_val` BIGINT);
INSERT INTO `order_items_seq` (`next_val`) SELECT 1
WHERE NOT EXISTS (SELECT 1 FROM `order_items_seq`);

CREATE TABLE IF NOT EXISTS `order_items_status_seq` (`next_val` BIGINT);
INSERT INTO `order_items_status_seq` (`next_val`) SELECT 1
WHERE NOT EXISTS (SELECT 1 FROM `order_items_status_seq`);

CREATE TABLE IF NOT EXISTS `order_updates_seq` (`next_val` BIGINT);
INSERT INTO `order_updates_seq` (`next_val`) SELECT 1
WHERE NOT EXISTS (SELECT 1 FROM `order_updates_seq`);

CREATE TABLE IF NOT EXISTS `collection_seq` (`next_val` BIGINT);
INSERT INTO `collection_seq` (`next_val`) SELECT 1
WHERE NOT EXISTS (SELECT 1 FROM `collection_seq`);

CREATE TABLE IF NOT EXISTS `collection_items_seq` (`next_val` BIGINT);
INSERT INTO `collection_items_seq` (`next_val`) SELECT 1
WHERE NOT EXISTS (SELECT 1 FROM `collection_items_seq`);

CREATE TABLE IF NOT EXISTS `tag_items_seq` (`next_val` BIGINT);
INSERT INTO `tag_items_seq` (`next_val`) SELECT 1
WHERE NOT EXISTS (SELECT 1 FROM `tag_items_seq`);
3 changes: 3 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ services:
database:
image: mysql:8.4
env_file: .env
ports:
- "${MYSQL_PORT:-3306}:3306"
volumes:
- mysql_data:/var/lib/mysql
- ./backend/init_tables.sql:/docker-entrypoint-initdb.d/1-init_tables.sql:ro
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "127.0.0.1"]
interval: 5s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -16,7 +16,7 @@
public class Permission {
@Id @GeneratedValue private int id;

@OneToMany(mappedBy = "permissions")
@ManyToMany(mappedBy = "permissions")
private List<User> users = new ArrayList<User>();

@ManyToOne
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.Table;
import java.util.ArrayList;
Expand All @@ -19,6 +21,10 @@ public class User {
private String passwordHash;

@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinTable(
name = "UserPermission",
joinColumns = @JoinColumn(name = "userId"),
inverseJoinColumns = @JoinColumn(name = "permissionId"))
private List<Permission> permissions = new ArrayList<Permission>();

public User() {}
Expand Down
Loading
Loading