Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Not necessarily perl, but I'm currently headed towards a perlish solution...

I have two databases, one for development and another for production. They do not share the same data. When I make structural changes in development, I need to apply them to production when I update.

As I am unaware of any native MySQL function combination that addresses this specific need I've begun to think about a perlsih solution.

My current solution is to dump the table structure creation sql, apply some alterations to translate CREATE statements into ALTER / CHANGE statements, and apply that as a patch. This has a few limitations- it doesn't drop leftover columns or keys, and it doesn't create missing columns. While this seems to suit my purposes, I thought I would ask around and see if anyone has seen a similar / better solution.

Here's what I'm using at the moment. YMMV.

#Step 1. Dump/export table creation sql # Note table and field names enclosed in backquotes #Step 2. Run this filter on it to create alter patch #Step 3. Manually add commands to drop keys, drop and create table / f +ield to top of patch. # Note: created tables and fields needn't be accurate, they just n +eed to exist #Step 4. Run this patch on the database while (<>) { s/^\s*CREATE TABLE (`\w+`) \(/ALTER TABLE $1\n/; s/^\s*(`\w+`)/ CHANGE $1 $1/; s/^\s*PRIMARY KEY/ DROP PRIMARY KEY, ADD PRIMARY KEY/; s/^\s*UNIQUE KEY (`\w+`)/ DROP INDEX $1, ADD UNIQUE $1/; s/^\s*KEY (`\w+`)/ DROP INDEX $1, ADD INDEX $1/; s/^\)//; s/ENGINE=(\w+)/ ENGINE=$1,\n/; s/DEFAULT CHARSET=(\w+)/ DEFAULT CHARSET=$1/; s/^\s+(.+?),?\s*$/ $1,\n/; print; }


"One is enough. If you are acquainted with the principle, what do you care for the myriad instances and applications?"
- Henry David Thoreau, Walden

In reply to Generating ALTER TABLE code for MySQL by blogical

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (3)
    As of 2018-01-21 12:09 GMT
    Find Nodes?
      Voting Booth?
      How did you see in the new year?

      Results (228 votes). Check out past polls.