From 21cf9c8f240d37797b2c9dc8d3522c6461459510 Mon Sep 17 00:00:00 2001 From: Nicolas Chang <25302138+NicolasCwy@users.noreply.github.com> Date: Sat, 23 Mar 2024 10:26:23 +0800 Subject: [PATCH 01/18] Update gradle config --- build.gradle | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/build.gradle b/build.gradle index ab30b57dcf5..8286146d22c 100644 --- a/build.gradle +++ b/build.gradle @@ -100,7 +100,9 @@ dependencies { exclude group: "org.apache.jmeter", module: "bom" } + liquibaseRuntime("org.liquibase:liquibase-core:4.19.0") liquibaseRuntime("info.picocli:picocli:4.7.1") + liquibaseRuntime('org.liquibase.ext:liquibase-hibernate5:3.6') liquibaseRuntime(sourceSets.main.output) } @@ -152,16 +154,6 @@ tasks.withType(cz.habarta.typescript.generator.gradle.GenerateTask) { optionalAnnotations = [ "javax.annotation.Nullable" ] - outputFileType = "implementationFile" - outputKind = "module" - indentString = " " - jackson2Configuration = [ - fieldVisibility: "ANY", - getterVisibility: "NONE", - isGetterVisibility: "NONE", - setterVisibility: "NONE", - creatorVisibility: "NONE", - ] mapEnum = "asEnum" nonConstEnums = true noFileComment = true From 1f6418461207003b8eee645589c307ac456cfe6d Mon Sep 17 00:00:00 2001 From: Nicolas Chang <25302138+NicolasCwy@users.noreply.github.com> Date: Sat, 23 Mar 2024 10:57:19 +0800 Subject: [PATCH 02/18] Update liquibase config for v9 --- build.gradle | 2 +- .../db/changelog/db.changelog-v9.xml | 488 +++++++++++++++++- 2 files changed, 472 insertions(+), 18 deletions(-) diff --git a/build.gradle b/build.gradle index 8286146d22c..f17069a02a5 100644 --- a/build.gradle +++ b/build.gradle @@ -102,7 +102,7 @@ dependencies { liquibaseRuntime("org.liquibase:liquibase-core:4.19.0") liquibaseRuntime("info.picocli:picocli:4.7.1") - liquibaseRuntime('org.liquibase.ext:liquibase-hibernate5:3.6') + liquibaseRuntime("org.postgresql:postgresql:42.7.2") liquibaseRuntime(sourceSets.main.output) } diff --git a/src/main/resources/db/changelog/db.changelog-v9.xml b/src/main/resources/db/changelog/db.changelog-v9.xml index 57b6e7f9587..9a60ac3ea81 100644 --- a/src/main/resources/db/changelog/db.changelog-v9.xml +++ b/src/main/resources/db/changelog/db.changelog-v9.xml @@ -4,33 +4,63 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd"> - - - - - - - + + + + - - + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + @@ -45,18 +75,33 @@ - + + + + + + + + + + + + + + + + @@ -73,10 +118,419 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + + + \ No newline at end of file From 8151bdea391cb14f2135328587d8be10d8dd095c Mon Sep 17 00:00:00 2001 From: Nicolas Chang <25302138+NicolasCwy@users.noreply.github.com> Date: Sun, 24 Mar 2024 12:33:49 +0800 Subject: [PATCH 03/18] Turn off table generate for prod --- src/main/java/teammates/common/util/HibernateUtil.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/teammates/common/util/HibernateUtil.java b/src/main/java/teammates/common/util/HibernateUtil.java index eedb4b7f757..43fd3ec9e0a 100644 --- a/src/main/java/teammates/common/util/HibernateUtil.java +++ b/src/main/java/teammates/common/util/HibernateUtil.java @@ -114,7 +114,7 @@ public static void buildSessionFactory(String dbUrl, String username, String pas .setProperty("hibernate.connection.username", username) .setProperty("hibernate.connection.password", password) .setProperty("hibernate.connection.url", dbUrl) - .setProperty("hibernate.hbm2ddl.auto", "update") + .setProperty("hibernate.hbm2ddl.auto", "validate") .setProperty("show_sql", "true") .setProperty("hibernate.current_session_context_class", "thread") // Uncomment only during migration for optimized batch-insertion, batch-update, and batch-fetch. @@ -124,6 +124,10 @@ public static void buildSessionFactory(String dbUrl, String username, String pas // .setProperty("hibernate.jdbc.fetch_size", "50") .addPackage("teammates.storage.sqlentity"); + if (Config.IS_DEV_SERVER) { + config.setProperty("hibernate.hbm2ddl.auto", "update"); + } + for (Class cls : ANNOTATED_CLASSES) { config = config.addAnnotatedClass(cls); } From 4765e3e17f4932005643e14363ac85db5a42d50d Mon Sep 17 00:00:00 2001 From: Nicolas Chang <25302138+NicolasCwy@users.noreply.github.com> Date: Sun, 24 Mar 2024 12:43:20 +0800 Subject: [PATCH 04/18] Add back wrongly deleted properties --- build.gradle | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/build.gradle b/build.gradle index f17069a02a5..40a12d57cea 100644 --- a/build.gradle +++ b/build.gradle @@ -154,6 +154,16 @@ tasks.withType(cz.habarta.typescript.generator.gradle.GenerateTask) { optionalAnnotations = [ "javax.annotation.Nullable" ] + outputFileType = "implementationFile" + outputKind = "module" + indentString = " " + jackson2Configuration = [ + fieldVisibility: "ANY", + getterVisibility: "NONE", + isGetterVisibility: "NONE", + setterVisibility: "NONE", + creatorVisibility: "NONE", + ] mapEnum = "asEnum" nonConstEnums = true noFileComment = true From 5a5ffc8001cec42b0f6a2156c02463f954911551 Mon Sep 17 00:00:00 2001 From: Nicolas Chang <25302138+NicolasCwy@users.noreply.github.com> Date: Sun, 24 Mar 2024 14:28:07 +0800 Subject: [PATCH 05/18] Update of changelog file --- .../{db.changelog-v9.xml => db.changelog-2024-03-24.xml} | 0 src/main/resources/db/changelog/db.changelog-root.xml | 4 ++-- 2 files changed, 2 insertions(+), 2 deletions(-) rename src/main/resources/db/changelog/{db.changelog-v9.xml => db.changelog-2024-03-24.xml} (100%) diff --git a/src/main/resources/db/changelog/db.changelog-v9.xml b/src/main/resources/db/changelog/db.changelog-2024-03-24.xml similarity index 100% rename from src/main/resources/db/changelog/db.changelog-v9.xml rename to src/main/resources/db/changelog/db.changelog-2024-03-24.xml diff --git a/src/main/resources/db/changelog/db.changelog-root.xml b/src/main/resources/db/changelog/db.changelog-root.xml index 66d4b7d7c88..c872d221a84 100644 --- a/src/main/resources/db/changelog/db.changelog-root.xml +++ b/src/main/resources/db/changelog/db.changelog-root.xml @@ -4,5 +4,5 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd"> - - + + \ No newline at end of file From 217e9cfe2586633a1013789b9c6a298110a1d4ca Mon Sep 17 00:00:00 2001 From: Nicolas Chang <25302138+NicolasCwy@users.noreply.github.com> Date: Sun, 24 Mar 2024 14:46:14 +0800 Subject: [PATCH 06/18] Fix lint --- src/main/resources/db/changelog/db.changelog-2024-03-24.xml | 2 +- src/main/resources/db/changelog/db.changelog-root.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/resources/db/changelog/db.changelog-2024-03-24.xml b/src/main/resources/db/changelog/db.changelog-2024-03-24.xml index 9a60ac3ea81..205645520ff 100644 --- a/src/main/resources/db/changelog/db.changelog-2024-03-24.xml +++ b/src/main/resources/db/changelog/db.changelog-2024-03-24.xml @@ -533,4 +533,4 @@ initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="feedback_questions" validate="true" /> - \ No newline at end of file + diff --git a/src/main/resources/db/changelog/db.changelog-root.xml b/src/main/resources/db/changelog/db.changelog-root.xml index c872d221a84..2f3ddab42bf 100644 --- a/src/main/resources/db/changelog/db.changelog-root.xml +++ b/src/main/resources/db/changelog/db.changelog-root.xml @@ -5,4 +5,4 @@ xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd"> - \ No newline at end of file + From 08bc71d557e910aee032ddf1807c7b54ef49ff7c Mon Sep 17 00:00:00 2001 From: Nicolas Chang <25302138+NicolasCwy@users.noreply.github.com> Date: Tue, 26 Mar 2024 15:16:15 +0800 Subject: [PATCH 07/18] Add configuration for generating changelog --- build.gradle | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/build.gradle b/build.gradle index 40a12d57cea..041fa3c368a 100644 --- a/build.gradle +++ b/build.gradle @@ -146,7 +146,23 @@ liquibase { username project.properties['liquibaseUsername'] password project.properties['liquibasePassword'] } + snapshot { + url project.properties['liquibaseDbUrl'] + username project.properties['liquibaseUsername'] + password project.properties['liquibasePassword'] + snapshotFormat "json" + outputFile "liquibase-snapshot.json" + } + diffMain { + searchPath "${projectDir}" + changeLogFile "src/main/resources/db/changelog/db.changelog-new.xml" + referenceUrl project.properties['liquibaseDbUrl'] + referenceUsername project.properties['liquibaseUsername'] + referencePassword project.properties['liquibasePassword'] + url "offline:postgresql?snapshot=liquibase-snapshot.json" + } } + runList = project.ext.runList } tasks.withType(cz.habarta.typescript.generator.gradle.GenerateTask) { From 5a7689432c14317aee6368baeb9de5fcda5ff13c Mon Sep 17 00:00:00 2001 From: Nicolas Chang <25302138+NicolasCwy@users.noreply.github.com> Date: Tue, 26 Mar 2024 15:24:06 +0800 Subject: [PATCH 08/18] Add schema migration docs --- docs/schema-migration.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 docs/schema-migration.md diff --git a/docs/schema-migration.md b/docs/schema-migration.md new file mode 100644 index 00000000000..df923b7b5a7 --- /dev/null +++ b/docs/schema-migration.md @@ -0,0 +1,27 @@ + + title: "Schema Migration" + + +# SQL Schema Migration + +Teammates uses _[Liquibase]_(https://docs.liquibase.com/start/home.html), a database schema change management solution that enables developers to revise and release database changes to production. If you were to change the schema of any entity, you would have to create a _Liquibase_ changelog which a maintainer will run to keep the production databases schema in sync with the code. + +## Liquibase in Teammates +_Liquibase_ is made available using the [gradle plugin](https://github.com/liquibase/liquibase-gradle-plugin), providing _liquibase_ functions as tasks. Try `gradle tasks | grep "liquibase"` to see all the tasks available. In teammates, change logs (more in the next section) are written in _XML_. + +## Change logs, change sets and change types +A _change log_ is a file that contains a series of _change sets_ (analagous to a transaction) which applies _change types_ (actions). You can refer to this page on liquibase on the types of [change types](https://docs.liquibase.com/change-types/home.html) that can be used. + +## How to use Liquibase in Teammates +1. Create an _XML_ change log file in `src/main/resources/db/changelog` naming convention is the `db.changelog-YYYY-MM-DD-entity.xml` e.g `db.changelog-2024-03-24-courses.xml`. +2. Add changelog file to be included in the `db.changelog-root` as the last entry + +## Generating liquibase change logs +1. Delete `postgres-data` to clear any old database schemas +2. Run `git checkout master` and +3. run the server using `./gradlew serverRun` to generate tables found on master +4. Generate snapshot of database by running `./gradlew liquibaseSnapshot -PrunList=snapshot`, the snapshot will be output to `liquibase-snapshot.json` +4. Checkout your branch and repeat steps 1 and 3 to generate the tables found on your branch +5. Run `./gradlew liquibaseDiffChangeLog -PrunList=diffMain` to generate changeLog to resolve database schema differences +6. Rename this file to the format `db.changelog-YYYY-MM-DD-entity.xml` and add it as a changelog in `db.changelog-root` + From 4c8ac0dc306b961f85ac0508dbc6b52c47dd7343 Mon Sep 17 00:00:00 2001 From: Nicolas Chang <25302138+NicolasCwy@users.noreply.github.com> Date: Tue, 26 Mar 2024 15:32:48 +0800 Subject: [PATCH 09/18] Update process.md --- docs/process.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/process.md b/docs/process.md index 521e00c6c11..c8480c52bc7 100644 --- a/docs/process.md +++ b/docs/process.md @@ -125,6 +125,7 @@ Make the changes to the code, tests, and documentations as needed by the issue. npm run lint ``` + * **Database schema changes** have a corresponding changelog (see [schema migration](schema-migration.md)) * **All affected tests are passing** on your dev server.
You are more than welcome to also ensure that all tests are passing on your dev server. * **Staging-tested (if need be)**: If your new code might behave differently on a remote server than how it behaves on the dev server, ensure that the affected tests are passing against the updated app running on your own GAE staging server. From 951f9a3ae13183f1c8adec79e11fdf0bb941b604 Mon Sep 17 00:00:00 2001 From: Nicolas Chang <25302138+NicolasCwy@users.noreply.github.com> Date: Tue, 26 Mar 2024 15:40:02 +0800 Subject: [PATCH 10/18] Temporary fix to build gradle --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 041fa3c368a..18998371faa 100644 --- a/build.gradle +++ b/build.gradle @@ -162,7 +162,7 @@ liquibase { url "offline:postgresql?snapshot=liquibase-snapshot.json" } } - runList = project.ext.runList + runList = "main" } tasks.withType(cz.habarta.typescript.generator.gradle.GenerateTask) { From f5067f31e4c2f639e14f5f0c4472c3459a85290a Mon Sep 17 00:00:00 2001 From: Nicolas Chang <25302138+NicolasCwy@users.noreply.github.com> Date: Tue, 26 Mar 2024 16:23:18 +0800 Subject: [PATCH 11/18] Add fix to choose liquibase activity --- build.gradle | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 18998371faa..b48d1356164 100644 --- a/build.gradle +++ b/build.gradle @@ -137,6 +137,10 @@ sourceSets { } } +if (!project.hasProperty("runList")) { + project.ext.set("runList", "main") +} + liquibase { activities { main { @@ -162,7 +166,7 @@ liquibase { url "offline:postgresql?snapshot=liquibase-snapshot.json" } } - runList = "main" + runList = project.ext.runList } tasks.withType(cz.habarta.typescript.generator.gradle.GenerateTask) { From 14f79f33326192ffd89f92f5ba257dc5612333df Mon Sep 17 00:00:00 2001 From: Nicolas <25302138+NicolasCwy@users.noreply.github.com> Date: Thu, 28 Mar 2024 00:23:58 +0800 Subject: [PATCH 12/18] Amend documentation for schema migration Co-authored-by: FergusMok --- docs/schema-migration.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/schema-migration.md b/docs/schema-migration.md index df923b7b5a7..29aadcbaa29 100644 --- a/docs/schema-migration.md +++ b/docs/schema-migration.md @@ -17,9 +17,9 @@ A _change log_ is a file that contains a series of _change sets_ (analagous to a 2. Add changelog file to be included in the `db.changelog-root` as the last entry ## Generating liquibase change logs -1. Delete `postgres-data` to clear any old database schemas +1. Delete the `postgres-data` folder to clear any old database schemas 2. Run `git checkout master` and -3. run the server using `./gradlew serverRun` to generate tables found on master +3. Run the server using `./gradlew serverRun` to generate tables found on master 4. Generate snapshot of database by running `./gradlew liquibaseSnapshot -PrunList=snapshot`, the snapshot will be output to `liquibase-snapshot.json` 4. Checkout your branch and repeat steps 1 and 3 to generate the tables found on your branch 5. Run `./gradlew liquibaseDiffChangeLog -PrunList=diffMain` to generate changeLog to resolve database schema differences From 21d0db53a670064d171b7dc5724b430e9470eb65 Mon Sep 17 00:00:00 2001 From: nicolascwy <25302138+NicolasCwy@users.noreply.github.com> Date: Thu, 28 Mar 2024 00:31:04 +0800 Subject: [PATCH 13/18] Add connection instructions to documentation --- docs/schema-migration.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/schema-migration.md b/docs/schema-migration.md index 29aadcbaa29..2be8f58acbd 100644 --- a/docs/schema-migration.md +++ b/docs/schema-migration.md @@ -9,6 +9,9 @@ Teammates uses _[Liquibase]_(https://docs.liquibase.com/start/home.html), a data ## Liquibase in Teammates _Liquibase_ is made available using the [gradle plugin](https://github.com/liquibase/liquibase-gradle-plugin), providing _liquibase_ functions as tasks. Try `gradle tasks | grep "liquibase"` to see all the tasks available. In teammates, change logs (more in the next section) are written in _XML_. +### Liquibase connection +Amend the `liquibaseDbUrl`, `liquibaseUsername` and `liquibasePassword` in `gradle.properties` to allow the _Liquibase_ plugin to connect your database. + ## Change logs, change sets and change types A _change log_ is a file that contains a series of _change sets_ (analagous to a transaction) which applies _change types_ (actions). You can refer to this page on liquibase on the types of [change types](https://docs.liquibase.com/change-types/home.html) that can be used. From 628f274ec60d819b970ae9fcff0752591291cad1 Mon Sep 17 00:00:00 2001 From: Nicolas Chang <25302138+NicolasCwy@users.noreply.github.com> Date: Mon, 1 Apr 2024 18:10:10 +0800 Subject: [PATCH 14/18] Revert naming changes and update schema process --- docs/schema-migration.md | 10 ++++++---- ...hangelog-2024-03-24.xml => db.changelog-V9.0.0.xml} | 0 2 files changed, 6 insertions(+), 4 deletions(-) rename src/main/resources/db/changelog/{db.changelog-2024-03-24.xml => db.changelog-V9.0.0.xml} (100%) diff --git a/docs/schema-migration.md b/docs/schema-migration.md index 2be8f58acbd..303e6d59b3c 100644 --- a/docs/schema-migration.md +++ b/docs/schema-migration.md @@ -16,15 +16,17 @@ Amend the `liquibaseDbUrl`, `liquibaseUsername` and `liquibasePassword` in `grad A _change log_ is a file that contains a series of _change sets_ (analagous to a transaction) which applies _change types_ (actions). You can refer to this page on liquibase on the types of [change types](https://docs.liquibase.com/change-types/home.html) that can be used. ## How to use Liquibase in Teammates -1. Create an _XML_ change log file in `src/main/resources/db/changelog` naming convention is the `db.changelog-YYYY-MM-DD-entity.xml` e.g `db.changelog-2024-03-24-courses.xml`. -2. Add changelog file to be included in the `db.changelog-root` as the last entry +In Teammates, the release leader will add a folder in `src/main/resources/db/changelog` for changelogs that will used in the next release. +1. Create an _XML_ change log file in `src/main/resources/db/changelog/` naming convention is the `db.changelog---.xml` e.g `db.changelog-courses-2048-migrate-courses.xml`. +2. Amend the `build.gradle` to specify the path to the your changelog file e.g `src/main/resources/db/changelog/V9.0.0/db.changelog-courses-2048-migrate-courses.xml`, then proceed to the next step to update the change log file in the next section. -## Generating liquibase change logs + +## Generating/ Updating liquibase change logs +1. Ensure `diff-main` activity in `build.gradle` is pointing to the latest release changelog 1. Delete the `postgres-data` folder to clear any old database schemas 2. Run `git checkout master` and 3. Run the server using `./gradlew serverRun` to generate tables found on master 4. Generate snapshot of database by running `./gradlew liquibaseSnapshot -PrunList=snapshot`, the snapshot will be output to `liquibase-snapshot.json` 4. Checkout your branch and repeat steps 1 and 3 to generate the tables found on your branch 5. Run `./gradlew liquibaseDiffChangeLog -PrunList=diffMain` to generate changeLog to resolve database schema differences -6. Rename this file to the format `db.changelog-YYYY-MM-DD-entity.xml` and add it as a changelog in `db.changelog-root` diff --git a/src/main/resources/db/changelog/db.changelog-2024-03-24.xml b/src/main/resources/db/changelog/db.changelog-V9.0.0.xml similarity index 100% rename from src/main/resources/db/changelog/db.changelog-2024-03-24.xml rename to src/main/resources/db/changelog/db.changelog-V9.0.0.xml From d952f3368407c07f5b33677b18c385b77c63dee1 Mon Sep 17 00:00:00 2001 From: Nicolas Chang <25302138+NicolasCwy@users.noreply.github.com> Date: Mon, 1 Apr 2024 21:50:58 +0800 Subject: [PATCH 15/18] Remove PR naming requirement --- docs/schema-migration.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/schema-migration.md b/docs/schema-migration.md index 303e6d59b3c..c4fdbf628e7 100644 --- a/docs/schema-migration.md +++ b/docs/schema-migration.md @@ -17,8 +17,8 @@ A _change log_ is a file that contains a series of _change sets_ (analagous to a ## How to use Liquibase in Teammates In Teammates, the release leader will add a folder in `src/main/resources/db/changelog` for changelogs that will used in the next release. -1. Create an _XML_ change log file in `src/main/resources/db/changelog/` naming convention is the `db.changelog---.xml` e.g `db.changelog-courses-2048-migrate-courses.xml`. -2. Amend the `build.gradle` to specify the path to the your changelog file e.g `src/main/resources/db/changelog/V9.0.0/db.changelog-courses-2048-migrate-courses.xml`, then proceed to the next step to update the change log file in the next section. +1. Create an _XML_ change log file in `src/main/resources/db/changelog/` naming convention is the `db.changelog--.xml` e.g `db.changelog-courses-migrate-courses.xml`. +2. Amend the `build.gradle` to specify the path to the your changelog file e.g `src/main/resources/db/changelog/V9.0.0/db.changelog-courses-migrate-courses.xml`, then proceed to the next step to update the change log file in the next section. ## Generating/ Updating liquibase change logs @@ -27,6 +27,6 @@ In Teammates, the release leader will add a folder in `src/main/resources/db/cha 2. Run `git checkout master` and 3. Run the server using `./gradlew serverRun` to generate tables found on master 4. Generate snapshot of database by running `./gradlew liquibaseSnapshot -PrunList=snapshot`, the snapshot will be output to `liquibase-snapshot.json` -4. Checkout your branch and repeat steps 1 and 3 to generate the tables found on your branch -5. Run `./gradlew liquibaseDiffChangeLog -PrunList=diffMain` to generate changeLog to resolve database schema differences +5. Checkout your branch and repeat steps 1 and 3 to generate the tables found on your branch +6. Run `./gradlew liquibaseDiffChangeLog -PrunList=diffMain` to generate changeLog to resolve database schema differences From 60946f977c7f08e365290b46ee0dfc162c1210b4 Mon Sep 17 00:00:00 2001 From: Nicolas Chang <25302138+NicolasCwy@users.noreply.github.com> Date: Mon, 1 Apr 2024 21:53:29 +0800 Subject: [PATCH 16/18] Fix changelog renaming --- src/main/resources/db/changelog/db.changelog-root.xml | 2 +- .../{db.changelog-V9.0.0.xml => db.changelog-v9.0.0.xml} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename src/main/resources/db/changelog/{db.changelog-V9.0.0.xml => db.changelog-v9.0.0.xml} (100%) diff --git a/src/main/resources/db/changelog/db.changelog-root.xml b/src/main/resources/db/changelog/db.changelog-root.xml index 2f3ddab42bf..af5c5e34d7e 100644 --- a/src/main/resources/db/changelog/db.changelog-root.xml +++ b/src/main/resources/db/changelog/db.changelog-root.xml @@ -4,5 +4,5 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd"> - + diff --git a/src/main/resources/db/changelog/db.changelog-V9.0.0.xml b/src/main/resources/db/changelog/db.changelog-v9.0.0.xml similarity index 100% rename from src/main/resources/db/changelog/db.changelog-V9.0.0.xml rename to src/main/resources/db/changelog/db.changelog-v9.0.0.xml From 2672b43b26f5c4b9d4427b887c2bf6e8064b0621 Mon Sep 17 00:00:00 2001 From: nicolascwy <25302138+NicolasCwy@users.noreply.github.com> Date: Thu, 4 Apr 2024 18:32:56 +0800 Subject: [PATCH 17/18] Update schema migration to only be done by release leader --- docs/process.md | 1 - docs/schema-migration.md | 23 +++++++++-------------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/docs/process.md b/docs/process.md index c8480c52bc7..521e00c6c11 100644 --- a/docs/process.md +++ b/docs/process.md @@ -125,7 +125,6 @@ Make the changes to the code, tests, and documentations as needed by the issue. npm run lint ``` - * **Database schema changes** have a corresponding changelog (see [schema migration](schema-migration.md)) * **All affected tests are passing** on your dev server.
You are more than welcome to also ensure that all tests are passing on your dev server. * **Staging-tested (if need be)**: If your new code might behave differently on a remote server than how it behaves on the dev server, ensure that the affected tests are passing against the updated app running on your own GAE staging server. diff --git a/docs/schema-migration.md b/docs/schema-migration.md index c4fdbf628e7..8cee17797cd 100644 --- a/docs/schema-migration.md +++ b/docs/schema-migration.md @@ -4,7 +4,7 @@ # SQL Schema Migration -Teammates uses _[Liquibase]_(https://docs.liquibase.com/start/home.html), a database schema change management solution that enables developers to revise and release database changes to production. If you were to change the schema of any entity, you would have to create a _Liquibase_ changelog which a maintainer will run to keep the production databases schema in sync with the code. +Teammates uses _[Liquibase]_(https://docs.liquibase.com/start/home.html), a database schema change management solution that enables developers to revise and release database changes to production. The maintainers in charge of releases (Release Leader) will be in charge of generating a _Liquibase_ changelog prior to each release to keep the production databases schema in sync with the code. Therefore this section is just for documentation purposes for contributors. ## Liquibase in Teammates _Liquibase_ is made available using the [gradle plugin](https://github.com/liquibase/liquibase-gradle-plugin), providing _liquibase_ functions as tasks. Try `gradle tasks | grep "liquibase"` to see all the tasks available. In teammates, change logs (more in the next section) are written in _XML_. @@ -15,18 +15,13 @@ Amend the `liquibaseDbUrl`, `liquibaseUsername` and `liquibasePassword` in `grad ## Change logs, change sets and change types A _change log_ is a file that contains a series of _change sets_ (analagous to a transaction) which applies _change types_ (actions). You can refer to this page on liquibase on the types of [change types](https://docs.liquibase.com/change-types/home.html) that can be used. -## How to use Liquibase in Teammates -In Teammates, the release leader will add a folder in `src/main/resources/db/changelog` for changelogs that will used in the next release. -1. Create an _XML_ change log file in `src/main/resources/db/changelog/` naming convention is the `db.changelog--.xml` e.g `db.changelog-courses-migrate-courses.xml`. -2. Amend the `build.gradle` to specify the path to the your changelog file e.g `src/main/resources/db/changelog/V9.0.0/db.changelog-courses-migrate-courses.xml`, then proceed to the next step to update the change log file in the next section. - - ## Generating/ Updating liquibase change logs -1. Ensure `diff-main` activity in `build.gradle` is pointing to the latest release changelog -1. Delete the `postgres-data` folder to clear any old database schemas -2. Run `git checkout master` and -3. Run the server using `./gradlew serverRun` to generate tables found on master -4. Generate snapshot of database by running `./gradlew liquibaseSnapshot -PrunList=snapshot`, the snapshot will be output to `liquibase-snapshot.json` -5. Checkout your branch and repeat steps 1 and 3 to generate the tables found on your branch -6. Run `./gradlew liquibaseDiffChangeLog -PrunList=diffMain` to generate changeLog to resolve database schema differences +1. Amend the `build.gradle` to specify the path to the your changelog file e.g `src/main/resources/db/changelog/db.changelog-.xml` +2. Ensure `diff-main` activity in `build.gradle` is pointing to the latest release changelog +3. Delete the `postgres-data` folder to clear any old database schemas +4. Run `git checkout ` and +5. Run the server using `./gradlew serverRun` to generate tables found on branch +6. Generate snapshot of database by running `./gradlew liquibaseSnapshot -PrunList=snapshot`, the snapshot will be output to `liquibase-snapshot.json` +7. Checkout your branch and repeat steps 3 and 5 to generate the tables found on your branch +8. Run `./gradlew liquibaseDiffChangeLog -PrunList=diffMain` to generate changeLog to resolve database schema differences From 78a15df76b4b133d366c02df267a588484ac369a Mon Sep 17 00:00:00 2001 From: nicolascwy <25302138+NicolasCwy@users.noreply.github.com> Date: Thu, 4 Apr 2024 18:47:28 +0800 Subject: [PATCH 18/18] Add section on gradle activities --- docs/schema-migration.md | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/docs/schema-migration.md b/docs/schema-migration.md index 8cee17797cd..a88f49ece0f 100644 --- a/docs/schema-migration.md +++ b/docs/schema-migration.md @@ -15,13 +15,20 @@ Amend the `liquibaseDbUrl`, `liquibaseUsername` and `liquibasePassword` in `grad ## Change logs, change sets and change types A _change log_ is a file that contains a series of _change sets_ (analagous to a transaction) which applies _change types_ (actions). You can refer to this page on liquibase on the types of [change types](https://docs.liquibase.com/change-types/home.html) that can be used. +## Gradle Activities for Liquibase +Activities in Gradle are a way of specifying different variables provided by gradle to the Liquibase plugin. The argument `runList` provided by `-pRunList=` e.g `./gradlew liquibaseSnapshot -PrunList=snapshot` is used to specify which activity to be used for the Liquibase command. In this case the `liquibaseSnapshot` command is run using the `snapshot` activity. + +Here is a brief description of the activities defined for Liquibase +1. Main: The default activity used by Liquibase commands and is used for running changelogs against a database. This is used by default if a `runList` is not defined +2. Snapshot: Used to specify output format and name for snapshots i.e JSON +3. diffMain: Specify the reference and the target database to generate changelog that contains operations to update reference database to the state of the target database. i.e the reference is the JSON file generated by the snapshot command, this can be replaced with a live database which is used as reference. + ## Generating/ Updating liquibase change logs -1. Amend the `build.gradle` to specify the path to the your changelog file e.g `src/main/resources/db/changelog/db.changelog-.xml` -2. Ensure `diff-main` activity in `build.gradle` is pointing to the latest release changelog -3. Delete the `postgres-data` folder to clear any old database schemas -4. Run `git checkout ` and -5. Run the server using `./gradlew serverRun` to generate tables found on branch -6. Generate snapshot of database by running `./gradlew liquibaseSnapshot -PrunList=snapshot`, the snapshot will be output to `liquibase-snapshot.json` -7. Checkout your branch and repeat steps 3 and 5 to generate the tables found on your branch -8. Run `./gradlew liquibaseDiffChangeLog -PrunList=diffMain` to generate changeLog to resolve database schema differences +1. Ensure `diff-main` activity in `build.gradle` is pointing to the latest release changelog `src/main/resources/db/changelog/db.changelog-.xml` +2. Delete the `postgres-data` folder to clear any old database schemas +3. Run `git checkout ` and +4. Run the server using `./gradlew serverRun` to generate tables found on branch +5. Generate snapshot of database by running `./gradlew liquibaseSnapshot -PrunList=snapshot`, the snapshot will be output to `liquibase-snapshot.json` +6. Checkout your branch and repeat steps 2 and 4 to generate the tables found on your branch +7. Run `./gradlew liquibaseDiffChangeLog -PrunList=diffMain` to generate changeLog to resolve database schema differences