Welcome Guest, Not a member yet? Register   Sign In
Add dummy content when running migrations
#1

Hi all, I'd like some feedback please, if possible. I'm using CI v3 and I was wondering whether there's a way I could enter some dummy content on the tables I generate running through Migrations.

For instance when I run this migration

PHP Code:
class Migration_Add_blog extends CI_Migration {

        public function 
up()
        {
                
$this->dbforge->add_field(array(
                        
'blog_id' => array(
                                
'type' => 'INT',
                                
'constraint' => 5,
                                
'unsigned' => TRUE,
                                
'auto_increment' => TRUE
                        
),
                        
'blog_title' => array(
                                
'type' => 'VARCHAR',
                                
'constraint' => '100',
                        ),
                        
'blog_description' => array(
                                
'type' => 'TEXT',
                                
'null' => TRUE,
                        ),
                ));
                
$this->dbforge->add_key('blog_id'TRUE);
                
$this->dbforge->create_table('blog');
        }

        public function 
down()
        {
                
$this->dbforge->drop_table('blog');
        }


Is there also a way I could enter a few dummy blog posts to see as an example ?

One additional question regarding the Migrations. If I have the migrations types set as
PHP Code:
$config['migration_type'] = 'timestamp'
when I run this command
PHP Code:
$this->migration->version(5); 
do I have to enter the full timestamp or just the number version and if so how can I get the timestamp ?

Thanks in advance
Reply
#2

(10-05-2015, 02:11 AM)Lykos22 Wrote: Hi all, I'd like some feedback please, if possible. I'm using CI v3 and I was wondering whether there's a way I could enter some dummy content on the tables I generate running through Migrations.

For instance when I run this migration


PHP Code:
class Migration_Add_blog extends CI_Migration {

 
       public function up()
 
       {
 
               $this->dbforge->add_field(array(
 
                       'blog_id' => array(
 
                               'type' => 'INT',
 
                               'constraint' => 5,
 
                               'unsigned' => TRUE,
 
                               'auto_increment' => TRUE
                        
),
 
                       'blog_title' => array(
 
                               'type' => 'VARCHAR',
 
                               'constraint' => '100',
 
                       ),
 
                       'blog_description' => array(
 
                               'type' => 'TEXT',
 
                               'null' => TRUE,
 
                       ),
 
               ));
 
               $this->dbforge->add_key('blog_id'TRUE);
 
               $this->dbforge->create_table('blog');
 
       }

 
       public function down()
 
       {
 
               $this->dbforge->drop_table('blog');
 
       }


Is there also a way I could enter a few dummy blog posts to see as an example ?

One additional question regarding the Migrations. If I have the migrations types set as
PHP Code:
$config['migration_type'] = 'timestamp'
when I run this command
PHP Code:
$this->migration->version(5); 
do I have to enter the full timestamp or just the number version and if so how can I get the timestamp ?

Thanks in advance

You can access the $this->db object so you can insert anything what you want with $this->db->insert() or $this->db->update(). Wink

About the timestamp: you have to find out a timestamp what follows the format (YYYYMMDDHHIISS -> 20121031100537) and append the name of the migration. But you should read the docs if you're not lazy... See http://www.codeigniter.com/user_guide/li...ation.html
Reply
#3

(10-05-2015, 07:09 AM)orionstar Wrote: You can access the $this->db object so you can insert anything what you want with $this->db->insert() or $this->db->update(). Wink

About the timestamp: you have to find out a timestamp what follows the format (YYYYMMDDHHIISS -> 20121031100537) and append the name of the migration. But you should read the docs if you're not lazy... See http://www.codeigniter.com/user_guide/li...ation.html

Thank you for your reply. About the timestamps, I'm afraid you didn't understood me.. The documentation says:

Quote:Each Migration is run in numeric order forward or backwards depending on the method taken.


If I set the migration type to Timestamp, do I have to run
Code:
$this->migration->version(5);

with the timestamp or just the number of the migration ? Also if I have set timestamps to migrations, do I have to generate somehow a timestamp in order to apply in to the name of the file etc? do something like this for instance
PHP Code:
echo date('YmdHis'); 
Reply
#4

(This post was last modified: 10-05-2015, 04:07 PM by orionstar.)

(10-05-2015, 07:30 AM)Lykos22 Wrote:
(10-05-2015, 07:09 AM)orionstar Wrote: You can access the $this->db object so you can insert anything what you want with $this->db->insert() or $this->db->update(). Wink

About the timestamp: you have to find out a timestamp what follows the format (YYYYMMDDHHIISS -> 20121031100537) and append the name of the migration. But you should read the docs if you're not lazy... See http://www.codeigniter.com/user_guide/li...ation.html

Thank you for your reply. About the timestamps, I'm afraid you didn't understood me.. The documentation says:


Quote:Each Migration is run in numeric order forward or backwards depending on the method taken.

If I set the migration type to Timestamp, do I have to run


Code:
$this->migration->version(5);

with the timestamp or just the number of the migration ? Also if I have set timestamps to migrations, do I have to generate somehow a timestamp in order to apply in to the name of the file etc? do something like this for instance


PHP Code:
echo date('YmdHis'); 

If you are using sequential based migrations then you specify a number, if you use timestamp based migrations you have to use the full timestamp.

Quote:Then in application/config/migration.php set $config['migration_version'] = 20121031100537;.

You can auto generate the timestamp or you can create it manually, for example I have a CLI tool for autogenerating migrations with properly formatted filename, CI not support this from out of box.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB