<p>Do you guys keep track of stored procedures and database schema in your source control system of choice?</p>

<p>When you make a change (add a table, update an stored proc, how do you get the changes into source control?  </p>

<p>We use SQL Server at work, and I've begun using darcs for versioning, but I'd be curious about general strategies as well as any handy tools.</p>

<p><em>Edit:</em> Wow, thanks for all the great suggestions, guys!  I wish I could select more than one "Accepted Answer"!</p>

[{'answer_id': 77187, 'author': 'Darren Kopp', 'author_id': 77, 'author_profile': 'https://Stackoverflow.com/users/77', 'pm_score': 0, 'selected': False, 'text': '<p>We keep stored procedures in source control. </p>\n'}, {'answer_id': 77196, 'author': 'ahockley', 'author_id': 8209, 'author_profile': 'https://Stackoverflow.com/users/8209', 'pm_score': 0, 'selected': False, 'text': "<p>Script everything (object creation, etc) and store those scripts in source control.  How do the changes get there?  It's part of the standard practice of how things are done.  Need to add a table?  Write a CREATE TABLE script.  Update a sproc?  Edit the stored procedure script.</p>\n\n<p>I prefer one script per object.</p>\n"}, {'answer_id': 77203, 'author': 'Manu', 'author_id': 2133, 'author_profile': 'https://Stackoverflow.com/users/2133', 'pm_score': 3, 'selected': False, 'text': '<p>create a "Database project" in Visual Studio to write and manage your sQL code and keep the project under version control together with the rest of your solution.</p>\n'}, {'answer_id': 77212, 'author': 'John Flinchbaugh', 'author_id': 12591, 'author_profile': 'https://Stackoverflow.com/users/12591', 'pm_score': 0, 'selected': False, 'text': "<p>For procs, write the procs with script wrappers into plain files, and apply the changes from those files.  If it applied correctly, then you can check in that file, and you'll be able to reproduce it from that file as well.</p>\n\n<p>For schema changes, you may need to check in scripts to incrementally make the changes you've made.  Write the script, apply it, and then check it in.  You can build a process then, to automatically apply each schema script in series.</p>\n"}, {'answer_id': 77216, 'author': 'Adrian Mouat', 'author_id': 4332, 'author_profile': 'https://Stackoverflow.com/users/4332', 'pm_score': 2, 'selected': False, 'text': '<p>I think you should write a script which automatically sets up your database, including any stored procedures. This script should then be placed in source control.</p>\n'}, {'answer_id': 77223, 'author': 'Rik', 'author_id': 5409, 'author_profile': 'https://Stackoverflow.com/users/5409', 'pm_score': 0, 'selected': False, 'text': "<p>We do keep stored procedures in source control. The way we (or at least I) do it is add a folder to my project, add a file for each SP and manually copy, paste the code into it. So when I change the SP, I manually need to change the file the source control.</p>\n\n<p>I'd be interested to hear if people can do this automatically.</p>\n"}, {'answer_id': 77238, 'author': 'japollock', 'author_id': 1210318, 'author_profile': 'https://Stackoverflow.com/users/1210318', 'pm_score': 3, 'selected': False, 'text': '<p>The solution we used at my last job was to number the scripts as they were added to source control:</p>\n\n<p>01.CreateUserTable.sql<br>\n02.PopulateUserTable<br>\n03.AlterUserTable.sql<br>\n04.CreateOrderTable.sql</p>\n\n<p>The idea was that we always knew which order to run the scripts, and we could avoid having to manage data integrity issues that might arise if you tried modifying script #1 (which would presumable cause the INSERTs in #2 to fail).</p>\n'}, {'answer_id': 77270, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>I highly recommend maintaining schema and stored procedures in source control.  </p>\n\n<p>Keeping stored procedures versioned allows them to be rolled back when determined to be problematic.  </p>\n\n<p>Schema is a less obvious answer depending on what you mean.  It is very useful to maintain the SQL that defines your tables in source control, for duplicating environments (prod/dev/user etc.).  </p>\n'}, {'answer_id': 77379, 'author': 'Rikalous', 'author_id': 4271, 'author_profile': 'https://Stackoverflow.com/users/4271', 'pm_score': 0, 'selected': False, 'text': "<p>We have been using an alternative approach in my current project - we haven't got the db under source control but instead have been using a database diff tool to script out the changes when we get to each release.<br>\nIt has been working very well so far.</p>\n"}, {'answer_id': 77443, 'author': 'Chris Hall', 'author_id': 5933, 'author_profile': 'https://Stackoverflow.com/users/5933', 'pm_score': 0, 'selected': False, 'text': '<p>We store everything related to an application in our SCM.  The DB scripts are generally stored in their own project, but are treated just like any other code... design, implement, test, commit.</p>\n'}, {'answer_id': 77500, 'author': 'Robert Paulson', 'author_id': 14033, 'author_profile': 'https://Stackoverflow.com/users/14033', 'pm_score': 6, 'selected': True, 'text': "<p>We choose to script everything, and that includes all stored procedures and schema changes. No wysiwyg tools, and no fancy 'sync' programs are necessary.</p>\n\n<p>Schema changes are easy, all you need to do is create and maintain a single file for that version, including all schema and data changes. This becomes your conversion script from version x to x+1. You can then run it against a production backup and integrate that into your 'daily build' to verify that it works without errors. Note it's important not to change or delete already written schema / data loading sql as you can end up breaking any sql written later.</p>\n\n<pre><code>-- change #1234\nALTER TABLE asdf ADD COLUMN MyNewID INT\nGO\n\n-- change #5678\nALTER TABLE asdf DROP COLUMN SomeOtherID\nGO\n</code></pre>\n\n<p>For stored procedures, we elect for a single file per sproc, and it uses the drop/create form. All stored procedures are recreated at deployment. The downside is that if a change was done outside source control, the change is lost. At the same time, that's true for any code, but your DBA'a need to be aware of this. This really stops people outside the team mucking with your stored procedures, as their changes are lost in an upgrade. </p>\n\n<p>Using Sql Server, the syntax looks like this:</p>\n\n<pre><code>if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[usp_MyProc]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)\ndrop procedure [usp_MyProc]\nGO\n\nCREATE PROCEDURE [usp_MyProc]\n(\n    @UserID INT\n)\nAS\n\nSET NOCOUNT ON\n\n-- stored procedure logic.\n\nSET NOCOUNT OFF\n\nGO  \n</code></pre>\n\n<p>The only thing left to do is write a utility program that collates all the individual files and creates a new file with the entire set of updates (as a single script). Do this by first adding the schema changes then recursing the directory structure and including all the stored procedure files.</p>\n\n<p>As an upside to scripting everything, you'll become much better at reading and writing SQL. You can also make this entire process more elaborate, but this is the basic format of how to source-control all sql without any special software.</p>\n\n<p>addendum: Rick is correct that you will lose permissions on stored procedures with DROP/CREATE, so you may need to write another script will re-enable specific permissions. This permission script would be the last to run. Our experience found more issues with ALTER verses DROP/CREATE semantics. YMMV</p>\n"}, {'answer_id': 77594, 'author': 'Ed Lucas', 'author_id': 12551, 'author_profile': 'https://Stackoverflow.com/users/12551', 'pm_score': 2, 'selected': False, 'text': '<p>Couple different perspectives from my experience.  In the Oracle world, everything was managed by "create" DDL scripts.  As ahockley mentioned, one script for each object.  If the object needs to change, its DDL script is modified.  There\'s one wrapper script that invokes all the object scripts so that you can deploy the current DB build to whatever environment you want.  This is for the main core create.  </p>\n\n<p>Obviously in a live application, whenever you push a new build that requires, say, a new column, you\'re not going to drop the table and create it new.  You\'re going to do an ALTER script and add the column.  So each time this kind of change needs to happen, there are always two things to do: 1) write the alter DDL and 2) update the core create DDL to reflect the change.  Both go into source control, but the single alter script is more of a momentary point in time change since it will only be used to apply a delta.</p>\n\n<p>You could also use a tool like ERWin to update the model and forward generate the DDL, but most DBAs I know don\'t trust a modeling tool to gen the script exactly the way they want.  You could also use ERWin to reverse engineer your core DDL script into a model periodically, but that\'s a lot of fuss to get it to look right (every blasted time you do it).</p>\n\n<p>In the Microsoft world, we employed a similar tactic, but we used the Red Gate product to help manage the scripts and deltas.  Still put the scripts in source control.  Still one script per object (table, sproc, whatever).  In the beginning, some of the DBAs really preferred using the SQL Server GUI to manage the objects rather than use scripts.  But that made it very difficult to manage the enterprise consistently as it grew.</p>\n\n<p>If the DDL is in source control, it\'s trivial to use any build tool (usually ant) to write a deployment script.</p>\n'}, {'answer_id': 77665, 'author': 'Dean Poulin', 'author_id': 5462, 'author_profile': 'https://Stackoverflow.com/users/5462', 'pm_score': 2, 'selected': False, 'text': '<p>In past experiences, I\'ve kept database changes source controlled in such a way that for each release of the product any database changes were always scripted out and stored in the release that we\'re working on.  The build process in place would automatically bring the database up to the current version based on a table in the database that stored the current version for each "application".  A custom .net utility application we wrote would then run and determine the current version of the database, and run any new scripts against it in order of the prefix numbers of the scripts.  Then we\'d run unit tests to make sure everything was all good.</p>\n\n<p>We\'d store the scripts in source control as follows (folder structure below):</p>\n\n<p>I\'m a little rusty on current naming conventions on tables and stored procedures so bare with my example...</p>\n\n<p>[root]<br>\n&nbsp;&nbsp;&nbsp;&nbsp;[application]<br>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[version]<br>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[script]  </p>\n\n<p>\\scripts<br>\n&nbsp;&nbsp;&nbsp;&nbsp;MyApplication\\<br>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1.2.1\\<br>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;001.MyTable.Create.sql<br>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;002.MyOtherTable.Create.sql<br>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;100.dbo.usp.MyTable.GetAllNewStuff.sql  </p>\n\n<p>With the use of a Versions table that would take into account the Application and Version the application would restore the weekly production backup, and run all the scripts needed against the database since the current version.  By using .net we were easily able to package this into a transaction and if anything failed we would rollback, and send emails out, so we knew that release had bad scripts.</p>\n\n<p>So, all developers would make sure to maintain this in source control so the coordinated release would make sure that all the scripts we plan to run against the database would run successfully.</p>\n\n<p>This is probably more information than you were looking for, but it worked very well for us and given the structure it was easy to get all developers on board.</p>\n\n<p>When release day came around the operations team would follow the release notes and pick up the scripts from source control and run the package against the database with the .net application we used during the nightly build process which would automatically package the scripts in transactions so if something failed it would automatically roll back and no impact to the database was made.</p>\n'}, {'answer_id': 77672, 'author': 'John Rudy', 'author_id': 14048, 'author_profile': 'https://Stackoverflow.com/users/14048', 'pm_score': 2, 'selected': False, 'text': "<p>Similar to Robert Paulson, above, our organization keeps the database under source control. However, our difference is that we try to limit the number of scripts we have. </p>\n\n<p>For any new project, there's a set procedure. We have a schema creation script at version 1, a stored proc creation script and possibly an initial data load creation script. All procs are kept in a single, admittedly massive file. If we're using Enterprise Library, we include a copy of the creation script for logging; if it's an ASP.NET project using the ASP.NET application framework (authentication, personalization, etc.), we include that script as well. (We generated it from Microsoft's tools, then tweaked it until it worked in a replicable fashion across different sites. Not fun, but a valuable time investment.)</p>\n\n<p>We use the magic CTRL+F to find the proc we like. :) (We'd love it if SQL Management Studio had code navigation like VS does. Sigh!)</p>\n\n<p>For subsequent versions, we usually have upgradeSchema, upgradeProc and/or updateDate scripts. For schema updates, we ALTER tables as much as possible, creating new ones as needed. For proc updates, we DROP and CREATE.</p>\n\n<p>One wrinkle does pop up with this approach. It's easy to generate a database, and it's easy to get a new one up to speed on the current DB version. However, care has to be taken with DAL generation (which we currently -- usually -- do with SubSonic), to ensure that DB/schema/proc changes are synchronized cleanly with the code used to access them. However, in our build paths is a batch file which generates the SubSonic DAL, so it's our SOP to checkout the DAL code, re-run that batch file, then check it all back in anytime the schema and/or procs change. (This, of course, triggers a source build, updating shared dependencies to the appropriate DLLs ... )</p>\n"}, {'answer_id': 77782, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>I run a job to script it out to a formal directory structure.</p>\n\n<p>The following is VS2005 code, command line project, called from a batch file, that does the work.  app.config keys at end of code.</p>\n\n<p>It is based on other code I found online.  Slightly a pain to set up, but works well once you get it working.</p>\n\n<pre><code>Imports Microsoft.VisualStudio.SourceSafe.Interop\nImports System\nImports System.Configuration\n\nModule Module1\n\n    Dim sourcesafeDataBase As String, sourcesafeUserName As String, sourcesafePassword As String, sourcesafeProjectName As String, fileFolderName As String\n\n\n    Sub Main()\n        If My.Application.CommandLineArgs.Count &gt; 0 Then\n            GetSetup()\n            For Each thisOption As String In My.Application.CommandLineArgs\n                Select Case thisOption.ToUpper\n                    Case "CHECKIN"\n                        DoCheckIn()\n                    Case "CHECKOUT"\n                        DoCheckOut()\n                    Case Else\n                        DisplayUsage()\n                End Select\n            Next\n        Else\n            DisplayUsage()\n        End If\n    End Sub\n\n    Sub DisplayUsage()\n        Console.Write(System.Environment.NewLine + "Usage: SourceSafeUpdater option" + System.Environment.NewLine + _\n            "CheckIn - Check in ( and adds any new ) files in the directory specified in .config" + System.Environment.NewLine + _\n            "CheckOut - Check out all files in the directory specified in .config" + System.Environment.NewLine + System.Environment.NewLine)\n    End Sub\n\n    Sub AddNewItems()\n        Dim db As New VSSDatabase\n        db.Open(sourcesafeDataBase, sourcesafeUserName, sourcesafePassword)\n        Dim Proj As VSSItem\n        Dim Flags As Integer = VSSFlags.VSSFLAG_DELTAYES + VSSFlags.VSSFLAG_RECURSYES + VSSFlags.VSSFLAG_DELNO\n        Try\n            Proj = db.VSSItem(sourcesafeProjectName, False)\n            Proj.Add(fileFolderName, "", Flags)\n        Catch ex As Exception\n            If Not ex.Message.ToString.ToLower.IndexOf("already exists") &gt; 0 Then\n                Console.Write(ex.Message)\n            End If\n        End Try\n        Proj = Nothing\n        db = Nothing\n    End Sub\n\n    Sub DoCheckIn()\n        AddNewItems()\n        Dim db As New VSSDatabase\n        db.Open(sourcesafeDataBase, sourcesafeUserName, sourcesafePassword)\n        Dim Proj As VSSItem\n        Dim Flags As Integer = VSSFlags.VSSFLAG_DELTAYES + VSSFlags.VSSFLAG_UPDUPDATE + VSSFlags.VSSFLAG_FORCEDIRYES + VSSFlags.VSSFLAG_RECURSYES\n        Proj = db.VSSItem(sourcesafeProjectName, False)\n        Proj.Checkin("", fileFolderName, Flags)\n        Dim File As String\n        For Each File In My.Computer.FileSystem.GetFiles(fileFolderName)\n            Try\n                Proj.Add(fileFolderName + File)\n            Catch ex As Exception\n                If Not ex.Message.ToString.ToLower.IndexOf("access code") &gt; 0 Then\n                    Console.Write(ex.Message)\n                End If\n            End Try\n        Next\n        Proj = Nothing\n        db = Nothing\n    End Sub\n\n    Sub DoCheckOut()\n        Dim db As New VSSDatabase\n        db.Open(sourcesafeDataBase, sourcesafeUserName, sourcesafePassword)\n        Dim Proj As VSSItem\n        Dim Flags As Integer = VSSFlags.VSSFLAG_REPREPLACE + VSSFlags.VSSFLAG_RECURSYES\n        Proj = db.VSSItem(sourcesafeProjectName, False)\n        Proj.Checkout("", fileFolderName, Flags)\n        Proj = Nothing\n        db = Nothing\n    End Sub\n\n    Sub GetSetup()\n        sourcesafeDataBase = ConfigurationManager.AppSettings("sourcesafeDataBase")\n        sourcesafeUserName = ConfigurationManager.AppSettings("sourcesafeUserName")\n        sourcesafePassword = ConfigurationManager.AppSettings("sourcesafePassword")\n        sourcesafeProjectName = ConfigurationManager.AppSettings("sourcesafeProjectName")\n        fileFolderName = ConfigurationManager.AppSettings("fileFolderName")\n\n    End Sub\n\nEnd Module\n\n\n\n&lt;add key="sourcesafeDataBase" value="C:\\wherever\\srcsafe.ini"/&gt;\n&lt;add key="sourcesafeUserName" value="vssautomateuserid"/&gt;\n&lt;add key="sourcesafePassword" value="pw"/&gt;\n&lt;add key="sourcesafeProjectName" value="$/where/you/want/it"/&gt;\n&lt;add key="fileFolderName" value="d:\\yourdirstructure"/&gt;\n</code></pre>\n'}, {'answer_id': 78261, 'author': 'Rick', 'author_id': 14138, 'author_profile': 'https://Stackoverflow.com/users/14138', 'pm_score': 3, 'selected': False, 'text': '<p>One thing to keep in mind with your drop/create scripts in SQL Server is that object-level permissions will be lost.  We changed our standard to use ALTER scripts instead, which maintains those permissions.</p>\n\n<p>There are a few other caveats, like the fact that dropping an object drops the dependency records used by sp_depends, and creating the object only creates the dependencies for that object.  So if you drop/create a view, sp_depends will no longer know of any objects referencing that view.</p>\n\n<p>Moral of the story, use ALTER scripts.</p>\n'}, {'answer_id': 78328, 'author': 'knight0323', 'author_id': 14281, 'author_profile': 'https://Stackoverflow.com/users/14281', 'pm_score': 1, 'selected': False, 'text': "<p>Stored procedures get 1 file per sp with the standard if exist drop/create statements at the top. Views and functions also get their own files so they are easier to version and reuse.</p>\n\n<p>Schema is all 1 script to begin with then we'll do version changes.</p>\n\n<p>All of this is stored in a visual studio database project connected to TFS (@ work or VisualSVN Server @ home for personal stuff) with a folder structure as follows:<br />\n- project<br />\n-- functions<br />\n-- schema<br />\n-- stored procedures<br />\n-- views<br /></p>\n"}, {'answer_id': 78415, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': "<p>At my company, we tend to store all database items in source control as individual scripts  just as you would for individual code files.  Any updates are first made in the database and then migrated into the source code repository so a history of changes is maintained.<br>\nAs a second step, all database changes are migrated to an integration database.  This integration database represents exactly what the production database should look like post deployment.  We also have a QA database which represents the current state of production (or the last deployment).  Once all changes are made in the Integration database, we use a schema diff tool (Red Gate's SQL Diff for SQL Server) to generate a script that will migrate all changes from one database to the other.<br>\nWe have found this to be fairly effective as it generates a single script that we can integrate with our installers easily.  The biggest issue we often have is developers forgetting to migrate their changes into integration.</p>\n"}, {'answer_id': 81694, 'author': 'icelava', 'author_id': 2663, 'author_profile': 'https://Stackoverflow.com/users/2663', 'pm_score': 3, 'selected': False, 'text': '<p>I agree with (and upvote) Robert Paulson\'s practice. That is assuming you are in control of a development team with the responsibility and discipline to adhere to such a practice.</p>\n\n<p>To "force" that onto my teams, our solutions maintain at least one database project from <strong><a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=7DE00386-893D-4142-A778-992B69D482AD&amp;displaylang=en" rel="noreferrer">Visual Studio Team Edition for Database Professionals</a></strong>. As with other projects in the solution, the database project gets versioned control. It makes it a natural development process to break the everything in the database into maintainable chunks, "disciplining" my team along the way.</p>\n\n<p>Of course, being a Visual Studio project, it is no where near perfect. There are many quirks you will run into that may frustrate or confuse you. It takes a fair bit of understanding how the project works before getting it to accomplish your tasks. Examples include</p>\n\n<ul>\n<li><a href="http://icelava.net/forums/post/2620.aspx" rel="noreferrer">deploying data from CSV files</a>.</li>\n<li><a href="http://icelava.net/forums/post/2756.aspx" rel="noreferrer">selective deployment of test data based on build type</a>.</li>\n<li><a href="http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=1750819&amp;SiteID=1" rel="noreferrer">Visual Studio crashing on comparing with databases with certain type of CLR assembly embedded within</a>.</li>\n<li>no means of differntiation between test/production databases that implement different authentication schemes - SQL users vs Active Directory users.</li>\n</ul>\n\n<p>But for teams who don\'t have a practice of versioning their database objects, this is a good start. The other famous alternative is of course, <a href="http://www.red-gate.com/products/index.htm" rel="noreferrer">Red Gate\'s suite of SQL Server products</a>, which most people who use them consider superior to Microsoft\'s offering.</p>\n'}, {'answer_id': 7841276, 'author': 'anopres', 'author_id': 1537, 'author_profile': 'https://Stackoverflow.com/users/1537', 'pm_score': 2, 'selected': False, 'text': "<p>I've found that by far, the easiest, fastest and safest way to do this is to just bite the bullet and use SQL Source Control from RedGate.  Scripted and stored in the repository in a matter of minutes.  I just wish that RedGate would look at the product as a loss leader so that it could get more widespread use.</p>\n"}, {'answer_id': 28448690, 'author': 'jlee-tessik', 'author_id': 4281149, 'author_profile': 'https://Stackoverflow.com/users/4281149', 'pm_score': -1, 'selected': False, 'text': '<p>If you\'re looking for an easy, ready-made solution, our <a href="http://tessik.com/sqlhistorian" rel="nofollow">Sql Historian</a> system uses a background process to automatically synchronizes DDL changes to TFS or SVN, transparent to anyone making changes on the database.  In my experience, the big problem is maintaining the code in source control with what was changed on your server--and that\'s because usually you have to rely on people (developers, even!) to change their workflow and remember to check in their changes after they\'ve already made it on the server. Putting that burden on a machine makes everyone\'s life easier. </p>\n'}]
['https://Stackoverflow.com/questions/77172', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/7856/']