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?
php database codeigniter migration
add a comment |
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?
php database codeigniter migration
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
add a comment |
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?
php database codeigniter migration
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
php database codeigniter migration
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
add a comment |
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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