|
| 1 | +# Database module |
| 2 | + |
| 3 | +## Database initialization |
| 4 | + |
| 5 | +Liquibase is used to manage database schema creation and changes and the configuration is made in [`changelog.xml`](src/main/resources/db/changelog.xml). |
| 6 | + |
| 7 | +When adding new entities or making changes to existing ones, please make sure |
| 8 | +to update the database changelog accordingly by adding a new file in the [`changesets directory`](src/main/resources/db/changesets). |
| 9 | + |
| 10 | +Numbering is important to keep track of the order of changes, so please follow the existing pattern when naming new changeset files. |
| 11 | +When a feature is backported in various branches, the numbering ensure that the changesets are applied in the correct order. |
| 12 | + |
| 13 | +eg. `main` can contain changesets 001 to 010, while `4.2.x` contains changesets 001 to 006 and 009 which was backported. |
| 14 | + |
| 15 | +Liquibase track changes applied to the current database in the `DATABASECHANGELOG` and `DATABASECHANGELOGLOCK` tables. |
| 16 | + |
| 17 | + |
| 18 | +## Old database initialization and migration system proposal |
| 19 | + |
| 20 | +GeoNetwork using its own mechanism based on Java and SQL to migrate from version to version. |
| 21 | +This mechanism does not work well when backporting changes to previous versions and does not support all possible migration paths. |
| 22 | + |
| 23 | +It is proposed the following strategy to move to Liquibase: **Old GeoNetwork system is used until version 4.4.x, then starting from version 4.6.x Liquibase only is used.** |
| 24 | + |
| 25 | +This means: |
| 26 | +* Users upgrading to version 4.6.x will first have to migrate to the latest 4.4.x version using the old system, then upgrade to 4.6.x which will use Liquibase. |
| 27 | +* New installations of version 4.6.x and later will use Liquibase only. |
| 28 | + |
| 29 | +This approach avoid to migrate all existing migration steps to Liquibase, which is hard to do because of: |
| 30 | +* Java migration steps |
| 31 | +* Hibernate automatic schema generation not described in SQL |
| 32 | +* Complexity to test all migration paths (which are not even working well with the old system) |
| 33 | + |
| 34 | + |
| 35 | +## Liquibase implementation status |
| 36 | + |
| 37 | +Draft experiment to implement initial database creation and initial data loading using Liquibase. |
| 38 | + |
| 39 | +- [ ] Databases tested (test: first run works, restart works) |
| 40 | + - [x] Postgres |
| 41 | + - [x] H2 (needed for test in GN4) |
| 42 | + - [ ] Other - discuss which database do we support and test? |
| 43 | +- [x] [Liquibase bean configuration](../domain/src/main/resources/config-spring-geonetwork.xml) with [`changelog.xml`](src/main/resources/db/changelog.xml) as main changelog file |
| 44 | +- [x] [Schema creation](src/main/resources/db/changesets/00000-initial-schema.xml) / Precondition: no table metadata exists |
| 45 | +- [x] [Initial data](src/main/resources/db/changesets/00001-initial-data.sql) / Precondition: table settings is empty |
| 46 | +- [x] [Initial languages](src/main/resources/db/changesets/00002-initial-data-languages-eng.sql) / Precondition: table language does not have eng |
| 47 | +- [ ] Other languages |
| 48 | +- [ ] Remove past database migrations |
| 49 | +- [ ] Remove old configuration `initial_data.xml` and `database_migration.xml` |
| 50 | +- [ ] Remove `db.migration_onstartup` property / Hibernate `hbm2ddl` property set to `none` |
| 51 | +- [ ] Remove `TestDatabasePopulator` |
| 52 | +- [ ] ... |
| 53 | + |
| 54 | +## GeoNetwork 5 and Liquibase |
| 55 | + |
| 56 | +GeoNetwork 5 can depend on the GeoNetwork 4 database module to initialize the database using Liquibase. |
| 57 | + |
| 58 | +To enable liquibase in GeoNetwork 5 by: |
| 59 | + |
| 60 | +* Adding liquibase dependency in `pom.xml`: |
| 61 | + |
| 62 | +```xml |
| 63 | +<dependency> |
| 64 | + <groupId>org.liquibase</groupId> |
| 65 | + <artifactId>liquibase-core</artifactId> |
| 66 | + <version>5.0.1</version> |
| 67 | + <exclusions> |
| 68 | + <exclusion> |
| 69 | + <artifactId>liquibase-commercial</artifactId> |
| 70 | + <groupId>org.liquibase</groupId> |
| 71 | + </exclusion> |
| 72 | + </exclusions> |
| 73 | +</dependency> |
| 74 | +``` |
| 75 | + |
| 76 | +* Adding dependency on GeoNetwork 4 database module in `shared/gn-domain/pom.xml`: (to rework once the dependency is available in a maven repository) |
| 77 | + |
| 78 | +```xml |
| 79 | + <dependency> |
| 80 | + <groupId>org.geonetwork</groupId> |
| 81 | + <artifactId>gn-database</artifactId> |
| 82 | + <version>${project.version}</version> |
| 83 | + </dependency> |
| 84 | +``` |
| 85 | + |
| 86 | +* Adding the following properties to `application.yml`: |
| 87 | + |
| 88 | +```yaml |
| 89 | + spring: |
| 90 | + application: |
| 91 | + name: GeoNetwork |
| 92 | + liquibase: |
| 93 | + enabled: true |
| 94 | + change-log: classpath:/db/changelog.xml |
| 95 | +``` |
| 96 | +
|
| 97 | +Then both GeoNetwork 4 and GeoNetwork 5 can use Liquibase to initialize the database schema and data. |
| 98 | +Liquibase has a lock mechanism to avoid multiple instances to apply changes at the same time. |
| 99 | +
|
| 100 | +
|
| 101 | +## Questions |
| 102 | +
|
| 103 | +To be investigated: |
| 104 | +* Check context and labels to flag GN4/GN5 changesets? (See https://docs.liquibase.com/reference-guide/changelog-attributes/what-are-contexts) |
| 105 | +* Define process to manage changes from GN4 and GN5? |
| 106 | +* How to generate changelogs? Intellij has support for liquibase https://www.jetbrains.com/help/idea/liquibase.html |
| 107 | +
|
0 commit comments