Compare commits

..

No commits in common. "master" and "v1.6.5" have entirely different histories.

14 changed files with 29 additions and 169 deletions

2
.gitignore vendored
View File

@ -1,2 +1,2 @@
.DS_Store
node_modules
node_modules

View File

@ -6,14 +6,6 @@ mockFunctions:
- mockTime
- mockUtcTime
sumViews: true
defaultDefiner: root@localhost
localRemotes:
- local
- docker
deprecMarkRegex: __$
deprecCommentRegex: ^@deprecated [0-9]{4}-[0-9]{2}-[0-9]{2}
deprecDateRegex: '[0-9]{4}-[0-9]{2}-[0-9]{2}'
deprecRetentionPeriod: 60 # Days
privileges:
userTable: global_priv
userWhere: >-

View File

@ -83,7 +83,7 @@ module.exports = class Dumper {
}
async runDump(command, args) {
const iniPath = path.join('remotes', this.opts.iniFile);
const iniPath = path.join(this.opts.subdir || '', 'remotes', this.opts.iniFile);
const myArgs = [
`--defaults-file=${iniPath}`
];

2
myt-clean.js Executable file → Normal file
View File

@ -10,7 +10,7 @@ class Clean extends Command {
static usage = {
description: 'Cleans old applied versions',
params: {
purge: 'Whether to remove non-existent scripts from DB log'
purge: 'Wether to remove non-existent scripts from DB log'
}
};

View File

@ -43,7 +43,7 @@ class Create extends Command {
const params = {
schema,
name,
definer: opts.defaultDefiner
definer: 'root@localhost'
};
switch (opts.type) {

2
myt-dump.js Executable file → Normal file
View File

@ -9,7 +9,7 @@ class Dump extends Command {
description: 'Dumps structure and fixtures from remote',
params: {
lock: 'Whether to lock tables on dump',
triggers: 'Whether to include triggers into dump'
triggers: 'Wether to include triggers into dump'
},
operand: 'remote'
};

0
myt-fixtures.js Executable file → Normal file
View File

29
myt-push.js Executable file → Normal file
View File

@ -15,9 +15,9 @@ class Push extends Command {
description: 'Apply changes into database',
params: {
force: 'Answer yes to all questions',
commit: 'Whether to save the commit SHA into database',
commit: 'Wether to save the commit SHA into database',
sums: 'Save SHA sums of pushed objects',
triggers: 'Whether to exclude triggers, used to generate local DB'
triggers: 'Wether to exclude triggers, used to generate local DB'
},
operand: 'remote'
};
@ -156,17 +156,7 @@ class Push extends Command {
throw new Error(`Cannot obtain exclusive lock, used by connection ${isUsed}`);
}
const [[scheduler]] = await conn.query(`SELECT @@event_scheduler state`);
if (scheduler.state === 'ON') await eventScheduler(false);
async function eventScheduler(isActive) {
await conn.query(
`SET GLOBAL event_scheduler = ${isActive ? 'ON' : 'OFF'}`
);
}
async function releaseLock() {
if (scheduler.state === 'ON') await eventScheduler(true);
await conn.query(`DO RELEASE_LOCK('myt_push')`);
}
@ -254,15 +244,13 @@ class Push extends Command {
let apply = false;
if (!version) {
if (!version)
this.emit('version', version, versionDir, 'wrongDirectory');
continue;
} else if (version.number.length != dbVersion.number.length) {
else if (version.number.length != dbVersion.number.length)
this.emit('version', version, versionDir, 'badVersion');
continue;
}
else
apply = version.apply;
apply = version.apply;
if (apply) showLog = true;
if (showLog) this.emit('version', version, versionDir);
if (!apply) continue;
@ -371,12 +359,9 @@ class Push extends Command {
const oldSql = await engine.fetchRoutine(type, schema, name);
const oldSum = engine.getShaSum(type, schema, name);
const localRemote = opts.remote == null
|| opts.localRemotes?.indexOf(opts.remote) !== -1;
const isMockFn = type == 'function'
&& schema == opts.versionSchema
&& localRemote
&& opts.remote == 'local'
&& opts.mockDate
&& opts.mockFunctions
&& opts.mockFunctions.indexOf(name) !== -1;

16
myt-run.js Executable file → Normal file
View File

@ -20,8 +20,7 @@ class Run extends Command {
ci: 'Workaround for continuous integration system',
network: 'Docker network to attach container to',
random: 'Whether to use a random container name and port',
tmpfs: 'Whether to use tmpfs mount for MySQL data',
keep: 'Keep container on failure'
tmpfs: 'Whether to use tmpfs mount for MySQL data'
}
};
@ -30,14 +29,12 @@ class Run extends Command {
ci: 'c',
network: 'n',
random: 'r',
tmpfs: 't',
keep: 'k'
tmpfs: 't'
},
boolean: [
'ci',
'random',
'tmpfs',
'keep'
'tmpfs'
]
};
@ -125,14 +122,13 @@ class Run extends Command {
try {
const server = new Server(ct, dbConfig);
const useCustom = opts.ci || opts.network
if (isRandom || useCustom) {
if (isRandom) {
try {
const netSettings = await ct.inspect({
format: '{{json .NetworkSettings}}'
});
if (useCustom) {
if (opts.ci || opts.network) {
dbConfig.host = opts.network
? netSettings.Networks[opts.network].IPAddress
: netSettings.Gateway;
@ -212,7 +208,7 @@ class Run extends Command {
return server;
} catch (err) {
try {
if (!opts.keep) await ct.rm({force: true});
await ct.rm({force: true});
} catch (e) {}
throw err;
}

0
myt-start.js Executable file → Normal file
View File

126
myt-version.js Executable file → Normal file
View File

@ -1,7 +1,6 @@
const Myt = require('./myt');
const Command = require('./lib/command');
const fs = require('fs-extra');
const SqlString = require('sqlstring');
/**
* Creates a new version.
@ -10,23 +9,18 @@ class Version extends Command {
static usage = {
description: 'Creates a new version',
params: {
name: 'Name for the new version',
deprecate: 'Whether to generate sql to delete deprecated objects'
name: 'Name for the new version'
},
operand: 'name'
};
static opts = {
alias: {
name: 'n',
deprecate: 'p'
name: 'n'
},
string: [
'name'
],
boolean: [
'deprecate'
],
default: {
remote: 'production'
}
@ -42,8 +36,7 @@ class Version extends Command {
},
versionCreated: function(versionName) {
console.log(`New version created: ${versionName}`);
},
deprecate: 'Generating SQL for deprecated objects deletion.'
}
};
async run(myt, opts) {
@ -128,16 +121,10 @@ class Version extends Command {
[opts.code, newVersion]
);
await fs.mkdir(newVersionDir);
if (opts.deprecate) {
this.emit('deprecate');
await deprecate(conn, opts, newVersionDir);
} else
await fs.writeFile(
`${newVersionDir}/00-firstScript.sql`,
'-- Place your SQL code here\n'
);
await fs.writeFile(
`${newVersionDir}/00-firstScript.sql`,
'-- Place your SQL code here\n'
);
this.emit('versionCreated', versionFolder);
await conn.query('COMMIT');
@ -150,105 +137,6 @@ class Version extends Command {
}
}
async function deprecate(conn, opts, newVersionDir) {
const now = new Date();
const minDeprecDate = new Date(now.getTime() - opts.deprecRetentionPeriod * 24 * 60 * 60 * 1000);
const deprecMarkRegex = opts.deprecMarkRegex;
const deprecCommentRegex = opts.deprecCommentRegex;
const deprecDateRegex = opts.deprecDateRegex;
const filePath = `${newVersionDir}/00-deprecate.sql`;
// Generate the drops of the primary keys
const [primaryKeys] = await conn.query(`
SELECT c.TABLE_SCHEMA 'schema', c.TABLE_NAME 'table'
FROM information_schema.COLUMNS c
LEFT JOIN information_schema.VIEWS v ON v.TABLE_SCHEMA = c.TABLE_SCHEMA
AND v.TABLE_NAME = c.TABLE_NAME
JOIN information_schema.STATISTICS s ON s.TABLE_SCHEMA = c.TABLE_SCHEMA
AND s.TABLE_NAME = c.TABLE_NAME
AND s.COLUMN_NAME = c.COLUMN_NAME
WHERE c.COLUMN_NAME REGEXP ? COLLATE utf8mb4_unicode_ci
AND c.COLUMN_COMMENT REGEXP ? COLLATE utf8mb4_unicode_ci
AND REGEXP_SUBSTR(c.COLUMN_COMMENT, ? COLLATE utf8mb4_unicode_ci) < ?
AND v.TABLE_NAME IS NULL
AND s.INDEX_NAME = 'PRIMARY'
`, [deprecMarkRegex, deprecCommentRegex, deprecDateRegex, minDeprecDate]);
primaryKeys.map(async row => {
await fs.appendFile(
filePath,
'ALTER TABLE ' + SqlString.escapeId(row.schema, true) + '.' +
SqlString.escapeId(row.table, true) + ' DROP PRIMARY KEY;\n'
);
});
// Generate the drops of the foreign keys
const [foreignKeys] = await conn.query(`
SELECT c.TABLE_SCHEMA 'schema', c.TABLE_NAME 'table', kcu.CONSTRAINT_NAME 'constraint'
FROM information_schema.COLUMNS c
LEFT JOIN information_schema.VIEWS v ON v.TABLE_SCHEMA = c.TABLE_SCHEMA
AND v.TABLE_NAME = c.TABLE_NAME
JOIN information_schema.KEY_COLUMN_USAGE kcu ON kcu.TABLE_SCHEMA = c.TABLE_SCHEMA
AND kcu.TABLE_NAME = c.TABLE_NAME
AND kcu.COLUMN_NAME = c.COLUMN_NAME
WHERE c.COLUMN_NAME REGEXP ? COLLATE utf8mb4_unicode_ci
AND c.COLUMN_COMMENT REGEXP ? COLLATE utf8mb4_unicode_ci
AND REGEXP_SUBSTR(c.COLUMN_COMMENT, ? COLLATE utf8mb4_unicode_ci) < ?
AND v.TABLE_NAME IS NULL
AND kcu.REFERENCED_COLUMN_NAME IS NOT NULL
`, [deprecMarkRegex, deprecCommentRegex, deprecDateRegex, minDeprecDate]);
foreignKeys.map(async row => {
await fs.appendFile(
filePath,
'ALTER TABLE ' + SqlString.escapeId(row.schema, true) + '.' +
SqlString.escapeId(row.table, true) + ' DROP FOREIGN KEY ' +
SqlString.escapeId(row.constraint, true) + ';\n'
);
});
// Generate the drops of the columns
const [columns] = await conn.query(`
SELECT c.TABLE_SCHEMA 'schema', c.TABLE_NAME 'table', c.COLUMN_NAME 'column'
FROM information_schema.COLUMNS c
LEFT JOIN information_schema.VIEWS v ON v.TABLE_SCHEMA = c.TABLE_SCHEMA
AND v.TABLE_NAME = c.TABLE_NAME
LEFT JOIN information_schema.KEY_COLUMN_USAGE kcu ON kcu.TABLE_SCHEMA = c.TABLE_SCHEMA
AND kcu.TABLE_NAME = c.TABLE_NAME
AND kcu.COLUMN_NAME = c.COLUMN_NAME
WHERE c.COLUMN_NAME REGEXP ? COLLATE utf8mb4_unicode_ci
AND c.COLUMN_COMMENT REGEXP ? COLLATE utf8mb4_unicode_ci
AND REGEXP_SUBSTR(c.COLUMN_COMMENT, ? COLLATE utf8mb4_unicode_ci) <?
AND v.TABLE_NAME IS NULL
`, [deprecMarkRegex, deprecCommentRegex, deprecDateRegex, minDeprecDate]);
columns.map(async row => {
await fs.appendFile(
filePath,
'ALTER TABLE ' + SqlString.escapeId(row.schema, true) + '.' +
SqlString.escapeId(row.table, true) + ' DROP COLUMN ' +
SqlString.escapeId(row.column, true) + ';\n'
);
});
// Generate the drops of the tables
const [tables] = await conn.query(`
SELECT TABLE_SCHEMA 'schema', TABLE_NAME 'table'
FROM information_schema.TABLES
WHERE TABLE_NAME REGEXP ? COLLATE utf8mb4_unicode_ci
AND TABLE_COMMENT REGEXP ? COLLATE utf8mb4_unicode_ci
AND REGEXP_SUBSTR(TABLE_COMMENT, ? COLLATE utf8mb4_unicode_ci) < ?
`, [deprecMarkRegex, deprecCommentRegex, deprecDateRegex, minDeprecDate]);
tables.map(async row => {
await fs.appendFile(
filePath,
'DROP TABLE ' + SqlString.escapeId(row.schema, true) + '.' +
SqlString.escapeId(row.table, true) + ';\n'
);
});
}
function randomName() {
const color = random(colors);
let plant = random(plants);

2
myt.js
View File

@ -19,7 +19,7 @@ class Myt {
params: {
remote: 'Name of remote to use',
workspace: 'The base directory of the project',
debug: 'Whether to enable debug mode',
debug: 'Wether to enable debug mode',
version: 'Display the version number and exit',
help: 'Display this help message'
}

5
package-lock.json generated
View File

@ -1,12 +1,12 @@
{
"name": "@verdnatura/myt",
"version": "1.6.13",
"version": "1.6.4",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@verdnatura/myt",
"version": "1.6.13",
"version": "1.6.4",
"license": "GPL-3.0",
"dependencies": {
"@sqltools/formatter": "^1.2.5",
@ -1169,7 +1169,6 @@
"resolved": "https://registry.npmjs.org/nodegit/-/nodegit-0.27.0.tgz",
"integrity": "sha512-E9K4gPjWiA0b3Tx5lfWCzG7Cvodi2idl3V5UD2fZrOrHikIfrN7Fc2kWLtMUqqomyoToYJLeIC8IV7xb1CYRLA==",
"hasInstallScript": true,
"license": "MIT",
"dependencies": {
"fs-extra": "^7.0.0",
"got": "^10.7.0",

View File

@ -1,6 +1,6 @@
{
"name": "@verdnatura/myt",
"version": "1.6.13",
"version": "1.6.5",
"author": "Verdnatura Levante SL",
"description": "MySQL version control",
"license": "GPL-3.0",