未加星标

Database Migration tools for Mongo DB

字体大小 | |
[数据库(综合) 所属分类 数据库(综合) | 发布者 店小二03 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

Database Migration tools for Mongo DB

When it comes to the problem of migrating database structure, some of you may think about relational databases: there a strict schema, and to remove something(field, table, index, etc.), you need to take action: execute an SQL statement. But when you work on schema-less databases, it may look like you don't need those migrations. But to be honest, are schema-less database are schemaless ? In fact, you have more freedom in column and document-based database, but sooner or later you will have to modify some of the results of your work: remove the index, transform column format, etc. That's why by this article I would like to review the available tools for Mongo migrations.

Mongobee

If you use Spring in your project, then MongoBee should be the most suitable tool for you. The idea is that you write Java methods(changesets), which describe what changes needs to be done. The method annotated by @ChangeSet is taken and applied to the database. Mongobee stores changesets history in dbchangelog collection. If you are a Spring guy, and like Java config among others, then you should choose this tool.

You have two options how to run Mongobee - inside Spring container at the beginning of startup or as an independent process, running separately from Spring.

The first option is good for the local environment when you have a local database and want to keep it up-to-date with shared one:

@Bean public Mongobee mongobee(){ Mongobee runner = new Mongobee("mongodb://YOUR_DB_HOST:27017/DB_NAME"); runner.setDbName("yourDbName"); // host must be set if not set in URI runner.setChangeLogsScanPackage( "com.example.yourapp.changelogs"); // package to scan for changesets runner.setEnabled(true); // optional: default is true return runner; }

The second option is suitable for shared environments - you don't run migrations on startup, but you run them manually before deploying a new version, which requires those changes.In this case, you can run migrations in some main method inside a separate module:

Mongobee runner = new Mongobee("mongodb://YOUR_DB_HOST:27017/DB_NAME"); runner.setDbName("yourDbName"); // host must be set if not set in URI runner.setChangeLogsScanPackage( "com.example.yourapp.changelogs"); // package to scan for changesets runner.setEnabled(true); // optional: default is true runner.execute(); // start migration methods

Then you can start writing

@ChangeSet(order = "001", id = "someChangeWithoutArgs", author = "testAuthor") public void someChange1() { // method without arguments can do some non-db changes } @ChangeSet(order = "002", id = "someChangeWithMongoDatabase", author = "testAuthor") public void someChange2(MongoDatabase db) { // type: com.mongodb.client.MongoDatabase : original MongoDB driver v. 3.x, operations allowed by driver are possible // example: MongoCollection<Document> mycollection = db.getCollection("mycollection"); Document doc = new Document("testName", "example").append("test", "1"); mycollection.insertOne(doc); } @ChangeSet(order = "003", id = "someChangeWithDb", author = "testAuthor") public void someChange3(DB db) { // This is deprecated in mongo-java-driver 3.x, use MongoDatabase instead // type: com.mongodb.DB : original MongoDB driver v. 2.x, operations allowed by driver are possible // example: DBCollection mycollection = db.getCollection("mycollection"); BasicDBObject doc = new BasicDBObject().append("test", "1"); mycollection .insert(doc); } @ChangeSet(order = "004", id = "someChangeWithJongo", author = "testAuthor") public void someChange4(Jongo jongo) { // type: org.jongo.Jongo : Jongo driver can be used, used for simpler notation // example: MongoCollection mycollection = jongo.getCollection("mycollection"); mycollection.insert("{test : 1}"); } @ChangeSet(order = "005", id = "someChangeWithSpringDataTemplate", author = "testAuthor") public void someChange5(MongoTemplate mongoTemplate) { // type: org.springframework.data.mongodb.core.MongoTemplate // Spring Data integration allows using MongoTemplate in the ChangeSet // example: mongoTemplate.save(myEntity); }

Mongobeehas a good Wiki page, if you choose this tool, you need to read it carefully.

Mongeez

Mongeez is something which you also can run with Spring, or without it, the only difference with Mongobee is that this tool forces you to write XML instructions, like this one:

<mongoChangeLog> <changeSet changeId="ChangeSet-1" author="mlysaght"> <script> db.organization.insert({ "Name" : "10Gen", "Location" : "NYC", DateFounded : {"Year":2008, "Month":01, "day":01}}); db.organization.insert({ "Name" : "SecondMarket", "Location" : "NYC", DateFounded : {"Year":2004, "Month":5, "day":4}}); </script> </changeSet> <changeSet changeId="ChangeSet-2" author="mlysaght"> <script> db.user.insert({ "Name" : "Michael Lysaght"}); </script> <script> db.user.insert({ "Name" : "Oleksii Iepishkin"}); </script> </changeSet> </mongoChangeLog>

This tool has an old XML-based config, the last commit to GitHub was made on Dec 25, 2015 and it looks like this is a bit older instrument than Mongobee.

These guys also have a Wiki page describing how to set up their tool, so don't miss a chance to read it.

java-mongo-migrations

java-mongo-migrations is a pretty simple library and it is written on top of Jongo . The main idea - you write migration classes, and run them somewhere:

public class MyApplication { public void start(){ List<MongoCommand> commands = new ArrayList<>(); commands.add(new FirstMigration()); commands.add(new SecondMigration()); try { MongoMigrations migrations = new MongoMigrations("mongo://localhost:27017/my_application_schema"); migrations.setSchemaVersionCollection("_my_custom_schema_version"); migrations.migrate(commands); } catch (MongoMigrationsFailureException e) { LOGGER.error("Failed to migrate database", e); } } }

I don't like to specify commands manually, and it'd be nice to specify package only. However, I like, that this tool is framework-agnostic.

Conclusion

My choice - Mongobee:

It can be framework agnostic I like their changeset format It's clear how the tool works, and Wiki has all needed information. Is MongoDB really a “schemaless” database?

本文数据库(综合)相关术语:系统安全软件

主题: SpringMongoDBJavaXMLSQLGitGitHub
分页:12
转载请注明
本文标题:Database Migration tools for Mongo DB
本站链接:http://www.codesec.net/view/531035.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(综合) | 评论(0) | 阅读(84)