Skip to content
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

Updated hibernate with schema export to automatically initialise db #3

Merged
merged 1 commit into from Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 7 additions & 13 deletions README.md
Expand Up @@ -10,7 +10,7 @@ The application can be reached at `localhost:8080`.

First a user has to be created and authenticated. To register a new user, a POST request to `api/v1/user/signup` must be sent.

#### Signup request:
#### Example signup request:
```json
{
"email": "user@example.com",
Expand All @@ -21,7 +21,7 @@ First a user has to be created and authenticated. To register a new user, a POST

After the successful registration, the user must be authenticated by sending a POST request to `api/v1/authentication`. A `Bearer` token is returned which is used for future requests.

#### Authentication request
#### Associated authentication request
```json
{
"email": "user@example.com",
Expand All @@ -31,12 +31,9 @@ After the successful registration, the user must be authenticated by sending a P

### Accessing the database

I have created an endpoint `api/v1/movie` with sample data to test this setup. The email address of a user can also be changed at `api/v1/user/email`.
I have created an endpoint `api/v1/movie` ~~with sample data to test this setup.~~ The email address of a user can be changed at `api/v1/user/email`.

#### Additional HTTP header
In order to change the email address, the header `X-Datasource: default` must be sent along. Otherwise, the lookup process would return the database of the authenticated user. It is only needed for `api/v1/user/email` and should not be used anywhere else.

#### Response of `GET` request `api/v1/movie/1`:
#### Example response of `GET` request `api/v1/movie/1`:

```json
{
Expand All @@ -47,7 +44,7 @@ In order to change the email address, the header `X-Datasource: default` must be
}
```

#### `PATCH` request to change email address:
#### Example `PATCH` request to change email address:
```json
{
"email": "new_user@example.com"
Expand All @@ -56,9 +53,6 @@ In order to change the email address, the header `X-Datasource: default` must be

The database is generated after the first access / request. The hashed email address serves as filename `database/[hashed_email].mv.db` and can be accessed through [localhost:8080/h2-console](http://localhost:8080/h2-console) (username and password is `admin`).

#### Information about schema generation
### Information about schema generation

Because of the dynamically generated databases, JPA (Hibernate) can't automatically initialise the databases. It instead generates a file [create.sql](src/main/resources/create.sql) which will be executed as soon as a new database is created. To update the file simply change [application.yml](src/main/resources/application.yml) accordingly:
```yml
spring.jpa.properties.javax.persistence.schema-generation.scripts.action: create
```
Hibernate automatically creates the schema of each database. In the case that the file [create.sql](src/main/resources/create.sql) doesn't exist, and a new datasource is being created, the application encounters an error and crashes.
5 changes: 5 additions & 0 deletions pom.xml
Expand Up @@ -41,6 +41,11 @@
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>
<dependency>
<groupId>com.github.v-ladynev</groupId>
<artifactId>fluent-hibernate-core</artifactId>
<version>0.3.1</version>
</dependency>

<dependency>
<groupId>io.jsonwebtoken</groupId>
Expand Down