Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions

Reply
 
Thread Tools Display Modes
  #1  
Old 01-03-2009, 07:43 PM
Jaxel Jaxel is offline
 
Join Date: Sep 2005
Posts: 1,160
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Installation Tables and Columns Help...

Okay, lets say I have a mod... and in version1, I have the following install code... (the code is an example, I know its not functioning, but thats got nothing to do with my problem)

Code:
$db->query_write("CREATE TABLE IF NOT EXISTS table (
  column1 int auto_increment,
  column2 int,
  PRIMARY KEY  (column1)
)");

Then I create a version2, with an extra column... so I made my install code this...

Code:
$db->query_write("CREATE TABLE IF NOT EXISTS table (
  column1 int auto_increment,
  column2 int,
  column3 int,
  PRIMARY KEY  (column1)
)");
$db->query_write("ALTER TABLE table ADD COLUMN column3 int)");

Now I am making a version 3... and there is yet another column to add... I tried the following code, but I got an error when it hit the ADD COLUMN column3 code... because column3 already exists.

Code:
$db->query_write("
CREATE TABLE IF NOT EXISTS table (
  column1 int auto_increment,
  column2 int,
  column3 int,
  column4 int,
  PRIMARY KEY  (column1)
)");
$db->query_write("ALTER TABLE table ADD COLUMN column3 int)");
$db->query_write("ALTER TABLE table ADD COLUMN column4 int)");
I tried looking for a possible IF NOT EXIST condition for altering tables to add columns, but it looks like you cant do it. I know I could just delete the add column3 query, and it would work great for people who are updating from version2 to version3. But that line would still be required if people were upgrading from version1. So is there a proper way for me to handle this problem?
Reply With Quote
  #2  
Old 01-03-2009, 08:55 PM
Kirk Y's Avatar
Kirk Y Kirk Y is offline
 
Join Date: Apr 2005
Location: Tallahassee, Florida
Posts: 2,604
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

You could run a query to retrieve the columns in "table" and then control the flow of logic from there.

PHP Code:
if (!in_array('column3'$columns))
{
    
// create column 3 as it does not exist yet

Reply With Quote
  #3  
Old 01-03-2009, 09:52 PM
Jaxel Jaxel is offline
 
Join Date: Sep 2005
Posts: 1,160
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Hmm... how would that code be written exactly? What would be the code to construct $columns?
Reply With Quote
  #4  
Old 01-03-2009, 10:22 PM
Kirk Y's Avatar
Kirk Y Kirk Y is offline
 
Join Date: Apr 2005
Location: Tallahassee, Florida
Posts: 2,604
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

PHP Code:
$columns $db->query_read("SHOW COLUMNS FROM " TABLE_PREFIX "table"); 
You'd need to loop through $columns and take out the "field" key.
Reply With Quote
  #5  
Old 01-04-2009, 03:31 AM
Jaxel Jaxel is offline
 
Join Date: Sep 2005
Posts: 1,160
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Huh? I'm confused? What would I need to loop? Wouldn't this be enough?

Code:
$columns = $db->query_read("SHOW COLUMNS FROM table");
if (!in_array('column3', $columns))
{
    $db->query_write("ALTER TABLE table ADD COLUMN column3 int)");
}
if (!in_array('column4', $columns))
{
    $db->query_write("ALTER TABLE table ADD COLUMN column4 int)");
}
Thanks for the help BTW.
Reply With Quote
  #6  
Old 01-04-2009, 03:45 AM
Kirk Y's Avatar
Kirk Y Kirk Y is offline
 
Join Date: Apr 2005
Location: Tallahassee, Florida
Posts: 2,604
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

If you look at what $columns contains, you'll see it is a two-dimensional array. You need to loop through it after your query to extract the relevant information; in this case - the field names.

PHP Code:
$columnquery $db->query_read("SHOW COLUMNS FROM table");
$columns = array();
while (
$column $db->fetch_array($columnquery))
{
    
$columns[] = $column['field'];

Then you can run the in_array check.
Reply With Quote
  #7  
Old 01-04-2009, 04:02 AM
Dismounted's Avatar
Dismounted Dismounted is offline
 
Join Date: Jun 2005
Location: Melbourne, Australia
Posts: 15,047
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

There is a reason why you are allowed to create multiple install codes in products. In the version 1 install code, use the first query. In the version 2 code, create a query using ALTER and add another column. In the version 3 install code, create another query.

On a new installation of the modification, vBulletin will run the install codes in order of version.
Reply With Quote
  #8  
Old 01-04-2009, 08:20 AM
Jaxel Jaxel is offline
 
Join Date: Sep 2005
Posts: 1,160
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

So this this shoudl work? the table name is video... and the column is timelength...

Code:
$columns = $db->query_read("SHOW COLUMNS FROM video");
$videocolumns = array();
while ($column = $db->fetch_array($columns))
{
  $videocolumns[] = $column['field'];
}
if (!in_array('timelength', $videocolumns))
{
  echo('<li>Altering Table <strong>' . TABLE_PREFIX . 'video</strong> ... ');
  $db->query_write("ALTER TABLE video ADD timelength int(10) unsigned NOT NULL");
}
I will use the install code stuff in future iterations...
Reply With Quote
  #9  
Old 01-04-2009, 09:01 AM
Dismounted's Avatar
Dismounted Dismounted is offline
 
Join Date: Jun 2005
Location: Melbourne, Australia
Posts: 15,047
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Why not use the install code functionality now?
Reply With Quote
  #10  
Old 01-04-2009, 01:26 PM
Jaxel Jaxel is offline
 
Join Date: Sep 2005
Posts: 1,160
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Because what if someone is upgrading from Version 1 to Version 3... They wont get the changes in Version 2, because its not in the new install code. If I put the new install code in Version 3; then it gets the redundancy error.

Anyway, the code worked, thanks Kirk!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 08:07 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.07572 seconds
  • Memory Usage 2,260KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (5)bbcode_code
  • (3)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete