Stored procedures/DB schema in source control

后端 未结 21 937
孤街浪徒
孤街浪徒 2020-12-04 05:56

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

When you make a change (add a table, update an stored proc,

相关标签:
21条回答
  • 2020-12-04 06:52

    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.
    As 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.
    We 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.

    0 讨论(0)
  • 2020-12-04 06:52

    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.

    I prefer one script per object.

    0 讨论(0)
  • 2020-12-04 06:54

    I run a job to script it out to a formal directory structure.

    The following is VS2005 code, command line project, called from a batch file, that does the work. app.config keys at end of code.

    It is based on other code I found online. Slightly a pain to set up, but works well once you get it working.

    Imports Microsoft.VisualStudio.SourceSafe.Interop
    Imports System
    Imports System.Configuration
    
    Module Module1
    
        Dim sourcesafeDataBase As String, sourcesafeUserName As String, sourcesafePassword As String, sourcesafeProjectName As String, fileFolderName As String
    
    
        Sub Main()
            If My.Application.CommandLineArgs.Count > 0 Then
                GetSetup()
                For Each thisOption As String In My.Application.CommandLineArgs
                    Select Case thisOption.ToUpper
                        Case "CHECKIN"
                            DoCheckIn()
                        Case "CHECKOUT"
                            DoCheckOut()
                        Case Else
                            DisplayUsage()
                    End Select
                Next
            Else
                DisplayUsage()
            End If
        End Sub
    
        Sub DisplayUsage()
            Console.Write(System.Environment.NewLine + "Usage: SourceSafeUpdater option" + System.Environment.NewLine + _
                "CheckIn - Check in ( and adds any new ) files in the directory specified in .config" + System.Environment.NewLine + _
                "CheckOut - Check out all files in the directory specified in .config" + System.Environment.NewLine + System.Environment.NewLine)
        End Sub
    
        Sub AddNewItems()
            Dim db As New VSSDatabase
            db.Open(sourcesafeDataBase, sourcesafeUserName, sourcesafePassword)
            Dim Proj As VSSItem
            Dim Flags As Integer = VSSFlags.VSSFLAG_DELTAYES + VSSFlags.VSSFLAG_RECURSYES + VSSFlags.VSSFLAG_DELNO
            Try
                Proj = db.VSSItem(sourcesafeProjectName, False)
                Proj.Add(fileFolderName, "", Flags)
            Catch ex As Exception
                If Not ex.Message.ToString.ToLower.IndexOf("already exists") > 0 Then
                    Console.Write(ex.Message)
                End If
            End Try
            Proj = Nothing
            db = Nothing
        End Sub
    
        Sub DoCheckIn()
            AddNewItems()
            Dim db As New VSSDatabase
            db.Open(sourcesafeDataBase, sourcesafeUserName, sourcesafePassword)
            Dim Proj As VSSItem
            Dim Flags As Integer = VSSFlags.VSSFLAG_DELTAYES + VSSFlags.VSSFLAG_UPDUPDATE + VSSFlags.VSSFLAG_FORCEDIRYES + VSSFlags.VSSFLAG_RECURSYES
            Proj = db.VSSItem(sourcesafeProjectName, False)
            Proj.Checkin("", fileFolderName, Flags)
            Dim File As String
            For Each File In My.Computer.FileSystem.GetFiles(fileFolderName)
                Try
                    Proj.Add(fileFolderName + File)
                Catch ex As Exception
                    If Not ex.Message.ToString.ToLower.IndexOf("access code") > 0 Then
                        Console.Write(ex.Message)
                    End If
                End Try
            Next
            Proj = Nothing
            db = Nothing
        End Sub
    
        Sub DoCheckOut()
            Dim db As New VSSDatabase
            db.Open(sourcesafeDataBase, sourcesafeUserName, sourcesafePassword)
            Dim Proj As VSSItem
            Dim Flags As Integer = VSSFlags.VSSFLAG_REPREPLACE + VSSFlags.VSSFLAG_RECURSYES
            Proj = db.VSSItem(sourcesafeProjectName, False)
            Proj.Checkout("", fileFolderName, Flags)
            Proj = Nothing
            db = Nothing
        End Sub
    
        Sub GetSetup()
            sourcesafeDataBase = ConfigurationManager.AppSettings("sourcesafeDataBase")
            sourcesafeUserName = ConfigurationManager.AppSettings("sourcesafeUserName")
            sourcesafePassword = ConfigurationManager.AppSettings("sourcesafePassword")
            sourcesafeProjectName = ConfigurationManager.AppSettings("sourcesafeProjectName")
            fileFolderName = ConfigurationManager.AppSettings("fileFolderName")
    
        End Sub
    
    End Module
    
    
    
    <add key="sourcesafeDataBase" value="C:\wherever\srcsafe.ini"/>
    <add key="sourcesafeUserName" value="vssautomateuserid"/>
    <add key="sourcesafePassword" value="pw"/>
    <add key="sourcesafeProjectName" value="$/where/you/want/it"/>
    <add key="fileFolderName" value="d:\yourdirstructure"/>
    
    0 讨论(0)
提交回复
热议问题