How should you build your database from source control?

后端 未结 11 1448
我寻月下人不归
我寻月下人不归 2020-11-30 15:48

There has been some discussion on the SO community wiki about whether database objects should be version controlled. However, I haven\'t seen much discussion about t

相关标签:
11条回答
  • 2020-11-30 16:28

    Every developer should have their own local database, and use source code control to publish to the team. My solution is here : http://dbsourcetools.codeplex.com/ Have fun, - Nathan

    0 讨论(0)
  • 2020-11-30 16:30

    By asking "teaser questions" you seem to be more interested in a discussion than someone's opinion of final answers. The active (>2500 members) mailing list agileDatabases has addressed many of these questions and is, in my experience, a sophisticated and civil forum for this kind of discussion.

    0 讨论(0)
  • 2020-11-30 16:31

    You might find that Liquibase handles a lot of what you're looking for.

    0 讨论(0)
  • 2020-11-30 16:34

    We use continuous integration via TeamCity. At each checkin to source control, the database and all the test data is re-built from scratch, then the code, then the unit tests are run against the code. If you're using a code-generation tool like CodeSmith, it can also be placed into your build process to generate your data access layer fresh with each build, making sure that all your layers "match up" and do not produce errors due to mismatched SP parameters or missing columns.

    Each build has its own collection of SQL scripts that are stored in the $project\SQL\ directory in source control, assigned a numerical prefix and executed in order. That way, we're practicing our deployment procedure at every build.

    Depending on the lookup table, most of our lookup values are also stored in scripts and run to make sure the configuration data is what we expect for, say, "reason_codes" or "country_codes". This way we can make a lookup data change in dev, test it out and then "promote" it through QA and production, instead of using a tool to modify lookup values in production, which can be dangerous for uptime.

    We also create a set of "rollback" scripts that undo our database changes, in case a build to production goes screwy. You can test the rollback scripts by running them, then re-running the unit tests for the build one version below yours, after its deployment scripts run.

    0 讨论(0)
  • 2020-11-30 16:34

    I basically agree with every answer given by van. Fore more insight, my baseline for database management is K. Scott Allen series (a must read, IMHO. And Jeff's opinion too it seems).

    • Database objects can always be rebuilt from scratch by launching a single SQL file (that can itself call other SQL files) : Create.sql. This can include static data insertion (lists...).
    • The SQL scripts are parameterized so that no environment-dependent and/or sensitive information is stored in plain files.
    • I use a custom batch file to launch Create.sql : Create.cmd. Its goal is mainly to check for pre-requisites (tools, environment variables...) and send parameters to the SQL script. It can also bulk-load static data from CSV files for performance issues.
    • Typically, system user credentials would be passed as a parameter to the Create.cmd file.

    IMHO, dynamic data loading should require another step, depending on your environment. Developers will want to load their database with test, junk or no data at all, while at the other end production managers will want to load production data. I would consider storing test data in source control as well (to ease unit testing, for instance).

    Once the first version of the database has been put into production, you will need not only build scripts (mainly for developers), but also upgrade scripts (based on the same principles) :

    • There must be a way to retrieve the version from the database (I use a stored procedure, but a table would do as well).
    • Before releasing a new version, I create an Upgrade.sql file (that can call other ones) that allows upgrading version N-1 to version N (N being the version being released). I store this script under a folder named N-1.
    • I have a batch file that does the upgrade : Upgrade.cmd. It can retrieve the current version (CV) of the database via a simple SELECT statement, launch the Upgrade.sql script stored under the CV folder, and loop until no folder is found. This way, you can automatically upgrade from, say, N-3 to N.

    Problems with this are :

    • It is difficult to automatically compare database schemas, depending on database vendors. This can lead to incomplete upgrade scripts.
    • Every change to the production environment (usually by DBAs for performance tuning) should find its way to the source control as well. To make sure of this, it is usually possible to log every modification to the database via a trigger. This log is reset after every upgrade.
    • More ideally, though, DBA initiated changes should be part of the release/upgrade process when possible.

    As to what kind of database objects do you want to have under source control ? Well, I would say as much as possible, but not more ;-) If you want to create users with passwords, get them a default password (login/login, practical for unit testing purposes), and make the password change a manual operation. This happens a lot with Oracle where schemas are also users...

    0 讨论(0)
  • 2020-11-30 16:35

    I treat the SQL as source-code when possible

    If I can write it in standard's compliant SQL then it generally goes in a file in my source control. The file will define as much as possible such as SPs, Table CREATE statements.

    I also include dummy data for testing in source control:

    1. proj/sql/setup_db.sql
    2. proj/sql/dummy_data.sql
    3. proj/sql/mssql_specific.sql
    4. proj/sql/mysql_specific.sql

    And then I abstract out all my SQL queries so that I can build the entire project for MySQL, Oracle, MSSQL or anything else.

    Build and test automation uses these build-scripts as they are as important as the app source and tests everything from integrity through triggers, procedures and logging.

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