Skip to content

Commit

Permalink
Merge pull request #34 from halvardssm/transition-to-modularity
Browse files Browse the repository at this point in the history
Transition to modularity
  • Loading branch information
halvardssm authored May 30, 2020
2 parents df07b7f + 2eeda66 commit ac5bc6c
Show file tree
Hide file tree
Showing 60 changed files with 2,298 additions and 1,398 deletions.
135 changes: 125 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,61 @@
name: Nessie CI
name: CI

on: [ pull_request, release ]
on:
push:
branches:
- master
pull_request:
branches:
- master
release:
types: [ published ]

jobs:
get-diff:
name: Get Diff
runs-on: ubuntu-latest

outputs:
qb: ${{ steps.qb.outputs.diff }}
cli: ${{ steps.cli.outputs.diff }}

steps:
- name: Clone repo
uses: actions/checkout@master

- name: Check Query Builder
id: qb
uses: technote-space/[email protected]
with:
PREFIX_FILTER: |
query-builder/
tests/query-builder/
tests/query-builder-migrations/
SUFFIX_FILTER: .ts

- name: Check CLI
id: cli
uses: technote-space/[email protected]
with:
PREFIX_FILTER: |
cli.ts
deps.ts
mod.ts
cli/**
clients/**
tests/cli/**
cli:
name: Run CLI
name: Test CLI
needs: get-diff
if: needs.get-diff.outputs.cli
runs-on: ubuntu-latest

steps:
- name: Install deno
uses: denolib/setup-deno@master
with:
deno-version: 1.0.0
deno-version: 1.0.3

- name: Nessie Init
run: deno run --allow-read --allow-write --allow-net https://raw.githubusercontent.com/$URL_PATH/cli.ts init
Expand All @@ -23,8 +67,10 @@ jobs:
env:
URL_PATH: ${{github.event.pull_request.head.repo.full_name||github.repository}}/${{github.event.pull_request.head.ref||'master'}}

tests:
name: Run tests
cli-migrations:
name: Test CLI Migrations
needs: get-diff
if: needs.get-diff.outputs.cli
runs-on: ubuntu-latest

services:
Expand All @@ -41,6 +87,7 @@ jobs:
--health-retries 5
ports:
- 5000:5432

mysql:
image: mysql
env:
Expand All @@ -57,13 +104,81 @@ jobs:
steps:
- name: Clone repo
uses: actions/checkout@master

- name: Install deno
uses: denolib/setup-deno@master
with:
deno-version: 1.0.0
- name: Check formatting
run: deno fmt --check
deno-version: 1.0.3

- name: Create SQLite db
run: make db-sqlite-start

- name: Run tests
run: make test
run: make test-cli-migrations

query-builder:
name: Test Query Builder
needs: get-diff
if: needs.get-diff.outputs.qb
runs-on: ubuntu-latest

steps:
- name: Clone repo
uses: actions/checkout@master

- name: Install deno
uses: denolib/setup-deno@master
with:
deno-version: 1.0.3

- name: Test query builder
run: make test-qb

query-builder-migrations:
name: Test Query Builder Migration
needs: [get-diff, query-builder]
if: needs.get-diff.outputs.qb
runs-on: ubuntu-latest

services:
postgres:
image: postgres
env:
POSTGRES_USER: root
POSTGRES_PASSWORD: pwd
POSTGRES_DB: nessie
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5000:5432

mysql:
image: mysql
env:
MYSQL_ALLOW_EMPTY_PASSWORD: true
MYSQL_DATABASE: nessie
options: >-
--health-cmd="mysqladmin ping"
--health-interval=10s
--health-timeout=5s
--health-retries=3
ports:
- 5001:3306

steps:
- name: Clone repo
uses: actions/checkout@master

- name: Install deno
uses: denolib/setup-deno@master
with:
deno-version: 1.0.3

- name: Create SQLite db
run: make db-sqlite-start

- name: Test query builder
run: make test-qb-migrations
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,5 @@ $RECYCLE.BIN/

# Custom rules (everything added below won't be overriden by 'Generate .gitignore File' if you use 'Update' option)
.vscode
data
data
/migrations
18 changes: 14 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,21 @@ migrate:
rollback:
deno run --allow-net --allow-read cli.ts rollback -c ${CONFIG_FILE}

test:
deno test --allow-write --allow-run --allow-read
test-clean: db-all-restart sleeper test
test-clean: db-all-restart sleeper
test-all:
make test-qb & make test-clean
make test-qb-migrations
make test-clean
make test-cli-migrations

test-qb:
deno test tests/query-builder
test-qb-migrations:
deno test --allow-write --allow-run --allow-read tests/query-builder-migrations
test-cli-migrations:
deno test --allow-write --allow-run --allow-read tests/cli
sleeper:
sleep 30s
sleep 45s

db-all-restart: db-all-stop db-all-start
db-all-start: db-pg-start db-mysql-start db-sqlite-start
Expand Down
75 changes: 57 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
![ci](https://github.com/halvardssm/deno-nessie/workflows/ci/badge.svg)
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/nessie/mod.ts)
[![(Deno)](https://img.shields.io/badge/deno-1.0.0-green.svg)](https://deno.land)
[![(Deno)](https://img.shields.io/badge/deno-1.0.3-green.svg)](https://deno.land)

# Nessie

<p align="center"><img src="./.github/logo.png" alt="Nessie logo" width="200" height="200"></p>

A database migration tool for [deno](https://deno.land) inspired by [Laravel](https://github.com/laravel/laravel). Supports PostgreSQL and MySQL, soon: SQLite. See [documentation](https://doc.deno.land/https/deno.land/x/nessie/mod.ts).
A modular database migration tool for [Deno](https://deno.land) inspired by [Laravel](https://github.com/laravel/laravel). Supports PostgreSQL, MySQL and SQLite.

See documentation for the [query builder](https://doc.deno.land/https/deno.land/x/nessie/qb.ts). \
See documentation for the [clients](https://doc.deno.land/https/deno.land/x/nessie/mod.ts).

## Supported databases

* [x] PostgreSQL
* [x] MySQL - Currently it works with password for 5.*, but for >=8 you have to send a blank password, see [Deno MySQL](https://deno.land/x/mysql/) for version support
* [x] SQLite

If you have a database system you would like to see in this list, feel free to make an issue or create a pr with your implementation.
If you have a database system you would like to see in this list, feel free to make an issue or create a pr with your implementation.

You can see examples of how to make a client plugin in the [clients folder](./clients) or in the section [How to make a client](#how-to-make-a-client).

## Usage

Expand All @@ -26,15 +31,19 @@ If you have a database system you would like to see in this list, feel free to m

```deno run --allow-net --allow-read --allow-write https://deno.land/x/nessie/cli.ts make create_users```

* `migrate`: Run migration - will migrate all migrations in your migration folder (sorted by timestamp) newer than the latest migration in your db
* `migrate [amount?]`: Run migration - will migrate your migrations in your migration folder (sorted by timestamp) newer than the latest migration in your db. Amount defines how many migrations, defaults to all available if not set.

```deno run --allow-net --allow-read https://deno.land/x/nessie/cli.ts migrate```

```deno run --allow-net --allow-read https://deno.land/x/nessie/cli.ts migrate 1```

```deno run --allow-net --allow-read https://deno.land/x/nessie/cli.ts migrate -c ./nessie.config.ts```

* `rollback`: Rollback - will rollback the latest migration
* `rollback [amount?]`: Rollback - will rollback your migrations. Amount defines how many migrations, defaults to 1 if not set.

```deno run --allow-net --allow-read https://deno.land/x/nessie/cli.ts rollback```

```deno run --allow-net --allow-read https://deno.land/x/nessie/cli.ts rollback 2```

### Flags

Expand All @@ -57,18 +66,18 @@ All contributions are welcome, make sure to read the [contributing guideline](./
`nessie.config.ts`

```ts
import { nessieConfigType } from "https://deno.land/x/nessie/mod.ts";
import { ClientPostgreSQL, nessieConfig } from "https://deno.land/x/nessie/mod.ts";

const migrationFolder = "./migrations";

const config: nessieConfigType = {
migrationFolder: "./migrations",
connection: { // These are the connection option from their respective db clients, will differ
host: "localhost",
const config: nessieConfig = {
client: new ClientPostgreSQL(migrationFolder, {
database: "nessie",
hostname: "localhost",
port: 5432,
user: "root",
password: "pwd",
name: "nessie",
},
dialect: "pg",
}),
};

export default config;
Expand All @@ -77,25 +86,55 @@ export default config;
Minimal example of a migration file

```ts
import { Schema } from "https://deno.land/x/nessie/mod.ts";
export const up = (): string => {
return "CREATE TABLE table1 (id int);";
};

export const down = (): string => {
return "DROP TABLE table1";
};
```

Using the native query builder

```ts
import { Schema, dbDialects } from "https://deno.land/x/nessie/qb.ts";

export const up = (schema: Schema): void => {
schema.create("users", (table) => {
const dialect: dbDialects = "mysql"

export const up = (): string => {
let query = new Schema(dialect).create("users", (table) => {
table.id();
table.string("name", 100).nullable();
table.boolean("is_true").default("false");
table.custom("custom_column int default 1");
table.timestamps();
});

schema.queryString(
query += new Schema(dialect).queryString(
"INSERT INTO users VALUES (DEFAULT, 'Deno', true, 2, DEFAULT, DEFAULT);",
);

return query
};

export const down = (schema: Schema): void => {
schema.drop("users");
return new Schema(dialect).drop("users");
};
```

See example folder for more

## How to make a client

A client needs to extend [AbstractClient](./clients/AbstractClient.ts) and implement the [ClientI interface](./clients/AbstractClient.ts).

`query`: Takes a query string or array of query strings and sends them of to the batabase for execution. Should return whatever the database responds.

`prepare`: Will be run when the migration or rollback commands are executed. This should create the connection, set up the `nessie_migrations` table and prepare the database for incoming migrations.

`migrate`: Takes a number as an optional input, will default to all files if not set. Will run `Math.min(amount, numberOfFiles)` migration files. Only handles the `up` method.

`rollback`: Takes a number as an optional input, will default to 1 if not set. Will run `Math.min(amount, numberOfFiles)` migration files. Only handles the `down` method.

`close`: Will be the last method run before the program is finished. This should close the database connection.
Loading

0 comments on commit ac5bc6c

Please sign in to comment.