{"id":1691,"date":"2010-09-14T20:41:59","date_gmt":"2010-09-14T15:11:59","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=1691"},"modified":"2010-09-14T20:41:59","modified_gmt":"2010-09-14T15:11:59","slug":"grails-execute-sql-script-in-bootstrap","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/grails-execute-sql-script-in-bootstrap\/","title":{"rendered":"Grails: execute sql script in Bootstrap"},"content":{"rendered":"<p>This blog might help you to speed up your bootstrap process, especially when you need to populate records in tables.<br \/>\nEarlier we used populate our database table by reading line by line from a CSV file and creating Domain Class object ad Save. But this was taking a huge time. And in our case, this data (more like a static information) was always same. So we came up with an idea &#8211; we took mysqldump for this particular table and saved it to our application&#8217;s web-app\/data directory. Now the thing was, we just need to execute this &#8220;sql&#8221; file during bootstrap!<\/p>\n<p>Below is the example how to execute database dump files in bootstrap!<\/p>\n<blockquote>\n<pre lang=\"groovy\">\r\nimport groovy.sql.Sql\r\nimport org.codehaus.groovy.grails.commons.ConfigurationHolder as CH\r\n\r\nString sqlFilePath = ApplicationHolder.application.parentContext.servletContext.getRealPath(\"\/data\/table_dump.sql\")\r\nString sqlString = new File(sqlFilePath).text\r\nSql sql = Sql.newInstance(CH.config.dataSource.url, CH.config.dataSource.username,\r\nCH.config.dataSource.password, CH.config.dataSource.driverClassName)\r\nsql.execute(sqlString)\r\n<\/pre>\n<\/blockquote>\n<p>Hope this helped!<\/p>\n<p>Cheers!<br \/>\nAnshul Sharma<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This blog might help you to speed up your bootstrap process, especially when you need to populate records in tables. Earlier we used populate our database table by reading line by line from a CSV file and creating Domain Class object ad Save. But this was taking a huge time. And in our case, this [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":9},"categories":[7],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1691"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=1691"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1691\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=1691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=1691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=1691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}