Migrations



( ZN >= 3.0.0 )

In this section we tried to include how to create and use migrations. Migrations usually do things like DBForge :: create a table, add columns, but the only difference is that the code is stored in a file. Migration files include two functions, up and down . If up is used, down is used to undo the operation. Of course, when these processes are performed, the files are created by versioning, which means that the process is done first. Thus, when the project is moved, instead of moving the database file directly, the version of the database is reconstructed with the files kept. Now let's go through some examples of using this class.

 

 

# Configuration


You can change the migrationTable value from the configuration file, which is shown below, to set the table name to hold the migration data. Migrations are set by default.

File:  Config / Database.php
Migration
String $ table = 'migrations' Specifies the table in which the table creation and retrieval operations performed by the migration operations are recorded.

 

 

# Methods


Migration
String create ( $ name , Int $ ver = 0 )
Bool delete ( String $ name , Int $ ver = 0 )
Bool deleteAll ( Void )
this version ( Int $ version = 0 )
Bool upAll ( String ... $ migrateNames )
Bool downAll ( String ... $ migrateNames )
Migrate
Bool up ( Void )
Bool down ( Void )

 

 

# Create ( ZN> = 3.0.0 )


This method is used to create migrations for Models / Migrations / directory. After you run the method, you can see how to create the file when you check this directory. The file is created with several predefined commands. You only fill in the up () and down () functions.

Parameters
String $ name The name of the migration to be created.
Int $ version = 0 Version number. If no value is specified, version 000 is assumed.
return Bool
Use of
File:  Controllers / MigrationExample.php
Migration::create('Blog');

Now let's create the version.

File:  Controllers / MigrationExample.php
Migration::create('Blog', 1); 
Created File:  Models / Migrations / BlogVersion / 001.php
Migration::create('Blog', 2);
Created File:  Models / Migrations / BlogVersion / 002.php
Console Usage
php zerocore run-class migration:create Blog 3
Created File: Projects / ProjectDirectory / Models / Migrations / BlogVersion / 003.php

 

 

# Up / Down  ( ZN >= 3.0.0 )


To use the migration files created, you need to import the Migrate prefix to your file . For example , if you assume that you have a Models / Migrations / Blog.php file, you should use MigrateBlog :: up / Migrate :: down () to use this class . So to create a table named Blog, you need to do MigrateBlog :: up () . But here you have to fill in the up function. Pre-defined as up () function, the for droptabl to ()method is defined. You can change this command if you like. The following example shows how to use the migration file and its contents.

Parameters
void
return Mixed
Use of
Created File:  Models / Migrations / Blog.php
class InternalMigrateBlogs extends ZNDatabaseInternalMigration
{
	#---------------------------------------------------------------------------
	# Class/Table Name
	#---------------------------------------------------------------------------
	const table = __CLASS__;

	#---------------------------------------------------------------------------
	# Up 
	#---------------------------------------------------------------------------
	public function up()
	{
		# Queries
	}

	#---------------------------------------------------------------------------
	# Down
	#---------------------------------------------------------------------------
	public function down()
	{
		$this->dropTable();
	}
}

In the above context, the contents of the up and down functions will be configured according to the database operations you plan to perform. The following table shows what you can do in the Up () function.

Migrate Up / Down Methods
up 
CreateTable () It's like using DBForge :: createTable () .
AddColumn () It's like using DBForge :: addColumn () .
modifycolumn () It's like using DBForge :: modifyColumn () .
Down
droptabl A () It's like using DBForge :: dropTable () .
dropcolumn () It's like using DBForge :: dropColumn () .
truncate () It's like using DBForge :: truncate () .
Edited File:  Models / Migrations / Blog.php
public function up()
{
    $this->createTable
    ([
        'id' => [DB::int(11), DB::autoIncrement(), DB::primaryKey()]
    ]);
}

public function down()
{
    $this->dropTable();
}

It can be used in any controller after saving the above file. Yada can be run with console command.

File: Controllers / MigrateExample.php
MigrateBlog::up()   # Create Table
MigrateBlog::down() # Drop Table
Console Usage
php zerocore run-class migrateblog:up
php zerocore run-class migrateblog:down

 

 

# UpAll /DownAll ( ZN >= 5.3.9 )


It is used to run multiple migration files at the same time.

Parameters
String ... $ migratenames The name of the files to be migrated.
return Bool
uses
Migration::upAll('Blogs', 'Users', 'Profiles');
Migration::downAll('Blogs', 'Users', 'Profiles');

 

 

# Version ( ZN >= 3.0.0 )


Migration is used to specify which version to use from migration files created in the database.

Parameters
Int $ Version Version number.
return this
Use of
File: Projects / ProjectDirectory / Controller / MigrateExample.php
MigrateBlog001:up();             # Create Table Version 1
MigrateBlog001:down();           # Drop Table Version 1
MigrateBlog::version(1)->up();   # Create Table Version 1
MigrateBlog::version(1)->down(); # Drop Table Version 1

 

 

# Delete  / DeleteAll ( ZN >= 3.0.0 )


Used to delete created migrations.

Parameters
String $ name It's the name of the migration.
Int $ version = 0 Migration version to be erased.
return Bool
Use of
File: Projects / ProjectDirectory / Controllers / MigrationExample.php
Migration::delete('Blog'); // Deleted Blog File
Migration::delete('Blog', 1); // Deleted Blog 001 File
Migration::delete('Blog', 2); // Deleted Blog 002 File
Migration::deleteAll(); // Deleted All File
Console Usage
php zerocore run-class migration:delete blog 1
php zerocore run-class migration:deleteall