upgrade.php 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422
  1. <?php
  2. if(!defined('POSTFIXADMIN')) {
  3. require_once('common.php');
  4. }
  5. /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */
  6. # @version $Id: upgrade.php 1798 2015-09-18 05:10:13Z gingerdog $
  7. # Note: run with upgrade.php?debug=1 to see all SQL error messages
  8. /**
  9. * Use this to check whether an object (Table, index etc) exists within a
  10. * PostgreSQL database.
  11. * @param String the object name
  12. * @return boolean true if it exists
  13. */
  14. function _pgsql_object_exists($name) {
  15. $sql = "select relname from pg_class where relname = '$name'";
  16. $r = db_query($sql);
  17. if($r['rows'] == 1) {
  18. return true;
  19. }
  20. return false;
  21. }
  22. function _pgsql_field_exists($table, $field) {
  23. # $table = table_by_key($table); # _pgsql_field_exists is always called with the expanded table name - don't expand it twice
  24. $sql = '
  25. SELECT
  26. a.attname,
  27. pg_catalog.format_type(a.atttypid, a.atttypmod) AS "Datatype"
  28. FROM
  29. pg_catalog.pg_attribute a
  30. WHERE
  31. a.attnum > 0
  32. AND NOT a.attisdropped
  33. AND a.attrelid = (
  34. SELECT c.oid
  35. FROM pg_catalog.pg_class c
  36. LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
  37. WHERE c.relname ~ ' . "'^($table)\$'
  38. AND pg_catalog.pg_table_is_visible(c.oid)
  39. )
  40. AND a.attname = '$field' ";
  41. $r = db_query($sql);
  42. $row = db_row($r['result']);
  43. if($row) {
  44. return true;
  45. }
  46. return false;
  47. }
  48. function _mysql_field_exists($table, $field) {
  49. # $table = table_by_key($table); # _mysql_field_exists is always called with the expanded table name - don't expand it twice
  50. $sql = "SHOW COLUMNS FROM $table LIKE '$field'";
  51. $r = db_query($sql);
  52. $row = db_row($r['result']);
  53. if($row) {
  54. return true;
  55. }
  56. return false;
  57. }
  58. function _db_field_exists($table, $field) {
  59. global $CONF;
  60. if($CONF['database_type'] == 'pgsql') {
  61. return _pgsql_field_exists($table, $field);
  62. } else {
  63. return _mysql_field_exists($table, $field);
  64. }
  65. }
  66. function _upgrade_filter_function($name) {
  67. return preg_match('/upgrade_[\d]+(_mysql|_pgsql)?$/', $name) == 1;
  68. }
  69. function _db_add_field($table, $field, $fieldtype, $after) {
  70. global $CONF;
  71. $query = "ALTER TABLE " . table_by_key($table) . " ADD COLUMN $field $fieldtype";
  72. if($CONF['database_type'] != 'pgsql') {
  73. $query .= " AFTER $after "; # PgSQL does not support to specify where to add the column, MySQL does
  74. }
  75. if(! _db_field_exists(table_by_key($table), $field)) {
  76. $result = db_query_parsed($query);
  77. } else {
  78. printdebug ("field already exists: $table.$field");
  79. }
  80. }
  81. function printdebug($text) {
  82. if (safeget('debug') != "") print "<p style='color:#999'>$text</p>";
  83. }
  84. $table = table_by_key('config');
  85. if($CONF['database_type'] == 'pgsql') {
  86. // check if table already exists, if so, don't recreate it
  87. $r = db_query("SELECT relname FROM pg_class WHERE relname = '$table'");
  88. if($r['rows'] == 0) {
  89. $pgsql = "
  90. CREATE TABLE $table (
  91. id SERIAL,
  92. name VARCHAR(20) NOT NULL UNIQUE,
  93. value VARCHAR(20) NOT NULL,
  94. PRIMARY KEY(id)
  95. )";
  96. db_query_parsed($pgsql);
  97. }
  98. }
  99. else {
  100. $mysql = "
  101. CREATE TABLE {IF_NOT_EXISTS} $table (
  102. `id` {AUTOINCREMENT} {PRIMARY},
  103. `name` VARCHAR(20) {LATIN1} NOT NULL DEFAULT '',
  104. `value` VARCHAR(20) {LATIN1} NOT NULL DEFAULT '',
  105. UNIQUE name ( `name` )
  106. )
  107. ";
  108. db_query_parsed($mysql, 0, " ENGINE = MYISAM COMMENT = 'PostfixAdmin settings'");
  109. }
  110. $sql = "SELECT * FROM $table WHERE name = 'version'";
  111. // insert into config('version', '01');
  112. $r = db_query($sql);
  113. if($r['rows'] == 1) {
  114. $rs = $r['result'];
  115. $row = db_array($rs);
  116. $version = $row['value'];
  117. } else {
  118. db_query_parsed("INSERT INTO $table (name, value) VALUES ('version', '0')", 0, '');
  119. $version = 0;
  120. }
  121. _do_upgrade($version);
  122. function _do_upgrade($current_version) {
  123. global $CONF;
  124. $target_version = 0;
  125. // Rather than being bound to an svn revision number, just look for the largest function name that matches upgrade_\d+...
  126. // $target_version = preg_replace('/[^0-9]/', '', '$Revision: 1798 $');
  127. $funclist = get_defined_functions();
  128. $our_upgrade_functions = array_filter($funclist['user'], '_upgrade_filter_function');
  129. foreach($our_upgrade_functions as $function_name) {
  130. $bits = explode("_", $function_name);
  131. $function_number = $bits[1];
  132. if($function_number > $current_version && $function_number > $target_version) {
  133. $target_version = $function_number;
  134. }
  135. }
  136. if ($current_version >= $target_version) {
  137. # already up to date
  138. echo "<p>Database is up to date</p>";
  139. return true;
  140. }
  141. echo "<p>Updating database:</p><p>- old version: $current_version; target version: $target_version</p>\n";
  142. echo "<div style='color:#999'>&nbsp;&nbsp;(If the update doesn't work, run setup.php?debug=1 to see the detailed error messages and SQL queries.)</div>";
  143. for ($i = $current_version +1; $i <= $target_version; $i++) {
  144. $function = "upgrade_$i";
  145. $function_mysql = $function . "_mysql";
  146. $function_pgsql = $function . "_pgsql";
  147. if (function_exists($function)) {
  148. echo "<p>updating to version $i (all databases)...";
  149. $function();
  150. echo " &nbsp; done";
  151. }
  152. if ($CONF['database_type'] == 'mysql' || $CONF['database_type'] == 'mysqli' ) {
  153. if (function_exists($function_mysql)) {
  154. echo "<p>updating to version $i (MySQL)...";
  155. $function_mysql();
  156. echo " &nbsp; done";
  157. }
  158. } elseif($CONF['database_type'] == 'pgsql') {
  159. if (function_exists($function_pgsql)) {
  160. echo "<p>updating to version $i (PgSQL)...";
  161. $function_pgsql();
  162. echo " &nbsp; done";
  163. }
  164. }
  165. // Update config table so we don't run the same query twice in the future.
  166. $i = (int) $i;
  167. $table = table_by_key('config');
  168. $sql = "UPDATE $table SET value = $i WHERE name = 'version'";
  169. db_query($sql);
  170. };
  171. }
  172. /**
  173. * Replaces database specific parts in a query
  174. * @param String sql query with placeholders
  175. * @param int (optional) whether errors should be ignored (0=false)
  176. * @param String (optional) MySQL specific code to attach, useful for COMMENT= on CREATE TABLE
  177. * @return String sql query
  178. */
  179. function db_query_parsed($sql, $ignore_errors = 0, $attach_mysql = "") {
  180. global $CONF;
  181. if ($CONF['database_type'] == 'mysql' || $CONF['database_type'] == 'mysqli' ) {
  182. $replace = array(
  183. '{AUTOINCREMENT}' => 'int(11) not null auto_increment',
  184. '{PRIMARY}' => 'primary key',
  185. '{UNSIGNED}' => 'unsigned' ,
  186. '{FULLTEXT}' => 'FULLTEXT',
  187. '{BOOLEAN}' => "tinyint(1) NOT NULL DEFAULT '" . db_get_boolean(False) . "'",
  188. '{UTF-8}' => '/*!40100 CHARACTER SET utf8 */',
  189. '{LATIN1}' => '/*!40100 CHARACTER SET latin1 */',
  190. '{IF_NOT_EXISTS}' => 'IF NOT EXISTS',
  191. '{RENAME_COLUMN}' => 'CHANGE COLUMN',
  192. '{MYISAM}' => 'ENGINE=MyISAM',
  193. '{INNODB}' => 'ENGINE=InnoDB',
  194. '{INT}' => 'integer NOT NULL DEFAULT 0',
  195. '{BIGINT}' => 'bigint NOT NULL DEFAULT 0',
  196. '{DATE}' => "timestamp NOT NULL default '2000-01-01'", # MySQL needs a sane default (no default is interpreted as CURRENT_TIMESTAMP, which is ...
  197. '{DATECURRENT}' => 'timestamp NOT NULL default CURRENT_TIMESTAMP', # only allowed once per table in MySQL
  198. );
  199. $sql = "$sql $attach_mysql";
  200. } elseif($CONF['database_type'] == 'pgsql') {
  201. $replace = array(
  202. '{AUTOINCREMENT}' => 'SERIAL',
  203. '{PRIMARY}' => 'primary key',
  204. '{UNSIGNED}' => '',
  205. '{FULLTEXT}' => '',
  206. '{BOOLEAN}' => "BOOLEAN NOT NULL DEFAULT '" . db_get_boolean(False) . "'",
  207. '{UTF-8}' => '', # UTF-8 is simply ignored.
  208. '{LATIN1}' => '', # same for latin1
  209. '{IF_NOT_EXISTS}' => '', # does not work with PgSQL
  210. '{RENAME_COLUMN}' => 'ALTER COLUMN', # PgSQL : ALTER TABLE x RENAME x TO y
  211. '{MYISAM}' => '',
  212. '{INNODB}' => '',
  213. '{INT}' => 'integer NOT NULL DEFAULT 0',
  214. '{BIGINT}' => 'bigint NOT NULL DEFAULT 0',
  215. 'int(1)' => 'int',
  216. 'int(10)' => 'int',
  217. 'int(11)' => 'int',
  218. 'int(4)' => 'int',
  219. '{DATE}' => "timestamp with time zone default '2000-01-01'", # stay in sync with MySQL
  220. '{DATECURRENT}' => 'timestamp with time zone default now()',
  221. );
  222. } else {
  223. echo "Sorry, unsupported database type " . $conf['database_type'];
  224. exit;
  225. }
  226. $replace['{BOOL_TRUE}'] = db_get_boolean(True);
  227. $replace['{BOOL_FALSE}'] = db_get_boolean(False);
  228. $query = trim(str_replace(array_keys($replace), $replace, $sql));
  229. if (safeget('debug') != "") {
  230. printdebug ($query);
  231. }
  232. $result = db_query($query, $ignore_errors);
  233. if (safeget('debug') != "") {
  234. print "<div style='color:#f00'>" . $result['error'] . "</div>";
  235. }
  236. return $result;
  237. }
  238. function _drop_index ($table, $index) {
  239. global $CONF;
  240. $table = table_by_key ($table);
  241. if ($CONF['database_type'] == 'mysql' || $CONF['database_type'] == 'mysqli' ) {
  242. return "ALTER TABLE $table DROP INDEX $index";
  243. } elseif($CONF['database_type'] == 'pgsql') {
  244. return "DROP INDEX $index"; # Index names are unique with a DB for PostgreSQL
  245. } else {
  246. echo "Sorry, unsupported database type " . $conf['database_type'];
  247. exit;
  248. }
  249. }
  250. function _add_index($table, $indexname, $fieldlist) {
  251. global $CONF;
  252. $table = table_by_key ($table);
  253. if ($CONF['database_type'] == 'mysql' || $CONF['database_type'] == 'mysqli' ) {
  254. $fieldlist = str_replace(',', '`,`', $fieldlist); # fix quoting if index contains multiple fields
  255. return "ALTER TABLE $table ADD INDEX `$indexname` ( `$fieldlist` )";
  256. } elseif($CONF['database_type'] == 'pgsql') {
  257. $pgindexname = $table . "_" . $indexname . '_idx';
  258. return "CREATE INDEX $pgindexname ON $table($fieldlist);"; # Index names are unique with a DB for PostgreSQL
  259. } else {
  260. echo "Sorry, unsupported database type " . $conf['database_type'];
  261. exit;
  262. }
  263. }
  264. function upgrade_1_mysql() {
  265. // CREATE MYSQL DATABASE TABLES.
  266. $admin = table_by_key('admin');
  267. $alias = table_by_key('alias');
  268. $domain = table_by_key('domain');
  269. $domain_admins = table_by_key('domain_admins');
  270. $log = table_by_key('log');
  271. $mailbox = table_by_key('mailbox');
  272. $vacation = table_by_key('vacation');
  273. $sql = array();
  274. $sql[] = "
  275. CREATE TABLE {IF_NOT_EXISTS} $admin (
  276. `username` varchar(255) NOT NULL default '',
  277. `password` varchar(255) NOT NULL default '',
  278. `created` datetime NOT NULL default '0000-00-00 00:00:00',
  279. `modified` datetime NOT NULL default '0000-00-00 00:00:00',
  280. `active` tinyint(1) NOT NULL default '1',
  281. PRIMARY KEY (`username`)
  282. ) {MYISAM} COMMENT='Postfix Admin - Virtual Admins';";
  283. $sql[] = "
  284. CREATE TABLE {IF_NOT_EXISTS} $alias (
  285. `address` varchar(255) NOT NULL default '',
  286. `goto` text NOT NULL,
  287. `domain` varchar(255) NOT NULL default '',
  288. `created` datetime NOT NULL default '0000-00-00 00:00:00',
  289. `modified` datetime NOT NULL default '0000-00-00 00:00:00',
  290. `active` tinyint(1) NOT NULL default '1',
  291. PRIMARY KEY (`address`)
  292. ) {MYISAM} COMMENT='Postfix Admin - Virtual Aliases'; ";
  293. $sql[] = "
  294. CREATE TABLE {IF_NOT_EXISTS} $domain (
  295. `domain` varchar(255) NOT NULL default '',
  296. `description` varchar(255) NOT NULL default '',
  297. `aliases` int(10) NOT NULL default '0',
  298. `mailboxes` int(10) NOT NULL default '0',
  299. `maxquota` bigint(20) NOT NULL default '0',
  300. `quota` bigint(20) NOT NULL default '0',
  301. `transport` varchar(255) default NULL,
  302. `backupmx` tinyint(1) NOT NULL default '0',
  303. `created` datetime NOT NULL default '0000-00-00 00:00:00',
  304. `modified` datetime NOT NULL default '0000-00-00 00:00:00',
  305. `active` tinyint(1) NOT NULL default '1',
  306. PRIMARY KEY (`domain`)
  307. ) {MYISAM} COMMENT='Postfix Admin - Virtual Domains'; ";
  308. $sql[] = "
  309. CREATE TABLE {IF_NOT_EXISTS} $domain_admins (
  310. `username` varchar(255) NOT NULL default '',
  311. `domain` varchar(255) NOT NULL default '',
  312. `created` datetime NOT NULL default '0000-00-00 00:00:00',
  313. `active` tinyint(1) NOT NULL default '1',
  314. KEY username (`username`)
  315. ) {MYISAM} COMMENT='Postfix Admin - Domain Admins';";
  316. $sql[] = "
  317. CREATE TABLE {IF_NOT_EXISTS} $log (
  318. `timestamp` datetime NOT NULL default '0000-00-00 00:00:00',
  319. `username` varchar(255) NOT NULL default '',
  320. `domain` varchar(255) NOT NULL default '',
  321. `action` varchar(255) NOT NULL default '',
  322. `data` varchar(255) NOT NULL default '',
  323. KEY timestamp (`timestamp`)
  324. ) {MYISAM} COMMENT='Postfix Admin - Log';";
  325. $sql[] = "
  326. CREATE TABLE {IF_NOT_EXISTS} $mailbox (
  327. `username` varchar(255) NOT NULL default '',
  328. `password` varchar(255) NOT NULL default '',
  329. `name` varchar(255) NOT NULL default '',
  330. `maildir` varchar(255) NOT NULL default '',
  331. `quota` bigint(20) NOT NULL default '0',
  332. `domain` varchar(255) NOT NULL default '',
  333. `created` datetime NOT NULL default '0000-00-00 00:00:00',
  334. `modified` datetime NOT NULL default '0000-00-00 00:00:00',
  335. `active` tinyint(1) NOT NULL default '1',
  336. PRIMARY KEY (`username`)
  337. ) {MYISAM} COMMENT='Postfix Admin - Virtual Mailboxes';";
  338. $sql[] = "
  339. CREATE TABLE {IF_NOT_EXISTS} $vacation (
  340. email varchar(255) NOT NULL ,
  341. subject varchar(255) NOT NULL,
  342. body text NOT NULL,
  343. cache text NOT NULL,
  344. domain varchar(255) NOT NULL ,
  345. created datetime NOT NULL default '0000-00-00 00:00:00',
  346. active tinyint(4) NOT NULL default '1',
  347. PRIMARY KEY (email),
  348. KEY email (email)
  349. ) {INNODB} DEFAULT CHARSET=latin1 COMMENT='Postfix Admin - Virtual Vacation' ;";
  350. foreach($sql as $query) {
  351. db_query_parsed($query);
  352. }
  353. }
  354. function upgrade_2_mysql() {
  355. # upgrade pre-2.1 database
  356. # from TABLE_BACKUP_MX.TXT
  357. $table_domain = table_by_key ('domain');
  358. if(!_mysql_field_exists($table_domain, 'transport')) {
  359. $result = db_query_parsed("ALTER TABLE $table_domain ADD COLUMN transport VARCHAR(255) AFTER maxquota;", TRUE);
  360. }
  361. if(!_mysql_field_exists($table_domain, 'backupmx')) {
  362. $result = db_query_parsed("ALTER TABLE $table_domain ADD COLUMN backupmx {BOOLEAN} AFTER transport;", TRUE);
  363. }
  364. }
  365. function upgrade_2_pgsql() {
  366. if(!_pgsql_object_exists(table_by_key('domain'))) {
  367. db_query_parsed("
  368. CREATE TABLE " . table_by_key('domain') . " (
  369. domain character varying(255) NOT NULL,
  370. description character varying(255) NOT NULL default '',
  371. aliases integer NOT NULL default 0,
  372. mailboxes integer NOT NULL default 0,
  373. maxquota integer NOT NULL default 0,
  374. quota integer NOT NULL default 0,
  375. transport character varying(255) default NULL,
  376. backupmx boolean NOT NULL default false,
  377. created timestamp with time zone default now(),
  378. modified timestamp with time zone default now(),
  379. active boolean NOT NULL default true,
  380. Constraint \"domain_key\" Primary Key (\"domain\")
  381. );
  382. CREATE INDEX domain_domain_active ON " . table_by_key('domain') . "(domain,active);
  383. COMMENT ON TABLE " . table_by_key('domain') . " IS 'Postfix Admin - Virtual Domains';
  384. ");
  385. }
  386. if(!_pgsql_object_exists(table_by_key('admin'))) {
  387. db_query_parsed('
  388. CREATE TABLE ' . table_by_key("admin") . ' (
  389. "username" character varying(255) NOT NULL,
  390. "password" character varying(255) NOT NULL default \'\',
  391. "created" timestamp with time zone default now(),
  392. "modified" timestamp with time zone default now(),
  393. "active" boolean NOT NULL default true,
  394. Constraint "admin_key" Primary Key ("username")
  395. );' . "
  396. COMMENT ON TABLE " . table_by_key('admin') . " IS 'Postfix Admin - Virtual Admins';
  397. ");
  398. }
  399. if(!_pgsql_object_exists(table_by_key('alias'))) {
  400. db_query_parsed('
  401. CREATE TABLE ' . table_by_key("alias") . ' (
  402. address character varying(255) NOT NULL,
  403. goto text NOT NULL,
  404. domain character varying(255) NOT NULL REFERENCES "' . table_by_key("domain") . '",
  405. created timestamp with time zone default now(),
  406. modified timestamp with time zone default now(),
  407. active boolean NOT NULL default true,
  408. Constraint "alias_key" Primary Key ("address")
  409. );
  410. CREATE INDEX alias_address_active ON ' . table_by_key("alias") . '(address,active);
  411. COMMENT ON TABLE ' . table_by_key("alias") . ' IS \'Postfix Admin - Virtual Aliases\';
  412. ');
  413. }
  414. if(!_pgsql_object_exists(table_by_key('domain_admins'))) {
  415. db_query_parsed('
  416. CREATE TABLE ' . table_by_key('domain_admins') . ' (
  417. username character varying(255) NOT NULL,
  418. domain character varying(255) NOT NULL REFERENCES "' . table_by_key('domain') . '",
  419. created timestamp with time zone default now(),
  420. active boolean NOT NULL default true
  421. );
  422. COMMENT ON TABLE ' . table_by_key('domain_admins') . ' IS \'Postfix Admin - Domain Admins\';
  423. ');
  424. }
  425. if(!_pgsql_object_exists(table_by_key('log'))) {
  426. db_query_parsed('
  427. CREATE TABLE ' . table_by_key('log') . ' (
  428. timestamp timestamp with time zone default now(),
  429. username character varying(255) NOT NULL default \'\',
  430. domain character varying(255) NOT NULL default \'\',
  431. action character varying(255) NOT NULL default \'\',
  432. data text NOT NULL default \'\'
  433. );
  434. COMMENT ON TABLE ' . table_by_key('log') . ' IS \'Postfix Admin - Log\';
  435. ');
  436. }
  437. if(!_pgsql_object_exists(table_by_key('mailbox'))) {
  438. db_query_parsed('
  439. CREATE TABLE ' . table_by_key('mailbox') . ' (
  440. username character varying(255) NOT NULL,
  441. password character varying(255) NOT NULL default \'\',
  442. name character varying(255) NOT NULL default \'\',
  443. maildir character varying(255) NOT NULL default \'\',
  444. quota integer NOT NULL default 0,
  445. domain character varying(255) NOT NULL REFERENCES "' . table_by_key('domain') . '",
  446. created timestamp with time zone default now(),
  447. modified timestamp with time zone default now(),
  448. active boolean NOT NULL default true,
  449. Constraint "mailbox_key" Primary Key ("username")
  450. );
  451. CREATE INDEX mailbox_username_active ON ' . table_by_key('mailbox') . '(username,active);
  452. COMMENT ON TABLE ' . table_by_key('mailbox') . ' IS \'Postfix Admin - Virtual Mailboxes\';
  453. ');
  454. }
  455. if(!_pgsql_object_exists(table_by_key('vacation'))) {
  456. db_query_parsed('
  457. CREATE TABLE ' . table_by_key('vacation') . ' (
  458. email character varying(255) PRIMARY KEY,
  459. subject character varying(255) NOT NULL,
  460. body text NOT NULL ,
  461. cache text NOT NULL ,
  462. "domain" character varying(255) NOT NULL REFERENCES "' . table_by_key('domain') . '",
  463. created timestamp with time zone DEFAULT now(),
  464. active boolean DEFAULT true NOT NULL
  465. );
  466. CREATE INDEX vacation_email_active ON ' . table_by_key('vacation') . '(email,active);');
  467. }
  468. if(!_pgsql_object_exists(table_by_key('vacation_notification'))) {
  469. db_query_parsed('
  470. CREATE TABLE ' . table_by_key('vacation_notification') . ' (
  471. on_vacation character varying(255) NOT NULL REFERENCES ' . table_by_key('vacation') . '(email) ON DELETE CASCADE,
  472. notified character varying(255) NOT NULL,
  473. notified_at timestamp with time zone NOT NULL DEFAULT now(),
  474. CONSTRAINT vacation_notification_pkey primary key(on_vacation,notified)
  475. );
  476. ');
  477. }
  478. // this handles anyone who is upgrading... (and should have no impact on new installees)
  479. $table_domain = table_by_key ('domain');
  480. $result = db_query_parsed("ALTER TABLE $table_domain ADD COLUMN transport VARCHAR(255)", TRUE);
  481. $result = db_query_parsed("ALTER TABLE $table_domain ADD COLUMN backupmx BOOLEAN DEFAULT false", TRUE);
  482. }
  483. function upgrade_3_mysql() {
  484. # upgrade pre-2.1 database
  485. # from TABLE_CHANGES.TXT
  486. $table_admin = table_by_key ('admin');
  487. $table_alias = table_by_key ('alias');
  488. $table_domain = table_by_key ('domain');
  489. $table_mailbox = table_by_key ('mailbox');
  490. $table_vacation = table_by_key ('vacation');
  491. if(!_mysql_field_exists($table_admin, 'created')) {
  492. db_query_parsed("ALTER TABLE $table_admin {RENAME_COLUMN} create_date created DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL;");
  493. }
  494. if(!_mysql_field_exists($table_admin, 'modified')) {
  495. db_query_parsed("ALTER TABLE $table_admin {RENAME_COLUMN} change_date modified DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL;");
  496. }
  497. if(!_mysql_field_exists($table_alias, 'created')) {
  498. db_query_parsed("ALTER TABLE $table_alias {RENAME_COLUMN} create_date created DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL;");
  499. }
  500. if(!_mysql_field_exists($table_alias, 'modified')) {
  501. db_query_parsed("ALTER TABLE $table_alias {RENAME_COLUMN} change_date modified DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL;");
  502. }
  503. if(!_mysql_field_exists($table_domain, 'created')) {
  504. db_query_parsed("ALTER TABLE $table_domain {RENAME_COLUMN} create_date created DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL;");
  505. }
  506. if(!_mysql_field_exists($table_domain, 'modified')) {
  507. db_query_parsed("ALTER TABLE $table_domain {RENAME_COLUMN} change_date modified DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL;");
  508. }
  509. if(!_mysql_field_exists($table_domain, 'aliases')) {
  510. db_query_parsed("ALTER TABLE $table_domain ADD COLUMN aliases INT(10) DEFAULT '-1' NOT NULL AFTER description;");
  511. }
  512. if(!_mysql_field_exists($table_domain, 'mailboxes')) {
  513. db_query_parsed("ALTER TABLE $table_domain ADD COLUMN mailboxes INT(10) DEFAULT '-1' NOT NULL AFTER aliases;");
  514. }
  515. if(!_mysql_field_exists($table_domain, 'maxquota')) {
  516. db_query_parsed("ALTER TABLE $table_domain ADD COLUMN maxquota INT(10) DEFAULT '-1' NOT NULL AFTER mailboxes;");
  517. }
  518. if(!_mysql_field_exists($table_domain, 'transport')) {
  519. db_query_parsed("ALTER TABLE $table_domain ADD COLUMN transport VARCHAR(255) AFTER maxquota;");
  520. }
  521. if(!_mysql_field_exists($table_domain, 'backupmx')) {
  522. db_query_parsed("ALTER TABLE $table_domain ADD COLUMN backupmx TINYINT(1) DEFAULT '0' NOT NULL AFTER transport;");
  523. }
  524. if(!_mysql_field_exists($table_mailbox, 'created')) {
  525. db_query_parsed("ALTER TABLE $table_mailbox {RENAME_COLUMN} create_date created DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL;");
  526. }
  527. if(!_mysql_field_exists($table_mailbox, 'modified')) {
  528. db_query_parsed("ALTER TABLE $table_mailbox {RENAME_COLUMN} change_date modified DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL;");
  529. }
  530. if(!_mysql_field_exists($table_mailbox, 'quota')) {
  531. db_query_parsed("ALTER TABLE $table_mailbox ADD COLUMN quota INT(10) DEFAULT '-1' NOT NULL AFTER maildir;");
  532. }
  533. if(!_mysql_field_exists($table_vacation, 'domain')) {
  534. db_query_parsed("ALTER TABLE $table_vacation ADD COLUMN domain VARCHAR(255) DEFAULT '' NOT NULL AFTER cache;");
  535. }
  536. if(!_mysql_field_exists($table_vacation, 'created')) {
  537. db_query_parsed("ALTER TABLE $table_vacation ADD COLUMN created DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL AFTER domain;");
  538. }
  539. if(!_mysql_field_exists($table_vacation, 'active')) {
  540. db_query_parsed("ALTER TABLE $table_vacation ADD COLUMN active TINYINT(1) DEFAULT '1' NOT NULL AFTER created;");
  541. }
  542. db_query_parsed("ALTER TABLE $table_vacation DROP PRIMARY KEY");
  543. db_query_parsed("ALTER TABLE $table_vacation ADD PRIMARY KEY(email)");
  544. db_query_parsed("UPDATE $table_vacation SET domain=SUBSTRING_INDEX(email, '@', -1) WHERE email=email;");
  545. }
  546. function upgrade_4_mysql() { # MySQL only
  547. # changes between 2.1 and moving to sourceforge
  548. $table_domain = table_by_key ('domain');
  549. $result = db_query_parsed("ALTER TABLE $table_domain ADD COLUMN quota int(10) NOT NULL default '0' AFTER maxquota", TRUE);
  550. # Possible errors that can be ignored:
  551. # - Invalid query: Table 'postfix.domain' doesn't exist
  552. }
  553. /**
  554. * Changes between 2.1 and moving to sf.net
  555. */
  556. function upgrade_4_pgsql() {
  557. $table_domain = table_by_key('domain');
  558. $table_admin = table_by_key('admin');
  559. $table_alias = table_by_key('alias');
  560. $table_domain_admins = table_by_key('domain_admins');
  561. $table_log = table_by_key('log');
  562. $table_mailbox = table_by_key('mailbox');
  563. $table_vacation = table_by_key('vacation');
  564. $table_vacation_notification = table_by_key('vacation_notification');
  565. if(!_pgsql_field_exists($table_domain, 'quota')) {
  566. $result = db_query_parsed("ALTER TABLE $table_domain ADD COLUMN quota int NOT NULL default '0'");
  567. }
  568. $result = db_query_parsed("ALTER TABLE $table_domain ALTER COLUMN domain DROP DEFAULT");
  569. if(!_pgsql_object_exists('domain_domain_active')) {
  570. $result = db_query_parsed("CREATE INDEX domain_domain_active ON $table_domain(domain,active)");
  571. }
  572. $result = db_query_parsed("ALTER TABLE $table_domain_admins ALTER COLUMN domain DROP DEFAULT");
  573. $result = db_query_parsed("ALTER TABLE $table_alias ALTER COLUMN address DROP DEFAULT");
  574. $result = db_query_parsed("ALTER TABLE $table_alias ALTER COLUMN domain DROP DEFAULT");
  575. if(!_pgsql_object_exists('alias_address_active')) {
  576. $result = db_query_parsed("CREATE INDEX alias_address_active ON $table_alias(address,active)");
  577. }
  578. $result = db_query_parsed("ALTER TABLE $table_domain_admins ALTER COLUMN username DROP DEFAULT");
  579. $result = db_query_parsed("ALTER TABLE $table_domain_admins ALTER COLUMN domain DROP DEFAULT");
  580. $result = db_query_parsed("
  581. BEGIN;
  582. ALTER TABLE $table_log RENAME COLUMN data TO data_old;
  583. ALTER TABLE $table_log ADD COLUMN data text NOT NULL default '';
  584. UPDATE $table_log SET data = CAST(data_old AS text);
  585. ALTER TABLE $table_log DROP COLUMN data_old;
  586. COMMIT;");
  587. $result = db_query_parsed("ALTER TABLE $table_mailbox ALTER COLUMN username DROP DEFAULT");
  588. $result = db_query_parsed("ALTER TABLE $table_mailbox ALTER COLUMN domain DROP DEFAULT");
  589. $result = db_query_parsed("
  590. BEGIN;
  591. ALTER TABLE $table_mailbox RENAME COLUMN domain TO domain_old;
  592. ALTER TABLE $table_mailbox ADD COLUMN domain varchar(255) REFERENCES $table_domain (domain);
  593. UPDATE $table_mailbox SET domain = domain_old;
  594. ALTER TABLE $table_mailbox DROP COLUMN domain_old;
  595. COMMIT;"
  596. );
  597. if(!_pgsql_object_exists('mailbox_username_active')) {
  598. db_query_parsed("CREATE INDEX mailbox_username_active ON $table_mailbox(username,active)");
  599. }
  600. $result = db_query_parsed("ALTER TABLE $table_vacation ALTER COLUMN body SET DEFAULT ''");
  601. if(_pgsql_field_exists($table_vacation, 'cache')) {
  602. $result = db_query_parsed("ALTER TABLE $table_vacation DROP COLUMN cache");
  603. }
  604. $result = db_query_parsed("
  605. BEGIN;
  606. ALTER TABLE $table_vacation RENAME COLUMN domain to domain_old;
  607. ALTER TABLE $table_vacation ADD COLUMN domain varchar(255) REFERENCES $table_domain;
  608. UPDATE $table_vacation SET domain = domain_old;
  609. ALTER TABLE $table_vacation DROP COLUMN domain_old;
  610. COMMIT;
  611. ");
  612. if(!_pgsql_object_exists('vacation_email_active')) {
  613. $result = db_query_parsed("CREATE INDEX vacation_email_active ON $table_vacation(email,active)");
  614. }
  615. if(!_pgsql_object_exists($table_vacation_notification)) {
  616. $result = db_query_parsed("
  617. CREATE TABLE $table_vacation_notification (
  618. on_vacation character varying(255) NOT NULL REFERENCES $table_vacation(email) ON DELETE CASCADE,
  619. notified character varying(255) NOT NULL,
  620. notified_at timestamp with time zone NOT NULL DEFAULT now(),
  621. CONSTRAINT vacation_notification_pkey primary key(on_vacation,notified));");
  622. }
  623. }
  624. # Possible errors that can be ignored:
  625. #
  626. # NO MySQL errors should be ignored below this line!
  627. /**
  628. * create tables
  629. * version: Sourceforge SVN r1 of DATABASE_MYSQL.txt
  630. * changes compared to DATABASE_MYSQL.txt:
  631. * - removed MySQL user and database creation
  632. * - removed creation of default superadmin
  633. */
  634. function upgrade_5_mysql() {
  635. $result = db_query_parsed("
  636. CREATE TABLE {IF_NOT_EXISTS} `" . table_by_key('admin') . "` (
  637. `username` varchar(255) NOT NULL default '',
  638. `password` varchar(255) NOT NULL default '',
  639. `created` datetime NOT NULL default '0000-00-00 00:00:00',
  640. `modified` datetime NOT NULL default '0000-00-00 00:00:00',
  641. `active` tinyint(1) NOT NULL default '1',
  642. PRIMARY KEY (`username`),
  643. KEY username (`username`)
  644. ) {MYISAM} DEFAULT {LATIN1} COMMENT='Postfix Admin - Virtual Admins'; ");
  645. $result = db_query_parsed("
  646. CREATE TABLE {IF_NOT_EXISTS} `" . table_by_key('alias') . "` (
  647. `address` varchar(255) NOT NULL default '',
  648. `goto` text NOT NULL,
  649. `domain` varchar(255) NOT NULL default '',
  650. `created` datetime NOT NULL default '0000-00-00 00:00:00',
  651. `modified` datetime NOT NULL default '0000-00-00 00:00:00',
  652. `active` tinyint(1) NOT NULL default '1',
  653. PRIMARY KEY (`address`),
  654. KEY address (`address`)
  655. ) {MYISAM} DEFAULT {LATIN1} COMMENT='Postfix Admin - Virtual Aliases';
  656. ");
  657. $result = db_query_parsed("
  658. CREATE TABLE {IF_NOT_EXISTS} `" . table_by_key('domain') . "` (
  659. `domain` varchar(255) NOT NULL default '',
  660. `description` varchar(255) NOT NULL default '',
  661. `aliases` int(10) NOT NULL default '0',
  662. `mailboxes` int(10) NOT NULL default '0',
  663. `maxquota` int(10) NOT NULL default '0',
  664. `quota` int(10) NOT NULL default '0',
  665. `transport` varchar(255) default NULL,
  666. `backupmx` tinyint(1) NOT NULL default '0',
  667. `created` datetime NOT NULL default '0000-00-00 00:00:00',
  668. `modified` datetime NOT NULL default '0000-00-00 00:00:00',
  669. `active` tinyint(1) NOT NULL default '1',
  670. PRIMARY KEY (`domain`),
  671. KEY domain (`domain`)
  672. ) {MYISAM} DEFAULT {LATIN1} COMMENT='Postfix Admin - Virtual Domains';
  673. ");
  674. $result = db_query_parsed("
  675. CREATE TABLE {IF_NOT_EXISTS} `" . table_by_key('domain_admins') . "` (
  676. `username` varchar(255) NOT NULL default '',
  677. `domain` varchar(255) NOT NULL default '',
  678. `created` datetime NOT NULL default '0000-00-00 00:00:00',
  679. `active` tinyint(1) NOT NULL default '1',
  680. KEY username (`username`)
  681. ) {MYISAM} DEFAULT {LATIN1} COMMENT='Postfix Admin - Domain Admins';
  682. ");
  683. $result = db_query_parsed("
  684. CREATE TABLE {IF_NOT_EXISTS} `" . table_by_key('log') . "` (
  685. `timestamp` datetime NOT NULL default '0000-00-00 00:00:00',
  686. `username` varchar(255) NOT NULL default '',
  687. `domain` varchar(255) NOT NULL default '',
  688. `action` varchar(255) NOT NULL default '',
  689. `data` varchar(255) NOT NULL default '',
  690. KEY timestamp (`timestamp`)
  691. ) {MYISAM} DEFAULT {LATIN1} COMMENT='Postfix Admin - Log';
  692. ");
  693. $result = db_query_parsed("
  694. CREATE TABLE {IF_NOT_EXISTS} `" . table_by_key('mailbox') . "` (
  695. `username` varchar(255) NOT NULL default '',
  696. `password` varchar(255) NOT NULL default '',
  697. `name` varchar(255) NOT NULL default '',
  698. `maildir` varchar(255) NOT NULL default '',
  699. `quota` int(10) NOT NULL default '0',
  700. `domain` varchar(255) NOT NULL default '',
  701. `created` datetime NOT NULL default '0000-00-00 00:00:00',
  702. `modified` datetime NOT NULL default '0000-00-00 00:00:00',
  703. `active` tinyint(1) NOT NULL default '1',
  704. PRIMARY KEY (`username`),
  705. KEY username (`username`)
  706. ) {MYISAM} DEFAULT {LATIN1} COMMENT='Postfix Admin - Virtual Mailboxes';
  707. ");
  708. $result = db_query_parsed("
  709. CREATE TABLE {IF_NOT_EXISTS} `" . table_by_key('vacation') . "` (
  710. `email` varchar(255) NOT NULL ,
  711. `subject` varchar(255) NOT NULL,
  712. `body` text NOT NULL,
  713. `cache` text NOT NULL,
  714. `domain` varchar(255) NOT NULL,
  715. `created` datetime NOT NULL default '0000-00-00 00:00:00',
  716. `active` tinyint(1) NOT NULL default '1',
  717. PRIMARY KEY (`email`),
  718. KEY email (`email`)
  719. ) {MYISAM} DEFAULT {LATIN1} COMMENT='Postfix Admin - Virtual Vacation';
  720. ");
  721. }
  722. /**
  723. * drop useless indicies (already available as primary key)
  724. */
  725. function upgrade_79_mysql() { # MySQL only
  726. $result = db_query_parsed(_drop_index('admin', 'username'), True);
  727. $result = db_query_parsed(_drop_index('alias', 'address'), True);
  728. $result = db_query_parsed(_drop_index('domain', 'domain'), True);
  729. $result = db_query_parsed(_drop_index('mailbox', 'username'), True);
  730. }
  731. function upgrade_81_mysql() { # MySQL only
  732. $table_vacation = table_by_key ('vacation');
  733. $table_vacation_notification = table_by_key('vacation_notification');
  734. $all_sql = explode("\n", trim("
  735. ALTER TABLE `$table_vacation` CHANGE `email` `email` VARCHAR( 255 ) {LATIN1} NOT NULL
  736. ALTER TABLE `$table_vacation` CHANGE `subject` `subject` VARCHAR( 255 ) {UTF-8} NOT NULL
  737. ALTER TABLE `$table_vacation` CHANGE `body` `body` TEXT {UTF-8} NOT NULL
  738. ALTER TABLE `$table_vacation` CHANGE `cache` `cache` TEXT {LATIN1} NOT NULL
  739. ALTER TABLE `$table_vacation` CHANGE `domain` `domain` VARCHAR( 255 ) {LATIN1} NOT NULL
  740. ALTER TABLE `$table_vacation` CHANGE `active` `active` TINYINT( 1 ) NOT NULL DEFAULT '1'
  741. ALTER TABLE `$table_vacation` DEFAULT {LATIN1}
  742. ALTER TABLE `$table_vacation` {INNODB}
  743. "));
  744. foreach ($all_sql as $sql) {
  745. $result = db_query_parsed($sql, TRUE);
  746. }
  747. }
  748. /**
  749. * Make logging translatable - i.e. create alias => create_alias
  750. */
  751. function upgrade_90() {
  752. $result = db_query_parsed("UPDATE " . table_by_key ('log') . " SET action = REPLACE(action,' ','_')", TRUE);
  753. # change edit_alias_state to edit_alias_active
  754. $result = db_query_parsed("UPDATE " . table_by_key ('log') . " SET action = 'edit_alias_state' WHERE action = 'edit_alias_active'", TRUE);
  755. }
  756. /**
  757. * MySQL only allow quota > 2 GB
  758. */
  759. function upgrade_169_mysql() {
  760. $table_domain = table_by_key ('domain');
  761. $table_mailbox = table_by_key ('mailbox');
  762. $result = db_query_parsed("ALTER TABLE $table_domain MODIFY COLUMN `quota` bigint(20) NOT NULL default '0'", TRUE);
  763. $result = db_query_parsed("ALTER TABLE $table_domain MODIFY COLUMN `maxquota` bigint(20) NOT NULL default '0'", TRUE);
  764. $result = db_query_parsed("ALTER TABLE $table_mailbox MODIFY COLUMN `quota` bigint(20) NOT NULL default '0'", TRUE);
  765. }
  766. /**
  767. * Create / modify vacation_notification table.
  768. * Note: This might not work if users used workarounds to create the table before.
  769. * In this case, dropping the table is the easiest solution.
  770. */
  771. function upgrade_318_mysql() {
  772. $table_vacation_notification = table_by_key('vacation_notification');
  773. $table_vacation = table_by_key('vacation');
  774. db_query_parsed( "
  775. CREATE TABLE {IF_NOT_EXISTS} $table_vacation_notification (
  776. on_vacation varchar(255) {LATIN1} NOT NULL,
  777. notified varchar(255) NOT NULL,
  778. notified_at timestamp NOT NULL default CURRENT_TIMESTAMP,
  779. PRIMARY KEY on_vacation (`on_vacation`, `notified`),
  780. CONSTRAINT `vacation_notification_pkey`
  781. FOREIGN KEY (`on_vacation`) REFERENCES $table_vacation(`email`) ON DELETE CASCADE
  782. )
  783. {INNODB}
  784. COMMENT='Postfix Admin - Virtual Vacation Notifications'
  785. ");
  786. # in case someone has manually created the table with utf8 fields before:
  787. $all_sql = explode("\n", trim("
  788. ALTER TABLE `$table_vacation_notification` CHANGE `notified` `notified` VARCHAR( 255 ) NOT NULL
  789. ALTER TABLE `$table_vacation_notification` DEFAULT CHARACTER SET utf8
  790. "));
  791. # Possible errors that can be ignored:
  792. # None.
  793. # If something goes wrong, the user should drop the vacation_notification table
  794. # (not a great loss) and re-create it using this function.
  795. foreach ($all_sql as $sql) {
  796. $result = db_query_parsed($sql);
  797. }
  798. }
  799. /**
  800. * Create fetchmail table
  801. */
  802. function upgrade_344_mysql() {
  803. $table_fetchmail = table_by_key('fetchmail');
  804. db_query_parsed( "
  805. CREATE TABLE IF NOT EXISTS $table_fetchmail(
  806. id int(11) unsigned not null auto_increment,
  807. mailbox varchar(255) not null default '',
  808. src_server varchar(255) not null default '',
  809. src_auth enum('password','kerberos_v5','kerberos','kerberos_v4','gssapi','cram-md5','otp','ntlm','msn','ssh','any'),
  810. src_user varchar(255) not null default '',
  811. src_password varchar(255) not null default '',
  812. src_folder varchar(255) not null default '',
  813. poll_time int(11) unsigned not null default 10,
  814. fetchall tinyint(1) unsigned not null default 0,
  815. keep tinyint(1) unsigned not null default 0,
  816. protocol enum('POP3','IMAP','POP2','ETRN','AUTO'),
  817. extra_options text,
  818. returned_text text,
  819. mda varchar(255) not null default '',
  820. date timestamp,
  821. primary key(id)
  822. );
  823. ");
  824. }
  825. function upgrade_344_pgsql() {
  826. $fetchmail = table_by_key('fetchmail');
  827. // a field name called 'date' is probably a bad idea.
  828. if(!_pgsql_object_exists('fetchmail')) {
  829. db_query_parsed( "
  830. create table $fetchmail(
  831. id serial,
  832. mailbox varchar(255) not null default '',
  833. src_server varchar(255) not null default '',
  834. src_auth varchar(15) NOT NULL,
  835. src_user varchar(255) not null default '',
  836. src_password varchar(255) not null default '',
  837. src_folder varchar(255) not null default '',
  838. poll_time integer not null default 10,
  839. fetchall boolean not null default false,
  840. keep boolean not null default false,
  841. protocol varchar(15) NOT NULL,
  842. extra_options text,
  843. returned_text text,
  844. mda varchar(255) not null default '',
  845. date timestamp with time zone default now(),
  846. primary key(id),
  847. CHECK (src_auth IN ('password','kerberos_v5','kerberos','kerberos_v4','gssapi','cram-md5','otp','ntlm','msn','ssh','any')),
  848. CHECK (protocol IN ('POP3', 'IMAP', 'POP2', 'ETRN', 'AUTO'))
  849. );
  850. ");
  851. }
  852. // MySQL expects sequences to start at 1. Stupid database.
  853. // fetchmail.php requires id parameters to be > 0, as it does if($id) like logic... hence if we don't
  854. // fudge the sequence starting point, you cannot delete/edit the first entry if using PostgreSQL.
  855. // I'm sure there's a more elegant way of fixing it properly.... but this should work for now.
  856. if(_pgsql_object_exists('fetchmail_id_seq')) {
  857. db_query_parsed("SELECT nextval('{$fetchmail}_id_seq')"); // I don't care about number waste.
  858. }
  859. }
  860. /**
  861. * Create alias_domain table - MySQL
  862. */
  863. # function upgrade_362_mysql() # renamed to _438 to make sure it runs after an upgrade from 2.2.x
  864. function upgrade_438_mysql() {
  865. # Table structure for table alias_domain
  866. #
  867. $table_alias_domain = table_by_key('alias_domain');
  868. db_query_parsed("
  869. CREATE TABLE IF NOT EXISTS $table_alias_domain (
  870. `alias_domain` varchar(255) NOT NULL default '',
  871. `target_domain` varchar(255) NOT NULL default '',
  872. `created` datetime NOT NULL default '0000-00-00 00:00:00',
  873. `modified` datetime NOT NULL default '0000-00-00 00:00:00',
  874. `active` tinyint(1) NOT NULL default '1',
  875. PRIMARY KEY (`alias_domain`),
  876. KEY `active` (`active`),
  877. KEY `target_domain` (`target_domain`)
  878. ) {MYISAM} COMMENT='Postfix Admin - Domain Aliases'
  879. ");
  880. }
  881. /**
  882. * Create alias_domain table - PgSQL
  883. */
  884. # function upgrade_362_pgsql() # renamed to _438 to make sure it runs after an upgrade from 2.2.x
  885. function upgrade_438_pgsql() {
  886. # Table structure for table alias_domain
  887. $table_alias_domain = table_by_key('alias_domain');
  888. $table_domain = table_by_key('domain');
  889. if(_pgsql_object_exists($table_alias_domain)) {
  890. return;
  891. }
  892. db_query_parsed("
  893. CREATE TABLE $table_alias_domain (
  894. alias_domain character varying(255) NOT NULL REFERENCES $table_domain(domain) ON DELETE CASCADE,
  895. target_domain character varying(255) NOT NULL REFERENCES $table_domain(domain) ON DELETE CASCADE,
  896. created timestamp with time zone default now(),
  897. modified timestamp with time zone default now(),
  898. active boolean NOT NULL default true,
  899. PRIMARY KEY(alias_domain))");
  900. db_query_parsed("CREATE INDEX alias_domain_active ON $table_alias_domain(alias_domain,active)");
  901. db_query_parsed("COMMENT ON TABLE $table_alias_domain IS 'Postfix Admin - Domain Aliases'");
  902. }
  903. /**
  904. * Change description fields to UTF-8
  905. */
  906. function upgrade_373_mysql() { # MySQL only
  907. $table_domain = table_by_key ('domain');
  908. $table_mailbox = table_by_key('mailbox');
  909. $all_sql = explode("\n", trim("
  910. ALTER TABLE `$table_domain` CHANGE `description` `description` VARCHAR( 255 ) {UTF-8} NOT NULL
  911. ALTER TABLE `$table_mailbox` CHANGE `name` `name` VARCHAR( 255 ) {UTF-8} NOT NULL
  912. "));
  913. foreach ($all_sql as $sql) {
  914. $result = db_query_parsed($sql);
  915. }
  916. }
  917. /**
  918. * add ssl option for fetchmail
  919. */
  920. function upgrade_439_mysql() {
  921. $table_fetchmail = table_by_key('fetchmail');
  922. if(!_mysql_field_exists($table_fetchmail, 'ssl')) {
  923. db_query_parsed("ALTER TABLE `$table_fetchmail` ADD `ssl` TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0' AFTER `protocol` ; ");
  924. }
  925. }
  926. function upgrade_439_pgsql() {
  927. $table_fetchmail = table_by_key('fetchmail');
  928. if(!_pgsql_field_exists($table_fetchmail, 'ssl')) {
  929. db_query_parsed("ALTER TABLE $table_fetchmail ADD COLUMN ssl BOOLEAN NOT NULL DEFAULT false");
  930. }
  931. }
  932. function upgrade_473_mysql() {
  933. $table_admin = table_by_key('admin');
  934. $table_alias = table_by_key('alias');
  935. $table_al_dom = table_by_key('alias_domain');
  936. $table_domain = table_by_key('domain');
  937. $table_dom_adm = table_by_key('domain_admins');
  938. $table_fmail = table_by_key('fetchmail');
  939. $table_mailbox = table_by_key('mailbox');
  940. $table_log = table_by_key('log');
  941. # tables were created without explicit charset before :-(
  942. $all_sql = explode("\n", trim("
  943. ALTER TABLE `$table_admin` CHANGE `username` `username` VARCHAR( 255 ) {LATIN1} NOT NULL
  944. ALTER TABLE `$table_admin` CHANGE `password` `password` VARCHAR( 255 ) {LATIN1} NOT NULL
  945. ALTER TABLE `$table_admin` DEFAULT {LATIN1}
  946. ALTER TABLE `$table_alias` CHANGE `address` `address` VARCHAR( 255 ) {LATIN1} NOT NULL
  947. ALTER TABLE `$table_alias` CHANGE `goto` `goto` TEXT {LATIN1} NOT NULL
  948. ALTER TABLE `$table_alias` CHANGE `domain` `domain` VARCHAR( 255 ) {LATIN1} NOT NULL
  949. ALTER TABLE `$table_alias` DEFAULT {LATIN1}
  950. ALTER TABLE `$table_al_dom` CHANGE `alias_domain` `alias_domain` VARCHAR( 255 ) {LATIN1} NOT NULL
  951. ALTER TABLE `$table_al_dom` CHANGE `target_domain` `target_domain` VARCHAR( 255 ) {LATIN1} NOT NULL
  952. ALTER TABLE `$table_al_dom` DEFAULT {LATIN1}
  953. ALTER TABLE `$table_domain` CHANGE `domain` `domain` VARCHAR( 255 ) {LATIN1} NOT NULL
  954. ALTER TABLE `$table_domain` CHANGE `transport` `transport` VARCHAR( 255 ) {LATIN1} NOT NULL
  955. ALTER TABLE `$table_domain` DEFAULT {LATIN1}
  956. ALTER TABLE `$table_dom_adm` CHANGE `username` `username` VARCHAR( 255 ) {LATIN1} NOT NULL
  957. ALTER TABLE `$table_dom_adm` CHANGE `domain` `domain` VARCHAR( 255 ) {LATIN1} NOT NULL
  958. ALTER TABLE `$table_dom_adm` DEFAULT {LATIN1}
  959. ALTER TABLE `$table_log` CHANGE `username` `username` VARCHAR( 255 ) {LATIN1} NOT NULL
  960. ALTER TABLE `$table_log` CHANGE `domain` `domain` VARCHAR( 255 ) {LATIN1} NOT NULL
  961. ALTER TABLE `$table_log` CHANGE `action` `action` VARCHAR( 255 ) {LATIN1} NOT NULL
  962. ALTER TABLE `$table_log` CHANGE `data` `data` VARCHAR( 255 ) {LATIN1} NOT NULL
  963. ALTER TABLE `$table_log` DEFAULT {LATIN1}
  964. ALTER TABLE `$table_mailbox` CHANGE `username` `username` VARCHAR( 255 ) {LATIN1} NOT NULL
  965. ALTER TABLE `$table_mailbox` CHANGE `password` `password` VARCHAR( 255 ) {LATIN1} NOT NULL
  966. ALTER TABLE `$table_mailbox` CHANGE `maildir` `maildir` VARCHAR( 255 ) {LATIN1} NOT NULL
  967. ALTER TABLE `$table_mailbox` CHANGE `domain` `domain` VARCHAR( 255 ) {LATIN1} NOT NULL
  968. ALTER TABLE `$table_mailbox` DEFAULT {LATIN1}
  969. ALTER TABLE `$table_fmail` CHANGE `mailbox` `mailbox` VARCHAR( 255 ) {LATIN1} NOT NULL
  970. ALTER TABLE `$table_fmail` CHANGE `src_server` `src_server` VARCHAR( 255 ) {LATIN1} NOT NULL
  971. ALTER TABLE `$table_fmail` CHANGE `src_user` `src_user` VARCHAR( 255 ) {LATIN1} NOT NULL
  972. ALTER TABLE `$table_fmail` CHANGE `src_password` `src_password` VARCHAR( 255 ) {LATIN1} NOT NULL
  973. ALTER TABLE `$table_fmail` CHANGE `src_folder` `src_folder` VARCHAR( 255 ) {LATIN1} NOT NULL
  974. ALTER TABLE `$table_fmail` CHANGE `mda` `mda` VARCHAR( 255 ) {LATIN1} NOT NULL
  975. ALTER TABLE `$table_fmail` CHANGE `mailbox` `mailbox` VARCHAR( 255 ) {LATIN1} NOT NULL
  976. ALTER TABLE `$table_fmail` CHANGE `extra_options` `extra_options` TEXT {LATIN1} NULL DEFAULT NULL
  977. ALTER TABLE `$table_fmail` CHANGE `returned_text` `returned_text` TEXT {LATIN1} NULL DEFAULT NULL
  978. ALTER TABLE `$table_fmail` DEFAULT {LATIN1}
  979. "));
  980. foreach ($all_sql as $sql) {
  981. $result = db_query_parsed($sql);
  982. }
  983. }
  984. function upgrade_479_mysql () {
  985. # ssl is a reserved word in MySQL and causes several problems. Renaming the field...
  986. $table_fmail = table_by_key('fetchmail');
  987. if(!_mysql_field_exists($table_fmail, 'usessl')) {
  988. db_query_parsed("ALTER TABLE `$table_fmail` CHANGE `ssl` `usessl` TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0'");
  989. }
  990. }
  991. function upgrade_479_pgsql () {
  992. $table_fmail = table_by_key('fetchmail');
  993. if(!_pgsql_field_exists($table_fmail, 'usessl')) {
  994. db_query_parsed("alter table $table_fmail rename column ssl to usessl");
  995. }
  996. }
  997. function upgrade_483_mysql () {
  998. $table_log = table_by_key('log');
  999. db_query_parsed("ALTER TABLE $table_log CHANGE `data` `data` TEXT {LATIN1} NOT NULL");
  1000. }
  1001. # Add a local_part field to the mailbox table, and populate it with the local part of the user's address.
  1002. # This is to make it easier (hopefully) to change the filesystem location of a mailbox in the future
  1003. # See https://sourceforge.net/forum/message.php?msg_id=5394663
  1004. function upgrade_495_pgsql() {
  1005. $table_mailbox = table_by_key('mailbox');
  1006. if(!_pgsql_field_exists($table_mailbox, 'local_part')) {
  1007. db_query_parsed("ALTER TABLE $table_mailbox add column local_part varchar(255) ");
  1008. db_query_parsed("UPDATE $table_mailbox SET local_part = substring(username from '^(.*)@')");
  1009. db_query_parsed("ALTER TABLE $table_mailbox alter column local_part SET NOT NULL");
  1010. }
  1011. }
  1012. # See https://sourceforge.net/forum/message.php?msg_id=5394663
  1013. function upgrade_495_mysql() {
  1014. $table_mailbox = table_by_key('mailbox');
  1015. if(!_mysql_field_exists($table_mailbox, 'local_part')) {
  1016. db_query_parsed("ALTER TABLE $table_mailbox add local_part varchar(255) AFTER quota"); // allow to be null
  1017. db_query_parsed("UPDATE $table_mailbox SET local_part = substring_index(username, '@', 1)");
  1018. db_query_parsed("ALTER TABLE $table_mailbox change local_part local_part varchar(255) NOT NULL"); // remove null-ness...
  1019. }
  1020. }
  1021. function upgrade_504_mysql() {
  1022. $table_mailbox = table_by_key('mailbox');
  1023. db_query_parsed("ALTER TABLE `$table_mailbox` CHANGE `local_part` `local_part` VARCHAR( 255 ) {LATIN1} NOT NULL");
  1024. }
  1025. function upgrade_655() {
  1026. db_query_parsed(_add_index('mailbox', 'domain', 'domain'));
  1027. db_query_parsed(_add_index('alias', 'domain', 'domain'));
  1028. }
  1029. /*
  1030. function number too small for upgrades from 2.3.x
  1031. -> adding activefrom and activeuntil to vacation table is now upgrade_964
  1032. -> the tables client_access, from_access, helo_access, rcpt_access, user_whitelist
  1033. are not used by PostfixAdmin - no replacement function needed
  1034. Note: Please never remove this function, even if it is disabled - it might be needed in case we have to debug a broken database upgrade etc.
  1035. Note: there never was a function upgrade_727_pgsql()
  1036. function upgrade_727_mysql() {
  1037. $table_vacation = table_by_key('vacation');
  1038. if(!_mysql_field_exists($table_vacation, 'activefrom')) {
  1039. db_query_parsed("ALTER TABLE $table_vacation add activefrom datetime default NULL");
  1040. }
  1041. if(!_mysql_field_exists($table_vacation, 'activeuntil')) {
  1042. db_query_parsed("ALTER TABLE $table_vacation add activeuntil datetime default NULL");
  1043. }
  1044. # the following tables are not used by postfixadmin
  1045. $table_client_access = table_by_key('client_access');
  1046. db_query_parsed("
  1047. CREATE TABLE IF NOT EXISTS $table_client_access (
  1048. `client` char(50) NOT NULL,
  1049. `action` char(50) NOT NULL default 'REJECT',
  1050. UNIQUE KEY `client` (`client`)
  1051. ) {MYISAM} COMMENT='Postfix Admin - Client Access'
  1052. ");
  1053. $table_from_access = table_by_key('from_access');
  1054. db_query_parsed("
  1055. CREATE TABLE IF NOT EXISTS $table_from_access (
  1056. `from_access` char(50) NOT NULL,
  1057. `action` char(50) NOT NULL default 'REJECT',
  1058. UNIQUE KEY `from_access` (`from_access`)
  1059. ) {MYISAM} COMMENT='Postfix Admin - From Access'
  1060. ");
  1061. $table_helo_access = table_by_key('helo_access');
  1062. db_query_parsed("
  1063. CREATE TABLE IF NOT EXISTS $table_helo_access (
  1064. `helo` char(50) NOT NULL,
  1065. `action` char(50) NOT NULL default 'REJECT',
  1066. UNIQUE KEY `helo` (`helo`)
  1067. ) {MYISAM} COMMENT='Postfix Admin - Helo Access'
  1068. ");
  1069. $table_rcpt_access = table_by_key('rcpt_access');
  1070. db_query_parsed("
  1071. CREATE TABLE IF NOT EXISTS $table_rcpt_access (
  1072. `rcpt` char(50) NOT NULL,
  1073. `action` char(50) NOT NULL default 'REJECT',
  1074. UNIQUE KEY `rcpt` (`rcpt`)
  1075. ) {MYISAM} COMMENT='Postfix Admin - Recipient Access'
  1076. ");
  1077. $table_user_whitelist = table_by_key('user_whitelist');
  1078. db_query_parsed("
  1079. CREATE TABLE IF NOT EXISTS $table_user_whitelist (
  1080. `recipient` char(50) NOT NULL,
  1081. `action` char(50) NOT NULL default 'REJECT',
  1082. UNIQUE KEY `recipient` (`recipient`)
  1083. ) {MYISAM} COMMENT='Postfix Admin - User whitelist'
  1084. ");
  1085. }
  1086. */
  1087. function upgrade_729() {
  1088. $table_quota = table_by_key('quota');
  1089. $table_quota2 = table_by_key('quota2');
  1090. # table for dovecot v1.0 & 1.1
  1091. # note: quota table created with old versions of upgrade.php (before r1605)
  1092. # will not have explicit "NOT NULL DEFAULT 0" for the "current" field
  1093. # (shouldn't hurt)
  1094. db_query_parsed("
  1095. CREATE TABLE {IF_NOT_EXISTS} $table_quota (
  1096. username VARCHAR(255) {LATIN1} NOT NULL,
  1097. path VARCHAR(100) {LATIN1} NOT NULL,
  1098. current {BIGINT},
  1099. PRIMARY KEY (username, path)
  1100. ) {MYISAM} ;
  1101. ");
  1102. # table for dovecot >= 1.2
  1103. db_query_parsed("
  1104. CREATE TABLE {IF_NOT_EXISTS} $table_quota2 (
  1105. username VARCHAR(100) {LATIN1} NOT NULL,
  1106. bytes {BIGINT},
  1107. messages integer NOT NULL DEFAULT 0,
  1108. PRIMARY KEY (username)
  1109. ) {MYISAM} ;
  1110. ");
  1111. }
  1112. function upgrade_730_pgsql() {
  1113. $table_quota = table_by_key('quota');
  1114. $table_quota2 = table_by_key('quota2');
  1115. db_query_parsed('CREATE LANGUAGE plpgsql', 1); /* will error if plpgsql is already installed */
  1116. # trigger for dovecot v1.0 & 1.1 quota table
  1117. # taken from http://wiki.dovecot.org/Quota/Dict
  1118. db_query_parsed("
  1119. CREATE OR REPLACE FUNCTION merge_quota() RETURNS TRIGGER AS \$merge_quota\$
  1120. BEGIN
  1121. UPDATE $table_quota SET current = NEW.current + current WHERE username = NEW.username AND path = NEW.path;
  1122. IF found THEN
  1123. RETURN NULL;
  1124. ELSE
  1125. RETURN NEW;
  1126. END IF;
  1127. END;
  1128. \$merge_quota\$ LANGUAGE plpgsql;
  1129. ");
  1130. db_query_parsed("
  1131. CREATE TRIGGER mergequota BEFORE INSERT ON $table_quota FOR EACH ROW EXECUTE PROCEDURE merge_quota();
  1132. ");
  1133. # trigger for dovecot >= 1.2 quota table
  1134. # taken from http://wiki.dovecot.org/Quota/Dict, table/trigger name changed to quota2 naming
  1135. db_query_parsed("
  1136. CREATE OR REPLACE FUNCTION merge_quota2() RETURNS TRIGGER AS \$\$
  1137. BEGIN
  1138. IF NEW.messages < 0 OR NEW.messages IS NULL THEN
  1139. -- ugly kludge: we came here from this function, really do try to insert
  1140. IF NEW.messages IS NULL THEN
  1141. NEW.messages = 0;
  1142. ELSE
  1143. NEW.messages = -NEW.messages;
  1144. END IF;
  1145. return NEW;
  1146. END IF;
  1147. LOOP
  1148. UPDATE $table_quota2 SET bytes = bytes + NEW.bytes,
  1149. messages = messages + NEW.messages
  1150. WHERE username = NEW.username;
  1151. IF found THEN
  1152. RETURN NULL;
  1153. END IF;
  1154. BEGIN
  1155. IF NEW.messages = 0 THEN
  1156. INSERT INTO $table_quota2 (bytes, messages, username) VALUES (NEW.bytes, NULL, NEW.username);
  1157. ELSE
  1158. INSERT INTO $table_quota2 (bytes, messages, username) VALUES (NEW.bytes, -NEW.messages, NEW.username);
  1159. END IF;
  1160. return NULL;
  1161. EXCEPTION WHEN unique_violation THEN
  1162. -- someone just inserted the record, update it
  1163. END;
  1164. END LOOP;
  1165. END;
  1166. \$\$ LANGUAGE plpgsql;
  1167. ");
  1168. db_query_parsed("
  1169. CREATE TRIGGER mergequota2 BEFORE INSERT ON $table_quota2
  1170. FOR EACH ROW EXECUTE PROCEDURE merge_quota2();
  1171. ");
  1172. }
  1173. function upgrade_945() {
  1174. _db_add_field('vacation', 'modified', '{DATECURRENT}', 'created');
  1175. }
  1176. function upgrade_946() {
  1177. # taken from upgrade_727_mysql, needs to be done for all databases
  1178. _db_add_field('vacation', 'activefrom', '{DATE}', 'body');
  1179. _db_add_field('vacation', 'activeuntil', '{DATE}', 'activefrom');
  1180. }
  1181. function upgrade_968_pgsql() {
  1182. # pgsql counterpart for upgrade_169_mysql() - allow really big quota
  1183. $table_domain = table_by_key ('domain');
  1184. $table_mailbox = table_by_key('mailbox');
  1185. db_query_parsed("ALTER TABLE $table_domain ALTER COLUMN quota type bigint");
  1186. db_query_parsed("ALTER TABLE $table_domain ALTER COLUMN maxquota type bigint");
  1187. db_query_parsed("ALTER TABLE $table_mailbox ALTER COLUMN quota type bigint");
  1188. }
  1189. function upgrade_1050() {
  1190. db_query_parsed(_add_index('log', 'domain_timestamp', 'domain,timestamp'));
  1191. }
  1192. function upgrade_1283() {
  1193. _db_add_field('admin', 'superadmin', '{BOOLEAN}', 'password');
  1194. }
  1195. function upgrade_1284() {
  1196. # migrate the ALL domain to the superadmin column
  1197. # Note: The ALL domain is not (yet) deleted to stay backwards-compatible for now (will be done in a later upgrade function)
  1198. $result = db_query("SELECT username FROM " . table_by_key('domain_admins') . " where domain='ALL'");
  1199. if ($result['rows'] > 0) {
  1200. while ($row = db_array ($result['result'])) {
  1201. printdebug ("Setting superadmin flag for " . $row['username']);
  1202. db_update('admin', 'username', $row['username'], array('superadmin' => db_get_boolean(true)) );
  1203. }
  1204. }
  1205. }
  1206. function upgrade_1345_mysql() {
  1207. # $table_vacation = table_by_key('vacation');
  1208. # adding and usage of reply_type field removed in r1610
  1209. # db_query_parsed("ALTER TABLE `$table_vacation` ADD `reply_type` VARCHAR( 20 ) NOT NULL AFTER `domain` ");
  1210. # obsoleted by upgrade_1610()
  1211. # db_query_parsed("ALTER TABLE `$table_vacation` ADD `interval_time` INT NOT NULL DEFAULT '0' AFTER `reply_type` ");
  1212. }
  1213. function upgrade_1519() {
  1214. _db_add_field('fetchmail', 'sslcertck', '{BOOLEAN}', 'usessl' );
  1215. _db_add_field('fetchmail', 'sslcertpath', "VARCHAR(255) {UTF-8} DEFAULT ''", 'sslcertck' );
  1216. _db_add_field('fetchmail', 'sslfingerprint', "VARCHAR(255) {LATIN1} DEFAULT ''", 'sslcertpath');
  1217. }
  1218. function upgrade_1610() {
  1219. # obsoletes upgrade_1345_mysql() - which means debug mode could print "field already exists"
  1220. _db_add_field('vacation', 'interval_time', '{INT}', 'domain');
  1221. }
  1222. function upgrade_1685_mysql() {
  1223. # Fix existing log entries broken by https://sourceforge.net/p/postfixadmin/bugs/317/
  1224. $table = table_by_key('log');
  1225. db_query_parsed("UPDATE $table SET data = domain WHERE data = '' AND domain LIKE '%@%'");
  1226. db_query_parsed("UPDATE $table SET domain=SUBSTRING_INDEX(domain, '@', -1) WHERE domain=data;");
  1227. }
  1228. function upgrade_1685_pgsql() {
  1229. $table = table_by_key('log');
  1230. db_query_parsed("UPDATE $table SET data = domain WHERE data = '' AND domain LIKE '%@%'");
  1231. db_query_parsed("UPDATE $table SET domain=SPLIT_PART(domain, '@', 2) WHERE domain=data;");
  1232. }
  1233. function upgrade_1761_mysql() {
  1234. # upgrade_1762 adds the 'modified' column as {DATECURRENT}, therefore we first need to change
  1235. # 'date' to {DATE} (mysql only allows one {DATECURRENT} column per table)
  1236. $table_fetchmail = table_by_key('fetchmail');
  1237. db_query_parsed("ALTER TABLE `$table_fetchmail` CHANGE `date` `date` {DATE}");
  1238. }
  1239. function upgrade_1762() {
  1240. _db_add_field('fetchmail', 'domain', "VARCHAR(255) {LATIN1} DEFAULT ''", 'id');
  1241. _db_add_field('fetchmail', 'active', '{BOOLEAN}', 'date');
  1242. _db_add_field('fetchmail', 'created', '{DATE}', 'date');
  1243. # If you followed SVN and got upgrade failures here, you might need to
  1244. # UPDATE config SET value=1760 WHERE name='version';
  1245. # and run setup.php again (upgrade_1761_mysql was added later).
  1246. _db_add_field('fetchmail', 'modified', '{DATECURRENT}', 'created');
  1247. }
  1248. function upgrade_1763_mysql() {
  1249. $table = table_by_key('fetchmail');
  1250. db_query_parsed("UPDATE $table SET domain=SUBSTRING_INDEX(mailbox, '@', -1) WHERE domain='';");
  1251. }
  1252. function upgrade_1763_pgsql() {
  1253. $table = table_by_key('fetchmail');
  1254. db_query_parsed("UPDATE $table SET domain=SPLIT_PART(mailbox, '@', 2) WHERE domain='';");
  1255. }
  1256. function upgrade_1767() {
  1257. # 'active' was just added, so make sure all existing jobs stay active
  1258. $table = table_by_key('fetchmail');
  1259. db_query_parsed("UPDATE $table SET active='{BOOL_TRUE}'");
  1260. }
  1261. function upgrade_1795_mysql() {
  1262. # upgrade_1761_mysql() was added later (in r1795) - make sure it runs for everybody
  1263. # (running it twice doesn't hurt)
  1264. upgrade_1761_mysql();
  1265. }
  1266. # TODO MySQL:
  1267. # - various varchar fields do not have a default value
  1268. # https://sourceforge.net/projects/postfixadmin/forums/forum/676076/topic/3419725
  1269. # - change default of all timestamp fields to {DATECURRENT} (CURRENT_TIMESTAMP} or {DATE}
  1270. # including vacation.activefrom/activeuntil (might have a different default as leftover from upgrade_727_mysql)
  1271. # including vacation.modified - should be {DATE}, not {DATECURRENT}
  1272. # https://sourceforge.net/tracker/?func=detail&aid=1699218&group_id=191583&atid=937964
  1273. # @todo vacation.email has 2 indizes