|
|
# Migrations
|
|
|
When an application is already deployed and changes to the database are necessary, a migration needs to be written. This can be done as follows. If you have never written a migration before, obtain `h2-1.3.176.jar` and `liquibase.zip` from another developer. Then add liquibase to your path and put the jar under `/usr/local/apps/h2`. When this is done, run the development branch with the following settings:
|
|
|
```
|
|
|
spring:
|
|
|
jpa:
|
|
|
hibernate:
|
|
|
ddl-auto: create
|
|
|
datasource:
|
|
|
url: jdbc:h2:file:./testdb-old;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;DATABASE_TO_UPPER=false;
|
|
|
username: sa
|
|
|
password:
|
|
|
```
|
|
|
Then run your branch, but change `testdb-old` to `testdb-new`. Afterwards, you can generate a changelog with the following script:
|
|
|
When an application is already deployed and changes to the database are necessary, a migration needs to be written. This can be done as follows.
|
|
|
|
|
|
### Setup Liquibase:
|
|
|
|
|
|
1. Install Liquibase (just from their website).
|
|
|
2. Copy the `liquibase.template.properties` file in the root directory (if it isn't available in your project, head to the TAM repository and copy it from there).
|
|
|
3. Paste the copy into the root directory and rename it to `liquibase.properties`.
|
|
|
4. Check the `build.gradle.kts` file and find the `h2Version` variable. In the `classpath` variable, replace `VERSION` with the version as it appears in the variable.
|
|
|
5. Check the following path in your file explorer: `$HOME\.gradle\caches\modules-2\files-2.1\com.h2database\h2\VERSION` and find the folder containing a `.jar` file. Replace `HASH` in the `classpath` variable with the name of this folder.
|
|
|
* If you can't find `modules-2` in the `.gradle/caches` folder, then check the [Gradle Docs](https://docs.gradle.org/current/userguide/directory_layout.html) and replace `modules-2` with the folder labeled `Shared caches (e.g. for artifacts of dependencies)`.
|
|
|
6. On a new line add the variable `changeSetAuthor` and set it equal to your GitLab username (usually your NetID).
|
|
|
|
|
|
Another option is to obtain `h2.jar` and `liquibase.zip` from another developer (or from the `.gradle` folder). Then add Liquibase to your path and put the jar under `/usr/local/apps/h2`.
|
|
|
|
|
|
### Create migrations:
|
|
|
|
|
|
1. Switch to the `dev` branch.
|
|
|
2. Make sure the `application.yaml` file has `url: jdbc:h2:file:./testdb-old;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;DATABASE_TO_UPPER=false;` in the `datasource` section.
|
|
|
3. Run the project normally, there should now be some `.db` files in the root directory.
|
|
|
4. Stop the project.
|
|
|
5. Switch back to your branch.
|
|
|
6. Make sure the `application.yaml` file has `url: jdbc:h2:file:./testdb-new;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;DATABASE_TO_UPPER=false;` in the `datasource` section.
|
|
|
7. Run the project normally.
|
|
|
8. Now run this command: `liquibase --changelogFile=new.yaml diff-changelog`, there should now be a file called `new.yaml` in the root directory.
|
|
|
9. If you encounter errors during this step, check the `liquibase.properties` file for instructions.
|
|
|
10. Now copy your changes from `changes.yaml` to the bottom of `migrations.yaml`. Double check whether the generated changelog makes sense. Sometimes, you might need to add default values or change column types.
|
|
|
11. Make sure to delete the `new.yaml` file before preforming more migrations.
|
|
|
|
|
|
Alternatively you can generate a changelog with the following script:
|
|
|
```
|
|
|
PROJ_DIR=$1
|
|
|
RESOURCES_DIR=$PROJ_DIR/src/main/resources
|
... | ... | @@ -27,6 +43,4 @@ liquibase \ |
|
|
--referenceUrl=jdbc:h2:$PROJ_DIR/testdb-new \
|
|
|
--referenceUsername=sa \
|
|
|
--referencePassword=
|
|
|
```
|
|
|
Finally, copy your changes from `changes.yaml` to the bottom of `migrations.yaml`. Double check whether the generated changelog makes sense. Sometimes, you might need to add default values or change column types.
|
|
|
|
|
|
``` |
|
|
\ No newline at end of file |