Skip to content
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

Added support for Postgresql database #412

Merged
merged 6 commits into from
May 17, 2024
Merged

Added support for Postgresql database #412

merged 6 commits into from
May 17, 2024

Conversation

TimShi
Copy link
Contributor

@TimShi TimShi commented May 9, 2024

Breaking Change

Refactored data package to support postgresql and cockroachdb. This includes breaking changes.

  1. Introduced postgresql package.
  2. The cockroach package is now a package under the postgresql package as a postgresql extension (cockroach db supports the postgresql wire protocol).
  3. Removed database specific packages from the data package. Code that uses db specific driver packages are moved into sub packages for that database. i.e. types/pqx.
  4. The db connection property is moved to data.db from data.cockroach

Other Changes

  1. Updated db example, added example for translating unique constraint violation.
  2. Updated tenancy and opa tests to have coverage for both postgresql and cockroach db

Type of Change

  • Bug Fix
  • New Feature
  • Breaking Change
  • Refactor
  • Documentation
  • Other (please describe)

Checklist

  • I have read the contributing guidelines
  • Existing issues have been referenced (where applicable)
  • I have verified this change is not present in other open pull requests
  • Functionality is documented
  • All code style checks pass
  • New code contribution is covered by automated tests
  • All new and existing tests pass

Copy link

github-actions bot commented May 9, 2024

PR Coverage Summary
Changed Statements 79
Covered Statements 56
Test Coverage 70.9%

PR Verification Succeeded: Coverage >= 70%

@TimShi TimShi force-pushed the feature/postgres branch from f622fe6 to 5ac7faa Compare May 9, 2024 20:10
…includes breaking changes.

1. Updated db example, added example for translating unique constraint violation.
2. Refactored db package (breaking change):
  a. Introduced postgresql package.
  b. The cockroach package is moved under the postgresql package as a postgresql extension.
  c. Removed database specific packages from the data package. Code that uses db specific driver packages are moved into sub packages for that database. i.e. types/pqx.
  d. The db connection property is moved to data.db from data.cockroach
3. Updated tenancy and opa tests to have coverage for both postgresql and cockroach db
@TimShi TimShi force-pushed the feature/postgres branch from 5ac7faa to cbead8e Compare May 9, 2024 22:10
@TimShi TimShi closed this May 10, 2024
@TimShi TimShi reopened this May 10, 2024
Comment on lines 12 to 18
var Module = &bootstrap.Module{
Name: "cockroach",
Precedence: bootstrap.DatabasePrecedence,
Options: []fx.Option{
fx.Provide(newAnnotatedGormDbCreator()),
},
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to use Modules field instead of having postgresql.Module in Use()? the difference is scenarios that Module is directly used, e.g. in tests

Comment on lines 32 to 33
GormConfigurerGroup = "gorm_config"
DatabaseCreatorGroup = "db_creator"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually keep one fx group per top-level package. Can we use gorm_config grpi[ to inject DB creators?

Comment on lines -74 to -96

//nolint:errorlint
func (t WebDataErrorTranslator) dataIntegrityErrorWithStatusCode(_ context.Context, err error, sc int) error {
switch err.(DataError).RootCause().(type) {
case *pgconn.PgError, *pq.Error:
default:
return NewErrorWithStatusCode(err.(DataError), sc)
}
msg := "duplicate keys"
matches := dataIntegrityRegexp.FindStringSubmatch(err.Error())
for i, name := range dataIntegrityRegexp.SubexpNames() {
if i >= len(matches) {
break
}
if name == "value" {
if matches[i] != "" {
msg = fmt.Sprintf("duplicate value: %s", matches[i])
}
break
}
}
return NewErrorWithStatusCode(err.(DataError), sc).WithMessage(msg)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this is moved to example, which would require all services to include this code. Could we also move it to postgresql package?

If service want to override the behavior, it could add translator with higher order

Comment on lines 205 to 209
// Use WithCause because we want to modify message but preserve the cause
CodedError: e.CodedError.WithCause(e.CodedError.Cause(), msg, args...),
details: e.details,
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this actually changed unwrapping behaviour, might causes behaviour changes on error translators.

Should we do this in errorutils.CodedError instead of here?

Comment on lines 36 to 37
BindDataProperties,
BindDatabaseProperties,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those two properties are defined and provided in same package, can we make DatabaseProperties as a field of DataProperties? Is there any case where we need to bind one properties but not the other one, in tests?

//
// SPDX-License-Identifier: Apache-2.0

package postgresql
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty file, should be deleted

di := &testDI{}
test.RunTest(context.Background(), t,
apptest.Bootstrap(),
dbtest.WithDBPlayback("testdb"),
dbtest.WithDBPlayback("testdb", dbtest.DBPort(26257)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like the copyist data need to be moved to this package too. (it's still under types/testdata now)

if valMsg != "" {
msg = fmt.Sprintf("%s; %s", msg, valMsg)
}
return e.WithCause(e.Cause(), msg)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with the other change in DataError, could be e.WithMessage(msg) now

@TimShi TimShi force-pushed the feature/postgres branch from cbfdba8 to 89470c8 Compare May 15, 2024 20:17
@TimShi TimShi merged commit df729b5 into main May 17, 2024
1 check passed
@TimShi TimShi deleted the feature/postgres branch May 17, 2024 13:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants