https://github.com/blackglory/migration-files
🌲 A utility for reading SQL-based migration files.
https://github.com/blackglory/migration-files
library nodejs npm-package typescript
Last synced: 10 months ago
JSON representation
🌲 A utility for reading SQL-based migration files.
- Host: GitHub
- URL: https://github.com/blackglory/migration-files
- Owner: BlackGlory
- License: mit
- Created: 2020-10-19T01:08:25.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-03-23T16:47:32.000Z (over 2 years ago)
- Last Synced: 2025-08-17T04:27:09.422Z (11 months ago)
- Topics: library, nodejs, npm-package, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/migration-files
- Size: 862 KB
- Stars: 3
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
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
```
### findMigrationFilenames
```ts
function findMigrationFilenames(dirname: string): Promise
```
### parseMigrationFile
```ts
function parseMigrationFile(filename: string, content: string): IMigration
```