Skip to content

Commit

Permalink
Merge pull request #21 from halvardssm/fix_path_resolve
Browse files Browse the repository at this point in the history
fix path resolve
  • Loading branch information
halvardssm authored May 15, 2020
2 parents a9902af + 108a103 commit f445e11
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 95 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ jobs:
with:
deno-version: 1.0.0

- name: Nessie Init
run: deno run --allow-read --allow-write --allow-net https://raw.githubusercontent.com/$URL_PATH/cli.ts init
env:
URL_PATH: ${{github.event.pull_request.head.repo.full_name||github.repository}}/${{github.event.pull_request.head.ref||'master'}}

- name: Create migration
run: deno run --allow-read --allow-write --allow-net https://raw.githubusercontent.com/$URL_PATH/cli.ts make test
env:
URL_PATH: ${{github.event.pull_request.head.repo.full_name||github.repository}}/${{github.event.pull_request.head.ref||'master'}}

tests:
name: Run tests
runs-on: ubuntu-latest
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ If you have a database system you would like to see in this list, feel free to m

## Usage

* `init`: Generates a `nessie.config.ts` file

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

* `make [name]`: Create migration

```deno run --allow-net --allow-read --allow-write https://deno.land/x/nessie/cli.ts make create_users```
Expand Down
20 changes: 17 additions & 3 deletions cli.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { State } from "./cli/state.ts";
import { Denomander } from "./deps.ts";
import { Denomander, resolve } from "./deps.ts";

const initDenomander = () => {
const program = new Denomander({
app_name: "Nessie Migrations",
app_description: "A database migration tool for Deno.",
app_version: "0.1.0",
app_version: "0.4.1",
});

program
Expand All @@ -14,6 +14,7 @@ const initDenomander = () => {
"-c --config",
"Path to config file, will default to ./nessie.config.ts",
)
.command("init", "Generates the config file")
.command("make [migrationName]", "Creates a migration file with the name")
.command("migrate", "Migrates one migration")
.command("rollback", "Rolls back one migration");
Expand All @@ -23,13 +24,26 @@ const initDenomander = () => {
return program;
};

const initNessie = async () => {
const responseFile = await fetch(
"https://deno.land/x/nessie/cli/templates/config.ts",
);

await Deno.writeTextFile(
resolve(Deno.cwd(), "nessie.config.ts"),
await responseFile.text(),
);
};

const run = async () => {
const prog = initDenomander();

const state = await new State(prog).init();

try {
if (prog.make) {
if (prog.init) {
await initNessie();
} else if (prog.make) {
await state.makeMigration(prog.make);
} else {
await state.initClient();
Expand Down
58 changes: 32 additions & 26 deletions cli/state.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
import {
ClientConfig,
Denomander,
ConnectionOptions,
Denomander,
MySQLClient,
open,
PGClient,
resolve,
} from "../deps.ts";
import stdConfig from "../nessie.config.ts";
import { dbDialects, nessieConfig } from "../mod.ts";
import { MySQL } from "./mysql.ts";
import { PGSQL } from "./pgsql.ts";
import { SQLite } from "./sqlite.ts";
import { ClientI, ClientTypes } from "./utils.ts";
import { ClientI, ClientTypes, parsePath } from "./utils.ts";

const STD_CONFIG_FILE = "nessie.config.ts";
const stdConfig: nessieConfig = {
migrationFolder: "./migrations",
connection: {
database: "nessie",
hostname: "localhost",
port: 5432,
user: "root",
password: "pwd",
},
dialect: "pg",
};

export class State {
private enableDebug: boolean;
Expand All @@ -24,30 +37,33 @@ export class State {

constructor(prog: Denomander) {
this.enableDebug = prog.debug;
this.configFile = prog.config;
this.configFile = parsePath(prog.config || STD_CONFIG_FILE);

this.debug(prog, "Program");
this.debug(this, "State");
}

async init() {
let config: nessieConfig = stdConfig as nessieConfig;

let config: nessieConfig = stdConfig;
let configRaw;
try {
const rawConfig = await import(
this._parsePath(this.configFile, "nessie.config.ts")
);

config = rawConfig.default;
this.debug("Checking config path");
configRaw = await import(this.configFile);
config = configRaw.default;
} catch (e) {
this.debug("Using standard config");
try {
this.debug(e, "Checking project root");

configRaw = await import(parsePath(STD_CONFIG_FILE));
config = configRaw.default;
} catch (er) {
this.debug(e, "Using standard config");
}
} finally {
this.debug(config, "Config");

this.migrationFolder = this._parsePath(
config.migrationFolder,
"migrations",
);
this.migrationFolder = resolve(config.migrationFolder || "migrations");

this.connection = config.connection;
this.dialect = config.dialect || "pg";

Expand Down Expand Up @@ -109,14 +125,4 @@ export class State {
console.log(output);
}
}

private _parsePath(path: string | undefined, defaultFolder?: string): string {
return !path
? `${Deno.cwd()}${defaultFolder ? `/${defaultFolder}` : ""}`
: path?.startsWith("/")
? path
: path.startsWith("./")
? `${Deno.cwd()}${path.substring(1)}`
: `${Deno.cwd()}/${path}`;
}
}
31 changes: 31 additions & 0 deletions cli/templates/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const configPg = {
migrationFolder: `./migrations`,
connection: {
database: "nessie",
hostname: "localhost",
port: 5432,
user: "root",
password: "pwd",
},
dialect: "pg",
};

const configMySql = {
migrationFolder: `./migrations`,
connection: {
hostname: "localhost",
port: 3306,
username: "root",
// password: "pwd", // uncomment this line for <8
db: "nessie",
},
dialect: "mysql",
};

const configSqLite = {
migrationFolder: `./migrations`,
connection: "sqlite.db",
dialect: "sqlite",
};

export default configPg;
2 changes: 1 addition & 1 deletion cli/templates/migration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Schema } from "https://deno.land/x/nessie/mod.ts";

export const up = (scema: Schema): void => {
export const up = (schema: Schema): void => {
};

export const down = (schema: Schema): void => {
Expand Down
16 changes: 13 additions & 3 deletions cli/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MySQLClient, PGClient, SQLiteClient } from "../deps.ts";
import { MySQLClient, PGClient, resolve, SQLiteClient } from "../deps.ts";
import Schema from "../src/Schema.ts";
import { State } from "./state.ts";

Expand Down Expand Up @@ -28,6 +28,16 @@ export type ClientTypes = {
sqlite?: SQLiteClient;
};

export const parsePath = (...path: string[]): string => {
if (
path.length === 1 &&
(path[0]?.startsWith("http://") || path[0]?.startsWith("https://"))
) {
return path[0];
}
return "file://" + resolve(...path);
};

export const filterAndSortFiles = (
files: Deno.DirEntry[],
queryResult: string | undefined,
Expand All @@ -50,7 +60,7 @@ export const traverseAndMigrateFiles = async (
) => {
if (files.length > 0) {
for await (const file of files) {
let { up } = await import(`${state.migrationFolder}/${file.name}`);
let { up } = await import(parsePath(state.migrationFolder, file.name));

const schema = new Schema(state.dialect);

Expand Down Expand Up @@ -85,7 +95,7 @@ export const traverseAndRollbackFiles = async (
queryfn: (query: string) => any,
) => {
if (typeof fileName === "string") {
let { down } = await import(`${state.migrationFolder}/${fileName}`);
let { down } = await import(parsePath(state.migrationFolder, fileName));

const schema = new Schema(state.dialect);

Expand Down
2 changes: 2 additions & 0 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Denomander from "https://deno.land/x/[email protected]/mod.ts";

export { relative, resolve } from "https://deno.land/[email protected]/path/mod.ts";
export { readJson } from "https://deno.land/[email protected]/fs/read_json.ts";
export {
assert,
assertArrayContains,
Expand Down
38 changes: 9 additions & 29 deletions nessie.config.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,11 @@
const configPg = {
migrationFolder: `${Deno.cwd()}/migrations`,
connection: {
database: "nessie",
hostname: "localhost",
port: 5000,
user: "root",
password: "pwd",
export default {
"migrationFolder": "./migrations",
"connection": {
"database": "nessie",
"hostname": "localhost",
"port": 5000,
"user": "root",
"password": "pwd",
},
dialect: "pg",
"dialect": "pg",
};

const configMySql = {
migrationFolder: `${Deno.cwd()}/migrations`,
connection: {
hostname: "localhost",
port: 5001,
username: "root",
// password: "pwd", // uncomment this line for <8
db: "nessie",
},
dialect: "mysql",
};

const configSqLite = {
migrationFolder: `${Deno.cwd()}/migrations`,
connection: "sqlite.db",
dialect: "sqlite",
};

export default configPg;
20 changes: 8 additions & 12 deletions tests/config/mysql.config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { nessieConfig } from "../../mod.ts";

const configMySql: nessieConfig = {
migrationFolder: `${Deno.cwd()}/tests/migrations`,
connection: {
hostname: "localhost",
port: 5001,
username: "root",
db: "nessie",
export default {
"migrationFolder": "./tests/migrations",
"connection": {
"hostname": "localhost",
"port": 5001,
"username": "root",
"db": "nessie",
},
dialect: "mysql",
"dialect": "mysql",
};

export default configMySql;
22 changes: 9 additions & 13 deletions tests/config/pg.config.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { nessieConfig } from "../../mod.ts";

const configPg: nessieConfig = {
migrationFolder: `${Deno.cwd()}/tests/migrations`,
connection: {
database: "nessie",
hostname: "localhost",
port: 5000,
user: "root",
password: "pwd",
export default {
"migrationFolder": "./tests/migrations",
"connection": {
"database": "nessie",
"hostname": "localhost",
"port": 5000,
"user": "root",
"password": "pwd",
},
dialect: "pg",
"dialect": "pg",
};

export default configPg;
12 changes: 4 additions & 8 deletions tests/config/sqlite.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { nessieConfig } from "../../mod.ts";

const configSqLite: nessieConfig = {
migrationFolder: `${Deno.cwd()}/tests/migrations`,
connection: `tests/data/sqlite.db`,
dialect: "sqlite",
export default {
"migrationFolder": "./tests/migrations",
"connection": "./tests/data/sqlite.db",
"dialect": "sqlite",
};

export default configSqLite;

0 comments on commit f445e11

Please sign in to comment.