Skip to content
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

Merged
merged 2 commits into from Sep 27, 2012

Conversation

gkathire
Copy link
Contributor

T4 template for creating code from existing DB

T4 template for creating code from existing DB
@daniellee
Copy link
Contributor

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!

@gkathire
Copy link
Contributor Author

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.

@daniellee
Copy link
Contributor

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
@gkathire
Copy link
Contributor Author

I just fixed the issue in my master branch, can you check if it works for you ?

@daniellee
Copy link
Contributor

Thanks for the fixes. I'll test it out tonight.

@daniellee
Copy link
Contributor

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.

@daniellee
Copy link
Contributor

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.

@daniellee
Copy link
Contributor

@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?

@gkathire
Copy link
Contributor Author

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.
If you want to add the view code in the migration script, you need to extract the view SQL and use the migration object that does the raw sql update.

daniellee added a commit that referenced this pull request Sep 27, 2012
T4 template for creating code from existing DB
@daniellee daniellee merged commit c9f4b15 into fluentmigrator:master Sep 27, 2012
@daniellee
Copy link
Contributor

I will later today add changes for Sql Server to create Foreign Keys and Indexes. Thanks for all your work!

@zidad
Copy link
Contributor

zidad commented Oct 24, 2012

@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...

@daniellee
Copy link
Contributor

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!

@hikalkan
Copy link

A usable (and working as I tested) tool is here: http://pfsolutions-mi.com/Product/FluentMigratorClassGenerator
It may be helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants