1

I have a custom module with my_module.install file defining my two data tables in mysql. Module is already installed and I am able to save data from forms into them. Now my requirements changed and I need to create a third table in the same module. How do I approach the reinstall of the module.

1) Prepare MYSQl scripts to backup the existing data into temp tables and after uninstall and reinstalling the module, copy back the data into tables from temp tables.

2) Looked into https://www.drupal.org/project/backup_migrate not compatible to drupal core version I am using 8.6.0

3) Any other modules or ideas?

edited after the suggestion: I have created the below function in mymodule.install file and also edited mymodule_schema() method and run drush updatedb. Still no db changes as expected table is not getting created. Not able to look into system table to get the recent version number as mentioned elsewhere because that table is not available in my current local DB.

   function mymodule_update_8601(){
    $spec = array(
    'fields' => array(
     'application_setup_id' => array(
     'type' => 'serial',
     'not null' => TRUE,
     'description' => 'Primary Key: ID',
     ),
    ),
   );
   $schema = Database::getConnection()->schema();
   $schema->createTable('application_setup', $spec);
  }
3

When you want to apply database schema changes you don't reinstall the module. You apply these changes via hook_update_N() from your MYMODULE.install file.

See Updating Database Schema and/or Data in Drupal 8 on drupal.org.

If your module is making a data model change related to database schema that your module defines with hook_schema(), then you need to properly update your data model. The two steps are:

  1. Update your hook_schema() code so that it reflects your new data model if the database table and field definitions have changed. This will make sure that people that install your module after you made the change will install the correct database tables. See the Schema API documentation for details on that.
  2. Write a hook_update_N() function. This will update the database for existing users of your module who already had it installed before you made the change so that they can continue to function. This is described below.

There also is a sample on how to add a new table from hook_update_N():

$spec = [
  'description' => 'My description',
  'fields'      => [
    'myfield1' => [
      'description' => 'Myfield1 description.',
      'type'        => 'varchar',
      'length'      => 255,
      'not null'    => TRUE,
      'default'     => '',
    ],
    'myfield2' => [
      'description' => 'Myfield2 description',
      'type'        => 'text',
      'not null'    => TRUE,
    ],
  ],
  'primary key' => ['myfield1'],
];
$schema = Database::getConnection()->schema();
$schema->createTable('mytable2', $spec);
  • Awesome....Thanks a lot mann...will implement this... – user3324848 Oct 19 '18 at 20:17
  • @user3324848 – Cool. Then please consider to upvote and accept that answer when you are done. – leymannx Oct 19 '18 at 20:25
  • I updated my hook_schema and added a hook_update_N method to create a new table in my My_module.module file and ran drush updatedb in my local. It did'nt create a new table. Am I missing something, how to invoke the hook_udpate_N method. – user3324848 Oct 23 '18 at 15:59
  • @user3324848 – And you replaced N with an incremental number starting from 8001? Hard to tell from a afar. Maybe you can drop your code on a gist or something like that, so that I can have a look at it. – leymannx Oct 23 '18 at 16:37
  • 1
    Its working now, I had to rename my module to lowercase because for some reason Drupal doesn't like mixed case for module names. – user3324848 Oct 30 '18 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.