Database Schema in MVC – Where Should the Database Schema Live?

mvcversion control

I'm starting to check in the SQL files for creating/maintaining our database. Previously SQL files were kept totally separate from our codebase's version control.

I'd like to pragmatically store my database construction/manipulation files with my other code. Do they make the most sense in a subdirectory of my Models or as a separate directory somewhere?

For the most part the SQL statements are the data my Models access, but some other stuff is maintenance scripts ect; stuff that doesn't pragmatically fit into my MVC folder structure.

Best Answer

No. Your SQL files either define your Schema or they define your data access.

The data retrieved from SQL files are not your model either, or rather database record sets should not be your model. There should be a level of translation that converts your record sets into data suitable for the Model.

In an ideal scenario your Model will be agnostic of data access logic and should mereley contain data that is pertinent to one or more Views as well as view state information as well.

Your Controller should bridge the gap between the View and the data contained in the Model such that only the Controller is concerned with data access matters.

On another note, if your SQL is NOT code then what is it? You are right to store SQL in version control because they define your application as much as anything.