-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigration.php
98 lines (74 loc) · 4.57 KB
/
migration.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
require ("config.php");
function email_letter($to, $from, $subject = 'no subject', $msg = 'no msg') {
$headers = "From: $from\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion ();
return mail ( $to, $subject, $msg, $headers );
}
$cb = new Couchbase ( "127.0.0.1:8091", "openmoney", "", "openmoney" );
$tradingNameJournal_lookup_function = 'function(doc,meta){if(doc.type==\"trading_name_journal\"&&doc.from&&doc.to&&doc.currency){emit(\"trading_name,\"+doc.to+\",\"+doc.currency,doc.amount);}if(doc.type==\"trading_name_journal\"&&doc.from&&doc.to&&doc.currency){emit(\"trading_name,\"+doc.from+\",\"+doc.currency,-doc.amount);}}';
$trading_name_journal_function_name = "tradingnamejournal";
$tradingName_lookup_function = 'function(doc,meta){if(doc.type==\"trading_name\"&&doc.name&&doc.currency&&doc.steward){emit(doc.name,doc.currency);}}';
$trading_name_function_name = "tradingname";
$designDoc = '{"views":{"' . $trading_name_journal_function_name . '":{"map":"' . $tradingNameJournal_lookup_function . '"},"' . $trading_name_function_name . '":{"map":"' . $tradingName_lookup_function . '"}}}';
// echo $designDoc;
$design_doc_name = "dev_migration";
$cb->setDesignDoc ($design_doc_name,json_decode(json_encode($designDoc)));
// $options = array ();
// // do trading name journal lookup
// $tradingnamejournal_result = $cb->view ( $design_doc_name, $trading_name_journal_function_name, $options );
// //print_r( $tradingnamejournal_result );
// foreach ( $tradingnamejournal_result ['rows'] as $journal_trading_name ) {
// $trading_name_journal = json_decode( $cb->get( $journal_trading_name['id'] ), true );
// $trading_name = json_decode( $cb->get ( $journal_trading_name['key'] ), true);
// if ($trading_name['name'] == $trading_name_journal['from']) {
// foreach($trading_name['steward'] as $steward) {
// $trading_name_view = json_decode( $cb->get("trading_name_view," . $steward . "," . $trading_name_journal['to'] . "," . $trading_name_journal['currency']) , true);
// if (!isset($trading_name_view['trading_name'])) {
// $trading_name_from_view['type'] = "trading_name_view";
// $trading_name_from_view['steward'] = array( $steward );
// $trading_name_from_view['trading_name'] = $trading_name_journal['to'];
// $trading_name_from_view['currency'] = $trading_name_journal['currency'];
// $trading_name_from_view['created'] = intval( round(microtime(true) * 1000) );
// $cb->set ("trading_name_view," . $steward . "," . $trading_name_from_view['trading_name'] . "," . $trading_name_from_view['currency'] , json_encode ( $trading_name_from_view ) );
// }
// }
// } else {
// foreach($trading_name['steward'] as $steward) {
// $trading_name_view = json_decode( $cb->get("trading_name_view," . $steward . "," . $trading_name_journal['from'] . "," . $trading_name_journal['currency']) , true);
// if (!isset($trading_name_view['trading_name'])) {
// $trading_name_from_view['type'] = "trading_name_view";
// $trading_name_from_view['steward'] = array( $steward );
// $trading_name_from_view['trading_name'] = $trading_name_journal['from'];
// $trading_name_from_view['currency'] = $trading_name_journal['currency'];
// $trading_name_from_view['created'] = intval( round(microtime(true) * 1000) );
// $cb->set ("trading_name_view," . $steward. "," . $trading_name_from_view['trading_name'] . "," . $trading_name_from_view['currency'] , json_encode ( $trading_name_from_view ) );
// }
// }
// }
// }
// do trading name lookup
$options = array ();
$tradingname_result = $cb->view ( $design_doc_name, $trading_name_function_name, $options );
//print_r( $tradingname_result );
foreach ( $tradingname_result ['rows'] as $trading_name ) {
//print_r($trading_name);
//init
$currency = $trading_name['value'];
$trading_name = $trading_name ['key'];
//do a lookup
$trading_name_array = $cb->get ( "trading_name," . $trading_name . "," . $currency );
$trading_name_array = json_decode ( $trading_name_array, true );
foreach($trading_name_array['steward'] as $steward) {
$currency_view = json_decode( $cb->get("currency_view," . $steward . "," . $currency), true);
if (!isset($currency_view['currency'])) {
$currency_view ['type'] = "currency_view";
$currency_view ['currency'] = "cc";
$currency_view ['steward'] = array ($steward);
$currency_view ['created'] = intval( round(microtime(true) * 1000) );
$cb->set ( "currency_view," . $steward . "," . $currency_view ['currency'], json_encode ( $currency_view ) );
}
}
}