2021-12-03 17:34:24 +05:00
|
|
|
# Миграции
|
2021-12-11 16:46:04 +05:00
|
|
|
## EF tools
|
|
|
|
https://docs.microsoft.com/ru-ru/ef/core/cli/dotnet
|
|
|
|
|
|
|
|
Установка:
|
|
|
|
```
|
|
|
|
dotnet tool install --global dotnet-ef
|
|
|
|
```
|
|
|
|
Обновление:
|
|
|
|
```
|
|
|
|
dotnet tool update --global dotnet-ef
|
|
|
|
```
|
|
|
|
|
2021-12-07 11:34:06 +05:00
|
|
|
## Создать миграцию
|
2021-09-13 09:39:11 +05:00
|
|
|
```
|
|
|
|
dotnet ef migrations add <MigrationName> --project AsbCloudDb
|
|
|
|
```
|
2021-12-07 11:34:06 +05:00
|
|
|
## Откатить миграцию
|
2021-09-13 09:39:11 +05:00
|
|
|
```
|
2022-01-05 17:50:45 +05:00
|
|
|
dotnet ef migrations remove <MigrationName> --project AsbCloudDb
|
2021-09-13 09:39:11 +05:00
|
|
|
```
|
2021-12-07 11:34:06 +05:00
|
|
|
\<MigrationName> - Name of migration class.
|
2022-01-05 17:50:45 +05:00
|
|
|
После создания миграции обязательно прочитать генерированный код.
|
2021-09-13 09:39:11 +05:00
|
|
|
|
2021-12-03 17:34:24 +05:00
|
|
|
## Применить миграции
|
|
|
|
При старте проекта применяются автоматически
|
2021-09-10 11:28:57 +05:00
|
|
|
```
|
|
|
|
dotnet ef database update --project .\AsbCloudDb
|
2021-12-03 17:34:24 +05:00
|
|
|
```
|
2021-09-29 10:09:40 +05:00
|
|
|
|
2021-12-07 11:34:06 +05:00
|
|
|
# Backup and restore
|
|
|
|
## Backup
|
2021-12-03 17:34:24 +05:00
|
|
|
Terminal:
|
|
|
|
```
|
2022-03-10 17:18:30 +05:00
|
|
|
sudo -u postgres pg_dump -Fc -U postgres postgres -W > dump_2022-03-09.bak
|
2021-12-03 17:34:24 +05:00
|
|
|
```
|
2021-11-17 13:06:48 +05:00
|
|
|
|
2021-12-07 11:34:06 +05:00
|
|
|
## Restore backup
|
|
|
|
### Step 1. (Re)Create DB and schema IF needed.
|
2021-12-03 17:34:24 +05:00
|
|
|
psql:
|
|
|
|
```
|
|
|
|
--drop schema public cascade;
|
|
|
|
CREATE DATABASE postgres;
|
|
|
|
\c postgres
|
|
|
|
create schema public;
|
|
|
|
```
|
|
|
|
|
2022-01-05 17:50:45 +05:00
|
|
|
### Step 2. Init timescaledb and prepare DB to restore
|
2021-12-03 17:34:24 +05:00
|
|
|
```
|
|
|
|
CREATE EXTENSION IF NOT EXISTS timescaledb;
|
|
|
|
SELECT timescaledb_pre_restore();
|
|
|
|
```
|
2021-12-07 11:34:06 +05:00
|
|
|
|
|
|
|
### Step 3. Restore DB, then [Longest operation]
|
2021-12-03 17:34:24 +05:00
|
|
|
Terminal:
|
|
|
|
```
|
2022-01-12 13:33:16 +05:00
|
|
|
sudo -u postgres pg_restore -Fc -d postgres dump_2021-11-26.bak
|
2021-12-03 17:34:24 +05:00
|
|
|
```
|
|
|
|
OR psql:
|
|
|
|
```
|
|
|
|
\! pg_restore -Fc -d postgres dump_2021-11-26.bak
|
|
|
|
```
|
2022-01-12 13:33:16 +05:00
|
|
|
win:
|
|
|
|
pg_restore -Fc -d postgres -U postgres -W dump_2022-01-11.bak
|
2022-01-05 17:50:45 +05:00
|
|
|
Then 'exit restore mode' psql:
|
2021-12-03 17:34:24 +05:00
|
|
|
```
|
|
|
|
SELECT timescaledb_post_restore();
|
|
|
|
```
|