Skip to content

Commit

Permalink
feat: copy new schema version to .history
Browse files Browse the repository at this point in the history
Refs: #23

PR-URL: #25
  • Loading branch information
tshemsedinov committed Sep 13, 2020
1 parent 1e8dad1 commit 3f8ddeb
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/schema-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const path = require('path');
const fs = require('fs').promises;

const { isUpperCamel } = require('./utils.js');
const { isUpperCamel, cpdir } = require('./utils.js');
const { Schema } = require('./schema.js');

class DatabaseSchema {
Expand Down Expand Up @@ -119,11 +119,17 @@ class DatabaseSchema {
const folder = path.basename(ps.path);
const date = folder.substring(0, folder.lastIndexOf('v') - 1);
console.log(`Previous schema: v${ps.database.version} (${date})`);
if (ps.database.version >= version) {
console.log('You have latest schema version. Migration is not needed.');
return;
}
const newFolder = path.join(this.path, `.history/${date}-v${version}`);
const mig = path.join(this.path, `.migration/${date}-v${version}`);
const migUp = mig + '-up.sql';
const migDn = mig + '-dn.sql';
console.log(`Save history: ${newFolder}`);
await fs.mkdir(newFolder);
await cpdir(this.path, newFolder);
console.log(`Migration up: ${migUp}`);
console.log(`Migration down: ${migDn}`);
}
Expand Down
14 changes: 14 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
'use strict';

const path = require('path');
const fs = require('fs').promises;

const escapeIdentifier = name => `"${name}"`;

const escapeKey = (key, escapeIdentifier) =>
Expand Down Expand Up @@ -30,6 +33,16 @@ const joinIterable = (val, sep) => {
return res;
};

const cpdir = async (from, to) => {
const files = await fs.readdir(from, { withFileTypes: true });
for (const file of files) {
if (file.isDirectory()) continue;
const fromPath = path.join(from, file.name);
const toPath = path.join(to, file.name);
await fs.copyFile(fromPath, toPath);
}
};

const toLowerCamel = s => s.charAt(0).toLowerCase() + s.slice(1);
const toUpperCamel = s => s.charAt(0).toUpperCase() + s.slice(1);
const isUpperCamel = s => !!s && s[0] === s[0].toUpperCase();
Expand All @@ -39,6 +52,7 @@ module.exports = {
escapeKey,
mapJoinIterable,
joinIterable,
cpdir,
toLowerCamel,
toUpperCamel,
isUpperCamel,
Expand Down
25 changes: 25 additions & 0 deletions test/schema/.history/2020-09-11-v3/.database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
({
name: 'example',
description: 'Example database schema',
version: 3,
driver: 'pg',

authors: [
{ name: 'Timur Shemsedinov', email: '[email protected]' },
],

extensions: [
'hstore',
'postgis',
'postgis_topology',
'pg_trgm',
],

connection: {
host: '127.0.0.1',
port: 5432,
database: 'application',
user: 'postgres',
password: 'postgres',
},
});
5 changes: 5 additions & 0 deletions test/schema/.history/2020-09-11-v3/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"parserOptions": {
"sourceType": "module"
}
}
4 changes: 4 additions & 0 deletions test/schema/.history/2020-09-11-v3/City.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
({
country: 'Country',
name: { type: 'string', unique: true },
});
4 changes: 4 additions & 0 deletions test/schema/.history/2020-09-11-v3/Country.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
({
planet: 'Planet',
name: { type: 'string', unique: true },
});
4 changes: 4 additions & 0 deletions test/schema/.history/2020-09-11-v3/District.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
({
city: 'City',
name: { type: 'string', unique: true },
});
3 changes: 3 additions & 0 deletions test/schema/.history/2020-09-11-v3/Planet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
({
name: { type: 'string', unique: true },
});

0 comments on commit 3f8ddeb

Please sign in to comment.