5

I have a large SQL file. Can I use this in my module installer?

11

Yes, if you follow the naming conventions for install scripts. For example:

app/code/local/Your/Module/sql/your_module_setup/install-1.0.0.sql
app/code/local/Your/Module/sql/your_module_setup/upgrade-1.0.0-1.0.1.sql

Or as data install script (if you are not adding/changing tables):

app/code/local/Your/Module/data/your_module_setup/data-install-1.0.0.sql app/code/local/Your/Module/data/your_module_setup/data-upgrade-1.0.0-1.0.1.sql

Update: for data install scripts, the SQL extension is not working.

You need to define the setup resource your_module_setup and the current version number in the module's config.xml like this:

<config>
    <modules>
        <Your_Module>
             <version>1.0.0</version>
        </Your_Module>
    </modules>
    <global>
        <resources>
            <your_module_setup>
                <setup>
                    <module>Your_Module</module>
                    <class>Mage_Core_Model_Resource_Setup</class>
                </setup>
            </your_module_setup>
        </resources>
    </global>
</config>

Clarification: Magento looks for files with this pattern that either end in ".php" or ".sql", you should only have one of them per upgrade, but not both.

If you want to know how they are executed, see Mage_Core_Model_Resource_Setup::modifyResourceDb()

8
  • Is it ".sql" or ".php" ? – Vicky Dev Jan 11 '16 at 10:20
  • 2
    sql. Magento looks for both and treats them accordingly – Fabian Schmengler Jan 11 '16 at 10:20
  • Thanks for info. Just for clarification, if ".sql" then only we can use mysql queries syntaxes in it, and if ".php" then both mysql and php, right ? – Vicky Dev Jan 11 '16 at 10:22
  • 1
    That's correct. – Fabian Schmengler Jan 11 '16 at 10:25
  • Thank's ! Working with the SQL file now. Great answer – Kurdt94 Jan 11 '16 at 15:47

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.