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?
-
didnt you guys wrote the migrations while developing the application? you wont get the Data back but you can restore all the tables if you developed the application conventionaly – Muhammad Omer Aslam Jun 15 '18 at 16:02
-
We were still learning Yii2 then so I didn't do that. – Paul Wakhungu Jun 15 '18 at 18:36
-
1well, you surely will do now, start doing it as i dont think there is any way to do that using models – Muhammad Omer Aslam Jun 15 '18 at 19:01
2 Answers
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".
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.

- 2,767
- 20
- 37
-
This was extremely helpful! At least from here, I can now fix a few missing things like the sizes for some of the columns – Paul Wakhungu Jun 15 '18 at 21:44
-
-
I installed this extension , but I cannot see that "Model and PhpDoc migrations" in gii menu. does it still supported ? – swapnil akolkar Feb 17 '23 at 16:04
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.

- 1,245
- 1
- 15
- 24
-
1the question is not about how to create mugrations or backup, but recovering the table that are dropped – Muhammad Omer Aslam Jun 15 '18 at 19:00
-
Ok I got it. But I am talking about process for recreating the database schema. – akshaypjoshi Jun 15 '18 at 19:04
-
This is useful for future, but I needed a solution for the current problem. – Paul Wakhungu Jun 15 '18 at 19:13