spicedb icon indicating copy to clipboard operation
spicedb copied to clipboard

Need a way to compare schemas that is formatting agnostic

Open theronic opened this issue 1 year ago • 12 comments

Problem Statement

If you call .readSchema after calling .writeSchema (via GRPC), Spice returns a schema string with reordered definitions that does not match the string that was written. I want to compare if my schema is up-to-date without writing a parser for the SpiceDB schema language.

Solution Brainstorm

Return the same schema text string that was written via .writeSchema.

Alternatively, is there a "compareSchema" function somewhere?

theronic avatar Mar 15 '24 17:03 theronic

@theronic https://github.com/authzed/spicedb/tree/main/pkg/diff has a diff package which can be used after parsing the schema

SpiceDB always returns the normalized, ordered schema and that won't be changing; we don't store the actual text

josephschorr avatar Mar 15 '24 17:03 josephschorr

Thanks @josephschorr, but my application is not written in Golang. Can diff be exposed via gRPC? If it's part of zed CLI, would have to shell out, or add a Golang wrapper in my Clojure application.

theronic avatar Mar 15 '24 17:03 theronic

@theronic What, exactly, are you trying to do? What is your intention around if the schema has changed?

josephschorr avatar Mar 15 '24 17:03 josephschorr

I'm trying to compare if the schema in prod SpiceDB instance differs from the Spice schema I have on-disk, e.g. when deploying a new version of app. At this stage, just convenient to check that I know I'm querying the right schema.

I guess I'll need schema migrations anyway 🤔

theronic avatar Mar 15 '24 17:03 theronic

We could look into adding a SchemaDiff API, but you could just also call WriteSchema; if there are no changes, it'll no-op

josephschorr avatar Mar 15 '24 17:03 josephschorr

Thanks. How do you keep track of Spice schema versioning?

Practical example similar to mismatched database schema: given a set of schema migrations, prod might think it has v3 of Spice Schema, but since schema is stateful, a different schema could have been written to Spice, so I'd like to ensure that the current production schema matches the previous migration that was run before running additional migrations.

theronic avatar Mar 15 '24 17:03 theronic

We have our database migrations also call WriteSchema, so they move in sync

josephschorr avatar Mar 15 '24 17:03 josephschorr

We should look into exporting the diff library via the API

josephschorr avatar Mar 15 '24 17:03 josephschorr

Thanks. How do you keep track of which schema version was written to Spice? External hash / commit / version number in DB?

Would be nice if SchemaService took a DiffSchemaRequest and returned whatever the diff pkg would return.

theronic avatar Mar 15 '24 17:03 theronic

Thanks. How do you keep track of which schema version was written to Spice? External hash / commit / version number in DB?

Yep

Would be nice if SchemaService took a DiffSchemaRequest and returned whatever the diff pkg would return.

Yeah, I updated the issue for that and reopened it

josephschorr avatar Mar 15 '24 17:03 josephschorr

Is there a BNF syntax spec somewhere for the Schema language in the meantime so I can parse the schema text and compare the sorted parse tree?

theronic avatar Mar 20 '24 10:03 theronic

Is there a BNF syntax spec somewhere for the Schema language in the meantime so I can parse the schema text and compare the sorted parse tree?

No. The implementation is considered the spec

josephschorr avatar Mar 25 '24 12:03 josephschorr