DapperLib / DapperAOT

Build time tools in the flavor of Dapper
Other
357 stars 19 forks source link

Suggestion: db schema as "additional file"; apply validation #42

Open mgravell opened 12 months ago

mgravell commented 12 months ago

context: https://twitter.com/IanStockport/status/1702313925810024716

imagine a "dotnet tool":

dapper schema {conn-key} 

which:

Where dbschema.txt could be something like (is there prior art here? plain text?):

(note on order: objects are invariant alphabetical; columns/parameters/etc are positional)

default schema dbo
server version 16.0.1000.6

table dbo.Foo
column Id int notnull identity readonly
column Name string
column Label string computed readonly

table someSchema.Bar
column Id int notnull identity readonly

view someView
column Id notnull null
...

we would then load the dbschema.txt and use it to validate commands at build time, without needing constant SQL access. Schema should be implied via "default schema" when omitted.

Possible checks:

Rewriting the db schema would be a case of re-running the tool, with the changes visible in source control.

Note: advanced SQL analysis is currently limited to SQL Server; we should probably at least not assume that when connecting (use the provider model), but... whatever works.

IanRingrose commented 12 months ago

Sort the table names, and the column names before writing file so diff/merge more likely to work in a senible way. The file should always be 100% same if database is logically the same.

Also think of how a build server could run tool to check that file is the same when database creation script is run then checked in file. But leave details of implementation to the users build management team.

xPaw commented 12 months ago

For inspiration, this exists in PHP land: https://github.com/staabm/phpstan-dba

amoerie commented 11 months ago

Not sure if you're aware about this, but I think this exists in F# already as a type provider: https://fsprojects.github.io/SQLProvider/