Question

Short version: Is it possible to create a database from a setup script?

Long version: I've set-up a module that creates it's own database resource connection for storing information in a database independent of the main Magento install. I've also got the migration scripts set-up to generate the tables. Is it possible using this migration script to attempt to create the database if it doesn't exist?

I've tried playing around a little, but as soon as I call startSetup, I get a database not found exception. I can't actually find where the dbname actually get's used. I can see it get's passed into the constructor when the adapter get's created, but couldn't seem to find where it's used.

Was it helpful?

Solution

Well I seem to have found a passable solution.

// first use the main database connection to create the external database
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$write->query('CREATE DATABASE IF NOT EXISTS external_db_name');

// then call startSetup, which will now be able to connect to the external database
// and allow us to create our tables
$installer = $this;
$installer->startSetup();
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top