diff --git a/.github/workflows/backend.yml b/.github/workflows/backend.yml index 6f62cde..8e7dce6 100644 --- a/.github/workflows/backend.yml +++ b/.github/workflows/backend.yml @@ -6,10 +6,12 @@ on: - 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: @@ -40,6 +42,8 @@ jobs: services: mysql: image: mysql:8.4 + ports: + - 3306:3306 env: MYSQL_ROOT_PASSWORD: ci-root-password MYSQL_DATABASE: sticker_business @@ -50,5 +54,15 @@ jobs: --health-retries=10 --health-start-period=30s steps: - - name: Confirm MySQL is healthy - run: echo "MySQL 8.4 is ready." + - name: Check out repository + uses: actions/checkout@v4 + + - name: Install MySQL client + run: | + sudo apt-get update + sudo apt-get install --no-install-recommends -y mysql-client + + - name: Run database 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 diff --git a/README.md b/README.md index 446f09f..162fa66 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,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. +- Runs `backend/init_tables.sql` to create the tables. Initialization scripts create new tables only when the database volume is empty; changing the script later does not update an existing database. - Open http://localhost:8080. You should see **Hello Docker World**. - Open http://localhost:5173. You should see the React starter page. diff --git a/backend/init_tables.sql b/backend/init_tables.sql new file mode 100644 index 0000000..149e91a --- /dev/null +++ b/backend/init_tables.sql @@ -0,0 +1,142 @@ +-- defaults to innoDB in case future changes to mySQL +CREATE TABLE IF NOT EXISTS `User` ( + `id` INT NOT NULL AUTO_INCREMENT, + `username` VARCHAR(255), + `passwordHash` VARCHAR(255), + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +CREATE TABLE IF NOT EXISTS `PermissionType` ( + `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, + `permissionTypeId` INT, + PRIMARY KEY (`id`), + KEY `IDX_Permission_permissionTypeId` (`permissionTypeId`), + CONSTRAINT `FK_Permission_permissionType` + FOREIGN KEY (`permissionTypeId`) REFERENCES `PermissionType` (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +CREATE TABLE IF NOT EXISTS `UserPermission` ( + `userId` INT NOT NULL, + `permissionId` INT NOT NULL, + PRIMARY KEY (`userId`, `permissionId`), + KEY `IDX_UserPermission_permissionId` (`permissionId`), + CONSTRAINT `FK_UserPermission_user` + FOREIGN KEY (`userId`) REFERENCES `User` (`id`), + CONSTRAINT `FK_UserPermission_permission` + FOREIGN KEY (`permissionId`) 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), + `svgPath` VARCHAR(255), + `active` TINYINT(1) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +CREATE TABLE IF NOT EXISTS `StickerSheets` ( + `id` INT NOT NULL AUTO_INCREMENT, + `filePath` VARCHAR(255), + `stickerIdsJSON` LONGTEXT, + `stickerIds` LONGTEXT, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +CREATE TABLE IF NOT EXISTS `PlacedOrder` ( + `id` INT NOT NULL AUTO_INCREMENT, + `customerId` INT, + `placementTimestamp` TIMESTAMP(6), + `status` VARCHAR(255), + `addressJSON` LONGTEXT, + `note` VARCHAR(255), + `address` LONGTEXT, + PRIMARY KEY (`id`), + KEY `IDX_PlacedOrder_customerId` (`customerId`), + CONSTRAINT `FK_PlacedOrder_customer` + FOREIGN KEY (`customerId`) REFERENCES `User` (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +CREATE TABLE IF NOT EXISTS `OrderItems` ( + `id` INT NOT NULL AUTO_INCREMENT, + `orderId` INT, + `sheetId` INT, + `quantity` INT NOT NULL, + PRIMARY KEY (`id`), + KEY `IDX_OrderItems_orderId` (`orderId`), + KEY `IDX_OrderItems_sheetId` (`sheetId`), + CONSTRAINT `FK_OrderItems_order` + FOREIGN KEY (`orderId`) REFERENCES `PlacedOrder` (`id`), + CONSTRAINT `FK_OrderItems_sheet` + FOREIGN KEY (`sheetId`) REFERENCES `StickerSheets` (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +CREATE TABLE IF NOT EXISTS `OrderItemsStatus` ( + `id` INT NOT NULL AUTO_INCREMENT, + `orderItemsId` INT, + `complete` TINYINT(1) NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `UK_OrderItemsStatus_orderItemsId` (`orderItemsId`), + CONSTRAINT `FK_OrderItemsStatus_orderItem` + FOREIGN KEY (`orderItemsId`) REFERENCES `OrderItems` (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +CREATE TABLE IF NOT EXISTS `OrderUpdates` ( + `id` INT NOT NULL AUTO_INCREMENT, + `orderId` INT, + `timestamp` TIMESTAMP(6), + `orderType` VARCHAR(255), + PRIMARY KEY (`id`), + KEY `IDX_OrderUpdates_orderId` (`orderId`), + CONSTRAINT `FK_OrderUpdates_order` + FOREIGN KEY (`orderId`) REFERENCES `PlacedOrder` (`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 `CollectionItems` ( + `id` INT NOT NULL AUTO_INCREMENT, + `stickerId` INT, + `collectionId` INT, + PRIMARY KEY (`id`), + KEY `IDX_CollectionItems_stickerId` (`stickerId`), + KEY `IDX_CollectionItems_collectionId` (`collectionId`), + CONSTRAINT `FK_CollectionItems_sticker` + FOREIGN KEY (`stickerId`) REFERENCES `Sticker` (`id`), + CONSTRAINT `FK_CollectionItems_collection` + FOREIGN KEY (`collectionId`) REFERENCES `Collection` (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +CREATE TABLE IF NOT EXISTS `TagItems` ( + `id` INT NOT NULL AUTO_INCREMENT, + `tagId` INT, + `stickerId` INT, + PRIMARY KEY (`id`), + KEY `IDX_TagItems_tagId` (`tagId`), + KEY `IDX_TagItems_stickerId` (`stickerId`), + CONSTRAINT `TAG_ID_FK` + FOREIGN KEY (`tagId`) REFERENCES `Tag` (`id`), + CONSTRAINT `STICKER_ID_FK` + FOREIGN KEY (`stickerId`) REFERENCES `Sticker` (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; \ No newline at end of file diff --git a/compose.yaml b/compose.yaml index a05f866..2a573d1 100644 --- a/compose.yaml +++ b/compose.yaml @@ -18,6 +18,7 @@ services: env_file: .env 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