-
Notifications
You must be signed in to change notification settings - Fork 0
add new default AttachmentDataSource and add migrate it if necessary #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ad00290
add new default AttachmentDataSource and add migrate it if necessary
J0nasMayr e0a231f
update readme
J0nasMayr 5b9979f
make default attachment data source opt-in
J0nasMayr 40d4946
add more docs and add test for config
J0nasMayr be96e86
requested changes
J0nasMayr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...n/java/it/aboutbits/springboot/emailservice/lib/application/JdbcAttachmentDataSource.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package it.aboutbits.springboot.emailservice.lib.application; | ||
|
|
||
| import it.aboutbits.springboot.emailservice.lib.AttachmentDataSource; | ||
| import it.aboutbits.springboot.emailservice.lib.exception.AttachmentException; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.jspecify.annotations.NullMarked; | ||
| import org.springframework.dao.DataAccessException; | ||
| import org.springframework.dao.EmptyResultDataAccessException; | ||
| import org.springframework.jdbc.core.JdbcTemplate; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
|
|
||
| @Slf4j | ||
| @NullMarked | ||
| public class JdbcAttachmentDataSource implements AttachmentDataSource { | ||
| private final JdbcTemplate jdbcTemplate; | ||
|
|
||
| public JdbcAttachmentDataSource(JdbcTemplate jdbcTemplate) { | ||
| this.jdbcTemplate = jdbcTemplate; | ||
| } | ||
|
|
||
| public void migrate() { | ||
| log.info("EmailService: running attachment payload DB migrations..."); | ||
|
|
||
| jdbcTemplate.execute( | ||
| //@formatter:off | ||
| """ | ||
|
|
||
| create table if not exists email_service_attachment_payloads | ||
| ( | ||
| id bigint generated by default as identity | ||
| primary key, | ||
| payload bytea not null, | ||
| created_at timestamp with time zone default now() not null | ||
| ); | ||
| """ | ||
| //@formatter:on | ||
| ); | ||
|
|
||
| log.info("EmailService: attachment payload migrations done!"); | ||
| } | ||
|
|
||
| @Override | ||
| public InputStream getAttachmentPayload(long fileReference) throws AttachmentException { | ||
| try { | ||
| var payload = jdbcTemplate.queryForObject( | ||
| "select payload from email_service_attachment_payloads where id = ?", | ||
| byte[].class, | ||
| fileReference | ||
| ); | ||
| if (payload == null) { | ||
| throw new AttachmentException("attachment payload not found: " + fileReference); | ||
| } | ||
| return new ByteArrayInputStream(payload); | ||
| } catch (EmptyResultDataAccessException e) { | ||
| throw new AttachmentException("attachment payload not found: " + fileReference, e); | ||
| } catch (DataAccessException e) { | ||
| throw new AttachmentException("failed to load attachment payload: " + fileReference, e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public long storeAttachmentPayload(InputStream payload) throws AttachmentException { | ||
| final byte[] bytes; | ||
| try (payload) { | ||
| bytes = payload.readAllBytes(); | ||
| } catch (IOException e) { | ||
| throw new AttachmentException("failed to read attachment payload", e); | ||
| } | ||
|
|
||
| try { | ||
| var id = jdbcTemplate.queryForObject( | ||
| "insert into email_service_attachment_payloads (payload) values (?) returning id", | ||
| Long.class, | ||
| (Object) bytes | ||
| ); | ||
| if (id == null) { | ||
| throw new AttachmentException("failed to store attachment payload"); | ||
| } | ||
| return id; | ||
| } catch (DataAccessException e) { | ||
| throw new AttachmentException("failed to store attachment payload", e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void releaseAttachment(long fileReference) throws AttachmentException { | ||
| try { | ||
| jdbcTemplate.update( | ||
| "delete from email_service_attachment_payloads where id = ?", | ||
| fileReference | ||
| ); | ||
| } catch (DataAccessException e) { | ||
| throw new AttachmentException("failed to release attachment payload: " + fileReference, e); | ||
| } | ||
| } | ||
| } |
25 changes: 0 additions & 25 deletions
25
...it/aboutbits/springboot/emailservice/lib/application/UnavailableAttachmentDataSource.java
This file was deleted.
Oops, something went wrong.
94 changes: 94 additions & 0 deletions
94
...va/it/aboutbits/springboot/emailservice/lib/application/JdbcAttachmentDataSourceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| package it.aboutbits.springboot.emailservice.lib.application; | ||
|
|
||
| import it.aboutbits.springboot.emailservice.lib.AttachmentDataSource; | ||
| import it.aboutbits.springboot.emailservice.lib.exception.AttachmentException; | ||
| import it.aboutbits.springboot.emailservice.support.database.WithPostgres; | ||
| import org.jspecify.annotations.NullMarked; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.test.context.SpringBootTest; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatCode; | ||
| import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
|
|
||
| @SpringBootTest | ||
| @WithPostgres | ||
| @NullMarked | ||
| class JdbcAttachmentDataSourceTest { | ||
| @Autowired | ||
| AttachmentDataSource attachmentDataSource; | ||
|
|
||
| @Test | ||
| void defaultAttachmentDataSource_shouldBeJdbcBased() { | ||
| assertThat(attachmentDataSource).isInstanceOf(JdbcAttachmentDataSource.class); | ||
| } | ||
|
|
||
| @Test | ||
| void givenPayload_store_shouldBeReadableAgain() throws Exception { | ||
| var payload = new byte[]{1, 2, 3, 4, 5}; | ||
|
|
||
| var fileReference = attachmentDataSource.storeAttachmentPayload(new ByteArrayInputStream(payload)); | ||
|
|
||
| try (var stored = attachmentDataSource.getAttachmentPayload(fileReference)) { | ||
| assertThat(stored.readAllBytes()).isEqualTo(payload); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void givenPayload_store_shouldClosePayloadStream() throws Exception { | ||
| var payload = new TrackingInputStream(new byte[]{1, 2, 3}); | ||
|
|
||
| attachmentDataSource.storeAttachmentPayload(payload); | ||
|
|
||
| assertThat(payload.closed).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| void givenMultiplePayloads_store_shouldReturnDistinctReferences() throws Exception { | ||
| var first = attachmentDataSource.storeAttachmentPayload(new ByteArrayInputStream(new byte[]{1})); | ||
| var second = attachmentDataSource.storeAttachmentPayload(new ByteArrayInputStream(new byte[]{2})); | ||
|
|
||
| assertThat(first).isNotEqualTo(second); | ||
| } | ||
|
|
||
| @Test | ||
| void givenUnknownReference_get_shouldFail() { | ||
| assertThatExceptionOfType(AttachmentException.class).isThrownBy( | ||
| () -> attachmentDataSource.getAttachmentPayload(1L) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void givenStoredPayload_release_shouldRemoveIt() throws Exception { | ||
| var fileReference = attachmentDataSource.storeAttachmentPayload(new ByteArrayInputStream(new byte[]{1, 2, 3})); | ||
|
|
||
| attachmentDataSource.releaseAttachment(fileReference); | ||
|
|
||
| assertThatExceptionOfType(AttachmentException.class).isThrownBy( | ||
| () -> attachmentDataSource.getAttachmentPayload(fileReference) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void givenUnknownReference_release_shouldBeIdempotent() { | ||
| assertThatCode( | ||
| () -> attachmentDataSource.releaseAttachment(1L) | ||
| ).doesNotThrowAnyException(); | ||
| } | ||
|
|
||
| private static final class TrackingInputStream extends ByteArrayInputStream { | ||
| private boolean closed = false; | ||
|
|
||
| private TrackingInputStream(byte[] buf) { | ||
| super(buf); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| closed = true; | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.