title: flyway integration into existing systems
body: <p>We're thinking about integrating flyway into our system, as it seems like a great tool to manage database migrations in an efficient way.</p>
  
  <p>However, I am not quite sure how to proceed:</p>
  
  <p>We have three different databases</p>
  
  <ol>
  <li>Production Environment (MySQL)</li>
  <li>Test Environment (MySQL)</li>
  <li>Unit Testing (H2 in-memory)</li>
  </ol>
  
  <p>They all contain different data (different users etc.). There is no common data (on the flyway page this is called reference data) between the databases, only the structure should remain the same.</p>
  
  <p>Looking at the website my understanding is to proceed like this:</p>
  
  <ol>
  <li><p>We have to extract the schema version from our production environment and save this in a file such as V1__BASE_version.sql. I would do this e.g. with this:</p>
  
  <p>mysqldump -d -u username -p password -h hostname dbname</p></li>
  <li><p>We make sure that the ddl matches that of the test environment by also getting a dump of the structure and comparing it to the structure from the production system (start with a diff and then manually where it is not clear). In the unlikely event that we find any difference we get rid of them by changing the db structure for test or live (depending on what makes more sense).</p></li>
  <li><p>We initialize all databases with data with an initial version as described by the homepage. For my different databases I use different -Durl=, -Dusername= and -Dpassword= paramters.</p>
  
  <p>mvn flyway:init -Dflyway.initVersion=1 -Dflyway.initDescription="Base version"</p></li>
  <li><p>Configure our spring setup to pick up flyway (as described on the homepage). This makes sure migrations are applied automatically and that the database is in a consistent state with the application</p></li>
  </ol>
  
  <p>Is this the correct way to do it?
  Did I forget any important steps?</p>
  
  <p>I have one remaining question about adding test data to the in-memory database:</p>
  
  <p>Where is the correct place to inset the unit test data?
  Even if I setup flyway using spring and make hibernate depend on it using</p>
  
  <pre><code>&lt;bean id="sessionFactory" class="..." depends-on="flyway"&gt;
  ...
  &lt;/bean&gt;
  </code></pre>
  
  <p>the datasource will be created before and this is currently the place where we add our schema and test data. If we still add the sample data when creating the datasource the schema will not be the correct one if I am not mistaken.</p>
  
  <p>How can we use test-data with flyway using an in-memory database?</p>
  
  <p>Thanks!</p>