/htdocs/artefact/resume/db/upgrade.php

https://github.com/richardmansfield/richardms-mahara · PHP · 114 lines · 72 code · 8 blank · 34 comment · 12 complexity · 8146254434f5aabb4f197749f7d449e5 MD5 · raw file

  1. <?php
  2. /**
  3. * Mahara: Electronic portfolio, weblog, resume builder and social networking
  4. * Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
  5. * http://wiki.mahara.org/Contributors
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @package mahara
  21. * @subpackage artefact-resume
  22. * @author Catalyst IT Ltd
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL
  24. * @copyright (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
  25. *
  26. */
  27. defined('INTERNAL') || die();
  28. function xmldb_artefact_resume_upgrade($oldversion=0) {
  29. $status = true;
  30. if ($oldversion < 2008012200) {
  31. if (is_mysql()) {
  32. $inttype = 'BIGINT(10)';
  33. }
  34. else {
  35. $inttype = 'INTEGER';
  36. }
  37. foreach (array(
  38. 'artefact_resume_employmenthistory',
  39. 'artefact_resume_educationhistory',
  40. 'artefact_resume_membership') as $table) {
  41. $records = get_records_array($table, '', '', 'startdate DESC', 'id,startdate,enddate');
  42. // Sigh. table_column is screwed beyond belief. We let it do its
  43. // work (in the case of start and stopdate at least because it does
  44. // cast the columns OK), then fix its bugs
  45. execute_sql('ALTER TABLE {' . $table . '} ADD displayorder ' . $inttype);
  46. table_column($table, 'startdate', 'startdate', 'text', null, null, '', 'not null');
  47. table_column($table, 'enddate', 'enddate', 'text', null, null, '', '');
  48. // MySQL docs say:
  49. // * BLOB and TEXT columns cannot have DEFAULT values.
  50. // It turns out they do - a default of ''. And dropping this results in:
  51. // mysql> ALTER TABLE "artefact_resume_employmenthistory" ALTER COLUMN startdate DROP DEFAULT;
  52. // ERROR 1101 (42000): BLOB/TEXT column 'startdate' can't have a default value
  53. //
  54. if (is_postgres()) {
  55. execute_sql('ALTER TABLE {' . $table . '} ALTER COLUMN startdate DROP DEFAULT');
  56. execute_sql('ALTER TABLE {' . $table . '} ALTER COLUMN enddate DROP DEFAULT');
  57. }
  58. if (!empty($records)) {
  59. foreach ($records as $k => $r) {
  60. set_field($table, 'displayorder', $k, 'id', $r->id);
  61. set_field($table, 'startdate',
  62. format_date(strtotime($r->startdate), 'strftimedate', 'current', 'artefact.resume'),
  63. 'id', $r->id);
  64. set_field($table, 'enddate',
  65. format_date(strtotime($r->enddate), 'strftimedate', 'current', 'artefact.resume'),
  66. 'id', $r->id);
  67. }
  68. }
  69. if (is_mysql()) {
  70. execute_sql('ALTER TABLE {' . $table .'} MODIFY displayorder ' . $inttype . ' NOT NULL');
  71. execute_sql('ALTER TABLE {' . $table .'} MODIFY startdate TEXT NOT NULL');
  72. }
  73. else {
  74. execute_sql('ALTER TABLE {' . $table . '} ALTER displayorder SET NOT NULL');
  75. execute_sql('ALTER TABLE {' . $table . '} ALTER COLUMN startdate SET NOT NULL');
  76. }
  77. }
  78. foreach (array(
  79. 'artefact_resume_certification',
  80. 'artefact_resume_book') as $table) {
  81. $records = get_records_array($table, '', '', 'date DESC', 'id,date');
  82. execute_sql('ALTER TABLE {' . $table . '} ADD displayorder ' . $inttype);
  83. table_column($table, 'date', 'date', 'text', null, null, '', 'not null');
  84. if (is_postgres()) {
  85. execute_sql('ALTER TABLE {' . $table . '} ALTER COLUMN date DROP DEFAULT');
  86. }
  87. if (!empty($records)) {
  88. foreach ($records as $k => $r) {
  89. set_field($table, 'displayorder', $k, 'id', $r->id);
  90. set_field($table, 'date',
  91. format_date(strtotime($r->date), 'strftimedate', 'current', 'artefact.resume'),
  92. 'id', $r->id);
  93. }
  94. }
  95. if (is_mysql()) {
  96. execute_sql('ALTER TABLE {' . $table . '} MODIFY displayorder ' . $inttype . ' NOT NULL');
  97. }
  98. else {
  99. execute_sql('ALTER TABLE {' . $table . '} ALTER displayorder SET NOT NULL');
  100. execute_sql('ALTER TABLE {' . $table . '} ALTER COLUMN date SET NOT NULL');
  101. }
  102. }
  103. }
  104. return $status;
  105. }
  106. ?>