go-structured-query icon indicating copy to clipboard operation
go-structured-query copied to clipboard

Suggestion: Support Table/Field creation from Table structure

Open ocomsoft opened this issue 3 years ago • 5 comments

When going to production you need to ensure the database is the same structure. It would make sense that we can run a function to create any missing tables/fields based on the structure generated

Just a suggestion.

ocomsoft avatar Oct 04 '20 07:10 ocomsoft

Something like an automatic migration that reads the table struct definitions in a Go file and runs CREATE TABLE/ALTER TABLE ADD COLUMNS accordingly?

bokwoon95 avatar Oct 04 '20 12:10 bokwoon95

Yeah. So if there is a Table or Field not in the database it adds it. I would not delete columns or tables.

Bonus points would be to change types but that could be very complex :)

ocomsoft avatar Oct 04 '20 21:10 ocomsoft

I've thought about this feature before, but I'm not sure if it's the right decision to generate the DDL statements based on the Go structs. It's nice to have some migration syntactic sugar like this but if I were to implement it, I would want to consider everything (delete tables, columns, rename columns, change types, etc). For example, if I decide not to delete tables or columns (a good choice btw) I must consider what are the alternatives that the programmer can take? Does it fit well with my proposed solution? There is a whole set of questions I must ask first, and I'm not sure about the answers.

In the current workflow, the code-generated struct definitions shouldn't be edited manually but instead generated from the database. So if I wanted to reference a new column in my code I must add it into the database first, which guarantees that the column already exists. That addresses the concern right?

bokwoon95 avatar Oct 06 '20 04:10 bokwoon95

Ok so the bit I think you are missing is a typical workflow.

The developer adds a field to a table (or a NEW table) and then adds the code to program then rebuilds the problem.. but then they want to put this into a testing server then later a production server.. how we do we ensure the databases have the same table structure that the code expects?

I would suggest a method must be called to do the 'migration' so they can choose to use this or not. But to me, I would like the code to ensure you have the fields in whatever database I use based on what the code is written to expect.

If the user needs more complex features than that then that is another problem. ie a new set of methods to create tables, fields etc...

I mean you don't have to have this feature. I just thought it would make sense.. I think I would end up using a combination of Gorm (that does something similar to what I described) and this library.

Once again - great work - Hope this is a success for you.

ocomsoft avatar Oct 06 '20 05:10 ocomsoft

Thanks for clarifying the workflow. It's quite helpful to know what the various workflows out there in the wild are like. I'll give it some more thought and also on how to implement it.

bokwoon95 avatar Oct 06 '20 15:10 bokwoon95