Starting with versioning mysql schemata without overkill. Good solutions?

后端 未结 11 1732
感动是毒
感动是毒 2020-12-28 23:40

I\'ve arrived at the point where I realise that I must start versioning my database schemata and changes. I consequently read the existing posts on SO about that topic but I

相关标签:
11条回答
  • 2020-12-28 23:58

    Our solution is MySQL Workbench. We regularly reverse-engineer the existing Database into a Model with the appropriate version number. It is then possible to easily perform Diffs between versions as needed. Plus, we get nice EER Diagrams, etc.

    0 讨论(0)
  • 2020-12-28 23:58

    I think this question deserves a modern answer so I'm going to give it myself. When I wrote the question in 2009 I don't think Phinx already existed and most definitely Laravel didn't.

    Today, the answer to this question is very clear: Write incremental DB migration scripts, each with an up and a down method and run all these scripts or a delta of them when installing or updating your app. And obviously add the migration scripts to your VCS.

    As mentioned in the beginning, there are excellent tools today in the PHP world which help you manage your migrations easily. Laravel has DB migrations built-in including the respective shell commands. Everyone else has a similarly powerful framework agnostic solution with Phinx.

    Both Artisan migrations (Laravel) and Phinx work the same. For every change in the DB, create a new migration, use plain SQL or the built-in query builder to write the up and down methods and run artisan migrate resp. phinx migrate in the console.

    0 讨论(0)
  • 2020-12-29 00:01

    At our company we did it this way:

    We put all tables / db objects in their own file, like tbl_Foo.sql. The files contain several "parts" that are delimited with

    -- part: create
    

    where create is just a descriptive identification for a given part, the file looks like:

    -- part: create
    IF not exists ...
    CREATE TABLE tbl_Foo ...
    -- part: addtimestamp
    IF not exists ...
    BEGIN
       ALTER TABLE ...
    END
    

    Then we have an xml file that references every single part that we want executed when we update database to new schema. It looks pretty much like this:

    <playlist>
       <classes>
         <class name="table" desc="Table creation" />
         <class name="schema" desc="Table optimization" />
       </classes>
       <dbschema>
          <steps db="a_database">
             <step file="tbl_Foo.sql" part="create" class="table" />
             <step file="tbl_Bar.sql" part="create" class="table" />
          </steps>
          <steps db="a_database">
             <step file="tbl_Foo.sql" part="addtimestamp" class="schema" />
          </steps>
       </dbschema>          
    </playlist>
    

    The <classes/> part if for GUI, and <dbschema/> with <steps/> is to partition changes. The <step/>:s are executed sequentially. We have some other entities, like sqlclr to do different things like deploy binary files, but that's pretty much it.

    Of course we have a component that takes that playlist file and a resource / filesystem object that crossreferences the playlist and takes out wanted parts and then runs them as admin on database.

    Since the "parts" in .sql's are written so they can be executed on any version of DB, we can run all parts on every previous/older version of DB and modify it to be current. Of course there are some cases where SQL server parses column names "early" and we have to later modify part's to become exec_sqls, but it doesn't happen often.

    0 讨论(0)
  • 2020-12-29 00:10

    Where I work we have an install script for each new version of the app which has the sql we need to run for the upgrade. This works well enough for 6 devs with some branching for maintenance releases. We're considering moving to Auto Patch http://autopatch.sourceforge.net/ which handles working out what patches to apply to any database you are upgrading. It looks like there may be some small complication handling branching with auto Patch, but it doesn't sound like that'll be an issue for you.

    0 讨论(0)
  • 2020-12-29 00:12

    Simple way for a small company: dump your database to SQL and add it to your repository. Then every time you change something, add the changes in the dump file.

    You can then use diff to see changes between versions, not to mention have comments explaining your changes. This will also make you virtually immune to MySQL upgrades.

    The one downside I've seen to this is that you have to remember to manually add the SQL to your dumpfile. You can train yourself to always remember, but be careful if you work with others. Missing an update could be a pain later on.

    This could be mitigated by creating some elaborate script to do it for you when submitting to subversion but it's a bit much for a one man show.

    Edit: In the year that's gone by since this answer, I've had to implement a versioning scheme for MySQL for a small team. Manually adding each change was seen as a cumbersome solution, much like it was mentioned in the comments, so we went with dumping the database and adding that file to version control.

    What we found was that test data was ending up in the dump and was making it quite difficult to figure out what had changed. This could be solved by dumping the schema only, but this was impossible for our projects since our applications depended on certain data in the database to function. Eventually we returned to manually adding changes to the database dump.

    Not only was this the simplest solution, but it also solved certain issues that some versions of MySQL have with exporting/importing. Normally we would have to dump the development database, remove any test data, log entries, etc, remove/change certain names where applicable and only then be able to create the production database. By manually adding changes we could control exactly what would end up in production, a little at a time, so that in the end everything was ready and moving to the production environment was as painless as possible.

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