Skip to content

Commit

Permalink
feat: implement migrate cli: metasql m [version]
Browse files Browse the repository at this point in the history
Refs: #30

PR-URL: #33
  • Loading branch information
tshemsedinov committed Sep 15, 2020
1 parent c8110fe commit 1deab98
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 7 deletions.
34 changes: 33 additions & 1 deletion lib/schema-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class DatabaseSchema {
async generate() {
const { name, driver, version } = this.database;
const now = new Date().toISOString().substring(0, 10);
console.log(`Migration ${driver} database: ${name} v${version} (${now})`);
console.log(`Generate migration: ${driver}:${name} v${version} (${now})`);
const ps = await this.getPreviousSchema();
if (!ps) {
console.log('Previous schema is not found in ../history');
Expand All @@ -131,6 +131,32 @@ class DatabaseSchema {
console.log(`Migration up: ${migUp}`);
console.log(`Migration down: ${migDn}`);
}

async migrate(version) {
if (version) console.log(`Migration to this version: ${version}`);
else console.log('Migration to the latest version');
const migPath = path.join(this.path, `migrations`);
const files = await fs.readdir(migPath, { withFileTypes: true });
files.sort((a, b) => a.name < b.name ? -1 : 1);
for (const file of files) {
if (file.isDirectory()) continue;
const { name } = file;
const from = name.lastIndexOf('v') + 1;
const to = name.lastIndexOf('-');
const v = parseInt(name.substring(from, to), 10);
if (!name.endsWith('-up.sql')) continue;
if (!version || v <= version) {
const fileName = path.join(migPath, name);
const sql = await fs.readFile(fileName, 'utf8');
console.log(`Apply script: ${name}`);
await this.execute(sql);
}
}
}

async execute(sql) {
throw new Error(`Method is not implemented: execute(${sql})`);
}
}

DatabaseSchema.implementations = {};
Expand All @@ -145,8 +171,14 @@ const generate = async schemaPath => {
await schema.generate();
};

const migrate = async (schemaPath, version) => {
const schema = await DatabaseSchema.load(schemaPath);
await schema.migrate(version);
};

module.exports = {
DatabaseSchema,
create,
generate,
migrate,
};
8 changes: 8 additions & 0 deletions lib/schema-pg.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const { Client } = require('pg');
const { DatabaseSchema } = require('./schema-db.js');
const { toLowerCamel, toUpperCamel, isUpperCamel } = require('./utils.js');

Expand Down Expand Up @@ -109,6 +110,13 @@ class PgSchema extends DatabaseSchema {
sql.push(');');
return sql.join('\n') + '\n\n' + idx.join('\n');
}

async execute(sql) {
const client = new Client(this.database.connection);
await client.connect();
await client.query(sql);
await client.end();
}
}

DatabaseSchema.implementations.pg = PgSchema;
109 changes: 105 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"typescript": "^4.0.2"
},
"dependencies": {
"@metarhia/common": "^2.2.0"
"@metarhia/common": "^2.2.0",
"pg": "^8.3.3"
}
}
3 changes: 2 additions & 1 deletion sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { SelectBuilder } = require('./lib/select-builder.js');
const { RawBuilder } = require('./lib/raw-builder.js');
const { ParamsBuilder } = require('./lib/params-builder.js');
const { PostgresParamsBuilder } = require('./lib/pg-params-builder.js');
const { create, generate } = require('./lib/schema-db.js');
const { create, generate, migrate } = require('./lib/schema-db.js');
require('./lib/schema-pg.js');

const pg = handler => {
Expand All @@ -23,5 +23,6 @@ module.exports = {
PostgresParamsBuilder,
create,
generate,
migrate,
pg,
};

0 comments on commit 1deab98

Please sign in to comment.