flyway Chinese tutorial

Foreword: I have encountered a problem at work recently. During the project development process, the development code can be version controlled through svn, but how should the database be managed? When multiple people contact the database, when the table, field or data is modified, how can it be synchronized? After searching hard, I found an open source project called flyway: http://flywaydb.org/, written in java, the official introduction is my original intention, how can I not be excited?

 

The official website is in English, but there is no Chinese document after checking. Is it not used by Chinese?

 

I slowly read the official documentation, tried to do it, and it was a success! (Leaving the computer, jumping a few times, and continuing to sit back at the computer)

 

Write your own tutorial

 

1. Put flyway-core-2.3.jar in the project lib, download address: http://flywaydb.org/getstarted/download.html

 

2. Create a path to save the sql version file in the src directory: src/db/migration, the default search path for flyway can be changed, but it is not necessary.



  

 

3. Add the sql file to the sql version file path, and the naming rules, such as: V1__2014_4_13.sql, start with V + version number + double underscore + description, the description can have an underscore, and the suffix is ​​sql. Don't ask if you can change this rule, otherwise, I'll bite you.

 

4. Add the java class of flyway, there are command line tools, but the java class is convenient to use, as follows:

package com.cms.flyway;

import java.io.IOException;
import java.util.Properties;

import com.googlecode.flyway.core.Flyway;

public class FlywayApp {
	
	// Read database configuration parameters
	private static Properties config = new Properties();
	static {
		try {
			config.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("activerecord.properties"));
		} catch (IOException e) {
			e.printStackTrace ();
		}
	}
	
	// Perform database version upgrade
	public static void migration() {
		// Create the Flyway instance
		Flyway flyway = new Flyway();
		
		// Point it to the database
		flyway.setDataSource(config.getProperty("com.et.ar.ActiveRecordBase.url"), config.getProperty("com.et.ar.ActiveRecordBase.username"), config.getProperty("com.et.ar.ActiveRecordBase.password"));
		flyway.setInitOnMigrate(true);
		
		// Start the migration
		flyway.migrate();
	}
}

 

5. When the server starts or the timer executes the migration() method of this class.

 

6. The first execution will generate a table dedicated to storing the database schema_version



 

7. After the database has new changes, export the new version of the sql file (such as: mysqldump -u -p databasename>/xx.sql) and change it to the new version named file and put it in the db.migration path, and flyway will automatically update it for you database version.

 

over!

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326637760&siteId=291194637