PHP code example of openbuildings / timestamped-migrations
1. Go to this page and download the library: Download openbuildings/timestamped-migrations library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
openbuildings / timestamped-migrations example snippets
class My_Migration extends Migration
{
public function up()
{
}
public function down()
{
}
}
defined('SYSPATH') OR die('No direct script access.');
class Create_Users extends Migration
{
public function up()
{
$this->create_table('users', array( ));
}
public function down()
{
$this->drop_table('users');
}
}
class Create_Table_Users_Also_Add_Login_And_Name_To_Users extends Migration
{
public function up()
{
$this->create_table('users', array( ));
$this->add_column('users', 'login', 'string');
$this->add_column('users', 'name', 'string');
}
public function down()
{
$this->drop_table('users');
$this->remove_column('users', 'login');
$this->remove_column('users', 'name');
}
}
php
defined('SYSPATH') OR die('No direct script access.');
class Create_User extends Migration
{
public function up()
{
$this->create_table( "users", array(
'title' => 'string',
'is_admin' => array('boolean', 'null' => FALSE, 'default' => 0)
));
$this->add_column("users", "latlon", array("type" => "POINT"));
$this->add_column("users", "email", array("string", "null" => FALSE));
$this->add_index("users", "latlon", "latlon", "spatial");
$this->add_index("users", "search", array("title", "email"), "fulltext");
$this->execute(
"INSERT INTO `users` (`id`, `title`, `is_admin`) VALUES(1, 'user1', 1);
INSERT INTO `users` (`id`, `title`, `is_admin`) VALUES(2, 'user2', 0);"
);
}
public function down()
{
$this->remove_index("users", "latlon");
$this->remove_index("users", "search");
$this->remove_column("users", "email");
$this->remove_column("users", "latlon");
$this->drop_table("users");
}
}
php
// Create a table with innoDB, UTF-8 as default charset, and guid for primary key.
$this->create_table( "users", array(
'title' => 'string',
'guid' => 'primary_key',
'is_admin' => array('boolean', 'null' => FALSE, 'default' => 0)
), array (
'id' => FALSE,
'options' => 'ENGINE=innoDB CHARSET=utf8'
));
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.