From 1127bed07f571846ac7436f50b12f4ab8a6bb84b Mon Sep 17 00:00:00 2001 From: Adnane Miliari Date: Thu, 28 Nov 2024 00:05:16 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=B1=20implement=20initial=20data=20see?= =?UTF-8?q?ding=20with=20SQL=20scripts=20instead=20of=20DbSeeder=20classes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../resources/shared-application-default.yml | 6 ++ .../resources/shared-application-docker.yml | 5 ++ .../main/resources/shared-application-eks.yml | 19 ++++-- .../resources/shared-application-kube.yml | 18 ++--- .../dev/nano/customer/CustomerDbSeeder.java | 22 ------- customer/src/main/resources/db/data.sql | 8 +++ .../notification/NotificationDbSeeder.java | 27 -------- .../src/main/resources/application-docker.yml | 63 ------------------ .../src/main/resources/application-eks.yml | 55 ---------------- .../src/main/resources/application-kube.yml | 66 ------------------- notification/src/main/resources/db/data.sql | 11 ++++ .../java/dev/nano/order/OrderDbSeeder.java | 23 ------- .../main/java/dev/nano/order/OrderEntity.java | 5 +- order/src/main/resources/db/data.sql | 6 ++ .../dev/nano/payment/PaymentDbSeeder.java | 23 ------- payment/src/main/resources/db/data.sql | 6 ++ .../dev/nano/product/ProductDbSeeder.java | 36 ---------- product/src/main/resources/db/data.sql | 6 ++ 18 files changed, 74 insertions(+), 331 deletions(-) delete mode 100644 customer/src/main/java/dev/nano/customer/CustomerDbSeeder.java create mode 100644 customer/src/main/resources/db/data.sql delete mode 100644 notification/src/main/java/dev/nano/notification/NotificationDbSeeder.java delete mode 100644 notification/src/main/resources/application-docker.yml delete mode 100644 notification/src/main/resources/application-eks.yml delete mode 100644 notification/src/main/resources/application-kube.yml create mode 100644 notification/src/main/resources/db/data.sql delete mode 100644 order/src/main/java/dev/nano/order/OrderDbSeeder.java create mode 100644 order/src/main/resources/db/data.sql delete mode 100644 payment/src/main/java/dev/nano/payment/PaymentDbSeeder.java create mode 100644 payment/src/main/resources/db/data.sql delete mode 100644 product/src/main/java/dev/nano/product/ProductDbSeeder.java create mode 100644 product/src/main/resources/db/data.sql diff --git a/common/src/main/resources/shared-application-default.yml b/common/src/main/resources/shared-application-default.yml index 10ca828..b97b8f8 100644 --- a/common/src/main/resources/shared-application-default.yml +++ b/common/src/main/resources/shared-application-default.yml @@ -31,6 +31,12 @@ spring: format_sql: true show-sql: true open-in-view: false + defer-datasource-initialization: true + sql: + init: + mode: always + data-locations: classpath*:db/data.sql + platform: postgresql rabbitmq: addresses: localhost:5672 diff --git a/common/src/main/resources/shared-application-docker.yml b/common/src/main/resources/shared-application-docker.yml index f3e2ed8..c8b4add 100644 --- a/common/src/main/resources/shared-application-docker.yml +++ b/common/src/main/resources/shared-application-docker.yml @@ -10,6 +10,11 @@ spring: password: password rabbitmq: addresses: rabbitmq:5672 + sql: + init: + mode: always + data-locations: classpath*:db/data.sql + platform: postgresql eureka: client: diff --git a/common/src/main/resources/shared-application-eks.yml b/common/src/main/resources/shared-application-eks.yml index 119d041..b55213e 100644 --- a/common/src/main/resources/shared-application-eks.yml +++ b/common/src/main/resources/shared-application-eks.yml @@ -5,12 +5,19 @@ management: spring: datasource: - url: jdbc:postgresql://demo-microservices.csetxdk14qax.us-east-1.rds.amazonaws.com:5432/${spring.application.name} - username: ${DB_USERNAME} - password: ${DB_PASSWORD} + url: jdbc:postgresql://postgres:5432/${spring.application.name} + username: miliariadnane + password: password rabbitmq: addresses: rabbitmq:5672 - jpa: - hibernate: - ddl-auto: update + sql: + init: + mode: always + data-locations: classpath*:db/data.sql + platform: postgresql +eureka: + client: + service-url: + defaultZone: http://eureka-server:8761/eureka + enabled: false diff --git a/common/src/main/resources/shared-application-kube.yml b/common/src/main/resources/shared-application-kube.yml index 2ae0e93..b37e2a8 100644 --- a/common/src/main/resources/shared-application-kube.yml +++ b/common/src/main/resources/shared-application-kube.yml @@ -5,14 +5,14 @@ management: spring: datasource: - url: jdbc:postgresql://postgres:5432/${spring.application.name} - username: miliariadnane - password: password + url: jdbc:postgresql://demo-microservices.csetxdk14qax.us-east-1.rds.amazonaws.com:5432/${spring.application.name} + username: ${DB_USERNAME} + password: ${DB_PASSWORD} rabbitmq: addresses: rabbitmq:5672 - -eureka: - client: - service-url: - defaultZone: http://eureka-server:8761/eureka - enabled: false + jpa: + hibernate: + ddl-auto: update + sql: + init: + mode: never diff --git a/customer/src/main/java/dev/nano/customer/CustomerDbSeeder.java b/customer/src/main/java/dev/nano/customer/CustomerDbSeeder.java deleted file mode 100644 index 8800489..0000000 --- a/customer/src/main/java/dev/nano/customer/CustomerDbSeeder.java +++ /dev/null @@ -1,22 +0,0 @@ -package dev.nano.customer; - -import org.springframework.boot.CommandLineRunner; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class CustomerDbSeeder { - - @Bean - CommandLineRunner commandLineRunner(CustomerRepository customerRepository) { - return args -> { - CustomerEntity customer = CustomerEntity.builder() - .name("Adnane Miliari") - .email("miliari.adnane@gmail.com") - .phone("+61112223333") - .address("Rabat, Morocco") - .build(); - customerRepository.save(customer); - }; - } -} diff --git a/customer/src/main/resources/db/data.sql b/customer/src/main/resources/db/data.sql new file mode 100644 index 0000000..d500c7d --- /dev/null +++ b/customer/src/main/resources/db/data.sql @@ -0,0 +1,8 @@ +INSERT INTO customer (id, name, email, phone, address) +VALUES (nextval('customer_sequence'), 'John Smith', 'john.smith@gmail.com', '+1234567890', '123 Main St, New York, NY'), + (nextval('customer_sequence'), 'Emma Wilson', 'emma.wilson@outlook.com', '+1987654321', + '456 Park Ave, London, UK'), + (nextval('customer_sequence'), 'Mohammed Ali', 'mohammed.ali@yahoo.com', '+212661234567', + 'Marina Street, Casablanca, Morocco'), + (nextval('customer_sequence'), 'Sarah Chen', 'sarah.chen@gmail.com', '+8613912345678', + '789 Nanjing Road, Shanghai, China'); diff --git a/notification/src/main/java/dev/nano/notification/NotificationDbSeeder.java b/notification/src/main/java/dev/nano/notification/NotificationDbSeeder.java deleted file mode 100644 index 7368e3b..0000000 --- a/notification/src/main/java/dev/nano/notification/NotificationDbSeeder.java +++ /dev/null @@ -1,27 +0,0 @@ -package dev.nano.notification; - -import org.springframework.boot.CommandLineRunner; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import java.time.LocalDateTime; - -@Configuration -public class NotificationDbSeeder { - - @Bean - CommandLineRunner commandLineRunner(NotificationRepository notificationRepository) { - return args -> { - NotificationEntity notification = NotificationEntity.builder() - .customerId(1L) - .customerName("Adnane Miliari Aka Nano") - .customerEmail("miliari.adnane@gmail.com") - .sender("NanoDev") - .message("Welcome to this demo in microservices") - .sentAt(LocalDateTime.now()) - .build(); - - notificationRepository.save(notification); - }; - } -} diff --git a/notification/src/main/resources/application-docker.yml b/notification/src/main/resources/application-docker.yml deleted file mode 100644 index ea70502..0000000 --- a/notification/src/main/resources/application-docker.yml +++ /dev/null @@ -1,63 +0,0 @@ -#Server -server: - port: 8004 - servlet: - context-path: /notification - error: - include-message: always - -#Management -management: - observations: - key-values: - application: ${spring.application.name} - tracing: - sampling: - probability: 1.0 - enabled: true - zipkin: - tracing: - endpoint: http://zipkin:9411/api/v2/spans - endpoints: - web: - exposure: - include: "*" - endpoint: - health: - show-details: always - -#Spring -spring: - application: - name: notification - datasource: - url: jdbc:postgresql://postgres:5432/notification - username: miliariadnane - password: password - rabbitmq: - addresses: rabbitmq:5672 - jpa: - hibernate: - ddl-auto: create-drop - properties: - hibernate: - dialect: org.hibernate.dialect.PostgreSQLDialect - format_sql: true - show-sql: true - -#Eureka-Client -eureka: - client: - service-url: - defaultZone: http://eureka-server:8761/eureka - fetch-registry: true - register-with-eureka: true - -#RabbitMQ -rabbitmq: - exchange: - internal: internal.exchange - queue: - notification: notification.queue - routing-key: - internal-notification: internal.notification.routing-key diff --git a/notification/src/main/resources/application-eks.yml b/notification/src/main/resources/application-eks.yml deleted file mode 100644 index b259307..0000000 --- a/notification/src/main/resources/application-eks.yml +++ /dev/null @@ -1,55 +0,0 @@ -#Server -server: - port: 8004 - servlet: - context-path: /notification - error: - include-message: always - -#Management -management: - observations: - key-values: - application: ${spring.application.name} - tracing: - sampling: - probability: 1.0 - enabled: true - zipkin: - tracing: - endpoint: http://zipkin:9411/api/v2/spans - endpoints: - web: - exposure: - include: "*" - endpoint: - health: - show-details: always - -#Spring -spring: - application: - name: notification - datasource: - url: jdbc:postgresql://demo-microservices.csetxdk14qax.us-east-1.rds.amazonaws.com:5432/notification - username: ${DB_USERNAME} - password: ${DB_PASSWORD} - rabbitmq: - addresses: rabbitmq:5672 - jpa: - hibernate: - ddl-auto: create-drop - properties: - hibernate: - dialect: org.hibernate.dialect.PostgreSQLDialect - format_sql: true - show-sql: true - -#RabbitMQ -rabbitmq: - exchange: - internal: internal.exchange - queue: - notification: notification.queue - routing-key: - internal-notification: internal.notification.routing-key diff --git a/notification/src/main/resources/application-kube.yml b/notification/src/main/resources/application-kube.yml deleted file mode 100644 index de05eee..0000000 --- a/notification/src/main/resources/application-kube.yml +++ /dev/null @@ -1,66 +0,0 @@ -#Server -server: - port: 8004 - servlet: - context-path: /notification - error: - include-message: always - -#Management -management: - observations: - key-values: - application: ${spring.application.name} - tracing: - sampling: - probability: 1.0 - enabled: true - zipkin: - tracing: - endpoint: http://zipkin:9411/api/v2/spans - endpoints: - web: - exposure: - include: "*" - endpoint: - health: - show-details: always - -#Spring -spring: - application: - name: notification - datasource: - url: jdbc:postgresql://postgres:5432/notification - username: miliariadnane - password: password - zipkin: - baseUrl: http://zipkin:9411 - rabbitmq: - addresses: rabbitmq:5672 - jpa: - hibernate: - ddl-auto: create-drop - properties: - hibernate: - dialect: org.hibernate.dialect.PostgreSQLDialect - format_sql: true - show-sql: true - -#Eureka-Client -eureka: - client: - service-url: - defaultZone: http://eureka-server:8761/eureka - fetch-registry: true - register-with-eureka: true - enabled: false - -#RabbitMQ -rabbitmq: - exchange: - internal: internal.exchange - queue: - notification: notification.queue - routing-key: - internal-notification: internal.notification.routing-key diff --git a/notification/src/main/resources/db/data.sql b/notification/src/main/resources/db/data.sql new file mode 100644 index 0000000..82bed77 --- /dev/null +++ b/notification/src/main/resources/db/data.sql @@ -0,0 +1,11 @@ +INSERT INTO notification (id, customer_id, customer_name, customer_email, sender, message, sent_at) +VALUES (nextval('notification_sequence'), 1, 'John Smith', 'john.smith@gmail.com', 'NanoShop', + 'Your order #1 has been confirmed', CURRENT_TIMESTAMP), + (nextval('notification_sequence'), 1, 'John Smith', 'john.smith@gmail.com', 'NanoShop', + 'Your payment for order #1 has been processed', CURRENT_TIMESTAMP), + (nextval('notification_sequence'), 2, 'Emma Wilson', 'emma.wilson@outlook.com', 'NanoShop', + 'Your order #3 has been shipped', CURRENT_TIMESTAMP), + (nextval('notification_sequence'), 3, 'Mohammed Ali', 'mohammed.ali@yahoo.com', 'NanoShop', + 'Payment reminder for order #4', CURRENT_TIMESTAMP), + (nextval('notification_sequence'), 4, 'Sarah Chen', 'sarah.chen@gmail.com', 'NanoShop', + 'Thank you for your purchase! Order #5 confirmed', CURRENT_TIMESTAMP); diff --git a/order/src/main/java/dev/nano/order/OrderDbSeeder.java b/order/src/main/java/dev/nano/order/OrderDbSeeder.java deleted file mode 100644 index 31a8381..0000000 --- a/order/src/main/java/dev/nano/order/OrderDbSeeder.java +++ /dev/null @@ -1,23 +0,0 @@ -package dev.nano.order; - -import org.springframework.boot.CommandLineRunner; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import java.time.LocalDateTime; - -@Configuration -public class OrderDbSeeder { - - @Bean - CommandLineRunner commandLineRunner(OrderRepository orderRepository) { - return args -> { - OrderEntity order = OrderEntity.builder() - .customerId(1L) - .productId(1L) - .amount(10) - .createAt(LocalDateTime.now()).build(); - orderRepository.save(order); - }; - } -} diff --git a/order/src/main/java/dev/nano/order/OrderEntity.java b/order/src/main/java/dev/nano/order/OrderEntity.java index e44464f..2335e67 100644 --- a/order/src/main/java/dev/nano/order/OrderEntity.java +++ b/order/src/main/java/dev/nano/order/OrderEntity.java @@ -10,7 +10,10 @@ @AllArgsConstructor @NoArgsConstructor @ToString @SuperBuilder @Entity(name = "Order") -@Table(name = "orders") +@Table( + name = "\"order\"", + schema = "public" +) public class OrderEntity { @Id diff --git a/order/src/main/resources/db/data.sql b/order/src/main/resources/db/data.sql new file mode 100644 index 0000000..09f7466 --- /dev/null +++ b/order/src/main/resources/db/data.sql @@ -0,0 +1,6 @@ +INSERT INTO "order" (id, customer_id, product_id, amount, create_at) +VALUES (nextval('order_sequence'), 1, 1, 2, CURRENT_TIMESTAMP), + (nextval('order_sequence'), 1, 3, 1, CURRENT_TIMESTAMP), + (nextval('order_sequence'), 2, 2, 3, CURRENT_TIMESTAMP), + (nextval('order_sequence'), 3, 4, 1, CURRENT_TIMESTAMP), + (nextval('order_sequence'), 4, 1, 2, CURRENT_TIMESTAMP); diff --git a/payment/src/main/java/dev/nano/payment/PaymentDbSeeder.java b/payment/src/main/java/dev/nano/payment/PaymentDbSeeder.java deleted file mode 100644 index 43b0323..0000000 --- a/payment/src/main/java/dev/nano/payment/PaymentDbSeeder.java +++ /dev/null @@ -1,23 +0,0 @@ -package dev.nano.payment; - -import org.springframework.boot.CommandLineRunner; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import java.time.LocalDateTime; - -@Configuration -public class PaymentDbSeeder { - - @Bean - CommandLineRunner commandLineRunner(PaymentRepository paymentRepository) { - return args -> { - PaymentEntity payment = PaymentEntity.builder() - .customerId(1L) - .orderId(1L) - .createAt(LocalDateTime.now()) - .build(); - paymentRepository.save(payment); - }; - } -} diff --git a/payment/src/main/resources/db/data.sql b/payment/src/main/resources/db/data.sql new file mode 100644 index 0000000..2d38fac --- /dev/null +++ b/payment/src/main/resources/db/data.sql @@ -0,0 +1,6 @@ +INSERT INTO payment (id, customer_id, order_id, create_at) +VALUES (nextval('payment_sequence'), 1, 1, CURRENT_TIMESTAMP), + (nextval('payment_sequence'), 1, 2, CURRENT_TIMESTAMP), + (nextval('payment_sequence'), 2, 3, CURRENT_TIMESTAMP), + (nextval('payment_sequence'), 3, 4, CURRENT_TIMESTAMP), + (nextval('payment_sequence'), 4, 5, CURRENT_TIMESTAMP); diff --git a/product/src/main/java/dev/nano/product/ProductDbSeeder.java b/product/src/main/java/dev/nano/product/ProductDbSeeder.java deleted file mode 100644 index 4c7e381..0000000 --- a/product/src/main/java/dev/nano/product/ProductDbSeeder.java +++ /dev/null @@ -1,36 +0,0 @@ -package dev.nano.product; - -import com.github.javafaker.Faker; -import org.springframework.boot.CommandLineRunner; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class ProductDbSeeder { - - private static final Faker faker = new Faker(); - - @Bean - CommandLineRunner commandLineRunner(ProductRepository productRepository) { - - return args -> { - /* generate 20 product using faker */ - - for (int i = 0; i < 20; i++) { - - String name = faker.commerce().productName(); - int price = faker.random().nextInt(100, 1000); - String productImageUrl = "https://picsum.photos/id/%s/200/200".formatted( - faker.random().nextInt(1, 1000) - ); - - productRepository.save(new ProductEntity( - faker.random().nextLong(), - name, - productImageUrl, - price - )); - } - }; - } -} diff --git a/product/src/main/resources/db/data.sql b/product/src/main/resources/db/data.sql new file mode 100644 index 0000000..c1561de --- /dev/null +++ b/product/src/main/resources/db/data.sql @@ -0,0 +1,6 @@ +INSERT INTO product (name, image, price) +VALUES (nextval('product_sequence'), 'MacBook Pro M3', 'https://picsum.photos/id/1/200/200', 1999), + (nextval('product_sequence'), 'iPhone 15 Pro', 'https://picsum.photos/id/2/200/200', 1299), + (nextval('product_sequence'), 'AirPods Pro', 'https://picsum.photos/id/3/200/200', 249), + (nextval('product_sequence'), 'iPad Air', 'https://picsum.photos/id/4/200/200', 599), + (nextval('product_sequence'), 'Apple Watch Series 9', 'https://picsum.photos/id/5/200/200', 499);