Skip to content

Commit cc9be6a

Browse files
authored
Merge pull request #22604 from owen-mc/docs/db-upgrade-downgrade
Docs: Update docs on db upgrade and downgrade
2 parents c7f49f2 + 9045b89 commit cc9be6a

1 file changed

Lines changed: 76 additions & 36 deletions

File tree

Lines changed: 76 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ This document explains how to write or generate those scripts.
1313
2. Run `misc/scripts/prepare-db-upgrade.sh --lang <lang>`. This will generate skeleton upgrade/downgrade scripts in the appropriate directories.
1414
3. Fill in the details in the two `upgrade.properties` files that it generated, and add any required upgrade queries.
1515

16+
The generated directory names are hashes of the old and new `.dbscheme` files. If the
17+
schema changes after generating the scripts, delete the generated directories and run the
18+
script again so that the directory names and schema snapshots use the correct hashes.
19+
1620
It may be helpful to look at some of the existing upgrade/downgrade scripts, to see how they work.
1721

1822
## Details
@@ -25,26 +29,52 @@ compatibility: partial
2529
some_relation.rel: run some_relation.qlo
2630
```
2731

28-
The `description` field is a textual description of the aim of the upgrade.
32+
The `description` field is a textual description of the aim of the step. Describe the
33+
operation in its actual direction: for example, a downgrade that removes a newly added
34+
table should say that it removes the table.
35+
36+
The `compatibility` field takes one of four values. In these definitions, the source schema is
37+
`old.dbscheme`, and the target schema is the other `.dbscheme` in the script directory. Thus,
38+
the source is the older schema for an upgrade and the newer schema for a downgrade.
39+
40+
* **full**: query results from the transformed database will be identical to results from a database built with the target version of the toolchain.
41+
42+
* **backwards**: the step is safe and preserves the meaning of the source database, but features provided by the target query and library packs may not work correctly on the transformed database.
2943

30-
The `compatibility` field takes one of four values:
44+
* **partial**: the step is safe and preserves the meaning of the source database, but rebuilding the database with the target version of the toolchain would produce better results.
3145

32-
* **full**: results from the upgraded snapshot will be identical to results from a snapshot built with the new version of the toolchain.
46+
* **breaking**: the step is unsafe and will prevent certain target queries from working.
3347

34-
* **backwards**: the step is safe and preserves the meaning of the old database, but new features may not work correctly on the upgraded snapshot.
48+
Choose compatibility independently for the upgrade and downgrade, because the two directions
49+
may preserve different amounts of information.
3550

36-
* **partial**: the step is safe and preserves the meaning of the old database, but you would get better results if you rebuilt the snapshot with the new version of the toolchain.
51+
The `some_relation.rel` line(s) are the actions required to transform the database in the
52+
direction of the step. Upgrade and downgrade directories use the same file name and command
53+
syntax, even though a file in a downgrade directory describes a downgrade. Diff `old.dbscheme`
54+
against the target `.dbscheme` in the generated directory to determine which actions are
55+
needed.
3756

38-
* **breaking**: the step is unsafe and will prevent certain queries from working.
57+
No action is needed for a relation added by the target schema if it should be empty in the
58+
transformed database. A missing relation is treated as empty, so do not add a `.rel` line just
59+
to create an empty file. If extraction would populate the new relation, however, the upgrade
60+
is not `full`: it will usually be `backwards`, because queries using the new relation may have
61+
degraded results on upgraded databases.
3962

40-
The `some_relation.rel` line(s) are the actions required to perform the database upgrade. Do a diff on the new vs old `.dbscheme` file to get an idea of what they have to achieve. Sometimes you won't need any upgrade commands – this happens when the dbscheme has changed in "cosmetic" ways, for example by adding/removing comments or changing union type relationships, but still retains the same on-disk format for all tables; the purpose of the upgrade script is then to document the fact that it's safe to replace the old dbscheme with the new one.
63+
A relation that exists in `old.dbscheme` but not in the target schema should normally be
64+
deleted explicitly with `relation.rel: delete` so that the transformation does not leave
65+
obsolete data behind.
66+
67+
Sometimes no commands are needed because the schema changed only cosmetically, for example
68+
by adding or removing comments or changing union type relationships without changing the
69+
on-disk format. The script then documents that it is safe to replace the old schema with the
70+
new one.
4171

4272
Ideally, your downgrade script will perfectly revert the changes applied by the upgrade script, such that applying the upgrade and then the downgrade will result in the same database you started with.
4373

44-
Some typical upgrade commands look like this:
74+
Some typical upgrade or downgrade commands look like this:
4575

4676
```
47-
// Delete a relation that has been replaced in the new scheme
77+
// Delete a relation that does not exist in the target schema
4878
obsolete.rel: delete
4979
5080
// Create a new version of a table by applying an expression (using a simple
@@ -83,7 +113,7 @@ To test the upgrade script, run:
83113
codeql test run --search-path=<old-extractor-pack> --search-path=<codeql-root> <test-dir>
84114
```
85115

86-
Where `<old-extractor-pack>` is an extractor pack containing the old extractor and dbscheme that pre-date your changes, `<test-dir>` is the directory containing the qltests for your language, and `<codeql-root>` is the root directory directory of the `github/codeql` clone that contains `<test-dir>`. This will run the tests using an old extractor, and the test databases will all be upgraded in place using your new upgrade script.
116+
Where `<old-extractor-pack>` is an extractor pack containing the old extractor and dbscheme that pre-date your changes, `<test-dir>` is the directory containing the qltests for your language, and `<codeql-root>` is the root directory of the `github/codeql` clone that contains `<test-dir>`. This will run the tests using an old extractor, and the test databases will all be upgraded in place using your new upgrade script.
87117

88118
To test the downgrade script, create an extractor pack that includes your new dbscheme and extractor changes. Then checkout the `main` branch of `codeql` (i.e. a branch that does not include your changes), and run:
89119

@@ -107,43 +137,53 @@ You might also choose to test with a real-world database.
107137

108138
5. Verify that your queries produced sensible results.
109139

110-
#### Doing the upgrade manually
140+
#### Creating the scripts manually
111141

112-
To create the upgrade directory manually, without using `prepare-db-upgrade.sh`:
142+
To create both directions manually, without using `prepare-db-upgrade.sh`, run the following
143+
commands from the repository root. First set `lang` to the language directory and `schema_file`
144+
to the repository-relative path of its `.dbscheme` file. For example, for Go:
113145

114-
1. Get a hash of the old `.dbscheme` file from `main` (i.e. from just before your changes). You can do this by checking out the code prior to your changes and running `git hash-object ql/lib/<mylang>.dbscheme`
146+
```sh
147+
lang=go
148+
schema_file=go/ql/lib/go.dbscheme
149+
```
115150

116-
2. Go back to your branch and create an upgrade directory with that hash as its name, for example:
117-
```
118-
mkdir ql/lib/upgrades/454f1e15151422355049dc4f1f0486a03baeffef
119-
```
151+
1. Get the hashes of the old `.dbscheme` from `main` and the new `.dbscheme` from
152+
your branch. For example:
120153

154+
```sh
155+
old_hash=$(git show "main:$schema_file" | git hash-object --stdin)
156+
new_hash=$(git hash-object "$schema_file")
157+
```
121158

122-
3. Copy the old `.dbscheme` file to that directory, using the name old.dbscheme.
159+
2. Create the upgrade directory using the old hash and the downgrade directory using the
160+
new hash:
123161

124-
```
125-
cp ql/lib/<mylang>.dbscheme ql/lib/upgrades/454f1e15151422355049dc4f1f0486a03baeffef/old.dbscheme
126-
```
127-
128-
4. Put a copy of your new `.dbscheme` file in that directory and create an `upgrade.properties` file (as described above).
162+
```sh
163+
upgrade_dir="$lang/ql/lib/upgrades/$old_hash"
164+
downgrade_dir="$lang/downgrades/$new_hash"
165+
mkdir -p "$upgrade_dir" "$downgrade_dir"
166+
```
129167

130-
#### Doing the downgrade manually
168+
3. Populate the upgrade directory. Here, `old.dbscheme` is the schema from `main`, and
169+
the other `.dbscheme` file is the new target schema:
131170

132-
The process is similar for downgrade scripts, but there is a reversal in terminology: your **new** dbscheme will now be the one called `old.dbscheme`.
171+
```sh
172+
git show "main:$schema_file" > "$upgrade_dir/old.dbscheme"
173+
cp "$schema_file" "$upgrade_dir/$(basename "$schema_file")"
174+
```
133175

134-
1. Get a hash of your new `.dbscheme` file, with `git hash-object ql/lib/<mylang>.dbscheme`
176+
4. Populate the downgrade directory in the opposite direction. For a downgrade, the new
177+
schema is called `old.dbscheme`, because it is the schema before the downgrade step:
135178

136-
2. Create a downgrade directory with that hash as its name, for example:
137-
```
138-
mkdir downgrades/9fdd1d40fd3c3f8f9db8fabf5a353580d14c663a
139-
```
140-
141-
3. Copy your new `.dbscheme` file to that directory, using the name `old.dbscheme`.
142-
```
143-
cp ql/lib/<mylang>.dbscheme ql/lib/upgrades/454f1e15151422355049dc4f1f0486a03baeffef/old.dbscheme
144-
```
179+
```sh
180+
cp "$schema_file" "$downgrade_dir/old.dbscheme"
181+
git show "main:$schema_file" > "$downgrade_dir/$(basename "$schema_file")"
182+
```
145183

146-
4. Put a copy of the `.dbscheme` from `main` in that directory and create an `upgrade.properties` file that performs the downgrade (as described above).
184+
5. Create an `upgrade.properties` file in each directory. The file in the upgrade directory
185+
describes the forward transformation, while the file in the downgrade directory describes
186+
the reverse transformation.
147187

148188
### Debugging your scripts
149189

0 commit comments

Comments
 (0)