views:

225

answers:

3

At my company, we save each database object (stored proc, view, etc) as an individual SQL file, and place them under source control that way.

Up until now, we've had a very flat storage model in our versioned file structure:

  • DatabaseProject
    • Functions
      • (all functions here; no further nesting)
    • StoredProcedures
      • (all stored procs in here; no further nesting)
    • Views
      • (ditto)

For a big new project, another idea has occurred to me: why not store these files by subject instead of in these prefab flat lists?

For example:

  • DatabaseProject
    • Reports
      • (individual stored procs, views, etc.)
      • SpecificReport
        • (more objects here, further nesting as necessary)
    • SpecificApplication
      • (all types of DB objects, with arbitrarily deep nesting)
    • et cetera....

The obvious flaw is that this folder structure doesn't impose any kind of namespace hierarchy on the database objects; it's for organization only. Thus, it would be very easy to introduce objects with duplicate names. You'd need some kind of build tool to survey the database project and die on naming conflicts.

What I'd like to know is: has anyone tried this method of organizing SQL files by application subject in their versioned file structure? Was it worth it? Did you create a build tool that would police the project as I have described?

+1  A: 

You should define a naming scheme for your database objects, so that it's clear where a view or SP is being used.

This can either be prefixes to describe the app modules, or separate schema names for modules/functionality.

No nesting required, and names in the VCS show up the same as in the database, and sort properly depending on the naming scheme.

devio
I should have mentioned: we've adopted a simple Hungarian notation for our database objects. Object names are prepended with "sp" for stored procedure, "vw" for view, and "fn" for function. What tends to happen in our flat storage structure is that, in order to keep database objects organized by subject, we tend to build ever larger prefixes, such as "spEAWizardDoSomething". In this name there are three prefixes: "sp" for stored procedure, "EA" as the code for the specific application, and "Wizard" to indicate a further subpiece of the "EA" application, in this case a wizard.
RenMan
Also, the whole reason I'm suggesting this is that, to me, it's not enough to divide objects by core type (view, function, etc.) I hate opening the "StoredProcedures" folder and seeing hundreds of files in there, from all different pieces of our solution.
RenMan
what abount using the db object name to derive its full pathname, such as sp\EA\Wizard\spEAWizardDoSomething.sql. In the end it only depends on the scheme you make up.
devio
I see possibilities there, definitely. Drawbacks as well. For example, instead of "EA", I'd rather name the folder "ElectronicAbacus" (or whatever it is), but keep the "EA" in the object name. Plus I wouldn't want the top level folder being "sp", because again, I don't want to segregate stored procedures from views and functions under this system; I want all pertinent DB objects in one folder; you can use the Hungarian notation to tell a stored proc from a view.
RenMan
A: 

We save our SQL files in a "SQL" solution folder with each project. That way, each project is "installed" separately.

Chris McCall
+1  A: 

I like to have my SQL scripts organized by topics, rather than by name. As a rule, I even group related items into single files. The main advantages of this are :

  • You do not clutter your filesystem/IDE with files (many of them being a few lines long).
  • The overall database structure shows more directly.

ON the other hand, it may be more difficult to find the source code related to a specific object...

As for duplicate names : it can never happen, because you obviously have automated scripts to build your database. Relying on your filesystem for this is looking for trouble...

As a conclusion, I would say that your current rules are much better than no rule at all.

Mac
I'd be less worried about the clutter from many small SQL files as long as I can arrange these in nested folders by application topic so that they're stowed out of the way. With our previous solution, where we just had a few folders: "StoredProcedures", "Views", "Functions", "Triggers", etc. we had some very large messy folders indeed.
RenMan