How to version control SQL Server databases?

后端 未结 10 1030
醉话见心
醉话见心 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
    
    0 讨论(0)
  • 2020-12-28 19:26

    I use bcp for this (bulk loading utility, part of a standard SQL Server install, Express edition included).

    Each table with data needs a control file Table.ctl and a data file Table.csv (these are text files that can be generated from an existing database using bcp). As text files, these can very easily be versioned.

    As part of my generation batches (see my answer there for more information), I iterate through every control file like this :

    SET BASE_NAME=MyDatabaseName
    SET CONNECT_STRING=.\SQLEXPRESS
    
    FOR /R %%i IN (.) DO (
      FOR %%j IN ("%%~fi\*.ctl") DO (
        ECHO +   %%~nj
        bcp %BASE_NAME%..%%~nj in "%%~dpsj%%~nj.csv" -T -E -S %CONNECT_STRING% -f "%%~dpsj%%~nj.ctl" >"%TMP%\%%~nj.log"
        IF %ERRORLEVEL% GTR 0 (
          TYPE "%TMP%\%%~nj.log"
          GOTO ERROR_USAGE
        )
      )
    )
    

    A current limitation of this script is that the name of the file must be the name of the table, which may not be possible if the table name contains specific special characters.

    0 讨论(0)
  • 2020-12-28 19:27

    Static data support is being added to SQL Source Control 2.0, currently available in beta. More information on how to try this can be found here:

    http://www.red-gate.com/messageboard/viewtopic.php?t=12298

    0 讨论(0)
  • 2020-12-28 19:28

    There is a free microsoft product called Database Publishing Wizard which you can use to script the entire database (schema and data). It is great for taking snapshots of the current state of a DB and will enable you to recreate from scratch at any point

    0 讨论(0)
提交回复
热议问题