Database Migrations
Required Namespace
  • Ness
    • Autopulse
      • Factory
        • Schema
        • Table
        • Column
        • Migration

You can include this library to your code file by typing the below code;



use Ness\Autopulse\Factory\Schema

use Ness\Autopulse\Factory\Table

use Ness\Autopulse\Factory\Column

use Ness\Autopulse\Factory\Migration


About Database Migrations #top

Ness PHP Framework supports database migration with its own classes. You simply need to declare tables, columns and connections and you are ready. First of all please include the required packages in your code. You can find the required packages in the left side of this page. After you have done with this you are ready to create migrations and database synch operations.


Schema Class #top

Schema class is used to define a database in your code. Hierarchically a schema class must contain;

  • schema
    • Table
      • Column
      • Column
      • column x n
    • Table
      • Column
      • Column
      • column x n
    • Table x n
      • Column
      • Column
      • column x n
For example a simple database to store your users and post can be;
  • users_database
    • Users
      • userid int AUTO_INREMENT PRIMARY KEY
      • username varchar(25)
      • password varchar(25)
    • User Data
      • dataid int AUTO_INREMENT PRIMARY KEY
      • userid int not null (FOREIGN KEY TO USERS TABLE AND userid column)
      • name varchar(25)
      • surname varchar(25)
      • email varchar(100) unique
      • phone_number
Please click here for example code to create the table above

Schema:Name

Set a new name for your database.


$schema = new Schema();
$schema->Name(string $database_name);


//or
$schema = new Schema(string $database_name);

Schema:Version

Set version number to track your migrations. Version number must set for detecting if migration should run.


$schema = new Schema(string $database_name);
$schema->Version(int $version_number);

Schema:getName

Returns the database name


$schema = new Schema(string $database_name);
$schema->getName();

returns: string $database_name

Schema:Tables

Set tables to your database


$schema = new Schema(string $database_name);
$schema->Version(int $version_number);
$schema->Tables(Table $tb1, Table $tb2, Table $tbn)

Schema:ForeignKey

Set Foreign Keys for tables.


$schema = new Schema(string $database_name);
$schema->ForeignKey($table1='table1', $key1='column1', $table2='table2', $key2='column2');
$schema->ForeignKey($table1='table2', $key1='column2', $table2='table21', $key2='column21');
$schema->ForeignKey($table1='table_n', $key1='column_n', $table2='table_mn', $key2='column_mn');


Table Class #top

Table class is a helper class for using with your Schema. You can define unlimited tables inside a schema. This helper class is used to generate tables for your database.

Here is an example for defining tables;


$table1 = new Table("table_name", 
new Column("column_id", "column_type", "constraints"),
new Column("column_id_2", "column_type_2", "constraints_2"),
new Column("column_id_n", "column_type_n", "constraints_n"),
);


Column Class#top

A simple class to generate columns for your database tables.

Here is an example for defining columns;


$userid = new Column(string "column_name", string  "column_type", string  "constraints");
$password = new Column(string "column_name", string "column_type", string "constraints");


$table1 = new Table("table_name", $userid, $password);


Migration Class #top

Migration class is used to manage; create and drop database operations of your project.

Migration:Up

Create a database from the given schema.


Migration::Up(Schema $database, MySqlConnect | DbConnect $connection);
                     
Migration:Down

Drop a database from the given schema.


Migration::Down(Schema $database, MySqlConnect | DbConnect $connection);
                 


Migration:Synch

Keep your database continuously updated independent from version number. Not recommended in production.


Migration::Synch(Schema $database, MySqlConnect | DbConnect $connection);
                    


Example #top