Migrations
Last updated
Last updated
When you are maintaining a software with database, it's obvious that you may need database changes.
You may create new tables
You may add new columns to existing tables
You may create new Objects
You may create new Object Specs for existing Objects
You may update Objects
You may update Object Specs
Migrations are placed in Migration
folder for each Module. If you need Migrations for your App, than you can use Migrations in app/Migration
folder.
There are two types of Migrations
Install migrations run when the system is installed to a new database. Which means that, you can assume that, target database is empty when you add a new migration.
[!TIP] As an example: If you want to change name of a table, you can directly rename it if it already exists in Install script. Since Installation only runs for empty databases, it will not have a table with the previous name.
You can use the following command to run migrations:
Install script aims to install an application from scratch. You may want to generate or update Install script with the following command:
This command will update app/Migration/Install.php with the migrations of all Data Types in the active database.
You can also filter table names
will generate migration for Data Type: users and faqs tables.
You may also use wildcards for table names
will generate a single migration file with tables starting with user*.
Upgrade migrations should run after each deployment. Upgrade migrations make it easy to maintain structure of your Database. Besides creating new Tables, Objects, Indexes, you can also delete or update existing ones using Upgrades.
You can use the following command to run migrations:
Example:
You can add Unique or Normal index to the Objects you have created.
You can also use Migrations in app
Folder. app
is considered as a Module inside of the System. Which means that, you can update your modules version from app/module.yaml
and you can write down Migration scripts in app/Migration/Install.php
and app/Migration/Upgrade.php
files.
Object Migrations works with upsert strategy. Which means that, when you write down a migration, if the target Object already exist, it will update the existing Object. If it doesn't exist, it will insert new Object.
[!WARNING] If you create a migration for an existing object and you don't define all of the existing Specs, it
won't
delete existing Object Specs.
[!WARNING] Objects check existence with
table_name
. If you want to rename an existing Object, then you can check Renaming Object Table Names section.
Example:
When creating additional Object Specs, you can choose the relative position of the newly created Object Spec.
Example:
will place votes
Object Spec, after test
column and will place vote_count
Object Spec, before test_2
column.
[!WARNING] By default, new Object Specs are placed Left Side Bottom. If target Column Name is not found, Default rules are applied.
You can create admin menus while creating an Object using Migrations.
With Parent Menu
Example:
Without Parent Menu
If you want to create a Main Menu to link to Object Listing Page, you can use the following example:
Example:
You can rename table names of existing Objects.
[!TIP] Renaming table name will change the name of Object. You may use
setName
function to override auto-generated object name
You can create new Object Specs for existing Objects using Migrations.
Example:
[!TIP] You can also check Create or Update Object section for adding new Specs to existing Objects. It will also upsert Object Specs.
You can remove an existing Object Specs using Migrations.
Example:
will remove Object Spec with column name: test_column_name
of the Object with table name: test_table_name
[!WARNING] This function removes column from database. Please beware that all data in the column will be destroyed.
Sometimes you may need different column types in database, for example: default column type for integers is int(11)
but you may want to increase the limit and update it to bigint(20)
. You can use columnType
function to update column type.
Example:
For migrations not to run more than once, you can use version numbers. Butterfly uses to version the Modules. Version numbers are written in module.yaml files. For more information about Modules you can check