SQL version control methodology

前端 未结 6 1164
一生所求
一生所求 2021-02-02 14:33

There are several questions on SO about version control for SQL and lots of resources on the web, but I can\'t find something that quite covers what I\'m trying to do.

F

相关标签:
6条回答
  • 2021-02-02 14:43

    We are using SQL Examiner for keeping database schema under version control. I've tried the VS2010 also, but in my opinion VS approach is too complex for small and mid-size projects. With SQL Examiner I mostly work with SSMS and use SQL Examiner to check-in updates to SVN (TFS and SourceSafe is supported also, but I never tried it).

    Here is description of SQL Examiner's approach: How to get your database under version control

    0 讨论(0)
  • 2021-02-02 14:45

    Try DBSourceTools. (http://dbsourcetools.codeplex.com)
    Its open source, and specifically designed to script an entire database - tables, views, procs to disk, and then re-create that database through a deployment target.
    You can script all data, or just specify which tables to script data for.
    Additionally, you can zip up the results for distribution.
    We use it for source control of databases, and to test update patches for new releases.
    In the back-end it's built around SMO, and thus supports SQL 2000, 2005 and 2008.
    DBDiff is integrated, to allow for schema comparisons.
    Have fun, - Nathan.

    0 讨论(0)
  • 2021-02-02 14:59

    Yes, you're asking for a lot, but they're all really pertinent points! Here at Red Gate we're moving towards a complete database development solution with our SQL Source Control SSMS extension and we're facing similar challenges.

    http://www.red-gate.com/products/SQL_Source_Control/index.htm

    For the upcoming release we're fully supporting schema changes, and supporting static data indirectly via our SQL Data Compare tool. All changes are saved as creation scripts, although when you're updating or deploying to a database, the tool will ensure that the changes are applied appropriately as an ALTER or CREATE.

    The most challenging requirement that doesn't yet have a simple solution is version management and deployment, which you describe very clearly. If you make complex changes to the schema and data, it may be inevitable that a handcrafted migration script is constructed to get between two adjacent versions, as not all of the 'intent' is always saved alongside a newer version. Column renames are a prime example. The solution could be for a system to be devised that saves the intent, or if this is too complex, allows the user to supply a custom script to perform the complex change. Some sort of version management framework would manage these and "magically" construct deployment scripts from two arbitrary versions.

    0 讨论(0)
  • 2021-02-02 15:05

    for this kind of issue use Visual studio team system 2008 for version controlling of your sql database.

    In tsf there are no. of feature avialbe like

    • Datacompare
    • Schemacompare
    • version controlling

    about database version control : http://www.codinghorror.com/blog/2006/12/is-your-database-under-version-control.html for more detail check : http://msdn.microsoft.com/en-us/library/ms364062(VS.80).aspx

    0 讨论(0)
  • 2021-02-02 15:06

    I struggled with this for several years before recently adopting a strategy that seems to work pretty well. Key points I live by:

    • The database doesn't need to be independently versioned from the app
    • All database update scripts should be idempotent

    As a result, I no longer create any kind of version tables. I simply add changes to a numbered sequence of .sql files that can be applied at any given time without corrupting the database. If it makes things easier, I'll write a simple installer screen for the app to allow administrators to run these scripts whenever they like.

    Of course, this method does impose a few requirements on the database design:

    • All schema changes are done through script - no GUI work.
    • Extra care must be taken to ensure all keys, constraints, etc.. are named so they can be referenced by a later update script, if necessary.
    • All update scripts should check for existing conditions.

    Examples from a recent project:

    001.sql:

    if object_id(N'dbo.Registrations') is null 
    begin
        create table dbo.Registrations
        (
            [Id]                    uniqueidentifier not null,
            [SourceA]               nvarchar(50)     null,
            [SourceB]               nvarchar(50)     null,
            [Title]                 nvarchar(50)     not null,
            [Occupation]            nvarchar(50)     not null,
            [EmailAddress]          nvarchar(100)    not null,
            [FirstName]             nvarchar(50)     not null,
            [LastName]              nvarchar(50)     not null,
            [ClinicName]            nvarchar(200)    not null,
            [ClinicAddress]         nvarchar(50)     not null,
            [ClinicCity]            nvarchar(50)     not null,
            [ClinicState]           nchar(2)         not null,
            [ClinicPostal]          nvarchar(10)     not null,
            [ClinicPhoneNumber]     nvarchar(10)     not null,
            [ClinicPhoneExtension]  nvarchar(10)     not null,
            [ClinicFaxNumber]       nvarchar(10)     not null,
            [NumberOfVets]          int              not null,  
            [IpAddress]             nvarchar(20)     not null,
            [MailOptIn]             bit              not null,
            [EmailOptIn]            bit              not null,
            [Created]               datetime         not null,
            [Modified]              datetime         not null,
            [Deleted]               datetime         null
        );
    end
    
    if not exists(select 1 from information_schema.table_constraints where constraint_name = 'pk_registrations')
        alter table dbo.Registrations add
            constraint pk_registrations primary key nonclustered (Id);
    
    if not exists (select 1 from sysindexes where [name] = 'ix_registrations_created')
        create clustered index ix_registrations_created
            on dbo.Registrations(Created);
    
    if not exists (select 1 from sysindexes where [name] = 'ix_registrations_email')
        create index ix_registrations_email
            on dbo.Registrations(EmailAddress);
    
    if not exists (select 1 from sysindexes where [name] = 'ix_registrations_email')
        create index ix_registrations_name_and_clinic
            on dbo.Registrations (FirstName,
                                  LastName,
                                  ClinicName);
    

    002.sql

    /**********************************************************************
      The original schema allowed null for these columns, but we don't want
      that, so update existing nulls and change the columns to disallow 
      null values
     *********************************************************************/
    
    update dbo.Registrations set SourceA = '' where SourceA is null;
    update dbo.Registrations set SourceB = '' where SourceB is null;
    alter table dbo.Registrations alter column SourceA nvarchar(50) not null;
    alter table dbo.Registrations alter column SourceB nvarchar(50) not null;
    
    /**********************************************************************
      The client wanted to modify the signup form to include a fax opt-in
     *********************************************************************/
    
    if not exists 
    (
        select 1 
          from information_schema.columns
         where table_schema = 'dbo'
           and table_name   = 'Registrations'
           and column_name  = 'FaxOptIn'
    )
    alter table dbo.Registrations 
        add FaxOptIn bit null 
            constraint df_registrations_faxoptin default 0;
    

    003.sql, 004.sql, etc...

    At any given time I can run the entire series of scripts against the database in any state and know that things will be immediately brought up to speed with the current version of the app. Because everything is scripted, it's much easier to build a simple installer to do this, and it's adding the schema changes to source control is no problem at all.

    0 讨论(0)
  • 2021-02-02 15:06

    You've got quite a rigorous set of requirements, I'm not sure whether you'll find something that puts checks in all the boxes, especially the multiple concurrent schemas and the intelligent version control.

    The most promising tool that I've read about that kind of fits is Liquibase.
    Here are some additional links:

    • http://en.wikipedia.org/wiki/LiquiBase
    • http://www.ibm.com/developerworks/java/library/j-ap08058/index.html
    0 讨论(0)
提交回复
热议问题