DB.pm 133 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102
  1. # -*- Mode: perl; indent-tabs-mode: nil -*-
  2. #
  3. # The contents of this file are subject to the Mozilla Public
  4. # License Version 1.1 (the "License"); you may not use this file
  5. # except in compliance with the License. You may obtain a copy of
  6. # the License at http://www.mozilla.org/MPL/
  7. #
  8. # Software distributed under the License is distributed on an "AS
  9. # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10. # implied. See the License for the specific language governing
  11. # rights and limitations under the License.
  12. #
  13. # The Original Code is the Bugzilla Bug Tracking System.
  14. #
  15. # Contributor(s): Max Kanat-Alexander <mkanat@bugzilla.org>
  16. # Noel Cragg <noel@red-bean.com>
  17. package Bugzilla::Install::DB;
  18. # NOTE: This package may "use" any modules that it likes,
  19. # localconfig is available, and params are up to date.
  20. use strict;
  21. use Bugzilla::Constants;
  22. use Bugzilla::Hook;
  23. use Bugzilla::Install::Util qw(indicate_progress install_string);
  24. use Bugzilla::Util;
  25. use Bugzilla::Series;
  26. use Date::Parse;
  27. use Date::Format;
  28. use IO::File;
  29. # NOTE: This is NOT the function for general table updates. See
  30. # update_table_definitions for that. This is only for the fielddefs table.
  31. sub update_fielddefs_definition {
  32. my $dbh = Bugzilla->dbh;
  33. # 2005-02-21 - LpSolit@gmail.com - Bug 279910
  34. # qacontact_accessible and assignee_accessible field names no longer exist
  35. # in the 'bugs' table. Their corresponding entries in the 'bugs_activity'
  36. # table should therefore be marked as obsolete, meaning that they cannot
  37. # be used anymore when querying the database - they are not deleted in
  38. # order to keep track of these fields in the activity table.
  39. if (!$dbh->bz_column_info('fielddefs', 'obsolete')) {
  40. $dbh->bz_add_column('fielddefs', 'obsolete',
  41. {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 'FALSE'});
  42. print "Marking qacontact_accessible and assignee_accessible as",
  43. " obsolete fields...\n";
  44. $dbh->do("UPDATE fielddefs SET obsolete = 1
  45. WHERE name = 'qacontact_accessible'
  46. OR name = 'assignee_accessible'");
  47. }
  48. # 2005-08-10 Myk Melez <myk@mozilla.org> bug 287325
  49. # Record each field's type and whether or not it's a custom field,
  50. # in fielddefs.
  51. $dbh->bz_add_column('fielddefs', 'type',
  52. {TYPE => 'INT2', NOTNULL => 1, DEFAULT => 0});
  53. $dbh->bz_add_column('fielddefs', 'custom',
  54. {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 'FALSE'});
  55. $dbh->bz_add_column('fielddefs', 'enter_bug',
  56. {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 'FALSE'});
  57. # Change the name of the fieldid column to id, so that fielddefs
  58. # can use Bugzilla::Object easily. We have to do this up here, because
  59. # otherwise adding these field definitions will fail.
  60. $dbh->bz_rename_column('fielddefs', 'fieldid', 'id');
  61. # If the largest fielddefs sortkey is less than 100, then
  62. # we're using the old sorting system, and we should convert
  63. # it to the new one before adding any new definitions.
  64. if (!$dbh->selectrow_arrayref(
  65. 'SELECT COUNT(id) FROM fielddefs WHERE sortkey >= 100'))
  66. {
  67. print "Updating the sortkeys for the fielddefs table...\n";
  68. my $field_ids = $dbh->selectcol_arrayref(
  69. 'SELECT id FROM fielddefs ORDER BY sortkey');
  70. my $sortkey = 100;
  71. foreach my $field_id (@$field_ids) {
  72. $dbh->do('UPDATE fielddefs SET sortkey = ? WHERE id = ?',
  73. undef, $sortkey, $field_id);
  74. $sortkey += 100;
  75. }
  76. }
  77. # Remember, this is not the function for adding general table changes.
  78. # That is below. Add new changes to the fielddefs table above this
  79. # comment.
  80. }
  81. # Small changes can be put directly into this function.
  82. # However, larger changes (more than three or four lines) should
  83. # go into their own private subroutine, and you should call that
  84. # subroutine from this function. That keeps this function readable.
  85. #
  86. # This function runs in historical order--from upgrades that older
  87. # installations need, to upgrades that newer installations need.
  88. # The order of items inside this function should only be changed if
  89. # absolutely necessary.
  90. #
  91. # The subroutines should have long, descriptive names, so that you
  92. # can easily see what is being done, just by reading this function.
  93. #
  94. # This function is mostly self-documenting. If you're curious about
  95. # what each of the added/removed columns does, you should see the schema
  96. # docs at:
  97. # http://www.ravenbrook.com/project/p4dti/tool/cgi/bugzilla-schema/
  98. #
  99. # When you add a change, you should only add a comment if you want
  100. # to describe why the change was made. You don't need to describe
  101. # the purpose of a column.
  102. #
  103. sub update_table_definitions {
  104. my $old_params = shift;
  105. my $dbh = Bugzilla->dbh;
  106. _update_pre_checksetup_bugzillas();
  107. $dbh->bz_add_column('attachments', 'submitter_id',
  108. {TYPE => 'INT3', NOTNULL => 1}, 0);
  109. $dbh->bz_rename_column('bugs_activity', 'when', 'bug_when');
  110. _add_bug_vote_cache();
  111. _update_product_name_definition();
  112. _add_bug_keyword_cache();
  113. $dbh->bz_add_column('profiles', 'disabledtext',
  114. {TYPE => 'MEDIUMTEXT', NOTNULL => 1}, '');
  115. _populate_longdescs();
  116. _update_bugs_activity_field_to_fieldid();
  117. if (!$dbh->bz_column_info('bugs', 'lastdiffed')) {
  118. $dbh->bz_add_column('bugs', 'lastdiffed', {TYPE =>'DATETIME'});
  119. $dbh->do('UPDATE bugs SET lastdiffed = NOW()');
  120. }
  121. _add_unique_login_name_index_to_profiles();
  122. $dbh->bz_add_column('profiles', 'mybugslink',
  123. {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 'TRUE'});
  124. _update_component_user_fields_to_ids();
  125. $dbh->bz_add_column('bugs', 'everconfirmed',
  126. {TYPE => 'BOOLEAN', NOTNULL => 1}, 1);
  127. $dbh->bz_add_column('products', 'maxvotesperbug',
  128. {TYPE => 'INT2', NOTNULL => 1, DEFAULT => '10000'});
  129. $dbh->bz_add_column('products', 'votestoconfirm',
  130. {TYPE => 'INT2', NOTNULL => 1}, 0);
  131. _populate_milestones_table();
  132. # 2000-03-22 Changed the default value for target_milestone to be "---"
  133. # (which is still not quite correct, but much better than what it was
  134. # doing), and made the size of the value field in the milestones table match
  135. # the size of the target_milestone field in the bugs table.
  136. $dbh->bz_alter_column('bugs', 'target_milestone',
  137. {TYPE => 'varchar(20)', NOTNULL => 1, DEFAULT => "'---'"});
  138. $dbh->bz_alter_column('milestones', 'value',
  139. {TYPE => 'varchar(20)', NOTNULL => 1});
  140. _add_products_defaultmilestone();
  141. # 2000-03-24 Added unique indexes into the cc and keyword tables. This
  142. # prevents certain database inconsistencies, and, moreover, is required for
  143. # new generalized list code to work.
  144. if (!$dbh->bz_index_info('cc', 'cc_bug_id_idx')
  145. || !$dbh->bz_index_info('cc', 'cc_bug_id_idx')->{TYPE})
  146. {
  147. $dbh->bz_drop_index('cc', 'cc_bug_id_idx');
  148. $dbh->bz_add_index('cc', 'cc_bug_id_idx',
  149. {TYPE => 'UNIQUE', FIELDS => [qw(bug_id who)]});
  150. }
  151. if (!$dbh->bz_index_info('keywords', 'keywords_bug_id_idx')
  152. || !$dbh->bz_index_info('keywords', 'keywords_bug_id_idx')->{TYPE})
  153. {
  154. $dbh->bz_drop_index('keywords', 'keywords_bug_id_idx');
  155. $dbh->bz_add_index('keywords', 'keywords_bug_id_idx',
  156. {TYPE => 'UNIQUE', FIELDS => [qw(bug_id keywordid)]});
  157. }
  158. _copy_from_comments_to_longdescs();
  159. _populate_duplicates_table();
  160. if (!$dbh->bz_column_info('email_setting', 'user_id')) {
  161. $dbh->bz_add_column('profiles', 'emailflags', {TYPE => 'MEDIUMTEXT'});
  162. }
  163. $dbh->bz_add_column('groups', 'isactive',
  164. {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 'TRUE'});
  165. $dbh->bz_add_column('attachments', 'isobsolete',
  166. {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 'FALSE'});
  167. $dbh->bz_drop_column("profiles", "emailnotification");
  168. $dbh->bz_drop_column("profiles", "newemailtech");
  169. # 2003-11-19; chicks@chicks.net; bug 225973: fix field size to accommodate
  170. # wider algorithms such as Blowfish. Note that this needs to be run
  171. # before recrypting passwords in the following block.
  172. $dbh->bz_alter_column('profiles', 'cryptpassword',
  173. {TYPE => 'varchar(128)'});
  174. _recrypt_plaintext_passwords();
  175. # 2001-06-15 kiko@async.com.br - Change bug:version size to avoid
  176. # truncates re http://bugzilla.mozilla.org/show_bug.cgi?id=9352
  177. $dbh->bz_alter_column('bugs', 'version',
  178. {TYPE => 'varchar(64)', NOTNULL => 1});
  179. _update_bugs_activity_to_only_record_changes();
  180. # bug 90933: Make disabledtext NOT NULL
  181. if (!$dbh->bz_column_info('profiles', 'disabledtext')->{NOTNULL}) {
  182. $dbh->bz_alter_column("profiles", "disabledtext",
  183. {TYPE => 'MEDIUMTEXT', NOTNULL => 1}, '');
  184. }
  185. $dbh->bz_add_column("bugs", "reporter_accessible",
  186. {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 'TRUE'});
  187. $dbh->bz_add_column("bugs", "cclist_accessible",
  188. {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 'TRUE'});
  189. $dbh->bz_add_column("bugs_activity", "attach_id", {TYPE => 'INT3'});
  190. _delete_logincookies_cryptpassword_and_handle_invalid_cookies();
  191. # qacontact/assignee should always be able to see bugs: bug 97471
  192. $dbh->bz_drop_column("bugs", "qacontact_accessible");
  193. $dbh->bz_drop_column("bugs", "assignee_accessible");
  194. # 2002-02-20 jeff.hedlund@matrixsi.com - bug 24789 time tracking
  195. $dbh->bz_add_column("longdescs", "work_time",
  196. {TYPE => 'decimal(5,2)', NOTNULL => 1, DEFAULT => '0'});
  197. $dbh->bz_add_column("bugs", "estimated_time",
  198. {TYPE => 'decimal(5,2)', NOTNULL => 1, DEFAULT => '0'});
  199. $dbh->bz_add_column("bugs", "remaining_time",
  200. {TYPE => 'decimal(5,2)', NOTNULL => 1, DEFAULT => '0'});
  201. $dbh->bz_add_column("bugs", "deadline", {TYPE => 'DATETIME'});
  202. _use_ip_instead_of_hostname_in_logincookies();
  203. $dbh->bz_add_column('longdescs', 'isprivate',
  204. {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 'FALSE'});
  205. $dbh->bz_add_column('attachments', 'isprivate',
  206. {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 'FALSE'});
  207. $dbh->bz_add_column("bugs", "alias", {TYPE => "varchar(20)"});
  208. $dbh->bz_add_index('bugs', 'bugs_alias_idx',
  209. {TYPE => 'UNIQUE', FIELDS => [qw(alias)]});
  210. _move_quips_into_db();
  211. $dbh->bz_drop_column("namedqueries", "watchfordiffs");
  212. _use_ids_for_products_and_components();
  213. _convert_groups_system_from_groupset();
  214. _convert_attachment_statuses_to_flags();
  215. _remove_spaces_and_commas_from_flagtypes();
  216. _setup_usebuggroups_backward_compatibility();
  217. _remove_user_series_map();
  218. # 2006-08-03 remi_zara@mac.com bug 346241, make series.creator nullable
  219. # This must happen before calling _copy_old_charts_into_database().
  220. if ($dbh->bz_column_info('series', 'creator')->{NOTNULL}) {
  221. $dbh->bz_alter_column('series', 'creator', {TYPE => 'INT3'});
  222. $dbh->do("UPDATE series SET creator = NULL WHERE creator = 0");
  223. }
  224. _copy_old_charts_into_database();
  225. _add_user_group_map_grant_type();
  226. _add_group_group_map_grant_type();
  227. $dbh->bz_add_column("profiles", "extern_id", {TYPE => 'varchar(64)'});
  228. $dbh->bz_add_column('flagtypes', 'grant_group_id', {TYPE => 'INT3'});
  229. $dbh->bz_add_column('flagtypes', 'request_group_id', {TYPE => 'INT3'});
  230. # mailto is no longer just userids
  231. $dbh->bz_rename_column('whine_schedules', 'mailto_userid', 'mailto');
  232. $dbh->bz_add_column('whine_schedules', 'mailto_type',
  233. {TYPE => 'INT2', NOTNULL => 1, DEFAULT => '0'});
  234. _add_longdescs_already_wrapped();
  235. # Moved enum types to separate tables so we need change the old enum
  236. # types to standard varchars in the bugs table.
  237. $dbh->bz_alter_column('bugs', 'bug_status',
  238. {TYPE => 'varchar(64)', NOTNULL => 1});
  239. # 2005-03-23 Tomas.Kopal@altap.cz - add default value to resolution,
  240. # bug 286695
  241. $dbh->bz_alter_column('bugs', 'resolution',
  242. {TYPE => 'varchar(64)', NOTNULL => 1, DEFAULT => "''"});
  243. $dbh->bz_alter_column('bugs', 'priority',
  244. {TYPE => 'varchar(64)', NOTNULL => 1});
  245. $dbh->bz_alter_column('bugs', 'bug_severity',
  246. {TYPE => 'varchar(64)', NOTNULL => 1});
  247. $dbh->bz_alter_column('bugs', 'rep_platform',
  248. {TYPE => 'varchar(64)', NOTNULL => 1}, '');
  249. $dbh->bz_alter_column('bugs', 'op_sys',
  250. {TYPE => 'varchar(64)', NOTNULL => 1});
  251. # When migrating quips from the '$datadir/comments' file to the DB,
  252. # the user ID should be NULL instead of 0 (which is an invalid user ID).
  253. if ($dbh->bz_column_info('quips', 'userid')->{NOTNULL}) {
  254. $dbh->bz_alter_column('quips', 'userid', {TYPE => 'INT3'});
  255. print "Changing owner to NULL for quips where the owner is",
  256. " unknown...\n";
  257. $dbh->do('UPDATE quips SET userid = NULL WHERE userid = 0');
  258. }
  259. _convert_attachments_filename_from_mediumtext();
  260. $dbh->bz_add_column('quips', 'approved',
  261. {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 'TRUE'});
  262. # 2002-12-20 Bug 180870 - remove manual shadowdb replication code
  263. $dbh->bz_drop_table("shadowlog");
  264. _rename_votes_count_and_force_group_refresh();
  265. # 2004/02/15 - Summaries shouldn't be null - see bug 220232
  266. if (!exists $dbh->bz_column_info('bugs', 'short_desc')->{NOTNULL}) {
  267. $dbh->bz_alter_column('bugs', 'short_desc',
  268. {TYPE => 'MEDIUMTEXT', NOTNULL => 1}, '');
  269. }
  270. $dbh->bz_add_column('products', 'classification_id',
  271. {TYPE => 'INT2', NOTNULL => 1, DEFAULT => '1'});
  272. _fix_group_with_empty_name();
  273. $dbh->bz_add_index('bugs_activity', 'bugs_activity_who_idx', [qw(who)]);
  274. # Add defaults for some fields that should have them but didn't.
  275. $dbh->bz_alter_column('bugs', 'status_whiteboard',
  276. {TYPE => 'MEDIUMTEXT', NOTNULL => 1, DEFAULT => "''"});
  277. $dbh->bz_alter_column('bugs', 'keywords',
  278. {TYPE => 'MEDIUMTEXT', NOTNULL => 1, DEFAULT => "''"});
  279. $dbh->bz_alter_column('bugs', 'votes',
  280. {TYPE => 'INT3', NOTNULL => 1, DEFAULT => '0'});
  281. $dbh->bz_alter_column('bugs', 'lastdiffed', {TYPE => 'DATETIME'});
  282. # 2005-03-09 qa_contact should be NULL instead of 0, bug 285534
  283. if ($dbh->bz_column_info('bugs', 'qa_contact')->{NOTNULL}) {
  284. $dbh->bz_alter_column('bugs', 'qa_contact', {TYPE => 'INT3'});
  285. $dbh->do("UPDATE bugs SET qa_contact = NULL WHERE qa_contact = 0");
  286. }
  287. # 2005-03-27 initialqacontact should be NULL instead of 0, bug 287483
  288. if ($dbh->bz_column_info('components', 'initialqacontact')->{NOTNULL}) {
  289. $dbh->bz_alter_column('components', 'initialqacontact',
  290. {TYPE => 'INT3'});
  291. }
  292. $dbh->do("UPDATE components SET initialqacontact = NULL " .
  293. "WHERE initialqacontact = 0");
  294. _migrate_email_prefs_to_new_table();
  295. _initialize_dependency_tree_changes_email_pref();
  296. _change_all_mysql_booleans_to_tinyint();
  297. # make classification_id field type be consistent with DB:Schema
  298. $dbh->bz_alter_column('products', 'classification_id',
  299. {TYPE => 'INT2', NOTNULL => 1, DEFAULT => '1'});
  300. # initialowner was accidentally NULL when we checked-in Schema,
  301. # when it really should be NOT NULL.
  302. $dbh->bz_alter_column('components', 'initialowner',
  303. {TYPE => 'INT3', NOTNULL => 1}, 0);
  304. # 2005-03-28 - bug 238800 - index flags.type_id for editflagtypes.cgi
  305. $dbh->bz_add_index('flags', 'flags_type_id_idx', [qw(type_id)]);
  306. # For a short time, the flags_type_id_idx was misnamed in upgraded installs.
  307. $dbh->bz_drop_index('flags', 'type_id');
  308. # 2005-04-28 - LpSolit@gmail.com - Bug 7233: add an index to versions
  309. $dbh->bz_alter_column('versions', 'value',
  310. {TYPE => 'varchar(64)', NOTNULL => 1});
  311. _add_versions_product_id_index();
  312. if (!exists $dbh->bz_column_info('milestones', 'sortkey')->{DEFAULT}) {
  313. $dbh->bz_alter_column('milestones', 'sortkey',
  314. {TYPE => 'INT2', NOTNULL => 1, DEFAULT => 0});
  315. }
  316. # 2005-06-14 - LpSolit@gmail.com - Bug 292544
  317. $dbh->bz_alter_column('bugs', 'creation_ts', {TYPE => 'DATETIME'});
  318. _fix_whine_queries_title_and_op_sys_value();
  319. _fix_attachments_submitter_id_idx();
  320. _copy_attachments_thedata_to_attach_data();
  321. _fix_broken_all_closed_series();
  322. # 2005-08-14 bugreport@peshkin.net -- Bug 304583
  323. # Get rid of leftover DERIVED group permissions
  324. use constant GRANT_DERIVED => 1;
  325. $dbh->do("DELETE FROM user_group_map WHERE grant_type = " . GRANT_DERIVED);
  326. # PUBLIC is a reserved word in Oracle.
  327. $dbh->bz_rename_column('series', 'public', 'is_public');
  328. # 2005-09-28 bugreport@peshkin.net Bug 149504
  329. $dbh->bz_add_column('attachments', 'isurl',
  330. {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 0});
  331. # 2005-10-21 LpSolit@gmail.com - Bug 313020
  332. $dbh->bz_add_column('namedqueries', 'query_type',
  333. {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 0});
  334. # 2005-11-04 LpSolit@gmail.com - Bug 305927
  335. $dbh->bz_alter_column('groups', 'userregexp',
  336. {TYPE => 'TINYTEXT', NOTNULL => 1, DEFAULT => "''"});
  337. # 2005-09-26 - olav@bkor.dhs.org - Bug 119524
  338. $dbh->bz_alter_column('logincookies', 'cookie',
  339. {TYPE => 'varchar(16)', PRIMARYKEY => 1, NOTNULL => 1});
  340. _clean_control_characters_from_short_desc();
  341. # 2005-12-07 altlst@sonic.net -- Bug 225221
  342. $dbh->bz_add_column('longdescs', 'comment_id',
  343. {TYPE => 'MEDIUMSERIAL', NOTNULL => 1, PRIMARYKEY => 1});
  344. _stop_storing_inactive_flags();
  345. _change_short_desc_from_mediumtext_to_varchar();
  346. # 2006-07-01 wurblzap@gmail.com -- Bug 69000
  347. $dbh->bz_add_column('namedqueries', 'id',
  348. {TYPE => 'MEDIUMSERIAL', NOTNULL => 1, PRIMARYKEY => 1});
  349. _move_namedqueries_linkinfooter_to_its_own_table();
  350. _add_classifications_sortkey();
  351. _move_data_nomail_into_db();
  352. # The products table lacked sensible defaults.
  353. $dbh->bz_alter_column('products', 'milestoneurl',
  354. {TYPE => 'TINYTEXT', NOTNULL => 1, DEFAULT => "''"});
  355. $dbh->bz_alter_column('products', 'disallownew',
  356. {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 0});
  357. $dbh->bz_alter_column('products', 'votesperuser',
  358. {TYPE => 'INT2', NOTNULL => 1, DEFAULT => 0});
  359. $dbh->bz_alter_column('products', 'votestoconfirm',
  360. {TYPE => 'INT2', NOTNULL => 1, DEFAULT => 0});
  361. # 2006-08-04 LpSolit@gmail.com - Bug 305941
  362. $dbh->bz_drop_column('profiles', 'refreshed_when');
  363. $dbh->bz_drop_column('groups', 'last_changed');
  364. # 2006-08-06 LpSolit@gmail.com - Bug 347521
  365. $dbh->bz_alter_column('flagtypes', 'id',
  366. {TYPE => 'SMALLSERIAL', NOTNULL => 1, PRIMARYKEY => 1});
  367. $dbh->bz_alter_column('keyworddefs', 'id',
  368. {TYPE => 'SMALLSERIAL', NOTNULL => 1, PRIMARYKEY => 1});
  369. # 2006-08-19 LpSolit@gmail.com - Bug 87795
  370. $dbh->bz_alter_column('tokens', 'userid', {TYPE => 'INT3'});
  371. $dbh->bz_drop_index('bugs', 'bugs_short_desc_idx');
  372. # The profiles table was missing some defaults.
  373. $dbh->bz_alter_column('profiles', 'disabledtext',
  374. {TYPE => 'MEDIUMTEXT', NOTNULL => 1, DEFAULT => "''"});
  375. $dbh->bz_alter_column('profiles', 'realname',
  376. {TYPE => 'varchar(255)', NOTNULL => 1, DEFAULT => "''"});
  377. _update_longdescs_who_index();
  378. $dbh->bz_add_column('setting', 'subclass', {TYPE => 'varchar(32)'});
  379. $dbh->bz_alter_column('longdescs', 'thetext',
  380. {TYPE => 'LONGTEXT', NOTNULL => 1}, '');
  381. # 2006-10-20 LpSolit@gmail.com - Bug 189627
  382. $dbh->bz_add_column('group_control_map', 'editcomponents',
  383. {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 'FALSE'});
  384. $dbh->bz_add_column('group_control_map', 'editbugs',
  385. {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 'FALSE'});
  386. $dbh->bz_add_column('group_control_map', 'canconfirm',
  387. {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 'FALSE'});
  388. # 2006-11-07 LpSolit@gmail.com - Bug 353656
  389. $dbh->bz_add_column('longdescs', 'type',
  390. {TYPE => 'INT2', NOTNULL => 1, DEFAULT => '0'});
  391. $dbh->bz_add_column('longdescs', 'extra_data', {TYPE => 'varchar(255)'});
  392. $dbh->bz_add_column('versions', 'id',
  393. {TYPE => 'MEDIUMSERIAL', NOTNULL => 1, PRIMARYKEY => 1});
  394. $dbh->bz_add_column('milestones', 'id',
  395. {TYPE => 'MEDIUMSERIAL', NOTNULL => 1, PRIMARYKEY => 1});
  396. _fix_uppercase_custom_field_names();
  397. _fix_uppercase_index_names();
  398. # 2007-05-17 LpSolit@gmail.com - Bug 344965
  399. _initialize_workflow($old_params);
  400. # 2007-08-08 LpSolit@gmail.com - Bug 332149
  401. $dbh->bz_add_column('groups', 'icon_url', {TYPE => 'TINYTEXT'});
  402. # 2007-08-21 wurblzap@gmail.com - Bug 365378
  403. _make_lang_setting_dynamic();
  404. # 2007-11-29 xiaoou.wu@oracle.com - Bug 153129
  405. _change_text_types();
  406. # 2007-09-09 LpSolit@gmail.com - Bug 99215
  407. _fix_attachment_modification_date();
  408. # This had the wrong definition in DB::Schema.
  409. $dbh->bz_alter_column('namedqueries', 'query_type',
  410. {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 0});
  411. $dbh->bz_drop_index('longdescs', 'longdescs_thetext_idx');
  412. _populate_bugs_fulltext();
  413. # 2008-01-18 xiaoou.wu@oracle.com - Bug 414292
  414. $dbh->bz_alter_column('series', 'query',
  415. { TYPE => 'MEDIUMTEXT', NOTNULL => 1 });
  416. ################################################################
  417. # New --TABLE-- changes should go *** A B O V E *** this point #
  418. ################################################################
  419. Bugzilla::Hook::process('install-update_db');
  420. $dbh->bz_setup_foreign_keys();
  421. }
  422. # Subroutines should be ordered in the order that they are called.
  423. # Thus, newer subroutines should be at the bottom.
  424. sub _update_pre_checksetup_bugzillas {
  425. my $dbh = Bugzilla->dbh;
  426. # really old fields that were added before checksetup.pl existed
  427. # but aren't in very old bugzilla's (like 2.1)
  428. # Steve Stock (sstock@iconnect-inc.com)
  429. $dbh->bz_add_column('bugs', 'target_milestone',
  430. {TYPE => 'varchar(20)', NOTNULL => 1, DEFAULT => "'---'"});
  431. $dbh->bz_add_column('bugs', 'qa_contact', {TYPE => 'INT3'});
  432. $dbh->bz_add_column('bugs', 'status_whiteboard',
  433. {TYPE => 'MEDIUMTEXT', NOTNULL => 1, DEFAULT => "''"});
  434. $dbh->bz_add_column('products', 'disallownew',
  435. {TYPE => 'BOOLEAN', NOTNULL => 1}, 0);
  436. $dbh->bz_add_column('products', 'milestoneurl',
  437. {TYPE => 'TINYTEXT', NOTNULL => 1}, '');
  438. $dbh->bz_add_column('components', 'initialqacontact',
  439. {TYPE => 'TINYTEXT'});
  440. $dbh->bz_add_column('components', 'description',
  441. {TYPE => 'MEDIUMTEXT', NOTNULL => 1}, '');
  442. }
  443. sub _add_bug_vote_cache {
  444. my $dbh = Bugzilla->dbh;
  445. # 1999-10-11 Restructured voting database to add a cached value in each
  446. # bug recording how many total votes that bug has. While I'm at it,
  447. # I removed the unused "area" field from the bugs database. It is
  448. # distressing to realize that the bugs table has reached the maximum
  449. # number of indices allowed by MySQL (16), which may make future
  450. # enhancements awkward.
  451. # (P.S. All is not lost; it appears that the latest betas of MySQL
  452. # support a new table format which will allow 32 indices.)
  453. $dbh->bz_drop_column('bugs', 'area');
  454. if (!$dbh->bz_column_info('bugs', 'votes')) {
  455. $dbh->bz_add_column('bugs', 'votes', {TYPE => 'INT3', NOTNULL => 1,
  456. DEFAULT => 0});
  457. $dbh->bz_add_index('bugs', 'bugs_votes_idx', [qw(votes)]);
  458. }
  459. $dbh->bz_add_column('products', 'votesperuser',
  460. {TYPE => 'INT2', NOTNULL => 1}, 0);
  461. }
  462. sub _update_product_name_definition {
  463. my $dbh = Bugzilla->dbh;
  464. # The product name used to be very different in various tables.
  465. #
  466. # It was varchar(16) in bugs
  467. # tinytext in components
  468. # tinytext in products
  469. # tinytext in versions
  470. #
  471. # tinytext is equivalent to varchar(255), which is quite huge, so I change
  472. # them all to varchar(64).
  473. # Only do this if these fields still exist - they're removed in
  474. # a later change
  475. if ($dbh->bz_column_info('products', 'product')) {
  476. $dbh->bz_alter_column('bugs', 'product',
  477. {TYPE => 'varchar(64)', NOTNULL => 1});
  478. $dbh->bz_alter_column('components', 'program', {TYPE => 'varchar(64)'});
  479. $dbh->bz_alter_column('products', 'product', {TYPE => 'varchar(64)'});
  480. $dbh->bz_alter_column('versions', 'program',
  481. {TYPE => 'varchar(64)', NOTNULL => 1});
  482. }
  483. }
  484. sub _add_bug_keyword_cache {
  485. my $dbh = Bugzilla->dbh;
  486. # 2000-01-16 Added a "keywords" field to the bugs table, which
  487. # contains a string copy of the entries of the keywords table for this
  488. # bug. This is so that I can easily sort and display a keywords
  489. # column in bug lists.
  490. if (!$dbh->bz_column_info('bugs', 'keywords')) {
  491. $dbh->bz_add_column('bugs', 'keywords',
  492. {TYPE => 'MEDIUMTEXT', NOTNULL => 1, DEFAULT => "''"});
  493. my @kwords;
  494. print "Making sure 'keywords' field of table 'bugs' is empty...\n";
  495. $dbh->do("UPDATE bugs SET keywords = '' WHERE keywords != ''");
  496. print "Repopulating 'keywords' field of table 'bugs'...\n";
  497. my $sth = $dbh->prepare("SELECT keywords.bug_id, keyworddefs.name " .
  498. "FROM keywords, keyworddefs " .
  499. "WHERE keyworddefs.id = keywords.keywordid " .
  500. "ORDER BY keywords.bug_id, keyworddefs.name");
  501. $sth->execute;
  502. my @list;
  503. my $bugid = 0;
  504. my @row;
  505. while (1) {
  506. my ($b, $k) = ($sth->fetchrow_array());
  507. if (!defined $b || $b ne $bugid) {
  508. if (@list) {
  509. $dbh->do("UPDATE bugs SET keywords = " .
  510. $dbh->quote(join(', ', @list)) .
  511. " WHERE bug_id = $bugid");
  512. }
  513. last if !$b;
  514. $bugid = $b;
  515. @list = ();
  516. }
  517. push(@list, $k);
  518. }
  519. }
  520. }
  521. # A helper for the function below.
  522. sub _write_one_longdesc {
  523. my ($id, $who, $when, $buffer) = (@_);
  524. my $dbh = Bugzilla->dbh;
  525. $buffer = trim($buffer);
  526. return if !$buffer;
  527. $dbh->do("INSERT INTO longdescs (bug_id, who, bug_when, thetext)
  528. VALUES (?,?,?,?)", undef, $id, $who,
  529. time2str("%Y/%m/%d %H:%M:%S", $when), $buffer);
  530. }
  531. sub _populate_longdescs {
  532. my $dbh = Bugzilla->dbh;
  533. # 2000-01-20 Added a new "longdescs" table, which is supposed to have
  534. # all the long descriptions in it, replacing the old long_desc field
  535. # in the bugs table. The below hideous code populates this new table
  536. # with things from the old field, with ugly parsing and heuristics.
  537. if ($dbh->bz_column_info('bugs', 'long_desc')) {
  538. my ($total) = $dbh->selectrow_array("SELECT COUNT(*) FROM bugs");
  539. print "Populating new long_desc table. This is slow. There are",
  540. " $total\nbugs to process; a line of dots will be printed",
  541. " for each 50.\n\n";
  542. local $| = 1;
  543. # On MySQL, longdescs doesn't benefit from transactions, but this
  544. # doesn't hurt.
  545. $dbh->bz_start_transaction();
  546. $dbh->do('DELETE FROM longdescs');
  547. my $sth = $dbh->prepare("SELECT bug_id, creation_ts, reporter,
  548. long_desc FROM bugs ORDER BY bug_id");
  549. $sth->execute();
  550. my $count = 0;
  551. while (my ($id, $createtime, $reporterid, $desc) =
  552. $sth->fetchrow_array())
  553. {
  554. $count++;
  555. indicate_progress({ total => $total, current => $count });
  556. $desc =~ s/\r//g;
  557. my $who = $reporterid;
  558. my $when = str2time($createtime);
  559. my $buffer = "";
  560. foreach my $line (split(/\n/, $desc)) {
  561. $line =~ s/\s+$//g; # Trim trailing whitespace.
  562. if ($line =~ /^------- Additional Comments From ([^\s]+)\s+(\d.+\d)\s+-------$/)
  563. {
  564. my $name = $1;
  565. my $date = str2time($2);
  566. # Oy, what a hack. The creation time is accurate to the
  567. # second. But the long text only contains things accurate
  568. # to the And so, if someone makes a comment within a
  569. # minute of the original bug creation, then the comment can
  570. # come *before* the bug creation. So, we add 59 seconds to
  571. # the time of all comments, so that they are always
  572. # considered to have happened at the *end* of the given
  573. # minute, not the beginning.
  574. $date += 59;
  575. if ($date >= $when) {
  576. _write_one_longdesc($id, $who, $when, $buffer);
  577. $buffer = "";
  578. $when = $date;
  579. my $s2 = $dbh->prepare("SELECT userid FROM profiles " .
  580. "WHERE login_name = ?");
  581. $s2->execute($name);
  582. ($who) = ($s2->fetchrow_array());
  583. if (!$who) {
  584. # This username doesn't exist. Maybe someone
  585. # renamed him or something. Invent a new profile
  586. # entry disabled, just to represent him.
  587. $dbh->do("INSERT INTO profiles (login_name,
  588. cryptpassword, disabledtext)
  589. VALUES (?,?,?)", undef, $name, '*',
  590. "Account created only to maintain"
  591. . " database integrity");
  592. $who = $dbh->bz_last_key('profiles', 'userid');
  593. }
  594. next;
  595. }
  596. }
  597. $buffer .= $line . "\n";
  598. }
  599. _write_one_longdesc($id, $who, $when, $buffer);
  600. } # while loop
  601. print "\n\n";
  602. $dbh->bz_drop_column('bugs', 'long_desc');
  603. $dbh->bz_commit_transaction();
  604. } # main if
  605. }
  606. sub _update_bugs_activity_field_to_fieldid {
  607. my $dbh = Bugzilla->dbh;
  608. # 2000-01-18 Added a new table fielddefs that records information about the
  609. # different fields we keep an activity log on. The bugs_activity table
  610. # now has a pointer into that table instead of recording the name directly.
  611. if ($dbh->bz_column_info('bugs_activity', 'field')) {
  612. $dbh->bz_add_column('bugs_activity', 'fieldid',
  613. {TYPE => 'INT3', NOTNULL => 1}, 0);
  614. $dbh->bz_add_index('bugs_activity', 'bugs_activity_fieldid_idx',
  615. [qw(fieldid)]);
  616. print "Populating new bugs_activity.fieldid field...\n";
  617. $dbh->bz_start_transaction();
  618. my $ids = $dbh->selectall_arrayref(
  619. 'SELECT DISTINCT fielddefs.id, bugs_activity.field
  620. FROM bugs_activity LEFT JOIN fielddefs
  621. ON bugs_activity.field = fielddefs.name', {Slice=>{}});
  622. foreach my $item (@$ids) {
  623. my $id = $item->{id};
  624. my $field = $item->{field};
  625. # If the id is NULL
  626. if (!$id) {
  627. $dbh->do("INSERT INTO fielddefs (name, description) VALUES " .
  628. "(?, ?)", undef, $field, $field);
  629. $id = $dbh->bz_last_key('fielddefs', 'id');
  630. }
  631. $dbh->do("UPDATE bugs_activity SET fieldid = ? WHERE field = ?",
  632. undef, $id, $field);
  633. }
  634. $dbh->bz_commit_transaction();
  635. $dbh->bz_drop_column('bugs_activity', 'field');
  636. }
  637. }
  638. sub _add_unique_login_name_index_to_profiles {
  639. my $dbh = Bugzilla->dbh;
  640. # 2000-01-22 The "login_name" field in the "profiles" table was not
  641. # declared to be unique. Sure enough, somehow, I got 22 duplicated entries
  642. # in my database. This code detects that, cleans up the duplicates, and
  643. # then tweaks the table to declare the field to be unique. What a pain.
  644. if (!$dbh->bz_index_info('profiles', 'profiles_login_name_idx')
  645. || !$dbh->bz_index_info('profiles', 'profiles_login_name_idx')->{TYPE})
  646. {
  647. print "Searching for duplicate entries in the profiles table...\n";
  648. while (1) {
  649. # This code is weird in that it loops around and keeps doing this
  650. # select again. That's because I'm paranoid about deleting entries
  651. # out from under us in the profiles table. Things get weird if
  652. # there are *three* or more entries for the same user...
  653. my $sth = $dbh->prepare("SELECT p1.userid, p2.userid, p1.login_name
  654. FROM profiles AS p1, profiles AS p2
  655. WHERE p1.userid < p2.userid
  656. AND p1.login_name = p2.login_name
  657. ORDER BY p1.login_name");
  658. $sth->execute();
  659. my ($u1, $u2, $n) = ($sth->fetchrow_array);
  660. last if !$u1;
  661. print "Both $u1 & $u2 are ids for $n! Merging $u2 into $u1...\n";
  662. foreach my $i (["bugs", "reporter"],
  663. ["bugs", "assigned_to"],
  664. ["bugs", "qa_contact"],
  665. ["attachments", "submitter_id"],
  666. ["bugs_activity", "who"],
  667. ["cc", "who"],
  668. ["votes", "who"],
  669. ["longdescs", "who"]) {
  670. my ($table, $field) = (@$i);
  671. print " Updating $table.$field...\n";
  672. $dbh->do("UPDATE $table SET $field = $u1 " .
  673. "WHERE $field = $u2");
  674. }
  675. $dbh->do("DELETE FROM profiles WHERE userid = $u2");
  676. }
  677. print "OK, changing index type to prevent duplicates in the",
  678. " future...\n";
  679. $dbh->bz_drop_index('profiles', 'profiles_login_name_idx');
  680. $dbh->bz_add_index('profiles', 'profiles_login_name_idx',
  681. {TYPE => 'UNIQUE', FIELDS => [qw(login_name)]});
  682. }
  683. }
  684. sub _update_component_user_fields_to_ids {
  685. my $dbh = Bugzilla->dbh;
  686. # components.initialowner
  687. my $comp_init_owner = $dbh->bz_column_info('components', 'initialowner');
  688. if ($comp_init_owner && $comp_init_owner->{TYPE} eq 'TINYTEXT') {
  689. my $sth = $dbh->prepare("SELECT program, value, initialowner
  690. FROM components");
  691. $sth->execute();
  692. while (my ($program, $value, $initialowner) = $sth->fetchrow_array()) {
  693. my ($id) = $dbh->selectrow_array(
  694. "SELECT userid FROM profiles WHERE login_name = ?",
  695. undef, $initialowner);
  696. unless (defined $id) {
  697. print "Warning: You have an invalid default assignee",
  698. " '$initialowner'\n in component '$value' of program",
  699. " '$program'!\n";
  700. $id = 0;
  701. }
  702. $dbh->do("UPDATE components SET initialowner = ?
  703. WHERE program = ? AND value = ?", undef,
  704. $id, $program, $value);
  705. }
  706. $dbh->bz_alter_column('components','initialowner',{TYPE => 'INT3'});
  707. }
  708. # components.initialqacontact
  709. my $comp_init_qa = $dbh->bz_column_info('components', 'initialqacontact');
  710. if ($comp_init_qa && $comp_init_qa->{TYPE} eq 'TINYTEXT') {
  711. my $sth = $dbh->prepare("SELECT program, value, initialqacontact
  712. FROM components");
  713. $sth->execute();
  714. while (my ($program, $value, $initialqacontact) =
  715. $sth->fetchrow_array())
  716. {
  717. my ($id) = $dbh->selectrow_array(
  718. "SELECT userid FROM profiles WHERE login_name = ?",
  719. undef, $initialqacontact);
  720. unless (defined $id) {
  721. if ($initialqacontact) {
  722. print "Warning: You have an invalid default QA contact",
  723. " $initialqacontact' in program '$program',",
  724. " component '$value'!\n";
  725. }
  726. $id = 0;
  727. }
  728. $dbh->do("UPDATE components SET initialqacontact = ?
  729. WHERE program = ? AND value = ?", undef,
  730. $id, $program, $value);
  731. }
  732. $dbh->bz_alter_column('components','initialqacontact',{TYPE => 'INT3'});
  733. }
  734. }
  735. sub _populate_milestones_table {
  736. my $dbh = Bugzilla->dbh;
  737. # 2000-03-21 Adding a table for target milestones to
  738. # database - matthew@zeroknowledge.com
  739. # If the milestones table is empty, and we're still back in a Bugzilla
  740. # that has a bugs.product field, that means that we just created
  741. # the milestones table and it needs to be populated.
  742. my $milestones_exist = $dbh->selectrow_array(
  743. "SELECT DISTINCT 1 FROM milestones");
  744. if (!$milestones_exist && $dbh->bz_column_info('bugs', 'product')) {
  745. print "Replacing blank milestones...\n";
  746. $dbh->do("UPDATE bugs
  747. SET target_milestone = '---'
  748. WHERE target_milestone = ' '");
  749. # If we are upgrading from 2.8 or earlier, we will have *created*
  750. # the milestones table with a product_id field, but Bugzilla expects
  751. # it to have a "product" field. So we change the field backward so
  752. # other code can run. The change will be reversed later in checksetup.
  753. if ($dbh->bz_column_info('milestones', 'product_id')) {
  754. # Dropping the column leaves us with a milestones_product_id_idx
  755. # index that is only on the "value" column. We need to drop the
  756. # whole index so that it can be correctly re-created later.
  757. $dbh->bz_drop_index('milestones', 'milestones_product_id_idx');
  758. $dbh->bz_drop_column('milestones', 'product_id');
  759. $dbh->bz_add_column('milestones', 'product',
  760. {TYPE => 'varchar(64)', NOTNULL => 1}, '');
  761. }
  762. # Populate the milestone table with all existing values in the database
  763. my $sth = $dbh->prepare("SELECT DISTINCT target_milestone, product
  764. FROM bugs");
  765. $sth->execute();
  766. print "Populating milestones table...\n";
  767. while (my ($value, $product) = $sth->fetchrow_array()) {
  768. # check if the value already exists
  769. my $sortkey = substr($value, 1);
  770. if ($sortkey !~ /^\d+$/) {
  771. $sortkey = 0;
  772. } else {
  773. $sortkey *= 10;
  774. }
  775. my $ms_exists = $dbh->selectrow_array(
  776. "SELECT value FROM milestones
  777. WHERE value = ? AND product = ?", undef, $value, $product);
  778. if (!$ms_exists) {
  779. $dbh->do("INSERT INTO milestones(value, product, sortkey)
  780. VALUES (?,?,?)", undef, $value, $product, $sortkey);
  781. }
  782. }
  783. }
  784. }
  785. sub _add_products_defaultmilestone {
  786. my $dbh = Bugzilla->dbh;
  787. # 2000-03-23 Added a defaultmilestone field to the products table, so that
  788. # we know which milestone to initially assign bugs to.
  789. if (!$dbh->bz_column_info('products', 'defaultmilestone')) {
  790. $dbh->bz_add_column('products', 'defaultmilestone',
  791. {TYPE => 'varchar(20)', NOTNULL => 1, DEFAULT => "'---'"});
  792. my $sth = $dbh->prepare(
  793. "SELECT product, defaultmilestone FROM products");
  794. $sth->execute();
  795. while (my ($product, $default_ms) = $sth->fetchrow_array()) {
  796. my $exists = $dbh->selectrow_array(
  797. "SELECT value FROM milestones
  798. WHERE value = ? AND product = ?",
  799. undef, $default_ms, $product);
  800. if (!$exists) {
  801. $dbh->do("INSERT INTO milestones(value, product) " .
  802. "VALUES (?, ?)", undef, $default_ms, $product);
  803. }
  804. }
  805. }
  806. }
  807. sub _copy_from_comments_to_longdescs {
  808. my $dbh = Bugzilla->dbh;
  809. # 2000-11-27 For Bugzilla 2.5 and later. Copy data from 'comments' to
  810. # 'longdescs' - the new name of the comments table.
  811. if ($dbh->bz_table_info('comments')) {
  812. my $quoted_when = $dbh->quote_identifier('when');
  813. $dbh->do("INSERT INTO longdescs (bug_when, bug_id, who, thetext)
  814. SELECT $quoted_when, bug_id, who, comment
  815. FROM comments");
  816. $dbh->bz_drop_table("comments");
  817. }
  818. }
  819. sub _populate_duplicates_table {
  820. my $dbh = Bugzilla->dbh;
  821. # 2000-07-15 Added duplicates table so Bugzilla tracks duplicates in a
  822. # better way than it used to. This code searches the comments to populate
  823. # the table initially. It's executed if the table is empty; if it's
  824. # empty because there are no dupes (as opposed to having just created
  825. # the table) it won't have any effect anyway, so it doesn't matter.
  826. my ($dups_exist) = $dbh->selectrow_array(
  827. "SELECT DISTINCT 1 FROM duplicates");
  828. # We also check against a schema change that happened later.
  829. if (!$dups_exist && !$dbh->bz_column_info('groups', 'isactive')) {
  830. # populate table
  831. print "Populating duplicates table from comments...\n";
  832. my $sth = $dbh->prepare(
  833. "SELECT longdescs.bug_id, thetext
  834. FROM longdescs LEFT JOIN bugs
  835. ON longdescs.bug_id = bugs.bug_id
  836. WHERE (" . $dbh->sql_regexp("thetext",
  837. "'[.*.]{3} This bug has been marked as a duplicate"
  838. . " of [[:digit:]]+ [.*.]{3}'")
  839. . ")
  840. AND resolution = 'DUPLICATE'
  841. ORDER BY longdescs.bug_when");
  842. $sth->execute();
  843. my (%dupes, $key);
  844. # Because of the way hashes work, this loop removes all but the
  845. # last dupe resolution found for a given bug.
  846. while (my ($dupe, $dupe_of) = $sth->fetchrow_array()) {
  847. $dupes{$dupe} = $dupe_of;
  848. }
  849. foreach $key (keys(%dupes)){
  850. $dupes{$key} =~ /^.*\*\*\* This bug has been marked as a duplicate of (\d+) \*\*\*$/ms;
  851. $dupes{$key} = $1;
  852. $dbh->do("INSERT INTO duplicates VALUES(?, ?)", undef,
  853. $dupes{$key}, $key);
  854. # BugItsADupeOf Dupe
  855. }
  856. }
  857. }
  858. sub _recrypt_plaintext_passwords {
  859. my $dbh = Bugzilla->dbh;
  860. # 2001-06-12; myk@mozilla.org; bugs 74032, 77473:
  861. # Recrypt passwords using Perl &crypt instead of the mysql equivalent
  862. # and delete plaintext passwords from the database.
  863. if ($dbh->bz_column_info('profiles', 'password')) {
  864. print <<ENDTEXT;
  865. Your current installation of Bugzilla stores passwords in plaintext
  866. in the database and uses mysql's encrypt function instead of Perl's
  867. crypt function to crypt passwords. Passwords are now going to be
  868. re-crypted with the Perl function, and plaintext passwords will be
  869. deleted from the database. This could take a while if your
  870. installation has many users.
  871. ENDTEXT
  872. # Re-crypt everyone's password.
  873. my $total = $dbh->selectrow_array('SELECT COUNT(*) FROM profiles');
  874. my $sth = $dbh->prepare("SELECT userid, password FROM profiles");
  875. $sth->execute();
  876. my $i = 1;
  877. print "Fixing passwords...\n";
  878. while (my ($userid, $password) = $sth->fetchrow_array()) {
  879. my $cryptpassword = $dbh->quote(bz_crypt($password));
  880. $dbh->do("UPDATE profiles " .
  881. "SET cryptpassword = $cryptpassword " .
  882. "WHERE userid = $userid");
  883. indicate_progress({ total => $total, current => $i, every => 10 });
  884. }
  885. print "\n";
  886. # Drop the plaintext password field.
  887. $dbh->bz_drop_column('profiles', 'password');
  888. }
  889. }
  890. sub _update_bugs_activity_to_only_record_changes {
  891. my $dbh = Bugzilla->dbh;
  892. # 2001-07-20 jake@bugzilla.org - Change bugs_activity to only record changes
  893. # http://bugzilla.mozilla.org/show_bug.cgi?id=55161
  894. if ($dbh->bz_column_info('bugs_activity', 'oldvalue')) {
  895. $dbh->bz_add_column("bugs_activity", "removed", {TYPE => "TINYTEXT"});
  896. $dbh->bz_add_column("bugs_activity", "added", {TYPE => "TINYTEXT"});
  897. # Need to get field id's for the fields that have multiple values
  898. my @multi;
  899. foreach my $f ("cc", "dependson", "blocked", "keywords") {
  900. my $sth = $dbh->prepare("SELECT id " .
  901. "FROM fielddefs " .
  902. "WHERE name = '$f'");
  903. $sth->execute();
  904. my ($fid) = $sth->fetchrow_array();
  905. push (@multi, $fid);
  906. }
  907. # Now we need to process the bugs_activity table and reformat the data
  908. print "Fixing activity log...\n";
  909. my $total = $dbh->selectrow_array('SELECT COUNT(*) FROM bugs_activity');
  910. my $sth = $dbh->prepare("SELECT bug_id, who, bug_when, fieldid,
  911. oldvalue, newvalue FROM bugs_activity");
  912. $sth->execute;
  913. my $i = 0;
  914. while (my ($bug_id, $who, $bug_when, $fieldid, $oldvalue, $newvalue)
  915. = $sth->fetchrow_array())
  916. {
  917. $i++;
  918. indicate_progress({ total => $total, current => $i, every => 10 });
  919. # Make sure (old|new)value isn't null (to suppress warnings)
  920. $oldvalue ||= "";
  921. $newvalue ||= "";
  922. my ($added, $removed) = "";
  923. if (grep ($_ eq $fieldid, @multi)) {
  924. $oldvalue =~ s/[\s,]+/ /g;
  925. $newvalue =~ s/[\s,]+/ /g;
  926. my @old = split(" ", $oldvalue);
  927. my @new = split(" ", $newvalue);
  928. my (@add, @remove) = ();
  929. # Find values that were "added"
  930. foreach my $value(@new) {
  931. if (! grep ($_ eq $value, @old)) {
  932. push (@add, $value);
  933. }
  934. }
  935. # Find values that were removed
  936. foreach my $value(@old) {
  937. if (! grep ($_ eq $value, @new)) {
  938. push (@remove, $value);
  939. }
  940. }
  941. $added = join (", ", @add);
  942. $removed = join (", ", @remove);
  943. # If we can't determine what changed, put a ? in both fields
  944. unless ($added || $removed) {
  945. $added = "?";
  946. $removed = "?";
  947. }
  948. # If the original field (old|new)value was full, then this
  949. # could be incomplete data.
  950. if (length($oldvalue) == 255 || length($newvalue) == 255) {
  951. $added = "? $added";
  952. $removed = "? $removed";
  953. }
  954. } else {
  955. $removed = $oldvalue;
  956. $added = $newvalue;
  957. }
  958. $added = $dbh->quote($added);
  959. $removed = $dbh->quote($removed);
  960. $dbh->do("UPDATE bugs_activity
  961. SET removed = $removed, added = $added
  962. WHERE bug_id = $bug_id AND who = $who
  963. AND bug_when = '$bug_when'
  964. AND fieldid = $fieldid");
  965. }
  966. print "\n";
  967. $dbh->bz_drop_column("bugs_activity", "oldvalue");
  968. $dbh->bz_drop_column("bugs_activity", "newvalue");
  969. }
  970. }
  971. sub _delete_logincookies_cryptpassword_and_handle_invalid_cookies {
  972. my $dbh = Bugzilla->dbh;
  973. # 2002-02-04 bbaetz@student.usyd.edu.au bug 95732
  974. # Remove logincookies.cryptpassword, and delete entries which become
  975. # invalid
  976. if ($dbh->bz_column_info("logincookies", "cryptpassword")) {
  977. # We need to delete any cookies which are invalid before dropping the
  978. # column
  979. print "Removing invalid login cookies...\n";
  980. # mysql doesn't support DELETE with multi-table queries, so we have
  981. # to iterate
  982. my $sth = $dbh->prepare("SELECT cookie FROM logincookies, profiles " .
  983. "WHERE logincookies.cryptpassword != " .
  984. "profiles.cryptpassword AND " .
  985. "logincookies.userid = profiles.userid");
  986. $sth->execute();
  987. while (my ($cookie) = $sth->fetchrow_array()) {
  988. $dbh->do("DELETE FROM logincookies WHERE cookie = $cookie");
  989. }
  990. $dbh->bz_drop_column("logincookies", "cryptpassword");
  991. }
  992. }
  993. sub _use_ip_instead_of_hostname_in_logincookies {
  994. my $dbh = Bugzilla->dbh;
  995. # 2002-03-15 bbaetz@student.usyd.edu.au - bug 129466
  996. # 2002-05-13 preed@sigkill.com - bug 129446 patch backported to the
  997. # BUGZILLA-2_14_1-BRANCH as a security blocker for the 2.14.2 release
  998. #
  999. # Use the ip, not the hostname, in the logincookies table
  1000. if ($dbh->bz_column_info("logincookies", "hostname")) {
  1001. # We've changed what we match against, so all entries are now invalid
  1002. $dbh->do("DELETE FROM logincookies");
  1003. # Now update the logincookies schema
  1004. $dbh->bz_drop_column("logincookies", "hostname");
  1005. $dbh->bz_add_column("logincookies", "ipaddr",
  1006. {TYPE => 'varchar(40)', NOTNULL => 1}, '');
  1007. }
  1008. }
  1009. sub _move_quips_into_db {
  1010. my $dbh = Bugzilla->dbh;
  1011. my $datadir = bz_locations->{'datadir'};
  1012. # 2002-07-15 davef@tetsubo.com - bug 67950
  1013. # Move quips to the db.
  1014. if (-e "$datadir/comments") {
  1015. print "Populating quips table from $datadir/comments...\n";
  1016. my $comments = new IO::File("$datadir/comments", 'r')
  1017. || die "$datadir/comments: $!";
  1018. $comments->untaint;
  1019. while (<$comments>) {
  1020. chomp;
  1021. $dbh->do("INSERT INTO quips (quip) VALUES (?)", undef, $_);
  1022. }
  1023. print <<EOT;
  1024. Quips are now stored in the database, rather than in an external file.
  1025. The quips previously stored in $datadir/comments have been copied into
  1026. the database, and that file has been renamed to $datadir/comments.bak
  1027. You may delete the renamed file once you have confirmed that all your
  1028. quips were moved successfully.
  1029. EOT
  1030. $comments->close;
  1031. rename("$datadir/comments", "$datadir/comments.bak")
  1032. || warn "Failed to rename: $!";
  1033. }
  1034. }
  1035. sub _use_ids_for_products_and_components {
  1036. my $dbh = Bugzilla->dbh;
  1037. # 2002-08-12 jake@bugzilla.org/bbaetz@student.usyd.edu.au - bug 43600
  1038. # Use integer IDs for products and components.
  1039. if ($dbh->bz_column_info("products", "product")) {
  1040. print "Updating database to use product IDs.\n";
  1041. # First, we need to remove possible NULL entries
  1042. # NULLs may exist, but won't have been used, since all the uses of them
  1043. # are in NOT NULL fields in other tables
  1044. $dbh->do("DELETE FROM products WHERE product IS NULL");
  1045. $dbh->do("DELETE FROM components WHERE value IS NULL");
  1046. $dbh->bz_add_column("products", "id",
  1047. {TYPE => 'SMALLSERIAL', NOTNULL => 1, PRIMARYKEY => 1});
  1048. $dbh->bz_add_column("components", "product_id",
  1049. {TYPE => 'INT2', NOTNULL => 1}, 0);
  1050. $dbh->bz_add_column("versions", "product_id",
  1051. {TYPE => 'INT2', NOTNULL => 1}, 0);
  1052. $dbh->bz_add_column("milestones", "product_id",
  1053. {TYPE => 'INT2', NOTNULL => 1}, 0);
  1054. $dbh->bz_add_column("bugs", "product_id",
  1055. {TYPE => 'INT2', NOTNULL => 1}, 0);
  1056. # The attachstatusdefs table was added in version 2.15, but
  1057. # removed again in early 2.17. If it exists now, we still need
  1058. # to perform this change with product_id because the code later on
  1059. # which converts the attachment statuses to flags depends on it.
  1060. # But we need to avoid this if the user is upgrading from 2.14
  1061. # or earlier (because it won't be there to convert).
  1062. if ($dbh->bz_table_info("attachstatusdefs")) {
  1063. $dbh->bz_add_column("attachstatusdefs", "product_id",
  1064. {TYPE => 'INT2', NOTNULL => 1}, 0);
  1065. }
  1066. my %products;
  1067. my $sth = $dbh->prepare("SELECT id, product FROM products");
  1068. $sth->execute;
  1069. while (my ($product_id, $product) = $sth->fetchrow_array()) {
  1070. if (exists $products{$product}) {
  1071. print "Ignoring duplicate product $product\n";
  1072. $dbh->do("DELETE FROM products WHERE id = $product_id");
  1073. next;
  1074. }
  1075. $products{$product} = 1;
  1076. $dbh->do("UPDATE components SET product_id = $product_id " .
  1077. "WHERE program = " . $dbh->quote($product));
  1078. $dbh->do("UPDATE versions SET product_id = $product_id " .
  1079. "WHERE program = " . $dbh->quote($product));
  1080. $dbh->do("UPDATE milestones SET product_id = $product_id " .
  1081. "WHERE product = " . $dbh->quote($product));
  1082. $dbh->do("UPDATE bugs SET product_id = $product_id " .
  1083. "WHERE product = " . $dbh->quote($product));
  1084. $dbh->do("UPDATE attachstatusdefs SET product_id = $product_id " .
  1085. "WHERE product = " . $dbh->quote($product))
  1086. if $dbh->bz_table_info("attachstatusdefs");
  1087. }
  1088. print "Updating the database to use component IDs.\n";
  1089. $dbh->bz_add_column("components", "id",
  1090. {TYPE => 'SMALLSERIAL', NOTNULL => 1, PRIMARYKEY => 1});
  1091. $dbh->bz_add_column("bugs", "component_id",
  1092. {TYPE => 'INT2', NOTNULL => 1}, 0);
  1093. my %components;
  1094. $sth = $dbh->prepare("SELECT id, value, product_id FROM components");
  1095. $sth->execute;
  1096. while (my ($component_id, $component, $product_id)
  1097. = $sth->fetchrow_array())
  1098. {
  1099. if (exists $components{$component}) {
  1100. if (exists $components{$component}{$product_id}) {
  1101. print "Ignoring duplicate component $component for",
  1102. " product $product_id\n";
  1103. $dbh->do("DELETE FROM components WHERE id = $component_id");
  1104. next;
  1105. }
  1106. } else {
  1107. $components{$component} = {};
  1108. }
  1109. $components{$component}{$product_id} = 1;
  1110. $dbh->do("UPDATE bugs SET component_id = $component_id " .
  1111. "WHERE component = " . $dbh->quote($component) .
  1112. " AND product_id = $product_id");
  1113. }
  1114. print "Fixing Indexes and Uniqueness.\n";
  1115. $dbh->bz_drop_index('milestones', 'milestones_product_idx');
  1116. $dbh->bz_add_index('milestones', 'milestones_product_id_idx',
  1117. {TYPE => 'UNIQUE', FIELDS => [qw(product_id value)]});
  1118. $dbh->bz_drop_index('bugs', 'bugs_product_idx');
  1119. $dbh->bz_add_index('bugs', 'bugs_product_id_idx', [qw(product_id)]);
  1120. $dbh->bz_drop_index('bugs', 'bugs_component_idx');
  1121. $dbh->bz_add_index('bugs', 'bugs_component_id_idx', [qw(component_id)]);
  1122. print "Removing, renaming, and retyping old product and",
  1123. " component fields.\n";
  1124. $dbh->bz_drop_column("components", "program");
  1125. $dbh->bz_drop_column("versions", "program");
  1126. $dbh->bz_drop_column("milestones", "product");
  1127. $dbh->bz_drop_column("bugs", "product");
  1128. $dbh->bz_drop_column("bugs", "component");
  1129. $dbh->bz_drop_column("attachstatusdefs", "product")
  1130. if $dbh->bz_table_info("attachstatusdefs");
  1131. $dbh->bz_rename_column("products", "product", "name");
  1132. $dbh->bz_alter_column("products", "name",
  1133. {TYPE => 'varchar(64)', NOTNULL => 1});
  1134. $dbh->bz_rename_column("components", "value", "name");
  1135. $dbh->bz_alter_column("components", "name",
  1136. {TYPE => 'varchar(64)', NOTNULL => 1});
  1137. print "Adding indexes for products and components tables.\n";
  1138. $dbh->bz_add_index('products', 'products_name_idx',
  1139. {TYPE => 'UNIQUE', FIELDS => [qw(name)]});
  1140. $dbh->bz_add_index('components', 'components_product_id_idx',
  1141. {TYPE => 'UNIQUE', FIELDS => [qw(product_id name)]});
  1142. $dbh->bz_add_index('components', 'components_name_idx', [qw(name)]);
  1143. }
  1144. }
  1145. # Helper for the below function.
  1146. #
  1147. # _list_bits(arg) returns a list of UNKNOWN<n> if the group
  1148. # has been deleted for all bits set in arg. When the activity
  1149. # records are converted from groupset numbers to lists of
  1150. # group names, _list_bits is used to fill in a list of references
  1151. # to groupset bits for groups that no longer exist.
  1152. sub _list_bits {
  1153. my ($num) = @_;
  1154. my $dbh = Bugzilla->dbh;
  1155. my @res;
  1156. my $curr = 1;
  1157. while (1) {
  1158. # Convert a big integer to a list of bits
  1159. my $sth = $dbh->prepare("SELECT ($num & ~$curr) > 0,
  1160. ($num & $curr),
  1161. ($num & ~$curr),
  1162. $curr << 1");
  1163. $sth->execute;
  1164. my ($more, $thisbit, $remain, $nval) = $sth->fetchrow_array;
  1165. push @res,"UNKNOWN<$curr>" if ($thisbit);
  1166. $curr = $nval;
  1167. $num = $remain;
  1168. last if !$more;
  1169. }
  1170. return @res;
  1171. }
  1172. sub _convert_groups_system_from_groupset {
  1173. my $dbh = Bugzilla->dbh;
  1174. # 2002-09-22 - bugreport@peshkin.net - bug 157756
  1175. #
  1176. # If the whole groups system is new, but the installation isn't,
  1177. # convert all the old groupset groups, etc...
  1178. #
  1179. # This requires:
  1180. # 1) define groups ids in group table
  1181. # 2) populate user_group_map with grants from old groupsets
  1182. # and blessgroupsets
  1183. # 3) populate bug_group_map with data converted from old bug groupsets
  1184. # 4) convert activity logs to use group names instead of numbers
  1185. # 5) identify the admin from the old all-ones groupset
  1186. # The groups system needs to be converted if groupset exists
  1187. if ($dbh->bz_column_info("profiles", "groupset")) {
  1188. # Some mysql versions will promote any unique key to primary key
  1189. # so all unique keys are removed first and then added back in
  1190. $dbh->bz_drop_index('groups', 'groups_bit_idx');
  1191. $dbh->bz_drop_index('groups', 'groups_name_idx');
  1192. my @primary_key = $dbh->primary_key(undef, undef, 'groups');
  1193. if (@primary_key) {
  1194. $dbh->do("ALTER TABLE groups DROP PRIMARY KEY");
  1195. }
  1196. $dbh->bz_add_column('groups', 'id',
  1197. {TYPE => 'MEDIUMSERIAL', NOTNULL => 1, PRIMARYKEY => 1});
  1198. $dbh->bz_add_index('groups', 'groups_name_idx',
  1199. {TYPE => 'UNIQUE', FIELDS => [qw(name)]});
  1200. # Convert all existing groupset records to map entries before removing
  1201. # groupset fields or removing "bit" from groups.
  1202. my $sth = $dbh->prepare("SELECT bit, id FROM groups WHERE bit > 0");
  1203. $sth->execute();
  1204. while (my ($bit, $gid) = $sth->fetchrow_array) {
  1205. # Create user_group_map membership grants for old groupsets.
  1206. # Get each user with the old groupset bit set
  1207. my $sth2 = $dbh->prepare("SELECT userid FROM profiles
  1208. WHERE (groupset & $bit) != 0");
  1209. $sth2->execute();
  1210. while (my ($uid) = $sth2->fetchrow_array) {
  1211. # Check to see if the user is already a member of the group
  1212. # and, if not, insert a new record.
  1213. my $query = "SELECT user_id FROM user_group_map
  1214. WHERE group_id = $gid AND user_id = $uid
  1215. AND isbless = 0";
  1216. my $sth3 = $dbh->prepare($query);
  1217. $sth3->execute();
  1218. if ( !$sth3->fetchrow_array() ) {
  1219. $dbh->do("INSERT INTO user_group_map
  1220. (user_id, group_id, isbless, grant_type)
  1221. VALUES ($uid, $gid, 0, " . GRANT_DIRECT . ")");
  1222. }
  1223. }
  1224. # Create user can bless group grants for old groupsets, but only
  1225. # if we're upgrading from a Bugzilla that had blessing.
  1226. if($dbh->bz_column_info('profiles', 'blessgroupset')) {
  1227. # Get each user with the old blessgroupset bit set
  1228. $sth2 = $dbh->prepare("SELECT userid FROM profiles
  1229. WHERE (blessgroupset & $bit) != 0");
  1230. $sth2->execute();
  1231. while (my ($uid) = $sth2->fetchrow_array) {
  1232. $dbh->do("INSERT INTO user_group_map
  1233. (user_id, group_id, isbless, grant_type)
  1234. VALUES ($uid, $gid, 1, " . GRANT_DIRECT . ")");
  1235. }
  1236. }
  1237. # Create bug_group_map records for old groupsets.
  1238. # Get each bug with the old group bit set.
  1239. $sth2 = $dbh->prepare("SELECT bug_id FROM bugs
  1240. WHERE (groupset & $bit) != 0");
  1241. $sth2->execute();
  1242. while (my ($bug_id) = $sth2->fetchrow_array) {
  1243. # Insert the bug, group pair into the bug_group_map.
  1244. $dbh->do("INSERT INTO bug_group_map (bug_id, group_id)
  1245. VALUES ($bug_id, $gid)");
  1246. }
  1247. }
  1248. # Replace old activity log groupset records with lists of names
  1249. # of groups.
  1250. $sth = $dbh->prepare("SELECT id FROM fielddefs
  1251. WHERE name = " . $dbh->quote('bug_group'));
  1252. $sth->execute();
  1253. my ($bgfid) = $sth->fetchrow_array;
  1254. # Get the field id for the old groupset field
  1255. $sth = $dbh->prepare("SELECT id FROM fielddefs
  1256. WHERE name = " . $dbh->quote('groupset'));
  1257. $sth->execute();
  1258. my ($gsid) = $sth->fetchrow_array;
  1259. # Get all bugs_activity records from groupset changes
  1260. if ($gsid) {
  1261. $sth = $dbh->prepare("SELECT bug_id, bug_when, who, added, removed
  1262. FROM bugs_activity WHERE fieldid = $gsid");
  1263. $sth->execute();
  1264. while (my ($bug_id, $bug_when, $who, $added, $removed) =
  1265. $sth->fetchrow_array)
  1266. {
  1267. $added ||= 0;
  1268. $removed ||= 0;
  1269. # Get names of groups added.
  1270. my $sth2 = $dbh->prepare("SELECT name FROM groups
  1271. WHERE (bit & $added) != 0
  1272. AND (bit & $removed) = 0");
  1273. $sth2->execute();
  1274. my @logadd;
  1275. while (my ($n) = $sth2->fetchrow_array) {
  1276. push @logadd, $n;
  1277. }
  1278. # Get names of groups removed.
  1279. $sth2 = $dbh->prepare("SELECT name FROM groups
  1280. WHERE (bit & $removed) != 0
  1281. AND (bit & $added) = 0");
  1282. $sth2->execute();
  1283. my @logrem;
  1284. while (my ($n) = $sth2->fetchrow_array) {
  1285. push @logrem, $n;
  1286. }
  1287. # Get list of group bits added that correspond to
  1288. # missing groups.
  1289. $sth2 = $dbh->prepare("SELECT ($added & ~BIT_OR(bit))
  1290. FROM groups");
  1291. $sth2->execute();
  1292. my ($miss) = $sth2->fetchrow_array;
  1293. if ($miss) {
  1294. push @logadd, _list_bits($miss);
  1295. print "\nWARNING - GROUPSET ACTIVITY ON BUG $bug_id",
  1296. " CONTAINS DELETED GROUPS\n";
  1297. }
  1298. # Get list of group bits deleted that correspond to
  1299. # missing groups.
  1300. $sth2 = $dbh->prepare("SELECT ($removed & ~BIT_OR(bit))
  1301. FROM groups");
  1302. $sth2->execute();
  1303. ($miss) = $sth2->fetchrow_array;
  1304. if ($miss) {
  1305. push @logrem, _list_bits($miss);
  1306. print "\nWARNING - GROUPSET ACTIVITY ON BUG $bug_id",
  1307. " CONTAINS DELETED GROUPS\n";
  1308. }
  1309. my $logr = "";
  1310. my $loga = "";
  1311. $logr = join(", ", @logrem) . '?' if @logrem;
  1312. $loga = join(", ", @logadd) . '?' if @logadd;
  1313. # Replace to old activity record with the converted data.
  1314. $dbh->do("UPDATE bugs_activity SET fieldid = $bgfid, added = " .
  1315. $dbh->quote($loga) . ", removed = " .
  1316. $dbh->quote($logr) .
  1317. " WHERE bug_id = $bug_id AND bug_when = " .
  1318. $dbh->quote($bug_when) .
  1319. " AND who = $who AND fieldid = $gsid");
  1320. }
  1321. # Replace groupset changes with group name changes in
  1322. # profiles_activity. Get profiles_activity records for groupset.
  1323. $sth = $dbh->prepare(
  1324. "SELECT userid, profiles_when, who, newvalue, oldvalue " .
  1325. "FROM profiles_activity " .
  1326. "WHERE fieldid = $gsid");
  1327. $sth->execute();
  1328. while (my ($uid, $uwhen, $uwho, $added, $removed) =
  1329. $sth->fetchrow_array)
  1330. {
  1331. $added ||= 0;
  1332. $removed ||= 0;
  1333. # Get names of groups added.
  1334. my $sth2 = $dbh->prepare("SELECT name FROM groups
  1335. WHERE (bit & $added) != 0
  1336. AND (bit & $removed) = 0");
  1337. $sth2->execute();
  1338. my @logadd;
  1339. while (my ($n) = $sth2->fetchrow_array) {
  1340. push @logadd, $n;
  1341. }
  1342. # Get names of groups removed.
  1343. $sth2 = $dbh->prepare("SELECT name FROM groups
  1344. WHERE (bit & $removed) != 0
  1345. AND (bit & $added) = 0");
  1346. $sth2->execute();
  1347. my @logrem;
  1348. while (my ($n) = $sth2->fetchrow_array) {
  1349. push @logrem, $n;
  1350. }
  1351. my $ladd = "";
  1352. my $lrem = "";
  1353. $ladd = join(", ", @logadd) . '?' if @logadd;
  1354. $lrem = join(", ", @logrem) . '?' if @logrem;
  1355. # Replace profiles_activity record for groupset change
  1356. # with group list.
  1357. $dbh->do("UPDATE profiles_activity " .
  1358. "SET fieldid = $bgfid, newvalue = " .
  1359. $dbh->quote($ladd) . ", oldvalue = " .
  1360. $dbh->quote($lrem) .
  1361. " WHERE userid = $uid AND profiles_when = " .
  1362. $dbh->quote($uwhen) .
  1363. " AND who = $uwho AND fieldid = $gsid");
  1364. }
  1365. }
  1366. # Identify admin group.
  1367. my ($admin_gid) = $dbh->selectrow_array(
  1368. "SELECT id FROM groups WHERE name = 'admin'");
  1369. if (!$admin_gid) {
  1370. $dbh->do(q{INSERT INTO groups (name, description)
  1371. VALUES ('admin', 'Administrators')});
  1372. $admin_gid = $dbh->bz_last_key('groups', 'id');
  1373. }
  1374. # Find current admins
  1375. my @admins;
  1376. # Don't lose admins from DBs where Bug 157704 applies
  1377. $sth = $dbh->prepare(
  1378. "SELECT userid, (groupset & 65536), login_name " .
  1379. "FROM profiles " .
  1380. "WHERE (groupset | 65536) = 9223372036854775807");
  1381. $sth->execute();
  1382. while ( my ($userid, $iscomplete, $login_name)
  1383. = $sth->fetchrow_array() )
  1384. {
  1385. # existing administrators are made members of group "admin"
  1386. print "\nWARNING - $login_name IS AN ADMIN IN SPITE OF BUG",
  1387. " 157704\n\n" if (!$iscomplete);
  1388. push(@admins, $userid) unless grep($_ eq $userid, @admins);
  1389. }
  1390. # Now make all those users admins directly. They were already
  1391. # added to every other group, above, because of their groupset.
  1392. foreach my $admin_id (@admins) {
  1393. $dbh->do("INSERT INTO user_group_map
  1394. (user_id, group_id, isbless, grant_type)
  1395. VALUES (?, ?, ?, ?)",
  1396. undef, $admin_id, $admin_gid, $_, GRANT_DIRECT)
  1397. foreach (0, 1);
  1398. }
  1399. $dbh->bz_drop_column('profiles','groupset');
  1400. $dbh->bz_drop_column('profiles','blessgroupset');
  1401. $dbh->bz_drop_column('bugs','groupset');
  1402. $dbh->bz_drop_column('groups','bit');
  1403. $dbh->do("DELETE FROM fielddefs WHERE name = "
  1404. . $dbh->quote('groupset'));
  1405. }
  1406. }
  1407. sub _convert_attachment_statuses_to_flags {
  1408. my $dbh = Bugzilla->dbh;
  1409. # September 2002 myk@mozilla.org bug 98801
  1410. # Convert the attachment statuses tables into flags tables.
  1411. if ($dbh->bz_table_info("attachstatuses")
  1412. && $dbh->bz_table_info("attachstatusdefs"))
  1413. {
  1414. print "Converting attachment statuses to flags...\n";
  1415. # Get IDs for the old attachment status and new flag fields.
  1416. my ($old_field_id) = $dbh->selectrow_array(
  1417. "SELECT id FROM fielddefs WHERE name='attachstatusdefs.name'")
  1418. || 0;
  1419. my ($new_field_id) = $dbh->selectrow_array(
  1420. "SELECT id FROM fielddefs WHERE name = 'flagtypes.name'");
  1421. # Convert attachment status definitions to flag types. If more than one
  1422. # status has the same name and description, it is merged into a single
  1423. # status with multiple inclusion records.
  1424. my $sth = $dbh->prepare(
  1425. "SELECT id, name, description, sortkey, product_id
  1426. FROM attachstatusdefs");
  1427. # status definition IDs indexed by name/description
  1428. my $def_ids = {};
  1429. # merged IDs and the IDs they were merged into. The key is the old ID,
  1430. # the value is the new one. This allows us to give statuses the right
  1431. # ID when we convert them over to flags. This map includes IDs that
  1432. # weren't merged (in this case the old and new IDs are the same), since
  1433. # it makes the code simpler.
  1434. my $def_id_map = {};
  1435. $sth->execute();
  1436. while (my ($id, $name, $desc, $sortkey, $prod_id) =
  1437. $sth->fetchrow_array())
  1438. {
  1439. my $key = $name . $desc;
  1440. if (!$def_ids->{$key}) {
  1441. $def_ids->{$key} = $id;
  1442. my $quoted_name = $dbh->quote($name);
  1443. my $quoted_desc = $dbh->quote($desc);
  1444. $dbh->do("INSERT INTO flagtypes (id, name, description,
  1445. sortkey, target_type)
  1446. VALUES ($id, $quoted_name, $quoted_desc,
  1447. $sortkey,'a')");
  1448. }
  1449. $def_id_map->{$id} = $def_ids->{$key};
  1450. $dbh->do("INSERT INTO flaginclusions (type_id, product_id)
  1451. VALUES ($def_id_map->{$id}, $prod_id)");
  1452. }
  1453. # Note: even though we've converted status definitions, we still
  1454. # can't drop the table because we need it to convert the statuses
  1455. # themselves.
  1456. # Convert attachment statuses to flags. To do this we select
  1457. # the statuses from the status table and then, for each one,
  1458. # figure out who set it and when they set it from the bugs
  1459. # activity table.
  1460. my $id = 0;
  1461. $sth = $dbh->prepare(
  1462. "SELECT attachstatuses.attach_id, attachstatusdefs.id,
  1463. attachstatusdefs.name, attachments.bug_id
  1464. FROM attachstatuses, attachstatusdefs, attachments
  1465. WHERE attachstatuses.statusid = attachstatusdefs.id
  1466. AND attachstatuses.attach_id = attachments.attach_id");
  1467. # a query to determine when the attachment status was set and who set it
  1468. my $sth2 = $dbh->prepare("SELECT added, who, bug_when
  1469. FROM bugs_activity
  1470. WHERE bug_id = ? AND attach_id = ?
  1471. AND fieldid = $old_field_id
  1472. ORDER BY bug_when DESC");
  1473. $sth->execute();
  1474. while (my ($attach_id, $def_id, $status, $bug_id) =
  1475. $sth->fetchrow_array())
  1476. {
  1477. ++$id;
  1478. # Determine when the attachment status was set and who set it.
  1479. # We should always be able to find out this info from the bug
  1480. # activity, but we fall back to default values just in case.
  1481. $sth2->execute($bug_id, $attach_id);
  1482. my ($added, $who, $when);
  1483. while (($added, $who, $when) = $sth2->fetchrow_array()) {
  1484. last if $added =~ /(^|[, ]+)\Q$status\E([, ]+|$)/;
  1485. }
  1486. $who = $dbh->quote($who); # "NULL" by default if $who is undefined
  1487. $when = $when ? $dbh->quote($when) : "NOW()";
  1488. $dbh->do("INSERT INTO flags (id, type_id, status, bug_id,
  1489. attach_id, creation_date, modification_date,
  1490. requestee_id, setter_id)
  1491. VALUES ($id, $def_id_map->{$def_id}, '+', $bug_id,
  1492. $attach_id, $when, $when, NULL, $who)");
  1493. }
  1494. # Now that we've converted both tables we can drop them.
  1495. $dbh->bz_drop_table("attachstatuses");
  1496. $dbh->bz_drop_table("attachstatusdefs");
  1497. # Convert activity records for attachment statuses into records
  1498. # for flags.
  1499. $sth = $dbh->prepare("SELECT attach_id, who, bug_when, added,
  1500. removed
  1501. FROM bugs_activity
  1502. WHERE fieldid = $old_field_id");
  1503. $sth->execute();
  1504. while (my ($attach_id, $who, $when, $old_added, $old_removed) =
  1505. $sth->fetchrow_array())
  1506. {
  1507. my @additions = split(/[, ]+/, $old_added);
  1508. @additions = map("$_+", @additions);
  1509. my $new_added = $dbh->quote(join(", ", @additions));
  1510. my @removals = split(/[, ]+/, $old_removed);
  1511. @removals = map("$_+", @removals);
  1512. my $new_removed = $dbh->quote(join(", ", @removals));
  1513. $old_added = $dbh->quote($old_added);
  1514. $old_removed = $dbh->quote($old_removed);
  1515. $who = $dbh->quote($who);
  1516. $when = $dbh->quote($when);
  1517. $dbh->do("UPDATE bugs_activity SET fieldid = $new_field_id, " .
  1518. "added = $new_added, removed = $new_removed " .
  1519. "WHERE attach_id = $attach_id AND who = $who " .
  1520. "AND bug_when = $when AND fieldid = $old_field_id " .
  1521. "AND added = $old_added AND removed = $old_removed");
  1522. }
  1523. # Remove the attachment status field from the field definitions.
  1524. $dbh->do("DELETE FROM fielddefs WHERE name='attachstatusdefs.name'");
  1525. print "done.\n";
  1526. }
  1527. }
  1528. sub _remove_spaces_and_commas_from_flagtypes {
  1529. my $dbh = Bugzilla->dbh;
  1530. # Get all names and IDs, to find broken ones and to
  1531. # check for collisions when renaming.
  1532. my $sth = $dbh->prepare("SELECT name, id FROM flagtypes");
  1533. $sth->execute();
  1534. my %flagtypes;
  1535. my @badflagnames;
  1536. # find broken flagtype names, and populate a hash table
  1537. # to check for collisions.
  1538. while (my ($name, $id) = $sth->fetchrow_array()) {
  1539. $flagtypes{$name} = $id;
  1540. if ($name =~ /[ ,]/) {
  1541. push(@badflagnames, $name);
  1542. }
  1543. }
  1544. if (@badflagnames) {
  1545. print "Removing spaces and commas from flag names...\n";
  1546. my ($flagname, $tryflagname);
  1547. my $sth = $dbh->prepare("UPDATE flagtypes SET name = ? WHERE id = ?");
  1548. foreach $flagname (@badflagnames) {
  1549. print " Bad flag type name \"$flagname\" ...\n";
  1550. # find a new name for this flagtype.
  1551. ($tryflagname = $flagname) =~ tr/ ,/__/;
  1552. # avoid collisions with existing flagtype names.
  1553. while (defined($flagtypes{$tryflagname})) {
  1554. print " ... can't rename as \"$tryflagname\" ...\n";
  1555. $tryflagname .= "'";
  1556. if (length($tryflagname) > 50) {
  1557. my $lastchanceflagname = (substr $tryflagname, 0, 47) . '...';
  1558. if (defined($flagtypes{$lastchanceflagname})) {
  1559. print " ... last attempt as \"$lastchanceflagname\" still failed.'\n",
  1560. "Rename the flag by hand and run checksetup.pl again.\n";
  1561. die("Bad flag type name $flagname");
  1562. }
  1563. $tryflagname = $lastchanceflagname;
  1564. }
  1565. }
  1566. $sth->execute($tryflagname, $flagtypes{$flagname});
  1567. print " renamed flag type \"$flagname\" as \"$tryflagname\"\n";
  1568. $flagtypes{$tryflagname} = $flagtypes{$flagname};
  1569. delete $flagtypes{$flagname};
  1570. }
  1571. print "... done.\n";
  1572. }
  1573. }
  1574. sub _setup_usebuggroups_backward_compatibility {
  1575. my $dbh = Bugzilla->dbh;
  1576. # 2002-11-24 - bugreport@peshkin.net - bug 147275
  1577. #
  1578. # If group_control_map is empty, backward-compatibility
  1579. # usebuggroups-equivalent records should be created.
  1580. my $entry = Bugzilla->params->{'useentrygroupdefault'};
  1581. my ($maps_exist) = $dbh->selectrow_array(
  1582. "SELECT DISTINCT 1 FROM group_control_map");
  1583. if (!$maps_exist) {
  1584. # Initially populate group_control_map.
  1585. # First, get all the existing products and their groups.
  1586. my $sth = $dbh->prepare("SELECT groups.id, products.id, groups.name,
  1587. products.name
  1588. FROM groups, products
  1589. WHERE isbuggroup != 0");
  1590. $sth->execute();
  1591. while (my ($groupid, $productid, $groupname, $productname)
  1592. = $sth->fetchrow_array())
  1593. {
  1594. if ($groupname eq $productname) {
  1595. # Product and group have same name.
  1596. $dbh->do("INSERT INTO group_control_map " .
  1597. "(group_id, product_id, entry, membercontrol, " .
  1598. "othercontrol, canedit) " .
  1599. "VALUES ($groupid, $productid, $entry, " .
  1600. CONTROLMAPDEFAULT . ", " .
  1601. CONTROLMAPNA . ", 0)");
  1602. } else {
  1603. # See if this group is a product group at all.
  1604. my $sth2 = $dbh->prepare("SELECT id FROM products
  1605. WHERE name = " .$dbh->quote($groupname));
  1606. $sth2->execute();
  1607. my ($id) = $sth2->fetchrow_array();
  1608. if (!$id) {
  1609. # If there is no product with the same name as this
  1610. # group, then it is permitted for all products.
  1611. $dbh->do("INSERT INTO group_control_map " .
  1612. "(group_id, product_id, entry, membercontrol, " .
  1613. "othercontrol, canedit) " .
  1614. "VALUES ($groupid, $productid, 0, " .
  1615. CONTROLMAPSHOWN . ", " .
  1616. CONTROLMAPNA . ", 0)");
  1617. }
  1618. }
  1619. }
  1620. }
  1621. }
  1622. sub _remove_user_series_map {
  1623. my $dbh = Bugzilla->dbh;
  1624. # 2004-07-17 GRM - Remove "subscriptions" concept from charting, and add
  1625. # group-based security instead.
  1626. if ($dbh->bz_table_info("user_series_map")) {
  1627. # Oracle doesn't like "date" as a column name, and apparently some DBs
  1628. # don't like 'value' either. We use the changes to subscriptions as
  1629. # something to hang these renamings off.
  1630. $dbh->bz_rename_column('series_data', 'date', 'series_date');
  1631. $dbh->bz_rename_column('series_data', 'value', 'series_value');
  1632. # series_categories.category_id produces a too-long column name for the
  1633. # auto-incrementing sequence (Oracle again).
  1634. $dbh->bz_rename_column('series_categories', 'category_id', 'id');
  1635. $dbh->bz_add_column("series", "public",
  1636. {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 'FALSE'});
  1637. # Migrate public-ness across from user_series_map to new field
  1638. my $sth = $dbh->prepare("SELECT series_id from user_series_map " .
  1639. "WHERE user_id = 0");
  1640. $sth->execute();
  1641. while (my ($public_series_id) = $sth->fetchrow_array()) {
  1642. $dbh->do("UPDATE series SET public = 1 " .
  1643. "WHERE series_id = $public_series_id");
  1644. }
  1645. $dbh->bz_drop_table("user_series_map");
  1646. }
  1647. }
  1648. sub _copy_old_charts_into_database {
  1649. my $dbh = Bugzilla->dbh;
  1650. my $datadir = bz_locations()->{'datadir'};
  1651. # 2003-06-26 Copy the old charting data into the database, and create the
  1652. # queries that will keep it all running. When the old charting system goes
  1653. # away, if this code ever runs, it'll just find no files and do nothing.
  1654. my $series_exists = $dbh->selectrow_array("SELECT 1 FROM series " .
  1655. $dbh->sql_limit(1));
  1656. if (!$series_exists && -d "$datadir/mining" && -e "$datadir/mining/-All-") {
  1657. print "Migrating old chart data into database...\n";
  1658. # We prepare the handle to insert the series data
  1659. my $seriesdatasth = $dbh->prepare(
  1660. "INSERT INTO series_data (series_id, series_date, series_value)
  1661. VALUES (?, ?, ?)");
  1662. my $deletesth = $dbh->prepare(
  1663. "DELETE FROM series_data WHERE series_id = ? AND series_date = ?");
  1664. my $groupmapsth = $dbh->prepare(
  1665. "INSERT INTO category_group_map (category_id, group_id)
  1666. VALUES (?, ?)");
  1667. # Fields in the data file (matches the current collectstats.pl)
  1668. my @statuses =
  1669. qw(NEW ASSIGNED REOPENED UNCONFIRMED RESOLVED VERIFIED CLOSED);
  1670. my @resolutions =
  1671. qw(FIXED INVALID WONTFIX LATER REMIND DUPLICATE WORKSFORME MOVED);
  1672. my @fields = (@statuses, @resolutions);
  1673. # We have a localization problem here. Where do we get these values?
  1674. my $all_name = "-All-";
  1675. my $open_name = "All Open";
  1676. my $products = $dbh->selectall_arrayref("SELECT name FROM products");
  1677. foreach my $product ((map { $_->[0] } @$products), "-All-") {
  1678. # First, create the series
  1679. my %queries;
  1680. my %seriesids;
  1681. my $query_prod = "";
  1682. if ($product ne "-All-") {
  1683. $query_prod = "product=" . html_quote($product) . "&";
  1684. }
  1685. # The query for statuses is different to that for resolutions.
  1686. $queries{$_} = ($query_prod . "bug_status=$_") foreach (@statuses);
  1687. $queries{$_} = ($query_prod . "resolution=$_")
  1688. foreach (@resolutions);
  1689. foreach my $field (@fields) {
  1690. # Create a Series for each field in this product.
  1691. my $series = new Bugzilla::Series(undef, $product, $all_name,
  1692. $field, undef, 1,
  1693. $queries{$field}, 1);
  1694. $series->writeToDatabase();
  1695. $seriesids{$field} = $series->{'series_id'};
  1696. }
  1697. # We also add a new query for "Open", so that migrated products get
  1698. # the same set as new products (see editproducts.cgi.)
  1699. my @openedstatuses = ("UNCONFIRMED", "NEW", "ASSIGNED", "REOPENED");
  1700. my $query = join("&", map { "bug_status=$_" } @openedstatuses);
  1701. my $series = new Bugzilla::Series(undef, $product, $all_name,
  1702. $open_name, undef, 1,
  1703. $query_prod . $query, 1);
  1704. $series->writeToDatabase();
  1705. $seriesids{$open_name} = $series->{'series_id'};
  1706. # Now, we attempt to read in historical data, if any
  1707. # Convert the name in the same way that collectstats.pl does
  1708. my $product_file = $product;
  1709. $product_file =~ s/\//-/gs;
  1710. $product_file = "$datadir/mining/$product_file";
  1711. # There are many reasons that this might fail (e.g. no stats
  1712. # for this product), so we don't worry if it does.
  1713. my $in = new IO::File($product_file) or next;
  1714. # The data files should be in a standard format, even for old
  1715. # Bugzillas, because of the conversion code further up this file.
  1716. my %data;
  1717. my $last_date = "";
  1718. while (<$in>) {
  1719. if (/^(\d+\|.*)/) {
  1720. my @numbers = split(/\||\r/, $1);
  1721. # Only take the first line for each date; it was possible to
  1722. # run collectstats.pl more than once in a day.
  1723. next if $numbers[0] eq $last_date;
  1724. for my $i (0 .. $#fields) {
  1725. # $numbers[0] is the date
  1726. $data{$fields[$i]}{$numbers[0]} = $numbers[$i + 1];
  1727. # Keep a total of the number of open bugs for this day
  1728. if (grep { $_ eq $fields[$i] } @openedstatuses) {
  1729. $data{$open_name}{$numbers[0]} += $numbers[$i + 1];
  1730. }
  1731. }
  1732. $last_date = $numbers[0];
  1733. }
  1734. }
  1735. $in->close;
  1736. foreach my $field (@fields, $open_name) {
  1737. # Insert values into series_data: series_id, date, value
  1738. my %fielddata = %{$data{$field}};
  1739. foreach my $date (keys %fielddata) {
  1740. # We need to delete in case the text file had duplicate
  1741. # entries in it.
  1742. $deletesth->execute($seriesids{$field}, $date);
  1743. # We prepared this above
  1744. $seriesdatasth->execute($seriesids{$field},
  1745. $date, $fielddata{$date} || 0);
  1746. }
  1747. }
  1748. # Create the groupsets for the category
  1749. my $category_id =
  1750. $dbh->selectrow_array("SELECT id FROM series_categories " .
  1751. "WHERE name = " . $dbh->quote($product));
  1752. my $product_id =
  1753. $dbh->selectrow_array("SELECT id FROM products " .
  1754. "WHERE name = " . $dbh->quote($product));
  1755. if (defined($category_id) && defined($product_id)) {
  1756. # Get all the mandatory groups for this product
  1757. my $group_ids =
  1758. $dbh->selectcol_arrayref("SELECT group_id " .
  1759. "FROM group_control_map " .
  1760. "WHERE product_id = $product_id " .
  1761. "AND (membercontrol = " . CONTROLMAPMANDATORY .
  1762. " OR othercontrol = " . CONTROLMAPMANDATORY . ")");
  1763. foreach my $group_id (@$group_ids) {
  1764. $groupmapsth->execute($category_id, $group_id);
  1765. }
  1766. }
  1767. }
  1768. }
  1769. }
  1770. sub _add_user_group_map_grant_type {
  1771. my $dbh = Bugzilla->dbh;
  1772. # 2004-04-12 - Keep regexp-based group permissions up-to-date - Bug 240325
  1773. if ($dbh->bz_column_info("user_group_map", "isderived")) {
  1774. $dbh->bz_add_column('user_group_map', 'grant_type',
  1775. {TYPE => 'INT1', NOTNULL => 1, DEFAULT => '0'});
  1776. $dbh->do("DELETE FROM user_group_map WHERE isderived != 0");
  1777. $dbh->do("UPDATE user_group_map SET grant_type = " . GRANT_DIRECT);
  1778. $dbh->bz_drop_column("user_group_map", "isderived");
  1779. $dbh->bz_drop_index('user_group_map', 'user_group_map_user_id_idx');
  1780. $dbh->bz_add_index('user_group_map', 'user_group_map_user_id_idx',
  1781. {TYPE => 'UNIQUE',
  1782. FIELDS => [qw(user_id group_id grant_type isbless)]});
  1783. }
  1784. }
  1785. sub _add_group_group_map_grant_type {
  1786. my $dbh = Bugzilla->dbh;
  1787. # 2004-07-16 - Make it possible to have group-group relationships other than
  1788. # membership and bless.
  1789. if ($dbh->bz_column_info("group_group_map", "isbless")) {
  1790. $dbh->bz_add_column('group_group_map', 'grant_type',
  1791. {TYPE => 'INT1', NOTNULL => 1, DEFAULT => '0'});
  1792. $dbh->do("UPDATE group_group_map SET grant_type = " .
  1793. "IF(isbless, " . GROUP_BLESS . ", " .
  1794. GROUP_MEMBERSHIP . ")");
  1795. $dbh->bz_drop_index('group_group_map', 'group_group_map_member_id_idx');
  1796. $dbh->bz_drop_column("group_group_map", "isbless");
  1797. $dbh->bz_add_index('group_group_map', 'group_group_map_member_id_idx',
  1798. {TYPE => 'UNIQUE',
  1799. FIELDS => [qw(member_id grantor_id grant_type)]});
  1800. }
  1801. }
  1802. sub _add_longdescs_already_wrapped {
  1803. my $dbh = Bugzilla->dbh;
  1804. # 2005-01-29 - mkanat@bugzilla.org
  1805. if (!$dbh->bz_column_info('longdescs', 'already_wrapped')) {
  1806. # Old, pre-wrapped comments should not be auto-wrapped
  1807. $dbh->bz_add_column('longdescs', 'already_wrapped',
  1808. {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 'FALSE'}, 1);
  1809. # If an old comment doesn't have a newline in the first 81 characters,
  1810. # (or doesn't contain a newline at all) and it contains a space,
  1811. # then it's probably a mis-wrapped comment and we should wrap it
  1812. # at display-time.
  1813. print "Fixing old, mis-wrapped comments...\n";
  1814. $dbh->do(q{UPDATE longdescs SET already_wrapped = 0
  1815. WHERE (} . $dbh->sql_position(q{'\n'}, 'thetext') . q{ > 81
  1816. OR } . $dbh->sql_position(q{'\n'}, 'thetext') . q{ = 0)
  1817. AND SUBSTRING(thetext FROM 1 FOR 80) LIKE '% %'});
  1818. }
  1819. }
  1820. sub _convert_attachments_filename_from_mediumtext {
  1821. my $dbh = Bugzilla->dbh;
  1822. # 2002 November, myk@mozilla.org, bug 178841:
  1823. #
  1824. # Convert the "attachments.filename" column from a ridiculously large
  1825. # "mediumtext" to a much more sensible "varchar(100)". Also takes
  1826. # the opportunity to remove paths from existing filenames, since they
  1827. # shouldn't be there for security. Buggy browsers include them,
  1828. # and attachment.cgi now takes them out, but old ones need converting.
  1829. my $ref = $dbh->bz_column_info("attachments", "filename");
  1830. if ($ref->{TYPE} ne 'varchar(100)') {
  1831. print "Removing paths from filenames in attachments table...\n";
  1832. my $sth = $dbh->prepare("SELECT attach_id, filename FROM attachments " .
  1833. "WHERE " . $dbh->sql_position(q{'/'}, 'filename') . " > 0 OR " .
  1834. $dbh->sql_position(q{'\\\\'}, 'filename') . " > 0");
  1835. $sth->execute;
  1836. while (my ($attach_id, $filename) = $sth->fetchrow_array) {
  1837. $filename =~ s/^.*[\/\\]//;
  1838. my $quoted_filename = $dbh->quote($filename);
  1839. $dbh->do("UPDATE attachments SET filename = $quoted_filename " .
  1840. "WHERE attach_id = $attach_id");
  1841. }
  1842. print "Done.\n";
  1843. print "Resizing attachments.filename from mediumtext to",
  1844. " varchar(100).\n";
  1845. $dbh->bz_alter_column("attachments", "filename",
  1846. {TYPE => 'varchar(100)', NOTNULL => 1});
  1847. }
  1848. }
  1849. sub _rename_votes_count_and_force_group_refresh {
  1850. my $dbh = Bugzilla->dbh;
  1851. # 2003-04-27 - bugzilla@chimpychompy.org (GavinS)
  1852. #
  1853. # Bug 180086 (http://bugzilla.mozilla.org/show_bug.cgi?id=180086)
  1854. #
  1855. # Renaming the 'count' column in the votes table because Sybase doesn't
  1856. # like it
  1857. if ($dbh->bz_column_info('votes', 'count')) {
  1858. $dbh->bz_rename_column('votes', 'count', 'vote_count');
  1859. }
  1860. }
  1861. sub _fix_group_with_empty_name {
  1862. my $dbh = Bugzilla->dbh;
  1863. # 2005-01-12 Nick Barnes <nb@ravenbrook.com> bug 278010
  1864. # Rename any group which has an empty name.
  1865. # Note that there can be at most one such group (because of
  1866. # the SQL index on the name column).
  1867. my ($emptygroupid) = $dbh->selectrow_array(
  1868. "SELECT id FROM groups where name = ''");
  1869. if ($emptygroupid) {
  1870. # There is a group with an empty name; find a name to rename it
  1871. # as. Must avoid collisions with existing names. Start with
  1872. # group_$gid and add _<n> if necessary.
  1873. my $trycount = 0;
  1874. my $trygroupname;
  1875. my $sth = $dbh->prepare("SELECT 1 FROM groups where name = ?");
  1876. my $name_exists = 1;
  1877. while ($name_exists) {
  1878. $trygroupname = "group_$emptygroupid";
  1879. if ($trycount > 0) {
  1880. $trygroupname .= "_$trycount";
  1881. }
  1882. $name_exists = $dbh->selectrow_array($sth, undef, $trygroupname);
  1883. $trycount++;
  1884. }
  1885. $dbh->do("UPDATE groups SET name = ? WHERE id = ?",
  1886. undef, $trygroupname, $emptygroupid);
  1887. print "Group $emptygroupid had an empty name; renamed as",
  1888. " '$trygroupname'.\n";
  1889. }
  1890. }
  1891. # A helper for the emailprefs subs below
  1892. sub _clone_email_event {
  1893. my ($source, $target) = @_;
  1894. my $dbh = Bugzilla->dbh;
  1895. my $sth1 = $dbh->prepare("SELECT user_id, relationship FROM email_setting
  1896. WHERE event = $source");
  1897. my $sth2 = $dbh->prepare("INSERT into email_setting " .
  1898. "(user_id, relationship, event) VALUES (" .
  1899. "?, ?, $target)");
  1900. $sth1->execute();
  1901. while (my ($userid, $relationship) = $sth1->fetchrow_array()) {
  1902. $sth2->execute($userid, $relationship);
  1903. }
  1904. }
  1905. sub _migrate_email_prefs_to_new_table {
  1906. my $dbh = Bugzilla->dbh;
  1907. # 2005-03-29 - gerv@gerv.net - bug 73665.
  1908. # Migrate email preferences to new email prefs table.
  1909. if ($dbh->bz_column_info("profiles", "emailflags")) {
  1910. print "Migrating email preferences to new table...\n";
  1911. # These are the "roles" and "reasons" from the original code, mapped to
  1912. # the new terminology of relationships and events.
  1913. my %relationships = ("Owner" => REL_ASSIGNEE,
  1914. "Reporter" => REL_REPORTER,
  1915. "QAcontact" => REL_QA,
  1916. "CClist" => REL_CC,
  1917. "Voter" => REL_VOTER);
  1918. my %events = ("Removeme" => EVT_ADDED_REMOVED,
  1919. "Comments" => EVT_COMMENT,
  1920. "Attachments" => EVT_ATTACHMENT,
  1921. "Status" => EVT_PROJ_MANAGEMENT,
  1922. "Resolved" => EVT_OPENED_CLOSED,
  1923. "Keywords" => EVT_KEYWORD,
  1924. "CC" => EVT_CC,
  1925. "Other" => EVT_OTHER,
  1926. "Unconfirmed" => EVT_UNCONFIRMED);
  1927. # Request preferences
  1928. my %requestprefs = ("FlagRequestee" => EVT_FLAG_REQUESTED,
  1929. "FlagRequester" => EVT_REQUESTED_FLAG);
  1930. # We run the below code in a transaction to speed things up.
  1931. $dbh->bz_start_transaction();
  1932. # Select all emailflags flag strings
  1933. my $total = $dbh->selectrow_array('SELECT COUNT(*) FROM profiles');
  1934. my $sth = $dbh->prepare("SELECT userid, emailflags FROM profiles");
  1935. $sth->execute();
  1936. my $i = 0;
  1937. while (my ($userid, $flagstring) = $sth->fetchrow_array()) {
  1938. $i++;
  1939. indicate_progress({ total => $total, current => $i, every => 10 });
  1940. # If the user has never logged in since emailprefs arrived, and the
  1941. # temporary code to give them a default string never ran, then
  1942. # $flagstring will be null. In this case, they just get all mail.
  1943. $flagstring ||= "";
  1944. # The 255 param is here, because without a third param, split will
  1945. # trim any trailing null fields, which causes Perl to eject lots of
  1946. # warnings. Any suitably large number would do.
  1947. my %emailflags = split(/~/, $flagstring, 255);
  1948. my $sth2 = $dbh->prepare("INSERT into email_setting " .
  1949. "(user_id, relationship, event) VALUES (" .
  1950. "$userid, ?, ?)");
  1951. foreach my $relationship (keys %relationships) {
  1952. foreach my $event (keys %events) {
  1953. my $key = "email$relationship$event";
  1954. if (!exists($emailflags{$key})
  1955. || $emailflags{$key} eq 'on')
  1956. {
  1957. $sth2->execute($relationships{$relationship},
  1958. $events{$event});
  1959. }
  1960. }
  1961. }
  1962. # Note that in the old system, the value of "excludeself" is
  1963. # assumed to be off if the preference does not exist in the
  1964. # user's list, unlike other preferences whose value is
  1965. # assumed to be on if they do not exist.
  1966. #
  1967. # This preference has changed from global to per-relationship.
  1968. if (!exists($emailflags{'ExcludeSelf'})
  1969. || $emailflags{'ExcludeSelf'} ne 'on')
  1970. {
  1971. foreach my $relationship (keys %relationships) {
  1972. $dbh->do("INSERT into email_setting " .
  1973. "(user_id, relationship, event) VALUES (" .
  1974. $userid . ", " .
  1975. $relationships{$relationship}. ", " .
  1976. EVT_CHANGED_BY_ME . ")");
  1977. }
  1978. }
  1979. foreach my $key (keys %requestprefs) {
  1980. if (!exists($emailflags{$key}) || $emailflags{$key} eq 'on') {
  1981. $dbh->do("INSERT into email_setting " .
  1982. "(user_id, relationship, event) VALUES (" .
  1983. $userid . ", " . REL_ANY . ", " .
  1984. $requestprefs{$key} . ")");
  1985. }
  1986. }
  1987. }
  1988. print "\n";
  1989. # EVT_ATTACHMENT_DATA should initially have identical settings to
  1990. # EVT_ATTACHMENT.
  1991. _clone_email_event(EVT_ATTACHMENT, EVT_ATTACHMENT_DATA);
  1992. $dbh->bz_commit_transaction();
  1993. $dbh->bz_drop_column("profiles", "emailflags");
  1994. }
  1995. }
  1996. sub _initialize_dependency_tree_changes_email_pref {
  1997. my $dbh = Bugzilla->dbh;
  1998. # Check for any "new" email settings that wouldn't have been ported over
  1999. # during the block above. Since these settings would have otherwise
  2000. # fallen under EVT_OTHER, we'll just clone those settings. That way if
  2001. # folks have already disabled all of that mail, there won't be any change.
  2002. my %events = ("Dependency Tree Changes" => EVT_DEPEND_BLOCK);
  2003. foreach my $desc (keys %events) {
  2004. my $event = $events{$desc};
  2005. my $sth = $dbh->prepare("SELECT COUNT(*) FROM email_setting
  2006. WHERE event = $event");
  2007. $sth->execute();
  2008. if (!($sth->fetchrow_arrayref()->[0])) {
  2009. # No settings in the table yet, so we assume that this is the
  2010. # first time it's being set.
  2011. print "Initializing \"$desc\" email_setting ...\n";
  2012. _clone_email_event(EVT_OTHER, $event);
  2013. }
  2014. }
  2015. }
  2016. sub _change_all_mysql_booleans_to_tinyint {
  2017. my $dbh = Bugzilla->dbh;
  2018. # 2005-03-27: Standardize all boolean fields to plain "tinyint"
  2019. if ( $dbh->isa('Bugzilla::DB::Mysql') ) {
  2020. # This is a change to make things consistent with Schema, so we use
  2021. # direct-database access methods.
  2022. my $quip_info_sth = $dbh->column_info(undef, undef, 'quips', '%');
  2023. my $quips_cols = $quip_info_sth->fetchall_hashref("COLUMN_NAME");
  2024. my $approved_col = $quips_cols->{'approved'};
  2025. if ( $approved_col->{TYPE_NAME} eq 'TINYINT'
  2026. and $approved_col->{COLUMN_SIZE} == 1 )
  2027. {
  2028. # series.public could have been renamed to series.is_public,
  2029. # and so wouldn't need to be fixed manually.
  2030. if ($dbh->bz_column_info('series', 'public')) {
  2031. $dbh->bz_alter_column_raw('series', 'public',
  2032. {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => '0'});
  2033. }
  2034. $dbh->bz_alter_column_raw('bug_status', 'isactive',
  2035. {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => '1'});
  2036. $dbh->bz_alter_column_raw('rep_platform', 'isactive',
  2037. {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => '1'});
  2038. $dbh->bz_alter_column_raw('resolution', 'isactive',
  2039. {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => '1'});
  2040. $dbh->bz_alter_column_raw('op_sys', 'isactive',
  2041. {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => '1'});
  2042. $dbh->bz_alter_column_raw('bug_severity', 'isactive',
  2043. {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => '1'});
  2044. $dbh->bz_alter_column_raw('priority', 'isactive',
  2045. {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => '1'});
  2046. $dbh->bz_alter_column_raw('quips', 'approved',
  2047. {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => '1'});
  2048. }
  2049. }
  2050. }
  2051. # A helper for the below function.
  2052. sub _de_dup_version {
  2053. my ($product_id, $version) = @_;
  2054. my $dbh = Bugzilla->dbh;
  2055. print "Fixing duplicate version $version in product_id $product_id...\n";
  2056. $dbh->do('DELETE FROM versions WHERE product_id = ? AND value = ?',
  2057. undef, $product_id, $version);
  2058. $dbh->do('INSERT INTO versions (product_id, value) VALUES (?,?)',
  2059. undef, $product_id, $version);
  2060. }
  2061. sub _add_versions_product_id_index {
  2062. my $dbh = Bugzilla->dbh;
  2063. if (!$dbh->bz_index_info('versions', 'versions_product_id_idx')) {
  2064. my $dup_versions = $dbh->selectall_arrayref(
  2065. 'SELECT product_id, value FROM versions
  2066. GROUP BY product_id, value HAVING COUNT(value) > 1', {Slice=>{}});
  2067. foreach my $dup_version (@$dup_versions) {
  2068. _de_dup_version($dup_version->{product_id}, $dup_version->{value});
  2069. }
  2070. $dbh->bz_add_index('versions', 'versions_product_id_idx',
  2071. {TYPE => 'UNIQUE', FIELDS => [qw(product_id value)]});
  2072. }
  2073. }
  2074. sub _fix_whine_queries_title_and_op_sys_value {
  2075. my $dbh = Bugzilla->dbh;
  2076. if (!exists $dbh->bz_column_info('whine_queries', 'title')->{DEFAULT}) {
  2077. # The below change actually has nothing to do with the whine_queries
  2078. # change, it just has to be contained within a schema change so that
  2079. # it doesn't run every time we run checksetup.
  2080. # Old Bugzillas have "other" as an OS choice, new ones have "Other"
  2081. # (capital O).
  2082. print "Setting any 'other' op_sys to 'Other'...\n";
  2083. $dbh->do('UPDATE op_sys SET value = ? WHERE value = ?',
  2084. undef, "Other", "other");
  2085. $dbh->do('UPDATE bugs SET op_sys = ? WHERE op_sys = ?',
  2086. undef, "Other", "other");
  2087. if (Bugzilla->params->{'defaultopsys'} eq 'other') {
  2088. # We can't actually fix the param here, because WriteParams() will
  2089. # make $datadir/params unwriteable to the webservergroup.
  2090. # It's too much of an ugly hack to copy the permission-fixing code
  2091. # down to here. (It would create more potential future bugs than
  2092. # it would solve problems.)
  2093. print "WARNING: Your 'defaultopsys' param is set to 'other', but"
  2094. . " Bugzilla now\n"
  2095. . " uses 'Other' (capital O).\n";
  2096. }
  2097. # Add a DEFAULT to whine_queries stuff so that editwhines.cgi
  2098. # works on PostgreSQL.
  2099. $dbh->bz_alter_column('whine_queries', 'title', {TYPE => 'varchar(128)',
  2100. NOTNULL => 1, DEFAULT => "''"});
  2101. }
  2102. }
  2103. sub _fix_attachments_submitter_id_idx {
  2104. my $dbh = Bugzilla->dbh;
  2105. # 2005-06-29 bugreport@peshkin.net, bug 299156
  2106. if ($dbh->bz_index_info('attachments', 'attachments_submitter_id_idx')
  2107. && (scalar(@{$dbh->bz_index_info('attachments',
  2108. 'attachments_submitter_id_idx'
  2109. )->{FIELDS}}) < 2))
  2110. {
  2111. $dbh->bz_drop_index('attachments', 'attachments_submitter_id_idx');
  2112. }
  2113. $dbh->bz_add_index('attachments', 'attachments_submitter_id_idx',
  2114. [qw(submitter_id bug_id)]);
  2115. }
  2116. sub _copy_attachments_thedata_to_attach_data {
  2117. my $dbh = Bugzilla->dbh;
  2118. # 2005-08-25 - bugreport@peshkin.net - Bug 305333
  2119. if ($dbh->bz_column_info("attachments", "thedata")) {
  2120. print "Migrating attachment data to its own table...\n";
  2121. print "(This may take a very long time)\n";
  2122. $dbh->do("INSERT INTO attach_data (id, thedata)
  2123. SELECT attach_id, thedata FROM attachments");
  2124. $dbh->bz_drop_column("attachments", "thedata");
  2125. }
  2126. }
  2127. sub _fix_broken_all_closed_series {
  2128. my $dbh = Bugzilla->dbh;
  2129. # 2005-11-26 - wurblzap@gmail.com - Bug 300473
  2130. # Repair broken automatically generated series queries for non-open bugs.
  2131. my $broken_series_indicator =
  2132. 'field0-0-0=resolution&type0-0-0=notequals&value0-0-0=---';
  2133. my $broken_nonopen_series =
  2134. $dbh->selectall_arrayref("SELECT series_id, query FROM series
  2135. WHERE query LIKE '$broken_series_indicator%'");
  2136. if (@$broken_nonopen_series) {
  2137. print 'Repairing broken series...';
  2138. my $sth_nuke =
  2139. $dbh->prepare('DELETE FROM series_data WHERE series_id = ?');
  2140. # This statement is used to repair a series by replacing the broken
  2141. # query with the correct one.
  2142. my $sth_repair =
  2143. $dbh->prepare('UPDATE series SET query = ? WHERE series_id = ?');
  2144. # The corresponding series for open bugs look like one of these two
  2145. # variations (bug 225687 changed the order of bug states).
  2146. # This depends on the set of bug states representing open bugs not
  2147. # to have changed since series creation.
  2148. my $open_bugs_query_base_old =
  2149. join("&", map { "bug_status=" . url_quote($_) }
  2150. ('UNCONFIRMED', 'NEW', 'ASSIGNED', 'REOPENED'));
  2151. my $open_bugs_query_base_new =
  2152. join("&", map { "bug_status=" . url_quote($_) }
  2153. ('NEW', 'REOPENED', 'ASSIGNED', 'UNCONFIRMED'));
  2154. my $sth_openbugs_series =
  2155. $dbh->prepare("SELECT series_id FROM series WHERE query IN (?, ?)");
  2156. # Statement to find the series which has collected the most data.
  2157. my $sth_data_collected =
  2158. $dbh->prepare('SELECT count(*) FROM series_data
  2159. WHERE series_id = ?');
  2160. # Statement to select a broken non-open bugs count data entry.
  2161. my $sth_select_broken_nonopen_data =
  2162. $dbh->prepare('SELECT series_date, series_value FROM series_data' .
  2163. ' WHERE series_id = ?');
  2164. # Statement to select an open bugs count data entry.
  2165. my $sth_select_open_data =
  2166. $dbh->prepare('SELECT series_value FROM series_data' .
  2167. ' WHERE series_id = ? AND series_date = ?');
  2168. # Statement to fix a broken non-open bugs count data entry.
  2169. my $sth_fix_broken_nonopen_data =
  2170. $dbh->prepare('UPDATE series_data SET series_value = ?' .
  2171. ' WHERE series_id = ? AND series_date = ?');
  2172. # Statement to delete an unfixable broken non-open bugs count data
  2173. # entry.
  2174. my $sth_delete_broken_nonopen_data =
  2175. $dbh->prepare('DELETE FROM series_data' .
  2176. ' WHERE series_id = ? AND series_date = ?');
  2177. foreach (@$broken_nonopen_series) {
  2178. my ($broken_series_id, $nonopen_bugs_query) = @$_;
  2179. # Determine the product-and-component part of the query.
  2180. if ($nonopen_bugs_query =~ /^$broken_series_indicator(.*)$/) {
  2181. my $prodcomp = $1;
  2182. # If there is more than one series for the corresponding
  2183. # open-bugs series, we pick the one with the most data,
  2184. # which should be the one which was generated on creation.
  2185. # It's a pity we can't do subselects.
  2186. $sth_openbugs_series->execute(
  2187. $open_bugs_query_base_old . $prodcomp,
  2188. $open_bugs_query_base_new . $prodcomp);
  2189. my ($found_open_series_id, $datacount) = (undef, -1);
  2190. foreach my $open_ser_id ($sth_openbugs_series->fetchrow_array) {
  2191. $sth_data_collected->execute($open_ser_id);
  2192. my ($this_datacount) = $sth_data_collected->fetchrow_array;
  2193. if ($this_datacount > $datacount) {
  2194. $datacount = $this_datacount;
  2195. $found_open_series_id = $open_ser_id;
  2196. }
  2197. }
  2198. if ($found_open_series_id) {
  2199. # Move along corrupted series data and correct it. The
  2200. # corruption consists of it being the number of all bugs
  2201. # instead of the number of non-open bugs, so we calculate
  2202. # the correct count by subtracting the number of open bugs.
  2203. # If there is no corresponding open-bugs count for some
  2204. # reason (shouldn't happen), we drop the data entry.
  2205. print " $broken_series_id...";
  2206. $sth_select_broken_nonopen_data->execute($broken_series_id);
  2207. while (my $rowref =
  2208. $sth_select_broken_nonopen_data->fetchrow_arrayref)
  2209. {
  2210. my ($date, $broken_value) = @$rowref;
  2211. my ($openbugs_value) =
  2212. $dbh->selectrow_array($sth_select_open_data, undef,
  2213. $found_open_series_id, $date);
  2214. if (defined($openbugs_value)) {
  2215. $sth_fix_broken_nonopen_data->execute
  2216. ($broken_value - $openbugs_value,
  2217. $broken_series_id, $date);
  2218. }
  2219. else {
  2220. print <<EOT;
  2221. WARNING - During repairs of series $broken_series_id, the irreparable data
  2222. entry for date $date was encountered and is being deleted.
  2223. Continuing repairs...
  2224. EOT
  2225. $sth_delete_broken_nonopen_data->execute
  2226. ($broken_series_id, $date);
  2227. }
  2228. }
  2229. # Fix the broken query so that it collects correct data
  2230. # in the future.
  2231. $nonopen_bugs_query =~
  2232. s/^$broken_series_indicator/field0-0-0=resolution&type0-0-0=regexp&value0-0-0=./;
  2233. $sth_repair->execute($nonopen_bugs_query,
  2234. $broken_series_id);
  2235. }
  2236. else {
  2237. print <<EOT;
  2238. WARNING - Series $broken_series_id was meant to collect non-open bug
  2239. counts, but it has counted all bugs instead. It cannot be repaired
  2240. automatically because no series that collected open bug counts was found.
  2241. You'll probably want to delete or repair collected data for
  2242. series $broken_series_id manually
  2243. Continuing repairs...
  2244. EOT
  2245. } # if ($found_open_series_id)
  2246. } # if ($nonopen_bugs_query =~
  2247. } # foreach (@$broken_nonopen_series)
  2248. print " done.\n";
  2249. } # if (@$broken_nonopen_series)
  2250. }
  2251. sub _clean_control_characters_from_short_desc {
  2252. my $dbh = Bugzilla->dbh;
  2253. # Fixup for Bug 101380
  2254. # "Newlines, nulls, leading/trailing spaces are getting into summaries"
  2255. my $controlchar_bugs =
  2256. $dbh->selectall_arrayref("SELECT short_desc, bug_id FROM bugs WHERE " .
  2257. $dbh->sql_regexp('short_desc', "'[[:cntrl:]]'"));
  2258. if (scalar(@$controlchar_bugs)) {
  2259. my $msg = 'Cleaning control characters from bug summaries...';
  2260. my $found = 0;
  2261. foreach (@$controlchar_bugs) {
  2262. my ($short_desc, $bug_id) = @$_;
  2263. my $clean_short_desc = clean_text($short_desc);
  2264. if ($clean_short_desc ne $short_desc) {
  2265. print $msg if !$found;
  2266. $found = 1;
  2267. print " $bug_id...";
  2268. $dbh->do("UPDATE bugs SET short_desc = ? WHERE bug_id = ?",
  2269. undef, $clean_short_desc, $bug_id);
  2270. }
  2271. }
  2272. print " done.\n" if $found;
  2273. }
  2274. }
  2275. sub _stop_storing_inactive_flags {
  2276. my $dbh = Bugzilla->dbh;
  2277. # 2006-03-02 LpSolit@gmail.com - Bug 322285
  2278. # Do not store inactive flags in the DB anymore.
  2279. if ($dbh->bz_column_info('flags', 'id')->{'TYPE'} eq 'INT3') {
  2280. # We first have to remove all existing inactive flags.
  2281. if ($dbh->bz_column_info('flags', 'is_active')) {
  2282. $dbh->do('DELETE FROM flags WHERE is_active = 0');
  2283. }
  2284. # Now we convert the id column to the auto_increment format.
  2285. $dbh->bz_alter_column('flags', 'id',
  2286. {TYPE => 'MEDIUMSERIAL', NOTNULL => 1, PRIMARYKEY => 1});
  2287. # And finally, we remove the is_active column.
  2288. $dbh->bz_drop_column('flags', 'is_active');
  2289. }
  2290. }
  2291. sub _change_short_desc_from_mediumtext_to_varchar {
  2292. my $dbh = Bugzilla->dbh;
  2293. # short_desc should not be a mediumtext, fix anything longer than 255 chars.
  2294. if($dbh->bz_column_info('bugs', 'short_desc')->{TYPE} eq 'MEDIUMTEXT') {
  2295. # Move extremely long summaries into a comment ("from" the Reporter),
  2296. # and then truncate the summary.
  2297. my $long_summary_bugs = $dbh->selectall_arrayref(
  2298. 'SELECT bug_id, short_desc, reporter
  2299. FROM bugs WHERE CHAR_LENGTH(short_desc) > 255');
  2300. if (@$long_summary_bugs) {
  2301. print <<EOT;
  2302. WARNING: Some of your bugs had summaries longer than 255 characters.
  2303. They have had their original summary copied into a comment, and then
  2304. the summary was truncated to 255 characters. The affected bug numbers were:
  2305. EOT
  2306. my $comment_sth = $dbh->prepare(
  2307. 'INSERT INTO longdescs (bug_id, who, thetext, bug_when)
  2308. VALUES (?, ?, ?, NOW())');
  2309. my $desc_sth = $dbh->prepare('UPDATE bugs SET short_desc = ?
  2310. WHERE bug_id = ?');
  2311. my @affected_bugs;
  2312. foreach my $bug (@$long_summary_bugs) {
  2313. my ($bug_id, $summary, $reporter_id) = @$bug;
  2314. my $summary_comment = "The original summary for this bug"
  2315. . " was longer than 255 characters, and so it was truncated"
  2316. . " when Bugzilla was upgraded. The original summary was:"
  2317. . "\n\n$summary";
  2318. $comment_sth->execute($bug_id, $reporter_id, $summary_comment);
  2319. my $short_summary = substr($summary, 0, 252) . "...";
  2320. $desc_sth->execute($short_summary, $bug_id);
  2321. push(@affected_bugs, $bug_id);
  2322. }
  2323. print join(', ', @affected_bugs) . "\n\n";
  2324. }
  2325. $dbh->bz_alter_column('bugs', 'short_desc', {TYPE => 'varchar(255)',
  2326. NOTNULL => 1});
  2327. }
  2328. }
  2329. sub _move_namedqueries_linkinfooter_to_its_own_table {
  2330. my $dbh = Bugzilla->dbh;
  2331. if ($dbh->bz_column_info("namedqueries", "linkinfooter")) {
  2332. # Move link-in-footer information into a table of its own.
  2333. my $sth_read = $dbh->prepare('SELECT id, userid
  2334. FROM namedqueries
  2335. WHERE linkinfooter = 1');
  2336. my $sth_write = $dbh->prepare('INSERT INTO namedqueries_link_in_footer
  2337. (namedquery_id, user_id) VALUES (?, ?)');
  2338. $sth_read->execute();
  2339. while (my ($id, $userid) = $sth_read->fetchrow_array()) {
  2340. $sth_write->execute($id, $userid);
  2341. }
  2342. $dbh->bz_drop_column("namedqueries", "linkinfooter");
  2343. }
  2344. }
  2345. sub _add_classifications_sortkey {
  2346. my $dbh = Bugzilla->dbh;
  2347. # 2006-07-07 olav@bkor.dhs.org - Bug 277377
  2348. # Add a sortkey to the classifications
  2349. if (!$dbh->bz_column_info('classifications', 'sortkey')) {
  2350. $dbh->bz_add_column('classifications', 'sortkey',
  2351. {TYPE => 'INT2', NOTNULL => 1, DEFAULT => 0});
  2352. my $class_ids = $dbh->selectcol_arrayref(
  2353. 'SELECT id FROM classifications ORDER BY name');
  2354. my $sth = $dbh->prepare('UPDATE classifications SET sortkey = ? ' .
  2355. 'WHERE id = ?');
  2356. my $sortkey = 0;
  2357. foreach my $class_id (@$class_ids) {
  2358. $sth->execute($sortkey, $class_id);
  2359. $sortkey += 100;
  2360. }
  2361. }
  2362. }
  2363. sub _move_data_nomail_into_db {
  2364. my $dbh = Bugzilla->dbh;
  2365. my $datadir = bz_locations()->{'datadir'};
  2366. # 2006-07-14 karl@kornel.name - Bug 100953
  2367. # If a nomail file exists, move its contents into the DB
  2368. $dbh->bz_add_column('profiles', 'disable_mail',
  2369. { TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 'FALSE' });
  2370. if (-e "$datadir/nomail") {
  2371. # We have a data/nomail file, read it in and delete it
  2372. my %nomail;
  2373. print "Found a data/nomail file. Moving nomail entries into DB...\n";
  2374. my $nomail_file = new IO::File("$datadir/nomail", 'r');
  2375. while (<$nomail_file>) {
  2376. $nomail{trim($_)} = 1;
  2377. }
  2378. $nomail_file->close;
  2379. # Go through each entry read. If a user exists, set disable_mail.
  2380. my $query = $dbh->prepare('UPDATE profiles
  2381. SET disable_mail = 1
  2382. WHERE userid = ?');
  2383. foreach my $user_to_check (keys %nomail) {
  2384. my $uid;
  2385. if ($uid = Bugzilla::User::login_to_id($user_to_check)) {
  2386. my $user = new Bugzilla::User($uid);
  2387. print "\tDisabling email for user ", $user->email, "\n";
  2388. $query->execute($user->id);
  2389. delete $nomail{$user->email};
  2390. }
  2391. }
  2392. # If there are any nomail entries remaining, move them to nomail.bad
  2393. # and say something to the user.
  2394. if (scalar(keys %nomail)) {
  2395. print <<EOT;
  2396. WARNING: The following users were listed in data/nomail, but do not
  2397. have an account here. The unmatched entries have been moved
  2398. to $datadir/nomail.bad:
  2399. EOT
  2400. my $nomail_bad = new IO::File("$datadir/nomail.bad", '>>');
  2401. foreach my $unknown_user (keys %nomail) {
  2402. print "\t$unknown_user\n";
  2403. print $nomail_bad "$unknown_user\n";
  2404. delete $nomail{$unknown_user};
  2405. }
  2406. $nomail_bad->close;
  2407. print "\n";
  2408. }
  2409. # Now that we don't need it, get rid of the nomail file.
  2410. unlink "$datadir/nomail";
  2411. }
  2412. }
  2413. sub _update_longdescs_who_index {
  2414. my $dbh = Bugzilla->dbh;
  2415. # When doing a search on who posted a comment, longdescs is joined
  2416. # against the bugs table. So we need an index on both of these,
  2417. # not just on "who".
  2418. my $who_index = $dbh->bz_index_info('longdescs', 'longdescs_who_idx');
  2419. if (!$who_index || scalar @{$who_index->{FIELDS}} == 1) {
  2420. # If the index doesn't exist, this will harmlessly do nothing.
  2421. $dbh->bz_drop_index('longdescs', 'longdescs_who_idx');
  2422. $dbh->bz_add_index('longdescs', 'longdescs_who_idx', [qw(who bug_id)]);
  2423. }
  2424. }
  2425. sub _fix_uppercase_custom_field_names {
  2426. # Before the final release of 3.0, custom fields could be
  2427. # created with mixed-case names.
  2428. my $dbh = Bugzilla->dbh;
  2429. my $fields = $dbh->selectall_arrayref(
  2430. 'SELECT name, type FROM fielddefs WHERE custom = 1');
  2431. foreach my $row (@$fields) {
  2432. my ($name, $type) = @$row;
  2433. if ($name ne lc($name)) {
  2434. $dbh->bz_rename_column('bugs', $name, lc($name));
  2435. $dbh->bz_rename_table($name, lc($name))
  2436. if $type == FIELD_TYPE_SINGLE_SELECT;
  2437. $dbh->do('UPDATE fielddefs SET name = ? WHERE name = ?',
  2438. undef, lc($name), $name);
  2439. }
  2440. }
  2441. }
  2442. sub _fix_uppercase_index_names {
  2443. # We forgot to fix indexes in the above code.
  2444. my $dbh = Bugzilla->dbh;
  2445. my $fields = $dbh->selectcol_arrayref(
  2446. 'SELECT name FROM fielddefs WHERE type = ? AND custom = 1',
  2447. undef, FIELD_TYPE_SINGLE_SELECT);
  2448. foreach my $field (@$fields) {
  2449. my $indexes = $dbh->bz_table_indexes($field);
  2450. foreach my $name (keys %$indexes) {
  2451. next if $name eq lc($name);
  2452. my $index = $indexes->{$name};
  2453. # Lowercase the name and everything in the definition.
  2454. my $new_name = lc($name);
  2455. my @new_fields = map {lc($_)} @{$index->{FIELDS}};
  2456. my $new_def = {FIELDS => \@new_fields, TYPE => $index->{TYPE}};
  2457. $new_def = \@new_fields if !$index->{TYPE};
  2458. $dbh->bz_drop_index($field, $name);
  2459. $dbh->bz_add_index($field, $new_name, $new_def);
  2460. }
  2461. }
  2462. }
  2463. sub _initialize_workflow {
  2464. my $old_params = shift;
  2465. my $dbh = Bugzilla->dbh;
  2466. $dbh->bz_add_column('bug_status', 'is_open',
  2467. {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 'TRUE'});
  2468. # Till now, bug statuses were not customizable. Nevertheless, local
  2469. # changes are possible and so we will try to respect these changes.
  2470. # This means: get the status of bugs having a resolution different from ''
  2471. # and mark these statuses as 'closed', even if some of these statuses are
  2472. # expected to be open statuses. Bug statuses we have no information about
  2473. # are left as 'open'.
  2474. my @closed_statuses =
  2475. @{$dbh->selectcol_arrayref('SELECT DISTINCT bug_status FROM bugs
  2476. WHERE resolution != ?', undef, '')};
  2477. # Append the default list of closed statuses *unless* we detect at least
  2478. # one closed state in the DB (i.e. with is_open = 0). This would mean that
  2479. # the DB has already been updated at least once and maybe the admin decided
  2480. # that e.g. 'RESOLVED' is now an open state, in which case we don't want to
  2481. # override this attribute. At least one bug status has to be a closed state
  2482. # anyway (due to the 'duplicate_or_move_bug_status' parameter) so it's safe
  2483. # to use this criteria.
  2484. my $num_closed_states = $dbh->selectrow_array('SELECT COUNT(*) FROM bug_status
  2485. WHERE is_open = 0');
  2486. if (!$num_closed_states) {
  2487. @closed_statuses =
  2488. map {$dbh->quote($_)} (@closed_statuses, qw(RESOLVED VERIFIED CLOSED));
  2489. print "Marking closed bug statuses as such...\n";
  2490. $dbh->do('UPDATE bug_status SET is_open = 0 WHERE value IN (' .
  2491. join(', ', @closed_statuses) . ')');
  2492. }
  2493. # Populate the status_workflow table. We do nothing if the table already
  2494. # has entries. If all bug status transitions have been deleted, the
  2495. # workflow will be restored to its default schema.
  2496. my $count = $dbh->selectrow_array('SELECT COUNT(*) FROM status_workflow');
  2497. if (!$count) {
  2498. # Make sure the variables below are defined as
  2499. # status_workflow.require_comment cannot be NULL.
  2500. my $create = $old_params->{'commentoncreate'} || 0;
  2501. my $confirm = $old_params->{'commentonconfirm'} || 0;
  2502. my $accept = $old_params->{'commentonaccept'} || 0;
  2503. my $resolve = $old_params->{'commentonresolve'} || 0;
  2504. my $verify = $old_params->{'commentonverify'} || 0;
  2505. my $close = $old_params->{'commentonclose'} || 0;
  2506. my $reopen = $old_params->{'commentonreopen'} || 0;
  2507. # This was till recently the only way to get back to NEW for
  2508. # confirmed bugs, so we use this parameter here.
  2509. my $reassign = $old_params->{'commentonreassign'} || 0;
  2510. # This is the default workflow.
  2511. my @workflow = ([undef, 'UNCONFIRMED', $create],
  2512. [undef, 'NEW', $create],
  2513. [undef, 'ASSIGNED', $create],
  2514. ['UNCONFIRMED', 'NEW', $confirm],
  2515. ['UNCONFIRMED', 'ASSIGNED', $accept],
  2516. ['UNCONFIRMED', 'RESOLVED', $resolve],
  2517. ['NEW', 'ASSIGNED', $accept],
  2518. ['NEW', 'RESOLVED', $resolve],
  2519. ['ASSIGNED', 'NEW', $reassign],
  2520. ['ASSIGNED', 'RESOLVED', $resolve],
  2521. ['REOPENED', 'NEW', $reassign],
  2522. ['REOPENED', 'ASSIGNED', $accept],
  2523. ['REOPENED', 'RESOLVED', $resolve],
  2524. ['RESOLVED', 'UNCONFIRMED', $reopen],
  2525. ['RESOLVED', 'REOPENED', $reopen],
  2526. ['RESOLVED', 'VERIFIED', $verify],
  2527. ['RESOLVED', 'CLOSED', $close],
  2528. ['VERIFIED', 'UNCONFIRMED', $reopen],
  2529. ['VERIFIED', 'REOPENED', $reopen],
  2530. ['VERIFIED', 'CLOSED', $close],
  2531. ['CLOSED', 'UNCONFIRMED', $reopen],
  2532. ['CLOSED', 'REOPENED', $reopen]);
  2533. print "Now filling the 'status_workflow' table with valid bug status transitions...\n";
  2534. my $sth_select = $dbh->prepare('SELECT id FROM bug_status WHERE value = ?');
  2535. my $sth = $dbh->prepare('INSERT INTO status_workflow (old_status, new_status,
  2536. require_comment) VALUES (?, ?, ?)');
  2537. foreach my $transition (@workflow) {
  2538. my ($from, $to);
  2539. # If it's an initial state, there is no "old" value.
  2540. $from = $dbh->selectrow_array($sth_select, undef, $transition->[0])
  2541. if $transition->[0];
  2542. $to = $dbh->selectrow_array($sth_select, undef, $transition->[1]);
  2543. # If one of the bug statuses doesn't exist, the transition is invalid.
  2544. next if (($transition->[0] && !$from) || !$to);
  2545. $sth->execute($from, $to, $transition->[2] ? 1 : 0);
  2546. }
  2547. }
  2548. # Make sure the bug status used by the 'duplicate_or_move_bug_status'
  2549. # parameter has all the required transitions set.
  2550. Bugzilla::Status::add_missing_bug_status_transitions();
  2551. }
  2552. sub _make_lang_setting_dynamic {
  2553. my $dbh = Bugzilla->dbh;
  2554. my $count = $dbh->selectrow_array(q{SELECT 1 FROM setting
  2555. WHERE name = 'lang'
  2556. AND subclass IS NULL});
  2557. if ($count) {
  2558. $dbh->do(q{UPDATE setting SET subclass = 'Lang' WHERE name = 'lang'});
  2559. $dbh->do(q{DELETE FROM setting_value WHERE name = 'lang'});
  2560. }
  2561. }
  2562. sub _fix_attachment_modification_date {
  2563. my $dbh = Bugzilla->dbh;
  2564. if (!$dbh->bz_column_info('attachments', 'modification_time')) {
  2565. # Allow NULL values till the modification time has been set.
  2566. $dbh->bz_add_column('attachments', 'modification_time', {TYPE => 'DATETIME'});
  2567. print "Setting the modification time for attachments...\n";
  2568. $dbh->do('UPDATE attachments SET modification_time = creation_ts');
  2569. # Now force values to be always defined.
  2570. $dbh->bz_alter_column('attachments', 'modification_time',
  2571. {TYPE => 'DATETIME', NOTNULL => 1});
  2572. # Update the modification time for attachments which have been modified.
  2573. my $attachments =
  2574. $dbh->selectall_arrayref('SELECT attach_id, MAX(bug_when) FROM bugs_activity
  2575. WHERE attach_id IS NOT NULL ' .
  2576. $dbh->sql_group_by('attach_id'));
  2577. my $sth = $dbh->prepare('UPDATE attachments SET modification_time = ?
  2578. WHERE attach_id = ?');
  2579. $sth->execute($_->[1], $_->[0]) foreach (@$attachments);
  2580. }
  2581. # We add this here to be sure to have the index being added, due to the original
  2582. # patch omitting it.
  2583. $dbh->bz_add_index('attachments', 'attachments_modification_time_idx',
  2584. [qw(modification_time)]);
  2585. }
  2586. sub _change_text_types {
  2587. my $dbh = Bugzilla->dbh;
  2588. return if
  2589. $dbh->bz_column_info('namedqueries', 'query')->{TYPE} eq 'LONGTEXT';
  2590. _check_content_length('attachments', 'mimetype', 255, 'attach_id');
  2591. _check_content_length('fielddefs', 'description', 255, 'id');
  2592. _check_content_length('attachments', 'description', 255, 'attach_id');
  2593. $dbh->bz_alter_column('bugs', 'bug_file_loc',
  2594. { TYPE => 'MEDIUMTEXT'});
  2595. $dbh->bz_alter_column('longdescs', 'thetext',
  2596. { TYPE => 'LONGTEXT', NOTNULL => 1 });
  2597. $dbh->bz_alter_column('attachments', 'description',
  2598. { TYPE => 'TINYTEXT', NOTNULL => 1 });
  2599. $dbh->bz_alter_column('attachments', 'mimetype',
  2600. { TYPE => 'TINYTEXT', NOTNULL => 1 });
  2601. # This also changes NULL to NOT NULL.
  2602. $dbh->bz_alter_column('flagtypes', 'description',
  2603. { TYPE => 'MEDIUMTEXT', NOTNULL => 1 }, '');
  2604. $dbh->bz_alter_column('fielddefs', 'description',
  2605. { TYPE => 'TINYTEXT', NOTNULL => 1 });
  2606. $dbh->bz_alter_column('groups', 'description',
  2607. { TYPE => 'MEDIUMTEXT', NOTNULL => 1 });
  2608. $dbh->bz_alter_column('quips', 'quip',
  2609. { TYPE => 'MEDIUMTEXT', NOTNULL => 1 });
  2610. $dbh->bz_alter_column('namedqueries', 'query',
  2611. { TYPE => 'LONGTEXT', NOTNULL => 1 });
  2612. }
  2613. sub _check_content_length {
  2614. my ($table_name, $field_name, $max_length, $id_field) = @_;
  2615. my $dbh = Bugzilla->dbh;
  2616. my %contents = @{ $dbh->selectcol_arrayref(
  2617. "SELECT $id_field, $field_name FROM $table_name
  2618. WHERE CHAR_LENGTH($field_name) > ?", {Columns=>[1,2]}, $max_length) };
  2619. if (scalar keys %contents) {
  2620. print install_string('install_data_too_long',
  2621. { column => $field_name,
  2622. id_column => $id_field,
  2623. table => $table_name,
  2624. max_length => $max_length });
  2625. foreach my $id (keys %contents) {
  2626. my $string = $contents{$id};
  2627. # Don't dump the whole string--it could be 16MB.
  2628. if (length($string) > 80) {
  2629. $string = substr($string, 0, 30) . "..."
  2630. . substr($string, -30) . "\n";
  2631. }
  2632. print "$id: $string\n";
  2633. }
  2634. exit 3;
  2635. }
  2636. }
  2637. sub _populate_bugs_fulltext {
  2638. my $dbh = Bugzilla->dbh;
  2639. my $fulltext = $dbh->selectrow_array('SELECT 1 FROM bugs_fulltext '
  2640. . $dbh->sql_limit(1));
  2641. # We only populate the table if it's empty...
  2642. if (!$fulltext) {
  2643. # ... and if there are bugs in the bugs table.
  2644. my $bug_ids = $dbh->selectcol_arrayref('SELECT bug_id FROM bugs');
  2645. return if !@$bug_ids;
  2646. # Populating bugs_fulltext can be very slow for large installs,
  2647. # so we special-case any DB that supports GROUP_CONCAT, which is
  2648. # a much faster way to do things.
  2649. if (UNIVERSAL::can($dbh, 'sql_group_concat')) {
  2650. print "Populating bugs_fulltext...";
  2651. print " (this can take a long time.)\n";
  2652. $dbh->do(
  2653. q{INSERT INTO bugs_fulltext (bug_id, short_desc, comments,
  2654. comments_noprivate)
  2655. SELECT bugs.bug_id, bugs.short_desc, }
  2656. . $dbh->sql_group_concat('longdescs.thetext', '\'\n\'')
  2657. . ', ' . $dbh->sql_group_concat('nopriv.thetext', '\'\n\'') .
  2658. q{ FROM bugs
  2659. LEFT JOIN longdescs
  2660. ON bugs.bug_id = longdescs.bug_id
  2661. LEFT JOIN longdescs AS nopriv
  2662. ON longdescs.comment_id = nopriv.comment_id
  2663. AND nopriv.isprivate = 0 }
  2664. . $dbh->sql_group_by('bugs.bug_id', 'bugs.short_desc'));
  2665. }
  2666. # The slow way, without group_concat.
  2667. else {
  2668. print "Populating bugs_fulltext.short_desc...\n";
  2669. $dbh->do('INSERT INTO bugs_fulltext (bug_id, short_desc)
  2670. SELECT bug_id, short_desc FROM bugs');
  2671. my $count = 1;
  2672. my $sth_all = $dbh->prepare('SELECT thetext FROM longdescs
  2673. WHERE bug_id = ?');
  2674. my $sth_nopriv = $dbh->prepare(
  2675. 'SELECT thetext FROM longdescs
  2676. WHERE bug_id = ? AND isprivate = 0');
  2677. my $sth_update = $dbh->prepare(
  2678. 'UPDATE bugs_fulltext SET comments = ?, comments_noprivate = ?
  2679. WHERE bug_id = ?');
  2680. print "Populating bugs_fulltext comment fields...\n";
  2681. foreach my $id (@$bug_ids) {
  2682. my $all = $dbh->selectcol_arrayref($sth_all, undef, $id);
  2683. my $nopriv = $dbh->selectcol_arrayref($sth_nopriv, undef, $id);
  2684. $sth_update->execute(join("\n", @$all), join("\n", @$nopriv), $id);
  2685. indicate_progress({ total => scalar @$bug_ids, every => 100,
  2686. current => $count++ });
  2687. }
  2688. print "\n";
  2689. }
  2690. }
  2691. }
  2692. 1;
  2693. __END__
  2694. =head1 NAME
  2695. Bugzilla::Install::DB - Fix up the database during installation.
  2696. =head1 SYNOPSIS
  2697. use Bugzilla::Install::DB qw(indicate_progress);
  2698. Bugzilla::Install::DB::update_table_definitions();
  2699. indicate_progress({ total => $total, current => $count, every => 10 });
  2700. =head1 DESCRIPTION
  2701. This module is used primarily by L<checksetup.pl> to modify the
  2702. database during upgrades.
  2703. =head1 SUBROUTINES
  2704. =over
  2705. =item C<update_table_definitions()>
  2706. Description: This is the primary code that updates table definitions
  2707. during upgrades. If you modify the schema in some
  2708. way, you should add code to the end of this function to
  2709. make sure that your modifications happen over all installations.
  2710. Params: none
  2711. Returns: nothing
  2712. =item C<update_fielddefs_definition()>
  2713. Description: L<checksetup.pl> depends on the fielddefs table having
  2714. its schema adjusted before the rest of the tables. So
  2715. these schema updates happen in a separate function from
  2716. L</update_table_definitions()>.
  2717. Params: none
  2718. Returns: nothing
  2719. =back