Skip to content

WIP: Implement db model in go #8967

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed

WIP: Implement db model in go #8967

wants to merge 10 commits into from

Conversation

easyCZ
Copy link
Member

@easyCZ easyCZ commented Mar 27, 2022

Description

Prototype implementation of DB acccess model in go

Model code-generation

Initial model generation can be done automatically with

go install github.com/smallnest/gen@latest
gen \
	--sqltype=mysql \
	--connstr "<CONN_STRING>" \
	--database gitpod \
	--gorm \
	--table <table_name> \
	--overwrite \
	--json

which will reflect over the table and generate a model, super handy. However, manual adjustments need to be made to apply our custom transformers and custom data type handling.

Custom data types

In many places, we use custom data types with transformers. Below is a rough summary of how to deal with these.

Varchar with a ISO 8601 timestamp
This one is a bit nasty, as it doesn't use the native datetime type so GORM can't do automatic inference. We use it primary for creationTime fields (lastModified use DATETIME). However, we can deal with this by typing our custom timestamp fields appropriately. See VarcharTime implementation.

Simple JSON column
This one can be addressed as a combination of existing GORM JSON data type extensions, and pointer field which makes it nullable. Using gorm.io/datatypes has the nice benefit of getting access to JsonPath extractors which allow us to query the JSON structure slightly better.

Empty String to Undefined
Again, can be solved with a custom type alias which implements the Scanner and Valuer interfaces.

UUIDs
Using github.com/google/uuid makes UUIDs work out of the box for us.

Encrypted fields
We can extend fields to be encrypted when serialised and deserialized using https://gorm.io/docs/data_types.html#Value-based-on-Context

Related Issue(s)

Fixes #

How to test

  1. Port forward the preview DB with k port-forward mysql-0 3306:3306
  2. Run go tests with cd components/gitpod-db-go && go test ./...

Release Notes

Documentation

/uncc

@roboquat
Copy link
Contributor

@easyCZ: Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@stale
Copy link

stale bot commented Apr 8, 2022

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the meta: stale This issue/PR is stale and will be closed soon label Apr 8, 2022
@stale stale bot removed the meta: stale This issue/PR is stale and will be closed soon label Apr 9, 2022
@roboquat roboquat added size/L and removed size/XL labels Apr 9, 2022
@roboquat roboquat added size/XL and removed size/L labels Apr 10, 2022
@roboquat roboquat added size/XXL and removed size/XL labels Apr 11, 2022
@stale
Copy link

stale bot commented Apr 23, 2022

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the meta: stale This issue/PR is stale and will be closed soon label Apr 23, 2022
@easyCZ easyCZ removed the meta: stale This issue/PR is stale and will be closed soon label Apr 25, 2022
@stale
Copy link

stale bot commented May 25, 2022

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the meta: stale This issue/PR is stale and will be closed soon label May 25, 2022
@stale stale bot closed this May 31, 2022
@easyCZ easyCZ deleted the mp/go-db-model branch December 8, 2022 17:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants