sourcegraph/migrations
William Bezuidenhout 7dbfb87a11
repos: log corruption events per repo (#45667)
* return a bool for checkMaybeCorrupt

* store plumbing for SetCorruptedAt

* debug

* move to markIfCorrupted + test

* add additional checks for repo corruption

* add test cases for server repo corruption detection

* add corrupted_at field for sql mapping

* fix err not being returned

* ensure corrupted at gets mapped

* test cases to ensure corrupted at gets cleared

* add migration

* update types for CorrruptedAt

* fix sql query and remove field for now

* run go generate

* Update internal/database/gitserver_repos_test.go

Co-authored-by: Erik Seliger <erikseliger@me.com>

* Update internal/database/gitserver_repos_test.go

Co-authored-by: Erik Seliger <erikseliger@me.com>

* Update cmd/gitserver/server/cleanup.go

Co-authored-by: Erik Seliger <erikseliger@me.com>

* review comments

* review feedback

- check for err
- remove reason
- set default timestamp to 0 value

* tests pass 🎉

* reset stiched migration graph

* go gen magic

* move docstring

* check for err in corruption check

* fix tests 🤞

* add corruption_lod column and rename migration

* fix migration scripts

* go gen

* change corruption_log column type to jsonb

* update db mocks

* add logCorruption method

- log repo corruption into a JSON array
- limit repo corruption log to 10 elements
- db tests

* fix wording and remove test

* add CorruptionLog to ignored fields

* add more ignores

* remove comment

* review feedback

* log git corrupt stderr
* cap corruption reason to 1mb
* rename CorruptionLog -> CorruptionLogs
* rename corruption_log -> corruption_logs

* Update internal/database/gitserver_repos_test.go

Co-authored-by: Thorsten Ball <mrnugget@gmail.com>

* Fixes

Fix rename in cmp IgnoreFields
Fix malformed SQL

* fix test

* add comment to trigger sonar cloud analysis

* review comments

append to json array in postgres

* add comment about json query

* comments and fixes

* Update cmd/gitserver/server/server_test.go

Co-authored-by: Thorsten Ball <mrnugget@gmail.com>

* Fix indentation

* review feedback

- Make corrupted_at column nullable
- Check err returned from LogCorruption call
- Add condition to make sure we only log more
  repo corruptions if there repo isn't currently
  corrupt.
- Run sg generate

* Fix test

- update corrupted_at also when using GitserverRepo store update
- clear corrupted_at status during batch sync update

* Update cmd/gitserver/server/server_test.go

Co-authored-by: Thorsten Ball <mrnugget@gmail.com>

* store plumbing for SetCorruptedAt

* debug

* move to markIfCorrupted + test

* add additional checks for repo corruption

* add test cases for server repo corruption detection

* add migration

* update types for CorrruptedAt

* fix sql query and remove field for now

* review comments

* review feedback

- check for err
- remove reason
- set default timestamp to 0 value

* reset stiched migration graph

* add corruption_lod column and rename migration

* go gen

* add logCorruption method

- log repo corruption into a JSON array
- limit repo corruption log to 10 elements
- db tests

* fix wording and remove test

* Fix more indentation 🤦🏻

* review feedback: update test

* run sg generate again

* test fixes

- warn instead of return an error during cleanup
- delete repos so that the name can be reused

* fix bug

- repo was corrupt but did not set the reason so the repo never got
  cleaned up

* review feedback

- sql query indentation

* review feedback

- use assert module

* do not clear corruption status on err

* add test for LastError and corruptedAt

- fix SQL issue on LastError

Co-authored-by: Erik Seliger <erikseliger@me.com>
Co-authored-by: Thorsten Ball <mrnugget@gmail.com>
2023-01-09 17:24:32 +02:00
..
codeinsights insights: add base for data retention worker (#46082) 2023-01-04 14:07:25 +00:00
codeintel codeintel: Clean up triggers (#45681) 2022-12-15 08:58:38 -06:00
frontend repos: log corruption events per repo (#45667) 2023-01-09 17:24:32 +02:00
embed.go chore: Simplify embed files (#30248) 2022-01-27 10:49:43 -06:00
README.md doc: note that go generate scripts are deprecated (#35270) 2022-05-11 10:11:54 -07:00

Postgres Migrations

The children of this directory contain migrations for each Postgres database instance:

  • frontend is the main database (things should go here unless there is a good reason)
  • codeintel is a database containing only processed LSIF data (which can become extremely large)
  • codeinsights is a database containing only Code Insights time series data

The migration path for each database instance is the same and is described below. Each of the database instances described here are deployed separately, but are designed to be overlayable to reduce friction during development. That is, we assume that the names in each database do not overlap so that the same connection parameters can be used for both database instances.

Migrating up and down

Up migrations will happen automatically in development on service startup. In production environments, they are run by the migrator instance. You can run migrations manually during development via sg:

  • sg migration up runs all migrations to the latest version
  • sg migration up -db=frontend -target=<version> runs up migrations (relative to the current database version) on the frontend database until it hits the target version
  • sg migration undo -db=codeintel runs one down migration (relative to the current database version) on the codeintel database

Adding a migration

IMPORTANT: All migrations must be backwards-compatible, meaning that existing code must be able to operate successfully against the new (post-migration) database schema. Consult Writing database migrations in our developer documentation for additional context.

To create a new migration file, run the following command.

$ sg migration add -db=<db_name> <my_migration_name>
Migration files created
 Up query file: ~/migrations/codeintel/1644260831/up.sql
 Down query file: ~/migrations/codeintel/1644260831/down.sql
 Metadata file: ~/migrations/codeintel/1644260831/metadata.yaml

This will create an up and down pair of migration files (whose path is printed by the following command). Add SQL statements to these files that will perform the desired migration. After adding SQL statements to those files, update the schema doc via go generate ./internal/database/ (or regenerate everything via sg generate).

To pass CI, you'll additionally need to:

  • Ensure that your new migrations run against the current Go unit tests
  • Ensure that your new migrations can be run up, then down, then up again (idempotency test)
  • Ensure that your new migrations do not break the Go unit tests published with the previous release (backwards-compatibility test)

Reverting a migration

If a reverted PR contains a DB migration, it may still have been applied to Sourcegraph.com, k8s.sgdev.org, etc. due to their rollout schedules. In some cases, it may also have been part of a Sourcegraph release. To fix this, you should create a PR to revert the migrations of that commit. The sg migration revert <commit> command automates all the necessary changes the migration definitions.