Switch to H2 DB

This commit is contained in:
aditya.chandel
2025-01-17 10:58:42 -07:00
parent 0a37e0ec65
commit f5e1c1e7c8
9 changed files with 44 additions and 29 deletions
+1 -1
View File
@@ -40,7 +40,7 @@ dependencies {
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor' annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
// Database & Flyway // Database & Flyway
runtimeOnly 'org.mariadb.jdbc:mariadb-java-client' implementation 'com.h2database:h2'
implementation 'org.flywaydb:flyway-mysql:10.12.0' implementation 'org.flywaydb:flyway-mysql:10.12.0'
// Book and Image Libraries // Book and Image Libraries
BIN
View File
Binary file not shown.
@@ -17,8 +17,8 @@ public class AppSettingController {
} }
@PutMapping("/api/settings") @PutMapping("/api/settings")
public void updateSetting(@RequestParam String category, @RequestParam String key, @RequestParam String newValue) { public void updateSetting(@RequestParam String category, @RequestParam String name, @RequestParam String newValue) {
appSettingService.updateSetting(category, key, newValue); appSettingService.updateSetting(category, name, newValue);
} }
} }
@@ -15,9 +15,9 @@ public class AppSettingEntity {
@Column(name = "category", nullable = false) @Column(name = "category", nullable = false)
private String category; private String category;
@Column(name = "key1", nullable = false) @Column(name = "name", nullable = false)
private String key; private String name;
@Column(name = "value", nullable = false, columnDefinition = "TEXT") @Column(name = "val", nullable = false, columnDefinition = "TEXT")
private String value; private String val;
} }
@@ -6,5 +6,5 @@ import org.springframework.stereotype.Repository;
@Repository @Repository
public interface AppSettingsRepository extends JpaRepository<AppSettingEntity, Long> { public interface AppSettingsRepository extends JpaRepository<AppSettingEntity, Long> {
AppSettingEntity findByCategoryAndKey(String category, String key);; AppSettingEntity findByCategoryAndName(String category, String name);;
} }
@@ -19,7 +19,7 @@ public class AppSettingService {
public AppSettings getAppSettings() { public AppSettings getAppSettings() {
List<AppSettingEntity> settings = appSettingsRepository.findAll(); List<AppSettingEntity> settings = appSettingsRepository.findAll();
Map<String, Map<String, String>> settingsMap = settings.stream().collect(Collectors.groupingBy(AppSettingEntity::getCategory, Collectors.toMap(AppSettingEntity::getKey, AppSettingEntity::getValue))); Map<String, Map<String, String>> settingsMap = settings.stream().collect(Collectors.groupingBy(AppSettingEntity::getCategory, Collectors.toMap(AppSettingEntity::getName, AppSettingEntity::getVal)));
AppSettings appSettings = new AppSettings(); AppSettings appSettings = new AppSettings();
if (settingsMap.containsKey("epub")) { if (settingsMap.containsKey("epub")) {
Map<String, String> epubSettings = settingsMap.get("epub"); Map<String, String> epubSettings = settingsMap.get("epub");
@@ -33,13 +33,13 @@ public class AppSettingService {
} }
@Transactional @Transactional
public void updateSetting(String category, String key, String newValue) { public void updateSetting(String category, String name, String newValue) {
AppSettingEntity setting = appSettingsRepository.findByCategoryAndKey(category, key); AppSettingEntity setting = appSettingsRepository.findByCategoryAndName(category, name);
if (setting != null) { if (setting != null) {
setting.setValue(newValue); setting.setVal(newValue);
appSettingsRepository.save(setting); appSettingsRepository.save(setting);
} else { } else {
throw new IllegalArgumentException("Setting not found for category: " + category + " and key: " + key); throw new IllegalArgumentException("Setting not found for category: " + category + " and key: " + name);
} }
} }
} }
@@ -14,10 +14,10 @@ spring:
application: application:
name: booklore-api name: booklore-api
datasource: datasource:
driver-class-name: org.mariadb.jdbc.Driver driver-class-name: org.h2.Driver
url: ${SOLITUDE_API_MARIADB_URL:jdbc:mariadb://localhost:3306/booklore} url: jdbc:h2:tcp://localhost:9092/~/my-library-temp/config/db/booklore
username: ${SOLITUDE_API_MARIADB_USERNAME:root} username: sa
password: ${SOLITUDE_API_MARIADB_PASSWORD:cfcNEBdUm6QerR6oo2UM} password: password
jpa: jpa:
hibernate: hibernate:
naming: naming:
@@ -1,3 +1,4 @@
-- Create the library table
CREATE TABLE IF NOT EXISTS library CREATE TABLE IF NOT EXISTS library
( (
id BIGINT AUTO_INCREMENT PRIMARY KEY, id BIGINT AUTO_INCREMENT PRIMARY KEY,
@@ -6,6 +7,7 @@ CREATE TABLE IF NOT EXISTS library
icon VARCHAR(64) NOT NULL icon VARCHAR(64) NOT NULL
); );
-- Create the library_path table
CREATE TABLE IF NOT EXISTS library_path CREATE TABLE IF NOT EXISTS library_path
( (
id BIGINT AUTO_INCREMENT PRIMARY KEY, id BIGINT AUTO_INCREMENT PRIMARY KEY,
@@ -14,6 +16,7 @@ CREATE TABLE IF NOT EXISTS library_path
CONSTRAINT fk_library_path FOREIGN KEY (library_id) REFERENCES library (id) CONSTRAINT fk_library_path FOREIGN KEY (library_id) REFERENCES library (id)
); );
-- Create the book table
CREATE TABLE IF NOT EXISTS book CREATE TABLE IF NOT EXISTS book
( (
id BIGINT AUTO_INCREMENT PRIMARY KEY, id BIGINT AUTO_INCREMENT PRIMARY KEY,
@@ -27,11 +30,14 @@ CREATE TABLE IF NOT EXISTS book
epub_progress VARCHAR(1000) NULL, epub_progress VARCHAR(1000) NULL,
CONSTRAINT fk_library FOREIGN KEY (library_id) REFERENCES library (id) ON DELETE CASCADE, CONSTRAINT fk_library FOREIGN KEY (library_id) REFERENCES library (id) ON DELETE CASCADE,
CONSTRAINT unique_file_library UNIQUE (file_name, library_id), CONSTRAINT unique_file_library UNIQUE (file_name, library_id)
INDEX idx_library_id (library_id),
INDEX idx_last_read_time (last_read_time)
); );
-- Create indexes for the book table
CREATE INDEX IF NOT EXISTS idx_library_id ON book (library_id);
CREATE INDEX IF NOT EXISTS idx_last_read_time ON book (last_read_time);
-- Create the book_metadata table
CREATE TABLE IF NOT EXISTS book_metadata CREATE TABLE IF NOT EXISTS book_metadata
( (
book_id BIGINT NOT NULL PRIMARY KEY, book_id BIGINT NOT NULL PRIMARY KEY,
@@ -50,6 +56,7 @@ CREATE TABLE IF NOT EXISTS book_metadata
CONSTRAINT fk_book_metadata FOREIGN KEY (book_id) REFERENCES book (id) ON DELETE CASCADE CONSTRAINT fk_book_metadata FOREIGN KEY (book_id) REFERENCES book (id) ON DELETE CASCADE
); );
-- Create the author table
CREATE TABLE IF NOT EXISTS author CREATE TABLE IF NOT EXISTS author
( (
id BIGINT AUTO_INCREMENT PRIMARY KEY, id BIGINT AUTO_INCREMENT PRIMARY KEY,
@@ -57,12 +64,14 @@ CREATE TABLE IF NOT EXISTS author
CONSTRAINT unique_name UNIQUE (name) CONSTRAINT unique_name UNIQUE (name)
); );
-- Create the category table
CREATE TABLE IF NOT EXISTS category CREATE TABLE IF NOT EXISTS category
( (
id BIGINT AUTO_INCREMENT PRIMARY KEY, id BIGINT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL UNIQUE name VARCHAR(255) NOT NULL UNIQUE
); );
-- Create the book_viewer_setting table
CREATE TABLE IF NOT EXISTS book_viewer_setting CREATE TABLE IF NOT EXISTS book_viewer_setting
( (
book_id BIGINT PRIMARY KEY, book_id BIGINT PRIMARY KEY,
@@ -73,6 +82,7 @@ CREATE TABLE IF NOT EXISTS book_viewer_setting
CONSTRAINT fk_book_viewer_setting FOREIGN KEY (book_id) REFERENCES book (id) ON DELETE CASCADE CONSTRAINT fk_book_viewer_setting FOREIGN KEY (book_id) REFERENCES book (id) ON DELETE CASCADE
); );
-- Create the book_metadata_category_mapping table
CREATE TABLE IF NOT EXISTS book_metadata_category_mapping CREATE TABLE IF NOT EXISTS book_metadata_category_mapping
( (
book_id BIGINT NOT NULL, book_id BIGINT NOT NULL,
@@ -82,18 +92,21 @@ CREATE TABLE IF NOT EXISTS book_metadata_category_mapping
CONSTRAINT fk_book_metadata_category_mapping_category FOREIGN KEY (category_id) REFERENCES category (id) ON DELETE CASCADE CONSTRAINT fk_book_metadata_category_mapping_category FOREIGN KEY (category_id) REFERENCES category (id) ON DELETE CASCADE
); );
-- Create the book_metadata_author_mapping table
CREATE TABLE IF NOT EXISTS book_metadata_author_mapping CREATE TABLE IF NOT EXISTS book_metadata_author_mapping
( (
book_id BIGINT NOT NULL, book_id BIGINT NOT NULL,
author_id BIGINT NOT NULL, author_id BIGINT NOT NULL,
PRIMARY KEY (book_id, author_id), PRIMARY KEY (book_id, author_id),
CONSTRAINT fk_book_metadata_author_mapping_book FOREIGN KEY (book_id) REFERENCES book_metadata (book_id) ON DELETE CASCADE, CONSTRAINT fk_book_metadata_author_mapping_book FOREIGN KEY (book_id) REFERENCES book_metadata (book_id) ON DELETE CASCADE,
CONSTRAINT fk_book_metadata_author_mapping_author FOREIGN KEY (author_id) REFERENCES author (id) ON DELETE CASCADE, CONSTRAINT fk_book_metadata_author_mapping_author FOREIGN KEY (author_id) REFERENCES author (id) ON DELETE CASCADE
INDEX idx_book_metadata_id (book_id),
INDEX idx_author_id (author_id)
); );
-- Create indexes for the book_metadata_author_mapping table
CREATE INDEX IF NOT EXISTS idx_book_metadata_id ON book_metadata_author_mapping (book_id);
CREATE INDEX IF NOT EXISTS idx_author_id ON book_metadata_author_mapping (author_id);
-- Create the shelf table
CREATE TABLE IF NOT EXISTS shelf CREATE TABLE IF NOT EXISTS shelf
( (
id BIGINT AUTO_INCREMENT PRIMARY KEY, id BIGINT AUTO_INCREMENT PRIMARY KEY,
@@ -102,6 +115,7 @@ CREATE TABLE IF NOT EXISTS shelf
icon VARCHAR(64) NOT NULL icon VARCHAR(64) NOT NULL
); );
-- Create the book_shelf_mapping table
CREATE TABLE IF NOT EXISTS book_shelf_mapping CREATE TABLE IF NOT EXISTS book_shelf_mapping
( (
book_id BIGINT NOT NULL, book_id BIGINT NOT NULL,
@@ -111,11 +125,12 @@ CREATE TABLE IF NOT EXISTS book_shelf_mapping
CONSTRAINT fk_book_shelf_mapping_shelf FOREIGN KEY (shelf_id) REFERENCES shelf (id) ON DELETE CASCADE CONSTRAINT fk_book_shelf_mapping_shelf FOREIGN KEY (shelf_id) REFERENCES shelf (id) ON DELETE CASCADE
); );
-- Create the app_settings table
CREATE TABLE app_settings CREATE TABLE app_settings
( (
id BIGINT AUTO_INCREMENT PRIMARY KEY, id BIGINT AUTO_INCREMENT PRIMARY KEY,
category VARCHAR(255) NOT NULL, category VARCHAR(255) NOT NULL,
key1 VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL,
value TEXT NOT NULL, val TEXT NOT NULL,
UNIQUE (category, key1) UNIQUE (category, name)
); );
@@ -1,12 +1,12 @@
INSERT INTO booklore.shelf (name, icon) INSERT INTO shelf (name, icon)
VALUES ('Favorites', 'heart'); VALUES ('Favorites', 'heart');
INSERT INTO app_settings (category, key1, value) INSERT INTO app_settings (category, name, val)
VALUES ('epub', 'theme', 'white'), VALUES ('epub', 'theme', 'white'),
('epub', 'fontSize', '120'), ('epub', 'fontSize', '120'),
('epub', 'font', 'serif'); ('epub', 'font', 'serif');
INSERT INTO app_settings (category, key1, value) INSERT INTO app_settings (category, name, val)
VALUES ('pdf', 'spread', 'odd'), VALUES ('pdf', 'spread', 'odd'),
('pdf', 'zoom', 'page-fit'), ('pdf', 'zoom', 'page-fit'),
('pdf', 'sidebar', 'true'); ('pdf', 'sidebar', 'true');