diff --git a/.env.example b/.env.example new file mode 100644 index 0000000000000000000000000000000000000000..4b4f66186962c6392d2d53b80ef80bb466f51938 --- /dev/null +++ b/.env.example @@ -0,0 +1,3 @@ +DB_NAME = soap_db +DB_USER = root # Change this to your own username +DB_PASSWORD = root # Change this to your own password \ No newline at end of file diff --git a/.gitignore b/.gitignore index 9a0c1f08b2b10e5eca9cba7991bb5bce8c71e11f..36aa526ebd2e5bdb230e826ef472365b19a2095b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # Visual Studio Code .vscode +.env # Compiled class file target/ diff --git a/Makefile b/Makefile index 223d685bef44f2557a2e37bec70cd60cd0e9b9e5..6fef2ea4de2e44739b9df754ecaab02151b3ca6b 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,5 @@ +include .env + # Default target all: build run @@ -14,5 +16,17 @@ clean: mvn clean rm -f target/soap_service-1.0.jar +setup: create-db migration + +create-db: + mysql -u root -p -e "CREATE DATABASE IF NOT EXISTS $(DB_NAME)" + exit + +migration: + mvn flyway:migrate -Dflyway.url=jdbc:mysql://localhost:3306/$(DB_NAME) -Dflyway.user=$(DB_USER) -Dflyway.password=$(DB_PASSWORD) + +migration-repair: + mvn flyway:repair -Dflyway.url=jdbc:mysql://localhost:3306/$(DB_NAME) -Dflyway.user=$(DB_USER) -Dflyway.password=$(DB_PASSWORD) + # Phony targets to prevent conflicts with filenames .PHONY: all build run clean \ No newline at end of file diff --git a/pom.xml b/pom.xml index 821ddf4a35b0a9cfcff176409b83e5cc6345bbd8..e2e1da8ff0c701cf492bd35686efd279cd1033a6 100644 --- a/pom.xml +++ b/pom.xml @@ -13,6 +13,18 @@ <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> + <dependency> + <groupId>mysql</groupId> + <artifactId>mysql-connector-java</artifactId> + <version>8.0.33</version> + </dependency> + + <dependency> + <groupId>io.sunshower.zephyr</groupId> + <artifactId>flyway</artifactId> + <version>2.0.128.Final</version> + </dependency> + <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> @@ -22,12 +34,17 @@ <dependency> <groupId>com.sun.xml.ws</groupId> <artifactId>jaxws-rt</artifactId> - <version>2.3.6</version> <!-- Use the latest version --> + <version>2.3.6</version> </dependency> </dependencies> <build> <plugins> <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> + <plugin> + <groupId>org.flywaydb</groupId> + <artifactId>flyway-maven-plugin</artifactId> + <version>8.0.0</version> + </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> diff --git a/src/main/resources/db/migration/V1__init_db_shema.sql b/src/main/resources/db/migration/V1__init_db_shema.sql new file mode 100644 index 0000000000000000000000000000000000000000..5f44083360d176f8cbe6089873f488980a117822 --- /dev/null +++ b/src/main/resources/db/migration/V1__init_db_shema.sql @@ -0,0 +1,57 @@ +BEGIN; + +DROP TABLE IF EXISTS `book_collection`; +DROP TABLE IF EXISTS `in_collection`; +DROP TABLE IF EXISTS `collection_subscription`; +DROP TABLE IF EXISTS `author_subscription`; +DROP TABLE IF EXISTS `user_notification`; + +CREATE TABLE `book_collection` ( + `collection_id` int AUTO_INCREMENT PRIMARY KEY, + `created_by` varchar(25) NOT NULL, + `name` varchar(255) NOT NULL, + `desc` text NOT NULL, + `created_at` timestamp NOT NULL, + `updated_at` timestamp, + `is_deleted` BOOLEAN NOT NULL DEFAULT FALSE, + `deleted_at` timestamp +); + +CREATE TABLE `in_collection` ( + `collection_id` int, + `book_id` int, + PRIMARY KEY (`collection_id`, `book_id`) +); + +CREATE TABLE `collection_subscription` ( + `collection_id` int, + `username` varchar(25), + `subscribed_at` timestamp NOT NULL, + `is_unsubscribed` BOOLEAN NOT NULL DEFAULT FALSE, + `unsubscribed_at` timestamp, + PRIMARY KEY (`collection_id`, `username`) +); + +CREATE TABLE `author_subscription` ( + `author_id` int, + `username` varchar(25), + `subscribed_at` timestamp NOT NULL, + `is_unsubscribed` BOOLEAN NOT NULL DEFAULT FALSE, + `unsubscribed_at` timestamp, + PRIMARY KEY (`author_id`, `username`) +); + +CREATE TABLE `user_notification` ( + `notification_id` int AUTO_INCREMENT PRIMARY KEY, + `username` varchar(25), + `message` text NOT NULL, + `created_at` timestamp NOT NULL, + `status` ENUM('unread', 'read', 'archived') NOT NULL +); + +ALTER TABLE `in_collection` ADD FOREIGN KEY (`collection_id`) REFERENCES `book_collection` (`collection_id`); + +ALTER TABLE `collection_subscription` ADD FOREIGN KEY (`collection_id`) REFERENCES `book_collection` (`collection_id`); + +COMMIT; +``` \ No newline at end of file