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.

  • My recent edit request is wrong. Please reject it, or roll back if it is accepted by others. Commented Jan 20, 2019 at 7:53

3 Answers 3


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.

  • 3
    This. I usually have SQL files in their own dbschema directory...
    – yannis
    Commented Apr 17, 2012 at 14:44
  • That's why I'm storing it in version control, but where? My problem is where the files make logical sense among my Controller/Model/Ect folders.
    – Ben Brocka
    Commented Apr 17, 2012 at 14:44
  • 5
    @BenBrocka: I think the point of this answer is you create a new directory, so you will have these: Controller; Model; View; Schema; Etc... Commented Apr 17, 2012 at 14:47
  • +1 The controller probably shouldn't be concerned with data access either, right? It should only be taking in user input and providing the proper view. Nor should the view be concerned with data access. Ideally a DAL is injected and "magically" the model is populated. Commented Apr 17, 2012 at 17:31
  • @ElGringoGrande I agree that a DAL is a good idea, but where would you inject the DAL then? What would call the DAL?
    – maple_shaft
    Commented Apr 17, 2012 at 17:45

Have you looked at the SSMS plug-in SQL Source Control? It connects SQL Server to SVN, TFS, Vault, Mercurial, etc. Details at http://www.red-gate.com/products/sql-development/sql-source-control/


We keep each iteration's sql migration scripts in a big ugly .sql file that lives in TFS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.