Skip to content

Commit

Permalink
Normalize db names across PG and MySQL
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-north committed Mar 14, 2018
1 parent 7b265e5 commit 88b9cbb
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 75 deletions.
28 changes: 14 additions & 14 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"eslint.enable": false,
"search.exclude": {
"**/dist": true,
"**/node_modules": true,
Expand All @@ -9,26 +8,27 @@
"[handlebars]": {
"editor.formatOnSave": false
},
"database.connections": [
{
"type": "postgres",
"name": "northwind_user@localhost:5432 (postgres)",
"host": "localhost:5432",
"user": "northwind_user",
"password": "thewindisblowing",
"database": "nw_postgresql"
}
],
"sqltools.connections": [
{
"name": "Pg-Northwind",
"name": "PG:Northwind",
"server": "127.0.0.1",
"dialect": "PostgreSQL",
"port": 5432,
"database": "nw_postgresql",
"database": "northwind",
"username": "northwind_user",
"askForPassword": false,
"password": "theWindi$bl0wing",
"connectionTimeout": 15
},
{
"name": "MySQL:Northwind",
"server": "127.0.0.1",
"dialect": "MySQL",
"port": 3306,
"database": "northwind",
"username": "northwind_user",
"askForPassword": false,
"password": "thewindisblowing",
"password": "theWindi$bl0wing",
"connectionTimeout": 15
}
]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ Populate the database with data. This can be done one of two ways
Use the heroku toolbelt posgtres push utility (recommended)

```sh
heroku pg:push nw_postgresql DATABASE_URL --app replace-this-with-your-heroku-app-name
heroku pg:push northwind DATABASE_URL --app replace-this-with-your-heroku-app-name
```

#### If you don't have a local database to push
Expand Down
4 changes: 2 additions & 2 deletions database.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
"driver": "pg",
"port": 5432,
"user": "northwind_user",
"password": "thewindisblowing",
"password": "theWindi$bl0wing",
"host": "localhost",
"database": "nw_postgresql",
"database": "northwind",
"schema": "public"
},
"mysql": {
Expand Down
34 changes: 17 additions & 17 deletions scripts/db/setup/pg.sh
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
#!/usr/bin/env sh
echo "\033[0;32mSetting Up PostgreSQL Database. This may take a few minutes...\033[0m"
echo "\033[1;33m - Removing any existing nw_postgresql database (if this hangs: \"killall -9 psql\")\033[0m"
dropdb nw_postgresql
echo "\033[1;33m - Removing any existing northwind database (if this hangs: \"killall -9 psql\")\033[0m"
dropdb northwind
echo "\033[1;33m - Removing any existing northwind_user user\033[0m"
dropuser northwind_user

echo "\033[1;33m - Creating nw_postgresql database\033[0m"
createdb nw_postgresql
echo "\033[1;33m - Creating northwind database\033[0m"
createdb northwind

echo "\033[1;33m - Ensuring plpgsql extension is installed\033[0m"
psql nw_postgresql -c "CREATE EXTENSION IF NOT EXISTS plpgsql"
psql northwind -c "CREATE EXTENSION IF NOT EXISTS plpgsql"

echo "\033[1;33m - Setting up schema from ./sql/northwind.pg.sql \033[0m"
psql nw_postgresql < ./sql/northwind.pg.sql -q
psql northwind < ./sql/northwind.pg.sql -q
echo "\033[1;33m - Importing data from ./sql/northwind_data.sql (this may take a while)\033[0m"
psql nw_postgresql < ./sql/northwind_data.sql -q
psql northwind < ./sql/northwind_data.sql -q

echo "\033[1;33m - Creating user: northwind_user\033[0m"
psql template1 -c "create user northwind_user;"
psql template1 -c "alter user northwind_user password 'thewindisblowing';"
echo "\033[1;33m - Assigning ownership of nw_postgresql database to northwind_user\033[0m"
psql template1 -c "grant all on DATABASE nw_postgresql to northwind_user;"
psql nw_postgresql -c "GRANT ALL on ALL tables IN SCHEMA public to northwind_user"
psql nw_postgresql -c "alter table \"customerorder\" owner to northwind_user"
psql nw_postgresql -c "alter table \"orderdetail\" owner to northwind_user"
psql nw_postgresql -c "alter table \"product\" owner to northwind_user"
psql nw_postgresql -c "alter table \"employee\" owner to northwind_user"
psql nw_postgresql -c "alter table \"customer\" owner to northwind_user"
psql nw_postgresql -c "alter table \"supplier\" owner to northwind_user"
psql template1 -c "alter user northwind_user password 'theWindi\$bl0wing';"
echo "\033[1;33m - Assigning ownership of northwind database to northwind_user\033[0m"
psql template1 -c "grant all on DATABASE northwind to northwind_user;"
psql northwind -c "GRANT ALL on ALL tables IN SCHEMA public to northwind_user"
psql northwind -c "alter table \"customerorder\" owner to northwind_user"
psql northwind -c "alter table \"orderdetail\" owner to northwind_user"
psql northwind -c "alter table \"product\" owner to northwind_user"
psql northwind -c "alter table \"employee\" owner to northwind_user"
psql northwind -c "alter table \"customer\" owner to northwind_user"
psql northwind -c "alter table \"supplier\" owner to northwind_user"
18 changes: 9 additions & 9 deletions src/db/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ function timeVal(ms: number) {
}
}

export function colorizeQuery(query: string) {
return highlight(sqlFormatter.format(query), {
language: 'sql',
ignoreIllegals: true
});
}

export abstract class SQLDatabase<S extends SQLStatement = any> {
// tslint:disable-next-line:no-empty
public static async setup(): Promise<SQLDatabase<any>> {
Expand Down Expand Up @@ -54,13 +61,6 @@ export abstract class SQLDatabase<S extends SQLStatement = any> {
public abstract getAllViews(): Promise<string[]>;
public abstract getAllMaterializedViews(): Promise<string[]>;

protected colorizeQuery(query: string) {
return highlight(sqlFormatter.format(query), {
language: 'sql',
ignoreIllegals: true
});
}

protected logQuery(query: string, params: JSONArray, t: number) {
let timestring =
t < 5
Expand All @@ -70,7 +70,7 @@ export abstract class SQLDatabase<S extends SQLStatement = any> {
: chalk.bgRedBright.white(timeVal(t));
logger.info(
[
this.colorizeQuery(query) + ` (${timestring})`,
colorizeQuery(query) + ` (${timestring})`,
`${chalk.grey('PARAMS:')} ${JSON.stringify(params)}`
].join('\n')
);
Expand All @@ -97,7 +97,7 @@ export abstract class SQLDatabase<S extends SQLStatement = any> {
return result;
} catch (e) {
logger.error(`Problem running query
${this.colorizeQuery(query)}
${colorizeQuery(query)}
${chalk.yellow('PARAMS:')} ${JSON.stringify(params)}
${chalk.red('ERROR:')} ${e.toString()}`);
throw e;
Expand Down
89 changes: 57 additions & 32 deletions src/migration-utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import * as fs from 'fs';
import * as path from 'path';
import { colorizeQuery } from './db/db';

const MIGRATION_PATH = path.join(__dirname, '..', 'migrations');

export interface Migration {
_meta: {
version: number
version: number;
};
setup(options: any, seedLink: any): void;
up(db: any): void;
Expand All @@ -23,7 +24,6 @@ function getDbType(db: any) {
}

export function sqlFileMigration(name: string): Migration {

let dbm;
let type;
let seed;
Expand All @@ -38,45 +38,70 @@ export function sqlFileMigration(name: string): Migration {
type = dbm.dataType;
seed = seedLink;
Promise = options.Promise;
};
}

function up(db: any) {
let dbType = getDbType(db);
let filePath = path.join(MIGRATION_PATH, 'sqls', `${name}/${dbType}-up.sql`);
let filePath = path.join(
MIGRATION_PATH,
'sqls',
`${name}/${dbType}-up.sql`
);
return new Promise((resolve, reject) => {
fs.readFile(filePath, {
encoding: 'utf-8'
}, (err, data) => {
if (err) { return reject(err); }
// tslint:disable-next-line:no-console
console.log('received data: ' + data);

resolve(data);
});
})
.then((data) => {
return db.runSql(data);
});
};
fs.readFile(
filePath,
{
encoding: 'utf-8'
},
(err, data) => {
if (err) {
return reject(err);
}
// tslint:disable-next-line:no-console
console.log(
`Running UP migration: ${name}\n──────────────────────────────────────────────────────\n${colorizeQuery(
data
)}\n──────────────────────────────────────────────────────`
);
resolve(data);
}
);
}).then(data => {
return db.runSql(data);
});
}

function down(db: any) {
let dbType = getDbType(db);
let filePath = path.join(MIGRATION_PATH, 'sqls', `${name}/${dbType}-down.sql`);
let filePath = path.join(
MIGRATION_PATH,
'sqls',
`${name}/${dbType}-down.sql`
);
return new Promise((resolve, reject) => {
fs.readFile(filePath, {
encoding: 'utf-8'
}, (err, data) => {
if (err) { return reject(err); }
// tslint:disable-next-line:no-console
console.log('received data: ' + data);
fs.readFile(
filePath,
{
encoding: 'utf-8'
},
(err, data) => {
if (err) {
return reject(err);
}
// tslint:disable-next-line:no-console
console.log(
`Running DOWN migration: ${name}\n──────────────────────────────────────────────────────\n${colorizeQuery(
data
)}\n──────────────────────────────────────────────────────`
);

resolve(data);
});
})
.then((data) => {
return db.runSql(data);
});
};
resolve(data);
}
);
}).then(data => {
return db.runSql(data);
});
}

const _meta = {
version: 1
Expand Down

0 comments on commit 88b9cbb

Please sign in to comment.