-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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 helper to assert that tables are logically identical #42658
Comments
@akshayjshah I've went through similar problems when trying to vendorize cockroachdb using modules for: https:/lopezator/sqlfmt I've updated it recently to support v19.2.0 statements formatting, and had to add a few replaces in there, specifically:
https:/lopezator/sqlfmt/blob/master/go.mod#L91-L95 Btw, your approach to migrations is very interesting! Do you have some code uploaded somewhere? Maybe I could help. We are using https:/lopezator/migrator for our db migrations and we've been thinking about testing migrations somehow in here: lopezator/migrator#7 Your approach give us more ideas to think of and research, so thank you for opening it! |
We have marked this issue as stale because it has been inactive for |
Is your feature request related to a problem? Please describe.
Like many people, we need a way to manage SQL migrations in our codebase. Each migration includes an upward migration statement (
ALTER TABLE foo ADD COLUMN bar INT
) and a revert statement (ALTER TABLE foo DROP COLUMN bar
). In CI, we run tests to ensure that each migration is idempotent. Those tests set up the table(s), query for the initial schema (SHOW CREATE TABLE foo
), apply the migration, roll it back, query the schema again, and then assert that the twoCREATE TABLE
statements are the same.In many cases, the two schemas aren't exactly identical. For example, dropping and re-adding a column changes ordering in a few places. We'd like a principled, correct way to determine that two
CREATE TABLE
statements describe logically-equivalent schemas.Describe the solution you'd like
Our code happens to be written in Go. I'd love it if CRDB exposed an externally-consumable package of test helpers that included a function like this.
Describe alternatives you've considered
I've tried to use
github.com/cockroachdb/cockroach/pkg/sql/parser
, but it's not installable with modules:(Migrating the whole CockroachDB project to modules is tracked in #35426.) Even if I could install the parser in my project, it takes quite a bit of code to make the assertion I'd like. Code using the parser is also somewhat difficult to follow unless you're familiar with the parser package: I'm sure I'd have trouble understanding my own code a year from now.
Instead, we've settled on the pragmatic-but-gross approach of string manipulation: with a bunch of parens-counting and comma-splitting operations, we chop the SQL up into "parts", drop the
FAMILY
definitions, and assert that the remaining sets of "parts" are identical. It's short and fairly easy to read, but subtly wrong in a hundred ways.Additional context
Current string hackery that I'd like to replace:
The text was updated successfully, but these errors were encountered: