# Upgrade

In general NextGIS Web upgrade process consist of the following steps:

* Stop services
* Create backup
* Upgrade packages
* Apply migrations
* Start services

## Stopping services and creating backup

Previous sections describe services and backups. For example, it may look as
follows:

```none
# systemctl stop ngw.service ngw-maintenance.timer
# su ngw -c "nextgisweb backup"
```

## Upgrading packages

Switch to `ngw` user (do not forget about virtualenv and `NEXTGISWEB_CONFIG`
environment variable if you hadn’t added them to `.bashrc`):

```none
# su ngw
$ cd
```

Change directory to `package/nextgisweb`, pull latest changes, and look at
available version tags:

```none
$ cd package/nextgisweb
$ git pull
$ git tag -l '*.*.*'
4.3.0
4.3.1
4.3.2
4.4.0
```

Of course, you can use any other version, including a `master` branch, but
here we consider an upgrade to the `4.4.0` version as an example. So check it
out, upgrade the package in virtualenv and recompile i18n translations:

```none
$ git checkout 4.4.0
$ cd ..
$ pip install -e nextgisweb
$ nextgisweb-i18n -p nextgisweb compile
```

And then, go to the home directory and update Node.js and pnpm project
environment with workspaces, and build necessary files:

```none
$ cd
$ nextgisweb jsrealm install
$ pnpm run build
```

Repeat these steps with additional packages such as `nextgisweb_qgis` and
`nextgisweb_mapserver`. Some of them may require additional steps, such as
upgrading system software.

After that you return to `ngw` home directory:

```none
$ cd ~
```

<a id="applying-migrations"></a>

## Applying migrations

### Upgrading from 3.7.0 or higher

Automatic database migrations were introduced in version `3.7.0`. If you’re
upgrading from `3.7.0` or higher, check required migrations with:

```none
$ nextgisweb migration upgrade
```

And then apply them with:

```none
$ nextgisweb migration upgrade --no-dry-run
```

### Upgrading from previous versions

In case of upgrade from a version before `3.7.0`, you should manually apply
SQL migrations from `package/nextgisweb/migration` directory before you run
automatic migration.

Files in that directory are prefixed by date, so you have to run them one-by-one
in historical order. The command `nextgisweb sql -f` can help you with that:

```none
$ nextgisweb sql -f package/nextgisweb/migration/2001-01-01-filename.sql
```

Don’t worry about applying SQL-migration twice. If it’s already applied, it will
fail with an error and doesn’t change any data.

When all SQL-migrations are applied you should run automatic migrations with
commands given above:

```none
$ nextgisweb migration upgrade
$ nextgisweb migration upgrade --no-dry-run
```

## Starting services

Now you can start services back. It may look as follows:

```none
# systemctl start ngw.service ngw-maintenance.timer
```

## Upgrading PostgreSQL and PostGIS

PostgreSQL and PostGIS can be upgraded as described in corresponding docs,
including dump and restore major upgrades. But keep in mind the following:

- After dump and restore during PostgreSQL upgrade, ownership of PostGIS tables
  (`spatial_ref_sys`, etc.) can be lost. You will have to set it up again.
- NextGIS Web manages the `spatial_ref_sys` table, but it will be overwritten
  while restoring from a dump or upgrading PostGIS. So that, you will need to
  execute `nextgisweb spatial_ref_sys sync_postgis` command after restoration.
