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

Add YDB support #592

Merged
merged 5 commits into from
Oct 25, 2023
Merged

Add YDB support #592

merged 5 commits into from
Oct 25, 2023

Conversation

gingersamurai
Copy link
Contributor

Hello! I am part of YDB application team and we added YDB support to the Goose. We would greatly appreciate any feedback on it. Thanks!

@asmyasnikov
Copy link
Contributor

@mfridman Can you see this PR?

@mfridman
Copy link
Collaborator

mfridman commented Oct 6, 2023

Yep, just getting back into the swing of things (long few weeks of travelling).

I see there are a few PR's floating around, which one should I take a look at? I presume this one.

Thanks for putting up a PR, and apologize for the delay.

@@ -219,6 +220,7 @@ Examples:
goose mssql "sqlserver://user:password@dbname:1433?database=master" status
goose clickhouse "tcp://127.0.0.1:9000" status
goose vertica "vertica://user:password@localhost:5433/dbname?connection_load_balance=1" status
goose ydb "grpcs://localhost:2135/local?go_query_mode=scripting&go_fake_tx=scripting&go_query_bind=declare,numeric" status
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you drop a link to the docs or a comment on what these are?

go_query_mode=scripting
go_fake_tx=scripting
go_query_bind=declare,numeric

Copy link
Contributor

@asmyasnikov asmyasnikov Oct 23, 2023

Choose a reason for hiding this comment

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

go_query_mode - it is required because YDB have different methods for different types of queries (DDL, DML, etc.). Docs
go_fake_tx - special mode for external tools like goose, which requires a transactional DDL. Release in v3.44.0
go_query_bind=declare,numeric - released in v3.44.0 and published in article database/sql bindings for YDB in Go

)

const (
YDB_IMAGE = "cr.yandex/yc/yandex-docker-local-ydb"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Any chance we can pull this from DockerHub?

Copy link
Contributor

Choose a reason for hiding this comment

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

We have an different registry ghcr.io/ydb-platform/local-ydb (official github registry).
Is it ok for you?
If ok I will change a registry in next commit

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, that'd be great. It makes it a bit easier to maintain if the images are pulled from the same 1-2 registries. If those are down so be it.

Nothing against other registries, but if we have many different ones it increases the chances of flakes.

Copy link
Contributor

Choose a reason for hiding this comment

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

Switched docker image to ghcr.io/ydb-platform/local-ydb


func (c *Ydb) CreateTable(tableName string) string {
return fmt.Sprintf(`
CREATE TABLE %s (
Copy link
Collaborator

@mfridman mfridman Oct 23, 2023

Choose a reason for hiding this comment

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

Where possible, we try to add NOT NULL and a default. Would something like this make sense for ydb?

CREATE TABLE goose_db_version (
    version_id Uint64 NOT NULL,
    is_applied Bool NOT NULL,
    tstamp Timestamp DEFAULT CURRENT_TIMESTAMP, 
    PRIMARY KEY (version_id)
);

Nits,

  1. Remove the trailing semicolons from all queries
  2. Use q=<query> and return fmt.Sprintf(q, tableName) pattern like all the other dialect queries (this makes it easier to do maintain, do large search/replace, etc.)

Copy link
Contributor

Choose a reason for hiding this comment

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

YDB not supports DEFAULT and NOT NULL constraints for non-pk columns in latest release (23.3). We will do that in one of the next release. With back-compatible of course. But in current release we can do autocomplete tstamp field explicitly on insert stage with built-in function CurrentUtcTimestamp()

Copy link
Contributor

Choose a reason for hiding this comment

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

Fixed in 3dba2c8 issues about trailing semicolons and patern fmt.Sprintf(q, tableName)

}

func (c *Ydb) ListMigrations(tableName string) string {
return fmt.Sprintf(`--!syntax_pg
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you explain the --!syntax_pg ? I presume this is some hint if the database supports it, otherwise it's a noop?

Copy link
Contributor

@asmyasnikov asmyasnikov Oct 23, 2023

Choose a reason for hiding this comment

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

Native YQL in YDB not supported next statement

SELECT version_id, is_applied
FROM %s 
ORDER BY tstamp DESC

where ORDER BY column (tstamp) not present in results. It provokes undefined behaviour. And in YDB is too difficult to fix because some intermediate results are materialized in processing stage and missed tstamp column meta for ordering final results.

For garanted behaviour need to add tstamp to results like

SELECT version_id, is_applied, tstamp
FROM %s 
ORDER BY tstamp DESC

But this ad-hoc brokes in database/sql scanner - because core of goose put only two destination values in this line.
I think it is not good implicit contract for get last migration by timestamp field/column. But I understand - this is legacy.
Thats why I maked another ad-hoc for this trouble with --!syntax_pg flag.

We have a milestone for support postgresql syntax. In this mode (--!syntax_pg) a query

SELECT version_id, is_applied
FROM %s 
ORDER BY tstamp DESC

works fine. Because in postgresql-compatible mode we are processing queries with postgresql parser. And in this level we allowed some ttrade-off's for proccessing pg-queries under ydb core

Copy link
Contributor

Choose a reason for hiding this comment

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

added short comment into code about --!syntax_pg

@asmyasnikov
Copy link
Contributor

@mfridman I fixed all your issues. Can you see latest version of code in PR?

@@ -50,6 +51,9 @@ test-e2e-clickhouse:
test-e2e-vertica:
go test $(GO_TEST_FLAGS) ./tests/vertica

test-e2e-ydb:
go test $(GO_TEST_FLAGS) -parallel=1 ./tests/e2e -dialect=ydb
Copy link
Collaborator

Choose a reason for hiding this comment

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

Curious, why need to set -parallel=1?

Copy link
Contributor

Choose a reason for hiding this comment

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

Unfortunately, ydb container requires a lot of resources, and on a small machine, high parallelism with running many containers can provoke out-of-memory errors or unexpected behaviour. Because db docker image created for tests only, not for production. In production we have a distributed cluster of ydb nodes and perfect ACID guarantees

}
}
// Fetch port assigned to container
dsn := fmt.Sprintf("grpc://%s:%s/%s",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah interesting, this uses grpc. Shuld try https://connect.build ;)

}

func (c *Ydb) ListMigrations(tableName string) string {
// "--!syntax_pg" enables query processing with PostgreSQL-compatible syntax.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Perfect, thank you!

Copy link
Collaborator

@mfridman mfridman left a comment

Choose a reason for hiding this comment

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

Lgtm. Thanks for contributing a database 🎉

@mfridman mfridman merged commit 0c243fb into pressly:master Oct 25, 2023
9 checks passed
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.

3 participants