# migration-files

> Utilities for SQL-based migration files

Latest version **0.4.3** (published 2024-03-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install migration-files
pnpm add migration-files
yarn add migration-files
bun add migration-files
```

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.4.3 |
| Published | 2024-03-23 |
| First published | 2022-11-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 18.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | BlackGlory |
| Maintainers | black_glory |
| Keywords | sql, migration, migrations, up, down |

## Links

- npm: https://www.npmjs.com/package/migration-files
- Repository: https://github.com/BlackGlory/migration-files
- Homepage: https://github.com/BlackGlory/migration-files#readme
- Issues: https://github.com/BlackGlory/migration-files/issues
- npm.io page: https://npm.io/package/migration-files

## Dependencies (2)

- [extra-sort](https://npm.io/package/extra-sort.md) ^0.1.1
- [@blackglory/prelude](https://npm.io/package/@blackglory/prelude.md) ^0.1.8

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.4.3 (latest) — 2024-03-23
- 0.4.2 — 2024-01-20
- 0.4.1 — 2023-06-11
- 0.4.0 — 2022-11-27
- 0.3.0 — 2022-11-27

## README

# migration-files
Utilities for SQL-based migration files.

## Install
```sh
npm install --save migration-files
# or
yarn add migration-files
```

## Usage
```ts
import { map } from 'extra-promise'
import { findMigrationFilenames, readMigrationFile } from 'migration-files'

const filenames = await findMigrationFilenames('./migrations')
const migrations = await map(filenames, readMigrationFile)
```

## Migration format
001-initial.sql
```sql
--------------------------------------------------------------------------------
-- Up
--------------------------------------------------------------------------------
CREATE TABLE test (
  id INTEGER PRIMARY KEY
);

--------------------------------------------------------------------------------
-- Down
--------------------------------------------------------------------------------
DROP TABLE test;
```

002-add name column.sql
```sql
--------------------------------------------------------------------------------
-- Up
--------------------------------------------------------------------------------
ALTER TABLE test
  ADD COLUMN name TEXT;

--------------------------------------------------------------------------------
-- Down
--------------------------------------------------------------------------------
-- https://www.sqlite.org/faq.html#q11
BEGIN TRANSACTION;
  CREATE TEMPORARY TABLE test_backup (
    id   INTEGER PRIMARY KEY
  , name TEXT
  );
  INSERT INTO test_backup
        SELECT id, name FROM test;
  DROP TABLE test;
  CREATE TABLE test (
    id
  );
  INSERT INTO test
        SELECT id FROM test_backup;
  DROP TABLE test_backup;
COMMIT;
```

## API
```ts
interface IMigration {
  filename: string
  version: number
  name: string
  up: string
  down: string
}
```

### readMigrationFile
```ts
function readMigrationFile(filename: string): Promise<IMigration>
```

### findMigrationFilenames
```ts
function findMigrationFilenames(dirname: string): Promise<string[]>
```

### parseMigrationFile
```ts
function parseMigrationFile(filename: string, content: string): IMigration
```

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