Skip to content

Commit f1ac2ec

Browse files
committed
replace liquibase with a custom migrator
1 parent c3ca1c5 commit f1ac2ec

8 files changed

Lines changed: 83 additions & 188 deletions

File tree

pom.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@
3333
<groupId>org.springframework.boot</groupId>
3434
<artifactId>spring-boot-starter-validation</artifactId>
3535
</dependency>
36-
<dependency>
37-
<groupId>org.liquibase</groupId>
38-
<artifactId>liquibase-core</artifactId>
39-
</dependency>
4036
<dependency>
4137
<groupId>org.projectlombok</groupId>
4238
<artifactId>lombok</artifactId>

readme.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ public class App {
7474

7575
The following configuration options are available:
7676

77-
| Name | Default | Description |
78-
|--------------------------------------------|-------------|--------------------------------------------------------------------------------------------------------------------------|
79-
| `lib.emailservice.liquibase.enabled` | true | Enables liquibase migrations. **attention** `classpath:db/changelog/master.yml` will be run first and must be present! |
80-
| `lib.emailservice.scheduling.enabled` | true | Enables the scheduler sending the emails. |
81-
| `lib.emailservice.scheduling.interval` | 30000 | Specifies the milliseconds delay between runs of the scheduler. |
77+
| Name | Default | Description |
78+
|----------------------------------------|-------------|-----------------------------------------------------------------------|
79+
| `lib.emailservice.migrations.enabled` | true | Enables database migrations. |
80+
| `lib.emailservice.scheduling.enabled` | true | Enables the scheduler sending the emails. |
81+
| `lib.emailservice.scheduling.interval` | 30000 | Specifies the milliseconds delay between runs of the scheduler. |
8282

8383
## Building and releasing a new version:
8484

src/main/java/it/aboutbits/springboot/emailservice/EmailServiceConfiguration.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,28 @@
66
import it.aboutbits.springboot.emailservice.lib.application.EmailAttachmentMapperImpl;
77
import it.aboutbits.springboot.emailservice.lib.application.EmailMapper;
88
import it.aboutbits.springboot.emailservice.lib.application.EmailMapperImpl;
9+
import it.aboutbits.springboot.emailservice.lib.application.EmailServiceMigrator;
910
import it.aboutbits.springboot.emailservice.lib.application.ManageEmail;
1011
import it.aboutbits.springboot.emailservice.lib.application.QueryEmail;
1112
import it.aboutbits.springboot.emailservice.lib.application.SendScheduledEmails;
1213
import it.aboutbits.springboot.emailservice.lib.application.UnavailableAttachmentDataSource;
1314
import it.aboutbits.springboot.emailservice.lib.jpa.EmailRepository;
1415
import jakarta.persistence.EntityManager;
15-
import liquibase.integration.spring.SpringLiquibase;
1616
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
1717
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
1818
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
1919
import org.springframework.context.annotation.Bean;
20+
import org.springframework.jdbc.core.JdbcTemplate;
2021
import org.springframework.mail.javamail.JavaMailSender;
2122

22-
import javax.sql.DataSource;
2323
import java.util.List;
2424

2525
@AutoConfigurationPackage
2626
public class EmailServiceConfiguration {
27-
@Bean
28-
@ConditionalOnProperty(value = "lib.emailservice.liquibase.enabled", matchIfMissing = true)
29-
public SpringLiquibase springLiquibase(DataSource dataSource) {
30-
var liquibase = new SpringLiquibase();
31-
liquibase.setDataSource(dataSource);
32-
liquibase.setChangeLog("classpath:/db/changelog/emailservice/main.yml");
33-
liquibase.setShouldRun(true);
34-
return liquibase;
27+
@Bean(initMethod = "init")
28+
@ConditionalOnProperty(value = "lib.emailservice.migrations.enabled", matchIfMissing = true)
29+
public EmailServiceMigrator springLiquibase(JdbcTemplate jdbcTemplate) {
30+
return new EmailServiceMigrator(jdbcTemplate);
3531
}
3632

3733
@Bean
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package it.aboutbits.springboot.emailservice.lib.application;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.jdbc.core.JdbcTemplate;
5+
6+
@Slf4j
7+
public class EmailServiceMigrator {
8+
private final JdbcTemplate jdbcTemplate;
9+
10+
public EmailServiceMigrator(JdbcTemplate jdbcTemplate) {
11+
this.jdbcTemplate = jdbcTemplate;
12+
}
13+
14+
void init() {
15+
log.info("EmailService: running DB migrations...");
16+
17+
jdbcTemplate.execute("""
18+
19+
create table if not exists email_service_emails
20+
(
21+
id bigint generated by default as identity
22+
primary key,
23+
state text default 'PENDING'::text not null,
24+
subject text not null,
25+
recipients jsonb not null,
26+
from_address text not null,
27+
from_name text not null,
28+
text_body text not null,
29+
html_body text not null,
30+
attachments jsonb default '[]'::jsonb,
31+
sending_scheduled_at timestamp with time zone default now() not null,
32+
reference text,
33+
sent_at timestamp with time zone,
34+
error_at timestamp with time zone,
35+
error_message text,
36+
created_at timestamp with time zone default now() not null,
37+
updated_at timestamp with time zone default now() not null,
38+
constraint email_service_emails_one_body_not_empty
39+
check ((text_body <> ''::text) OR (html_body <> ''::text))
40+
);
41+
42+
create index if not exists email_service_emails_reference_index
43+
on email_service_emails (reference);
44+
45+
create index if not exists email_service_emails_state_index
46+
on email_service_emails (state);
47+
48+
create index if not exists email_service_emails_sending_scheduled_at_index
49+
on email_service_emails (sending_scheduled_at);
50+
51+
create table if not exists email_service_email_attachments
52+
(
53+
id bigint generated by default as identity
54+
primary key,
55+
email_id bigint not null
56+
constraint email_service_email_attachments_email_id
57+
references email_service_emails,
58+
file_name text not null,
59+
content_type text not null,
60+
reference text not null
61+
);
62+
63+
create index if not exists email_service_email_attachments_email_id_index
64+
on email_service_email_attachments (email_id);
65+
66+
""");
67+
68+
log.info("EmailService: done!");
69+
}
70+
}

src/main/java/it/aboutbits/springboot/emailservice/lib/application/QueryEmail.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ List<Email> readyToSend() {
4747
.setParameter("scheduledBefore", OffsetDateTime.now())
4848
.setHint("jakarta.persistence.fetchgraph", entityGraph)
4949
.getResultList();
50-
//return emailRepository.findReadyToSend(OffsetDateTime.now());
5150
}
5251

5352
public Optional<EmailDto> byId(final long id) {

src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"properties": [
33
{
4-
"name": "lib.emailservice.liquibase.enabled",
4+
"name": "lib.emailservice.migrations.enabled",
55
"type": "java.lang.Boolean",
6-
"description": "Enables liquibase migrations.",
6+
"description": "Enables database migrations.",
77
"defaultValue": true
88
},
99
{

src/main/resources/db/changelog/emailservice/2023-01-30-create-email-tables.yml

Lines changed: 0 additions & 160 deletions
This file was deleted.

src/main/resources/db/changelog/emailservice/main.yml

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)