5 * Setup the core values in the database
10 require_once 'Pman.php';
11 class Pman_Xtuple_UpdateDatabase extends Pman
14 static $cli_desc = "Update SQL - Beta";
16 static $cli_opts = array(
18 'desc' => 'Source directory for json files.',
28 'desc' => 'Base Currency',
36 'fix-sequences' => array(
37 'desc' => 'Run fix sequences -- for testing',
52 $ff = HTML_FlexyFramework::get();
53 if (!empty($ff->cli)) {
59 function get($k = '',$opt)
66 if(!empty($opt['fix-sequences'])){
67 $this->fixSequences();
75 if(empty($opt['source'])){
76 die("Missing Source directory for account json files! Try -f [JSON file path] \n");
79 if (!file_exists($opt['source'])) {
80 die("can not found account json file : {$opt['source']} \n");
83 $accounts = json_decode(file_get_contents($opt['source']),true);
85 DB_DataObject::factory('accnt')->importFromArray($this, $accounts);
90 if($k == 'curr_symbol'){
91 if(!empty($opt['base'])){
92 DB_DataObject::factory('curr_symbol')->setBase($this, $opt['base']);
96 if(!empty($opt['source'])){
98 if (!file_exists($opt['source'])) {
99 die("can not found currency json file : {$opt['source']} \n");
102 $currencies = json_decode(file_get_contents($opt['source']),true);
104 DB_DataObject::factory('curr_symbol')->importFromArray($this, $currencies);
108 die("Missing Base Currency or Source directory for Currency json files! Try -b [base currency] or -f [JSON file path]\n");
111 if($k == 'location'){
112 if(empty($opt['source'])){
113 die("Missing Source directory for location json file! Try -f [JSON file path] \n");
116 if (!file_exists($opt['source'])) {
117 die("can not found location json file : {$opt['source']} \n");
120 $locations = json_decode(file_get_contents($opt['source']),true);
122 DB_DataObject::factory('location')->importFromArray($this, $locations);
128 if(empty($opt['source'])){
129 die("Missing Source directory for terms json file! Try -f [JSON file path] \n");
132 if (!file_exists($opt['source'])) {
133 die("can not found terms json file : {$opt['source']} \n");
136 $terms = json_decode(file_get_contents($opt['source']),true);
138 DB_DataObject::factory('terms')->importFromArray($this, $terms);
144 $this->updateData($k);
151 function updateData($k='') {
152 //DB_DataObject::debugLevel(1);
153 $tables = empty($k) ? array(
155 // 'curr_symbol', //?? fixme
167 // 'location', //?? just check hat one exists.
172 foreach($tables as $t) {
173 $cs = DB_DataObject::factory($t);
174 $cs->initDatabase($this);
181 function fixSequences()
183 DB_DataObject::debugLevel(1);
184 $cs = DB_DataObject::factory('core_enum');
186 SELECT 'SELECT SETVAL(' ||
187 quote_literal(quote_ident(nspname) || '.' || quote_ident(S.relname))||
188 ', MAX(' || quote_ident(C.attname)|| ') ) FROM ' || nspname || '.' || quote_ident(T.relname)|| ';' as cmd
194 WHERE S.relkind = 'S'
196 AND D.refobjid = T.oid
197 AND D.refobjid = C.attrelid
198 AND D.refobjsubid = C.attnum
199 AND NS.oid = T.relnamespace
202 while ($cs->fetch()) {