1

I wrote an application in Yii2, and unfortunately, we've lost all the database back ups. What we now have is the application files only. Is there a shorter way of recreating the 98 database tables based on the existing models? I notice that some 22 Tables' Schema were cached under "/app/runtime/cache". Has anyone done something like this?

rob006
  • 21,383
  • 5
  • 53
  • 74
Paul Wakhungu
  • 322
  • 2
  • 18

2 Answers2

5

I have to warn that I have used this ones and it's very helpful, when you need some a$$ saving.

This is a very interesting extension for Gii, that will at least help you restart the database, and then you will be working on it to fix some things.

What it will allow you to do, it's to build migrations from the PHPDoc in your models. The use those migrations to rebuild the database.

You need to install https://github.com/Insolita/yii2-migrik by using composer, if it gives you some trouble use version 2.3 and not 3.

Add

"insolita/yii2-migration-generator": "2.3"

Then open Gii and use "Model and PhpDoc migrations".

  1. Model and PhpDoc migrations

Config

  1. Generated file

Now use Yii2 migration system to build the tables, check the migrations, compare them to the models and add relations, them UP them and will need to fix some stuff. It's not perfect. But it saves time.

https://www.yiiframework.com/doc/guide/2.0/en/db-migrations

Good luck.

Pablo Palacios
  • 2,767
  • 20
  • 37
0

You need to create migration scripts for each model to do database backup.

You need to create database schema in Migration script up() function and need to use following command to manage database.

- migrate                      Manages application migrations.
    migrate/create             Creates a new migration.
    migrate/down               Downgrades the application by reverting old migrations.
    migrate/fresh              Truncates the whole database and starts the migration from the beginning.
    migrate/history            Displays the migration history.
    migrate/mark               Modifies the migration history to the specified version.
    migrate/new                Displays the un-applied new migrations.
    migrate/redo               Redoes the last few migrations.
    migrate/to                 Upgrades or downgrades till the specified version.
    migrate/up (default)       Upgrades the application by applying new migrations.

For more please refer Official Documentation for Yii2 DB Migrations.

Migration script keeps track of database and if it lost again, you can easily create it.

Thanks.

akshaypjoshi
  • 1,245
  • 1
  • 15
  • 24