Skip to content

Commit

Permalink
first
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanflorence committed Nov 29, 2024
0 parents commit 6223d57
Show file tree
Hide file tree
Showing 14 changed files with 1,771 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Publish

on:
push:
tags:
- v[0-9]*

jobs:
build:
runs-on: ubuntu-latest

permissions:
contents: write
id-token: write

steps:
- uses: actions/checkout@v4

- uses: pnpm/action-setup@v4

- uses: actions/setup-node@v4
with:
node-version: '20.x'
registry-url: 'https://registry.npmjs.org'
cache: 'pnpm'

- run: pnpm install

- name: Publish to npm
run: npm publish --provenance --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/.tsimp
/dist
/node_modules
/tmp
/db

/*.tsbuildinfo
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# migralite CHANGELOG

## v0.0.1 (Nov 18, 2024)

- Initial release
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Ryan Florence

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
194 changes: 194 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
# Migralite

A type-safe database migration tool for SQLite. Manage your database schema changes with timestamped migrations through a CLI or API interface.

## Features

- ✨ Simple CLI interface for managing migrations
- 🔄 Bidirectional migrations (up/down)
- 📁 File-based migrations with timestamps
- 🔍 Dry-run mode for validating changes
- 📊 Migration status tracking
- 🔒 Type-safe TypeScript implementation

## Installation

```bash
npm install @ryanflorence/migralite
```

## CLI Usage

The CLI provides several commands for managing your database migrations:

### Create a New Migration

```bash
migralite create -n "add users table"
```

This creates a new migration directory with two files:

- `+.sql`: The up migration (apply changes)
- `-.sql`: The down migration (rollback changes)

### Apply Migrations

Apply all pending migrations:

```bash
migralite up
```

Apply migrations up to a specific one:

```bash
migralite up 20240329123000
```

### Rollback Migrations

Rollback the most recent migration:

```bash
migralite rollback
```

Rollback multiple migrations:

```bash
migralite rollback --steps 3
```

### Check Migration Status

```bash
migralite status
```

### Environment Variables

- `MIGRATIONS_DIR`: Directory for migration files (default: `./db/migrations`)
- `DB_PATH`: Path to SQLite database file (default: `./db/database.db`)

## Programmatic API

The migration tool can also be used programmatically in your Node.js applications.

### Basic Usage

```typescript
import { Migrator } from "@ryanflorence/migralite";
import Database from "better-sqlite3";

let db = new Database("path/to/database.db");
let migrator = new Migrator(db, "path/to/migrations");

// Apply all pending migrations
await migrator.up();

// Rollback last migration
await migrator.rollback();
```

### API Reference

#### `Migrator` Class

```typescript
class Migrator {
constructor(db: Database, dir: string, options?: { dry: boolean });
}
```

##### Methods

###### `create(name: string, upSql: string, downSql: string)`

Creates a new migration.

```typescript
let result = await migrator.create(
"add-users-table",
"CREATE TABLE users (...)",
"DROP TABLE users",
);
// Returns: { name: string, up: string, down: string }
```

###### `up(to?: string)`

Applies pending migrations.

```typescript
// Apply all pending migrations
let applied = await migrator.up();

// Apply up to specific migration
let applied = await migrator.up("20240329123000");
// Returns: string[] (applied migration files)
```

###### `rollback(steps?: number)`

Rolls back applied migrations.

```typescript
// Rollback last migration
let rolledBack = await migrator.rollback();

// Rollback multiple migrations
let rolledBack = await migrator.rollback(3);
// Returns: string[] (rolled back migration files)
```

###### `getPendingMigrations()`

Gets list of pending migrations.

```typescript
let pending = await migrator.getPendingMigrations();
// Returns: string[]
```

###### `getAppliedMigrations()`

Gets list of applied migrations.

```typescript
let applied = await migrator.getAppliedMigrations();
// Returns: MigrationEntry[]
```

#### Types

```typescript
type MigrationEntry = {
id: string;
name: string;
applied_at: string;
};

type MigratorOptions = {
dry: boolean;
};
```

## Migration File Structure

Each migration is stored in a timestamped directory:

```
migrations/
└─ 20240329123000-add-users/
├─ +.sql # Up migration
└─ -.sql # Down migration
```

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

## License

MIT
43 changes: 43 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "@ryanflorence/migralite",
"version": "0.0.0",
"description": "Database migration for SQLite Databases",
"author": "Ryan Florence <[email protected]>",
"repository": {
"type": "git",
"url": "git+https://github.com/ryanflorence/migralite.git"
},
"license": "MIT",
"files": [
"dist",
"LICENSE",
"README.md"
],
"type": "module",
"bin": "./dist/cli.js",
"exports": {
".": "./dist/migralite.js",
"./package.json": "./package.json"
},
"devDependencies": {
"@types/better-sqlite3": "^7.6.12",
"@types/node": "^22.4.1",
"better-sqlite3": "^11.6.0",
"prettier": "^3.3.3",
"tsimp": "^2.0.11",
"typescript": "^5.5.4"
},
"scripts": {
"build": "tsc --project tsconfig.lib.json",
"test": "node --import tsimp/import --test ./src/**/*.spec.ts",
"test:watch": "node --import tsimp/import --test --watch ./src/**/*.spec.ts",
"prepare": "pnpm run build",
"cli": "MIGRATIONS_DIR=tmp/migrations DB_PATH=tmp/db.sqlite node --import tsimp/import ./src/cli.ts"
},
"packageManager": "[email protected]",
"dependencies": {
"arg": "^5.0.2",
"picocolors": "^1.1.1",
"tiny-invariant": "^1.3.3"
}
}
Loading

0 comments on commit 6223d57

Please sign in to comment.