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
T4 template for creating code from existing DB #293
Conversation
T4 template for creating code from existing DB
Thanks for the pull request. A lot of people have asked for something like this. I was wondering how do you test the C# code in this? For example, the SqlServerSchemaReader class is pretty basic and would need to be extended, I tried running the T4 template against an MS Sql Server database and it worked pretty well for the tables (though indexes and primary/foreign keys are not implemented yet). Though there is a little bug with schemas. The schema name is not quoted. Create.Table("<#=tbl.Name#>")<#if (string.IsNullOrEmpty(tbl.Schema) == false ){#>.InSchema(<#=tbl.Schema#>)<#}# And it crashes for every for loop for indices and foreign keys if there are none. For example: List<TableIndex> IndexList = tbl.Indices;
foreach(TableIndex indx in IndexList) then the variable IndexList is null and the foreach loop crashes so maybe something like this would fix that: List<TableIndex> IndexList = tbl.Indices ?? new List<TableIndex>();
foreach(TableIndex indx in IndexList) Which databases have the best support? I understand that you have mostly tested with Sqlite? Thanks again for all your hard work! |
Thanks for reviewing code and testing it in SqlServer. I did the extensive testing only in Sqlite. I'll make the changes you have mentioned and send another pull request. |
You don't need to create a new pull request. Just update this one. You can do that by making the change and then: git push origin master |
fix for non sqlite db code generation
I just fixed the issue in my master branch, can you check if it works for you ? |
Thanks for the fixes. I'll test it out tonight. |
Back reviewing this after a bit of a pause. It works much better now but is pretty basic still. I'm thinking that we could make this even better by taking in more functionality from the Subsonic templates. I'm looking at https://github.com/subsonic/SubSonic-3.0-Templates/blob/master/ActiveRecord/SQLServer.ttinclude and it has code for Foreign Keys, stored procedures and more. I assume that you based this on this file: https://github.com/toptensoftware/PetaPoco/blob/master/PetaPoco/Models/Generated/PetaPoco.Core.ttinclude and the first comment is that it is a reduced version of the Subsonic template. This is a cool tech demo but without support for views, SPs, indexes (indices) and foreign keys it is a bit limited. Do you have time to continue with this? Or should I accept this and improve it a bit on my own? This is a good first version that needs to be worked on some more. |
Did a little trial run with generating foreign key expressions for SqlServer and it wasn't too hard. So I'll finish off the SqlServer generator tomorrow evening and merge it in then. |
@gkathire The code for generating views doesn't seem right to me. It creates migrations for tables and uses the view name as the table name and creates a bunch of columns. Surely views and SPs should be sql scripts? |
The code does not support view generation. The PetaPoco and Subsonic templates uses the "IncludeViews" option to generate the Poco associated with the view. By default I disabled the view selection. |
T4 template for creating code from existing DB
I will later today add changes for Sql Server to create Foreign Keys and Indexes. Thanks for all your work! |
@daniellee, @gkathire, how you test the development of the template? Would you mind if I moved some of the logic inside the FM.Core.ttinclude to a separate project? I'd like to try to add support for compound primary and foreign keys, but it's probably easier if the code is a bit more testable... |
I asked how @gkathire tests this but I don't think he had a testing strategy. This is a contrib project and is not really usable yet. I would still choose an sql script over this. So if you feel like polishing this up and adding tests and support for other databases that would be great! |
A usable (and working as I tested) tool is here: http://pfsolutions-mi.com/Product/FluentMigratorClassGenerator |
T4 template for creating code from existing DB