How to version control SQL Server databases?

后端 未结 10 1075
醉话见心
醉话见心 2020-12-28 19:04

I have SQL Server databases and do changes in them. Some database tables have records that are starting records required my app to run. I would like to do version control ov

10条回答
  •  再見小時候
    2020-12-28 19:25

    For database (schema) versioning we use custom properties, which are added to the database when the installer is ran. The contents of these scripts is generated with our build scripts.

    The script to set the properties looks like this:

    DECLARE @AssemblyDescription sysname
    SET @AssemblyDescription = N'DailyBuild_20090322.1'
    
    DECLARE @AssemblyFileVersion sysname
    SET @AssemblyFileVersion = N'0.9.3368.58294'
    
    -- The extended properties DatabaseDescription and DatabaseFileVersion contain the
    -- AssemblyDescription and AssemblyFileVersion of the build that was used for the
    -- database script that creates the database structure.
    -- 
    -- The current value of these properties can be displayed with the following query:
    -- SELECT * FROM sys.extended_properties
    
    IF EXISTS (SELECT * FROM sys.extended_properties WHERE class_desc = 'DATABASE' AND name = N'DatabaseDescription')
    BEGIN
        EXEC sys.sp_updateextendedproperty @name = N'DatabaseDescription', @value = @AssemblyDescription
    END
    ELSE
    BEGIN
        EXEC sys.sp_addextendedproperty @name = N'DatabaseDescription', @value = @AssemblyDescription
    END
    
    IF EXISTS (SELECT * FROM sys.extended_properties WHERE class_desc = 'DATABASE' AND name = N'DatabaseFileVersion')
    BEGIN
        EXEC sys.sp_updateextendedproperty @name = N'DatabaseFileVersion', @value = @AssemblyFileVersion
    END
    ELSE
    BEGIN
        EXEC sys.sp_addextendedproperty @name = N'DatabaseFileVersion', @value = @AssemblyFileVersion
    END
    GO
    

提交回复
热议问题