# larvitdbmigration

> node.js database migration tool

Latest version **7.0.227** (published 2026-10-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install larvitdbmigration
pnpm add larvitdbmigration
yarn add larvitdbmigration
bun add larvitdbmigration
```

## Health

**Score 65/100 (B)** — status: active.

Positive: has types; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 7.0.227 |
| Published | 2026-10-26 |
| First published | 2015-11-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 46.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Mikael 'Lilleman' Göransson |
| Maintainers | vktr, lilleman, gagge, klabbekluck, jaggu-snorr, jaggu-larvit-npm-publish |
| Keywords | db, mysql, mariadb, migration, elasticsearch |

## Links

- npm: https://www.npmjs.com/package/larvitdbmigration
- Repository: https://github.com/larvit/larvitdbmigration
- Homepage: https://github.com/larvit/larvitdbmigration#readme
- Issues: https://github.com/larvit/larvitdbmigration/issues
- npm.io page: https://npm.io/package/larvitdbmigration

## Dependencies (3)

- [axios](https://npm.io/package/axios.md) 1.20.0
- [mysql2](https://npm.io/package/mysql2.md) 3.24.6
- [larvitutils](https://npm.io/package/larvitutils.md) 5.1.215

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 7.0.227 (latest) — 2026-10-26
- 7.0.226 — 2026-10-20
- 7.0.225 — 2026-10-12
- 7.0.224 — 2026-10-05
- 7.0.220 — 2026-09-14
- 7.0.219 — 2026-09-07
- 7.0.218 — 2026-08-31
- 7.0.217 — 2026-08-25
- 7.0.216 — 2026-08-10
- 7.0.215 — 2026-08-03
- 7.0.214 — 2026-07-27
- 7.0.213 — 2026-07-13
- 7.0.212 — 2026-07-06
- 7.0.211 — 2026-06-29
- 7.0.188 — 2025-12-01
- … 228 more at https://npm.io/package/larvitdbmigration/versions

## README

[![Build Status](https://github.com/larvit/larvitdbmigration/actions/workflows/ci.yml/badge.svg)](https://github.com/larvit/larvitdbmigration/actions)

# Database migration tool

This is used to keep track of the database structure, content etc, and update it when need be via deploys.

Supported databases:

* MariaDB (and MySQL)
* Elasticsearch

A table/index by default called db_version will be created, containing a single integer.

Scripts will be placed by default in process.cwd()/dbmigration/<version>.js

Each migration script will be ran, and the db_version increased, until no more migration scripts exists.

## Installation

    npm i larvitdbmigration

## Usage

### Application startup script

In your application startup script, do something like this:

#### MariaDb and MySQL

```javascript
'use strict';

const DbMigration = require('larvitdbmigration');
const Db = require('larvitdb');
const dbDriver = new Db({
	host: '127.0.0.1',
	user: 'foo',
	password: 'bar',
	database: 'baz'
});
const dbMigration = new DbMigration({
	dbType: 'mariadb',
	dbDriver,
	tableName: 'db_version', // Optional - used as index name for elasticsearch
	migrationScriptPath: './dbmigration', // Optional
	log // Optional, will use log.silly(), log.debug(), log.verbose(), log.info(), log.warn() and log.error() if given.
});

dbMigration.run().then(() => {
	// Now database is migrated and ready for use!
}).catch(err => {
	throw err;
});
```

#### Elasticsearch

```javascript
'use strict';

const DbMigration = require('larvitdbmigration');
const dbMigration = new DbMigration({
	dbType: 'elasticsearch',
	url: 'http://127.0.0.1:9200',
	indexName: 'db_version', // Optional
	migrationScriptPath: './dbmigration', // Optional
	axios // Optional, will use default axios instance if not specified.
	log // Optional, will use log.silly(), log.debug(), log.verbose(), log.info(), log.warn() and log.error() if given.
});

dbMigration.run().then(() => {
	// Now database is migrated and ready for use!
}).catch(err => {
	throw err;
});
```

### Example migration scripts

Lets say the current database have a table like this:

```SQL
CREATE TABLE bloj (nisse int(11));
```

And in the next deploy we'd like to change the column name "nisse" to "hasse". For this you can do one of two methods:

#### MariaDB / MySQL, Javascript

Create the file process.cwd()/migrationScriptPath/1.js with this content:

```javascript
'use strict';

// Always make the function async (or explicitly return a promise, see elasticsearch example below)
exports = module.exports = async function (options) {
	const {db} = options;

	await db.query('ALTER TABLE bloj CHANGE nisse hasse int(11);');
};
```

#### Elasticsearch

Create the file process.cwd()/migrationScriptPath/1.js with this content:

```javascript
'use strict';

const axios = require('axios');

exports = module.exports = async function (options) {
	const {url, log} = options;

	log.info('Some script-specific logging');

	await axios.put(`${url}/some_index/_mapping`, {
		properties: {
			names: {
				type: 'string',
				position_increment_gap: 100
			}
		}
	});
};
```

#### SQL

_IMPORTANT!_ SQL files will be ignored if a .js file exists.

Create the file process.cwd()/migrationScriptPath/1.sql with this content:

```SQL
ALTER TABLE bloj CHANGE nisse hasse int(11);
```

#### Summary

Tadaaa! Now this gets done once and the version will be bumped to 1. If you then create a script named "2.js" or "2.sql" you might guess what happends. :)

## Changelog
### 7.0.0
* Replaced got with axios (latest versions of got required ES modules and adopters of this lib is not quite ready for it).

### 6.0.0
* Removed locking mechanism for Elasticsearch migrations, there is no support for it in Elasticsearch.
* Rewrote library in TypeScript.
* Updated all dependencies to latest version (and replaced request with got).
* The Elasticsearch driver now only sends url and log instance to the running migration script (instead of the driver instance as it were before).

---
_Source: https://npm.io/package/larvitdbmigration · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
