@@ -11,7 +11,8 @@ var Slouch = require('couch-slouch'),
1111var Cluster = function ( params ) {
1212 this . _params = params ;
1313
14- this . _slouch = new Slouch ( params . source ) ;
14+ this . _sourceSlouch = new Slouch ( params . source ) ;
15+ this . _targetSlouch = new Slouch ( params . target ) ;
1516
1617 if ( this . _params . concurrency === 1 ) {
1718 // Don't use a throttler
@@ -25,13 +26,42 @@ var Cluster = function (params) {
2526
2627Cluster . prototype . replicate = function ( ) {
2728 var self = this ;
28- return self . _slouch . db . all ( ) . each ( function ( db ) {
29+ return self . _sourceSlouch . db . all ( ) . each ( function ( db ) {
2930 return self . _replicateDB ( db ) ;
3031 } , self . _throttler ) ;
3132} ;
3233
34+ Cluster . prototype . _createDBIfMissing = function ( db ) {
35+ var self = this ;
36+ return self . _targetSlouch . db . exists ( db ) . then ( function ( exists ) {
37+ if ( ! exists ) {
38+ return self . _targetSlouch . db . create ( db ) ;
39+ }
40+ } ) ;
41+ } ;
42+
43+ Cluster . prototype . _replicateSecurity = function ( db ) {
44+ var self = this ;
45+ return self . _sourceSlouch . security . get ( db ) . then ( function ( security ) {
46+ return self . _targetSlouch . security . set ( db , security ) ;
47+ } ) ;
48+ } ;
49+
50+ Cluster . prototype . _replicateRawDB = function ( db ) {
51+ return this . _sourceSlouch . db . replicate ( {
52+ source : this . _params . source + '/' + db ,
53+ target : this . _params . target + '/' + db
54+ } ) ;
55+ } ;
56+
3357Cluster . prototype . _replicateDB = function ( db ) {
34- console . log ( db ) ;
58+ var self = this ;
59+ return self . _createDBIfMissing ( db ) . then ( function ( ) {
60+ // Replicate security first so that security is put in place before data is copied over
61+ return self . _replicateSecurity ( db ) ;
62+ } ) . then ( function ( ) {
63+ return self . _replicateRawDB ( db ) ;
64+ } ) ;
3565} ;
3666
3767module . exports = Cluster ;
0 commit comments