Create database with codeigniter's migration class









up vote
0
down vote

favorite












I want to integrate Codeigniter's migration class to my project's build process. Can i use the migration class to create databases, or it's only purpose to keep up to date the database structure?



My migration class looks like this:



class Migration_base extends CI_Migration 
public function up()

$this->dbforge->create_database('my_db');

public function down()

$this->dbforge->drop_database('my_db');




When i run this code:



class Migrate extends CI_Controller

public function index()

$this->load->library('migration');

if ($this->migration->current() === FALSE)

show_error($this->migration->error_string());






I get this message:



Database error: A Database Error Occurred
Unable to connect to your database server using the provided settings.
Filename: path_to_codeigniter/codeigniter/framework/system/database/DB_driver.php
Line Number: 436


It seems the database must already exist before I can use migration class. Am I correct and need to write a wrapper around migration class where i create the database first?










share|improve this question























  • The error does not (necessarily) mean you don't have a database system, i.e. MySQL, installed on the server. What the error means is that you have not supplied the correct credentials to connect to the database system. That information is supplied in /application/config/database.php. You also have to loaded the CI database class. Documentation
    – DFriend
    Nov 11 at 21:45











  • The credentials are correct. If i create manually the database (i.e. 'my_db'), i can use the migration class to insert new tables and colums etc. into 'my_db'. The error message occur when i want to create 'my_db' with the migration class.
    – fff
    Nov 11 at 22:05















up vote
0
down vote

favorite












I want to integrate Codeigniter's migration class to my project's build process. Can i use the migration class to create databases, or it's only purpose to keep up to date the database structure?



My migration class looks like this:



class Migration_base extends CI_Migration 
public function up()

$this->dbforge->create_database('my_db');

public function down()

$this->dbforge->drop_database('my_db');




When i run this code:



class Migrate extends CI_Controller

public function index()

$this->load->library('migration');

if ($this->migration->current() === FALSE)

show_error($this->migration->error_string());






I get this message:



Database error: A Database Error Occurred
Unable to connect to your database server using the provided settings.
Filename: path_to_codeigniter/codeigniter/framework/system/database/DB_driver.php
Line Number: 436


It seems the database must already exist before I can use migration class. Am I correct and need to write a wrapper around migration class where i create the database first?










share|improve this question























  • The error does not (necessarily) mean you don't have a database system, i.e. MySQL, installed on the server. What the error means is that you have not supplied the correct credentials to connect to the database system. That information is supplied in /application/config/database.php. You also have to loaded the CI database class. Documentation
    – DFriend
    Nov 11 at 21:45











  • The credentials are correct. If i create manually the database (i.e. 'my_db'), i can use the migration class to insert new tables and colums etc. into 'my_db'. The error message occur when i want to create 'my_db' with the migration class.
    – fff
    Nov 11 at 22:05













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want to integrate Codeigniter's migration class to my project's build process. Can i use the migration class to create databases, or it's only purpose to keep up to date the database structure?



My migration class looks like this:



class Migration_base extends CI_Migration 
public function up()

$this->dbforge->create_database('my_db');

public function down()

$this->dbforge->drop_database('my_db');




When i run this code:



class Migrate extends CI_Controller

public function index()

$this->load->library('migration');

if ($this->migration->current() === FALSE)

show_error($this->migration->error_string());






I get this message:



Database error: A Database Error Occurred
Unable to connect to your database server using the provided settings.
Filename: path_to_codeigniter/codeigniter/framework/system/database/DB_driver.php
Line Number: 436


It seems the database must already exist before I can use migration class. Am I correct and need to write a wrapper around migration class where i create the database first?










share|improve this question















I want to integrate Codeigniter's migration class to my project's build process. Can i use the migration class to create databases, or it's only purpose to keep up to date the database structure?



My migration class looks like this:



class Migration_base extends CI_Migration 
public function up()

$this->dbforge->create_database('my_db');

public function down()

$this->dbforge->drop_database('my_db');




When i run this code:



class Migrate extends CI_Controller

public function index()

$this->load->library('migration');

if ($this->migration->current() === FALSE)

show_error($this->migration->error_string());






I get this message:



Database error: A Database Error Occurred
Unable to connect to your database server using the provided settings.
Filename: path_to_codeigniter/codeigniter/framework/system/database/DB_driver.php
Line Number: 436


It seems the database must already exist before I can use migration class. Am I correct and need to write a wrapper around migration class where i create the database first?







php database codeigniter migration






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 19:37

























asked Nov 11 at 15:15









fff

534




534











  • The error does not (necessarily) mean you don't have a database system, i.e. MySQL, installed on the server. What the error means is that you have not supplied the correct credentials to connect to the database system. That information is supplied in /application/config/database.php. You also have to loaded the CI database class. Documentation
    – DFriend
    Nov 11 at 21:45











  • The credentials are correct. If i create manually the database (i.e. 'my_db'), i can use the migration class to insert new tables and colums etc. into 'my_db'. The error message occur when i want to create 'my_db' with the migration class.
    – fff
    Nov 11 at 22:05

















  • The error does not (necessarily) mean you don't have a database system, i.e. MySQL, installed on the server. What the error means is that you have not supplied the correct credentials to connect to the database system. That information is supplied in /application/config/database.php. You also have to loaded the CI database class. Documentation
    – DFriend
    Nov 11 at 21:45











  • The credentials are correct. If i create manually the database (i.e. 'my_db'), i can use the migration class to insert new tables and colums etc. into 'my_db'. The error message occur when i want to create 'my_db' with the migration class.
    – fff
    Nov 11 at 22:05
















The error does not (necessarily) mean you don't have a database system, i.e. MySQL, installed on the server. What the error means is that you have not supplied the correct credentials to connect to the database system. That information is supplied in /application/config/database.php. You also have to loaded the CI database class. Documentation
– DFriend
Nov 11 at 21:45





The error does not (necessarily) mean you don't have a database system, i.e. MySQL, installed on the server. What the error means is that you have not supplied the correct credentials to connect to the database system. That information is supplied in /application/config/database.php. You also have to loaded the CI database class. Documentation
– DFriend
Nov 11 at 21:45













The credentials are correct. If i create manually the database (i.e. 'my_db'), i can use the migration class to insert new tables and colums etc. into 'my_db'. The error message occur when i want to create 'my_db' with the migration class.
– fff
Nov 11 at 22:05





The credentials are correct. If i create manually the database (i.e. 'my_db'), i can use the migration class to insert new tables and colums etc. into 'my_db'. The error message occur when i want to create 'my_db' with the migration class.
– fff
Nov 11 at 22:05













1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Your suspicion that a workaround will be needed in order to create a database as part of a migration seems to be true. I don't know if it could be called a bug or missing feature but it will not do it as written.



The big issue is that the _migration_table that the class creates needs to be in the database being migrated. A classic "chicken or egg" problem.



Another possible issue, something the documentation assumes and doesn't address, is that the database to be migrated is the one that should be "loaded".



I think the following version of your Migrate controller will handle both of those issues.



class Migrate extends CI_Controller

public function index()

if( ! isset($this->db))

throw new RuntimeException("Must have a database loaded to run a migration");


// Are we connected to 'my_db' ?
if( ! $this->db->database !== 'my_db')

//find out if that db even exists
$this->load->dbutil();
if( ! $this->dbutil->database_exists('my_db'))

// try to create 'my_db'
$this->load->dbforge();
if( ! $this->dbforge->create_database('my_db'))

throw new RuntimeException("Could not create the database 'my_db");



// Connection data for 'my_db' must be available
// in /config/database.php for this to work.
if(($db = $this->load->database('my_db', TRUE)) === TRUE)

$this->db = $db; //replace the previously loaded database

else

throw new RuntimeException("Could not load 'my_db' database");



$this->load->library('migration');

if($this->migration->current() === FALSE)

show_error($this->migration->error_string());





Please know I have not tested this code. There may be syntax, logic or other errors. If nothing else, hopefully, it gives you a good starting place






share|improve this answer




















  • Thank you for your time and your answer, it led me to the solution. One important addition: I hat to delete the database name from the database configuration file, because it throws an error if db did not exist.
    – fff
    Nov 12 at 20:54










  • Interesting. Didn't know that would happen. How did you work around that?
    – DFriend
    Nov 12 at 21:18










  • I rewritten your code a bit. In the database config file i added all the credentials except the db name. Then i load the dbutil class, and check the database i want to create exists. If not i create it, then i can run the migration like a charm. The only downside i can't autoload the database, but i wrote a core controller and load there. It's work because my migration run in cli mode, and i dont extend it from the core controller.
    – fff
    Nov 12 at 21:54











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250117%2fcreate-database-with-codeigniters-migration-class%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










Your suspicion that a workaround will be needed in order to create a database as part of a migration seems to be true. I don't know if it could be called a bug or missing feature but it will not do it as written.



The big issue is that the _migration_table that the class creates needs to be in the database being migrated. A classic "chicken or egg" problem.



Another possible issue, something the documentation assumes and doesn't address, is that the database to be migrated is the one that should be "loaded".



I think the following version of your Migrate controller will handle both of those issues.



class Migrate extends CI_Controller

public function index()

if( ! isset($this->db))

throw new RuntimeException("Must have a database loaded to run a migration");


// Are we connected to 'my_db' ?
if( ! $this->db->database !== 'my_db')

//find out if that db even exists
$this->load->dbutil();
if( ! $this->dbutil->database_exists('my_db'))

// try to create 'my_db'
$this->load->dbforge();
if( ! $this->dbforge->create_database('my_db'))

throw new RuntimeException("Could not create the database 'my_db");



// Connection data for 'my_db' must be available
// in /config/database.php for this to work.
if(($db = $this->load->database('my_db', TRUE)) === TRUE)

$this->db = $db; //replace the previously loaded database

else

throw new RuntimeException("Could not load 'my_db' database");



$this->load->library('migration');

if($this->migration->current() === FALSE)

show_error($this->migration->error_string());





Please know I have not tested this code. There may be syntax, logic or other errors. If nothing else, hopefully, it gives you a good starting place






share|improve this answer




















  • Thank you for your time and your answer, it led me to the solution. One important addition: I hat to delete the database name from the database configuration file, because it throws an error if db did not exist.
    – fff
    Nov 12 at 20:54










  • Interesting. Didn't know that would happen. How did you work around that?
    – DFriend
    Nov 12 at 21:18










  • I rewritten your code a bit. In the database config file i added all the credentials except the db name. Then i load the dbutil class, and check the database i want to create exists. If not i create it, then i can run the migration like a charm. The only downside i can't autoload the database, but i wrote a core controller and load there. It's work because my migration run in cli mode, and i dont extend it from the core controller.
    – fff
    Nov 12 at 21:54















up vote
1
down vote



accepted










Your suspicion that a workaround will be needed in order to create a database as part of a migration seems to be true. I don't know if it could be called a bug or missing feature but it will not do it as written.



The big issue is that the _migration_table that the class creates needs to be in the database being migrated. A classic "chicken or egg" problem.



Another possible issue, something the documentation assumes and doesn't address, is that the database to be migrated is the one that should be "loaded".



I think the following version of your Migrate controller will handle both of those issues.



class Migrate extends CI_Controller

public function index()

if( ! isset($this->db))

throw new RuntimeException("Must have a database loaded to run a migration");


// Are we connected to 'my_db' ?
if( ! $this->db->database !== 'my_db')

//find out if that db even exists
$this->load->dbutil();
if( ! $this->dbutil->database_exists('my_db'))

// try to create 'my_db'
$this->load->dbforge();
if( ! $this->dbforge->create_database('my_db'))

throw new RuntimeException("Could not create the database 'my_db");



// Connection data for 'my_db' must be available
// in /config/database.php for this to work.
if(($db = $this->load->database('my_db', TRUE)) === TRUE)

$this->db = $db; //replace the previously loaded database

else

throw new RuntimeException("Could not load 'my_db' database");



$this->load->library('migration');

if($this->migration->current() === FALSE)

show_error($this->migration->error_string());





Please know I have not tested this code. There may be syntax, logic or other errors. If nothing else, hopefully, it gives you a good starting place






share|improve this answer




















  • Thank you for your time and your answer, it led me to the solution. One important addition: I hat to delete the database name from the database configuration file, because it throws an error if db did not exist.
    – fff
    Nov 12 at 20:54










  • Interesting. Didn't know that would happen. How did you work around that?
    – DFriend
    Nov 12 at 21:18










  • I rewritten your code a bit. In the database config file i added all the credentials except the db name. Then i load the dbutil class, and check the database i want to create exists. If not i create it, then i can run the migration like a charm. The only downside i can't autoload the database, but i wrote a core controller and load there. It's work because my migration run in cli mode, and i dont extend it from the core controller.
    – fff
    Nov 12 at 21:54













up vote
1
down vote



accepted







up vote
1
down vote



accepted






Your suspicion that a workaround will be needed in order to create a database as part of a migration seems to be true. I don't know if it could be called a bug or missing feature but it will not do it as written.



The big issue is that the _migration_table that the class creates needs to be in the database being migrated. A classic "chicken or egg" problem.



Another possible issue, something the documentation assumes and doesn't address, is that the database to be migrated is the one that should be "loaded".



I think the following version of your Migrate controller will handle both of those issues.



class Migrate extends CI_Controller

public function index()

if( ! isset($this->db))

throw new RuntimeException("Must have a database loaded to run a migration");


// Are we connected to 'my_db' ?
if( ! $this->db->database !== 'my_db')

//find out if that db even exists
$this->load->dbutil();
if( ! $this->dbutil->database_exists('my_db'))

// try to create 'my_db'
$this->load->dbforge();
if( ! $this->dbforge->create_database('my_db'))

throw new RuntimeException("Could not create the database 'my_db");



// Connection data for 'my_db' must be available
// in /config/database.php for this to work.
if(($db = $this->load->database('my_db', TRUE)) === TRUE)

$this->db = $db; //replace the previously loaded database

else

throw new RuntimeException("Could not load 'my_db' database");



$this->load->library('migration');

if($this->migration->current() === FALSE)

show_error($this->migration->error_string());





Please know I have not tested this code. There may be syntax, logic or other errors. If nothing else, hopefully, it gives you a good starting place






share|improve this answer












Your suspicion that a workaround will be needed in order to create a database as part of a migration seems to be true. I don't know if it could be called a bug or missing feature but it will not do it as written.



The big issue is that the _migration_table that the class creates needs to be in the database being migrated. A classic "chicken or egg" problem.



Another possible issue, something the documentation assumes and doesn't address, is that the database to be migrated is the one that should be "loaded".



I think the following version of your Migrate controller will handle both of those issues.



class Migrate extends CI_Controller

public function index()

if( ! isset($this->db))

throw new RuntimeException("Must have a database loaded to run a migration");


// Are we connected to 'my_db' ?
if( ! $this->db->database !== 'my_db')

//find out if that db even exists
$this->load->dbutil();
if( ! $this->dbutil->database_exists('my_db'))

// try to create 'my_db'
$this->load->dbforge();
if( ! $this->dbforge->create_database('my_db'))

throw new RuntimeException("Could not create the database 'my_db");



// Connection data for 'my_db' must be available
// in /config/database.php for this to work.
if(($db = $this->load->database('my_db', TRUE)) === TRUE)

$this->db = $db; //replace the previously loaded database

else

throw new RuntimeException("Could not load 'my_db' database");



$this->load->library('migration');

if($this->migration->current() === FALSE)

show_error($this->migration->error_string());





Please know I have not tested this code. There may be syntax, logic or other errors. If nothing else, hopefully, it gives you a good starting place







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 1:43









DFriend

6,5391520




6,5391520











  • Thank you for your time and your answer, it led me to the solution. One important addition: I hat to delete the database name from the database configuration file, because it throws an error if db did not exist.
    – fff
    Nov 12 at 20:54










  • Interesting. Didn't know that would happen. How did you work around that?
    – DFriend
    Nov 12 at 21:18










  • I rewritten your code a bit. In the database config file i added all the credentials except the db name. Then i load the dbutil class, and check the database i want to create exists. If not i create it, then i can run the migration like a charm. The only downside i can't autoload the database, but i wrote a core controller and load there. It's work because my migration run in cli mode, and i dont extend it from the core controller.
    – fff
    Nov 12 at 21:54

















  • Thank you for your time and your answer, it led me to the solution. One important addition: I hat to delete the database name from the database configuration file, because it throws an error if db did not exist.
    – fff
    Nov 12 at 20:54










  • Interesting. Didn't know that would happen. How did you work around that?
    – DFriend
    Nov 12 at 21:18










  • I rewritten your code a bit. In the database config file i added all the credentials except the db name. Then i load the dbutil class, and check the database i want to create exists. If not i create it, then i can run the migration like a charm. The only downside i can't autoload the database, but i wrote a core controller and load there. It's work because my migration run in cli mode, and i dont extend it from the core controller.
    – fff
    Nov 12 at 21:54
















Thank you for your time and your answer, it led me to the solution. One important addition: I hat to delete the database name from the database configuration file, because it throws an error if db did not exist.
– fff
Nov 12 at 20:54




Thank you for your time and your answer, it led me to the solution. One important addition: I hat to delete the database name from the database configuration file, because it throws an error if db did not exist.
– fff
Nov 12 at 20:54












Interesting. Didn't know that would happen. How did you work around that?
– DFriend
Nov 12 at 21:18




Interesting. Didn't know that would happen. How did you work around that?
– DFriend
Nov 12 at 21:18












I rewritten your code a bit. In the database config file i added all the credentials except the db name. Then i load the dbutil class, and check the database i want to create exists. If not i create it, then i can run the migration like a charm. The only downside i can't autoload the database, but i wrote a core controller and load there. It's work because my migration run in cli mode, and i dont extend it from the core controller.
– fff
Nov 12 at 21:54





I rewritten your code a bit. In the database config file i added all the credentials except the db name. Then i load the dbutil class, and check the database i want to create exists. If not i create it, then i can run the migration like a charm. The only downside i can't autoload the database, but i wrote a core controller and load there. It's work because my migration run in cli mode, and i dont extend it from the core controller.
– fff
Nov 12 at 21:54


















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250117%2fcreate-database-with-codeigniters-migration-class%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto