144 lines
7.7 KiB
JavaScript
144 lines
7.7 KiB
JavaScript
// The "Database schema" health check (projects with Doctrine): a non-modal banner over the editors, a badge in the status bar and on the
|
|
// Run widget, and one-click fixes, each confirmed (and, for a production database, its name typed) before anything runs.
|
|
// PHP (ide/src/Doctrine/SchemaCheck.php, SchemaWatcher.php) checks in the background when the site starts, when entities / migrations change,
|
|
// and when the site's output or an HTTP answer holds a "table not found" error; it sends the event doctrine.schema.
|
|
import { backend } from './backend.js';
|
|
import { el, clear, icon, notify, prefs, t, confirm, dialog } from './ui.js';
|
|
import { bannerOf, badgeOf, planText, needsName, signature } from './schema-model.js';
|
|
|
|
const cssHref = new URL('../css/doctrine.css', import.meta.url).href;
|
|
if (![...document.querySelectorAll('link[rel=stylesheet]')].some((l) => l.href === cssHref)) document.head.append(el('link', { rel: 'stylesheet', href: cssHref }));
|
|
|
|
export class SchemaCheck {
|
|
constructor(app) {
|
|
this.app = app;
|
|
this.result = null;
|
|
this.busy = false;
|
|
this.dismissed = '';
|
|
this.node = el('div.sc-banner', { hidden: true });
|
|
this.badge = el('span.sb-item.clickable.sc-badge', { hidden: true, onclick: () => this.check(true) });
|
|
this.runBadge = el('span.sc-runbadge', { hidden: true, title: t('Database schema') });
|
|
const center = document.getElementById('center');
|
|
if (center) center.prepend(this.node);
|
|
document.getElementById('sbTask')?.before(this.badge);
|
|
document.getElementById('runConfig')?.append(this.runBadge);
|
|
backend.on('doctrine.schema', (p) => this.received(p));
|
|
this.sendAuto();
|
|
}
|
|
|
|
/** the Settings toggle (kept here, told to PHP) */
|
|
static auto() { return prefs.get('schemaAutoCheck', true); }
|
|
sendAuto() { backend.invoke('doctrine.schema.auto', { enabled: SchemaCheck.auto() }).catch(() => null); }
|
|
setAuto(on) { prefs.set('schemaAutoCheck', !!on); this.sendAuto(); }
|
|
|
|
received(p) {
|
|
if (p.state === 'skipped') return;
|
|
if (p.state === 'error') { this.busy = false; this.render(); return; }
|
|
this.busy = p.state === 'checking';
|
|
if (!this.busy) this.result = p;
|
|
else if (!this.result) this.result = { state: 'checking', problems: [] };
|
|
if (!this.busy && p.state === 'ok' && p.reason === 'fix') this.dismissed = '';
|
|
this.render();
|
|
}
|
|
|
|
/** the action "Check the database schema" */
|
|
async check(show = true) {
|
|
this.dismissed = '';
|
|
this.busy = true;
|
|
this.render();
|
|
try {
|
|
this.result = await backend.invoke('doctrine.schema.check', {});
|
|
} catch (e) {
|
|
notify(e.message, 'error', { title: t('Database schema') });
|
|
}
|
|
this.busy = false;
|
|
this.render();
|
|
if (show && this.result?.state === 'ok') notify(t('The database matches the entities: nothing to fix.'), 'success', { title: t('Database schema') });
|
|
}
|
|
|
|
render() {
|
|
const b = bannerOf(this.result, this.dismissed);
|
|
const badge = this.busy ? { kind: 'checking', count: 0 } : badgeOf(this.result);
|
|
this.badge.hidden = !badge.kind;
|
|
this.badge.setAttribute('data-tour', 'schema-badge');
|
|
clear(this.badge).append(icon(badge.kind === 'problem' ? 'warning' : 'database'),
|
|
el('span', {}, badge.kind === 'problem' ? t('{n} database problems', { n: badge.count }) : badge.kind === 'checking' ? t('Checking the database…') : t('Database OK')));
|
|
this.badge.className = 'sb-item clickable sc-badge ' + badge.kind;
|
|
this.badge.title = t('Check the database schema');
|
|
this.runBadge.hidden = badge.kind !== 'problem';
|
|
this.runBadge.textContent = String(badge.count || '');
|
|
clear(this.node);
|
|
this.node.hidden = !b;
|
|
if (!b) return;
|
|
this.node.className = 'sc-banner ' + b.level;
|
|
this.node.setAttribute('data-tour', 'schema-banner');
|
|
this.node.append(
|
|
icon('warning'),
|
|
el('div.sc-text', {}, el('strong', {}, t('Database schema')), ...b.lines.map((l) => el('div', {}, l)),
|
|
b.production ? el('div.sc-prod', {}, t('{name} is a production database: nothing is fixed by itself.', { name: this.result.connection || '' })) : null),
|
|
el('div.sc-fixes', {}, b.fixes.map((f) => el(`button.btn.small${f.primary ? '.primary' : ''}`, { 'data-tour': 'schema-fix-' + f.id, onclick: () => this.fix(f.id) }, f.label))),
|
|
el('button.icon-btn', { title: t('Hide'), onclick: () => { this.dismissed = b.key; this.render(); } }, icon('close')));
|
|
}
|
|
|
|
async fix(id) {
|
|
const r = this.result;
|
|
if (id === 'openDoctrine') { this.app.showTool('doctrine'); return; }
|
|
if (id === 'recheck') { this.check(); return; }
|
|
let text;
|
|
if (id === 'all') text = t('Fix everything, in this order:') + '\n' + planText(r.plan);
|
|
else if (id === 'createDatabase') text = t('Create the database (doctrine:database:create --if-not-exists)?');
|
|
else if (id === 'migrate') text = t('Run the pending migrations (doctrine:migrations:migrate)?');
|
|
else if (id === 'schemaUpdate') text = t('Update the schema (doctrine:schema:update --force)?');
|
|
else if (id === 'diff') {
|
|
// the SQL first
|
|
let sql;
|
|
try { sql = await backend.invoke('doctrine.migrations', { action: 'sql' }); } catch (e) { sql = { statements: [], output: e.message }; }
|
|
const ok = await new Promise((resolve) => {
|
|
let done = false;
|
|
dialog({ title: t('Make a migration'), wide: true,
|
|
body: el('div.db-form', {}, el('p', {}, t('These {n} statements will be in the new migration:', { n: sql.statements?.length || 0 })), el('pre.dc-code', {}, (sql.statements || []).join(';\n') + ';')),
|
|
buttons: [{ label: t('Cancel') }, { label: t('Write the migration'), primary: true, action: () => { done = true; resolve(true); } }], onClose: () => { if (!done) resolve(false); } });
|
|
});
|
|
if (!ok) return;
|
|
text = null;
|
|
}
|
|
if (text !== null && !(await confirm(t('Database schema'), text, { ok: t('Run') }))) return;
|
|
let name;
|
|
if (needsName(id, r)) {
|
|
name = await this.askName(r.connection || '');
|
|
if (name === null) return;
|
|
}
|
|
const busy = notify(t('Running through the console…'), 'info', { title: t('Database schema'), timeout: 0 });
|
|
let out;
|
|
try { out = await backend.invoke('doctrine.schema.fix', { fix: id, confirm: name }); } catch (e) { out = { ok: false, steps: [], error: e.message, check: this.result }; }
|
|
busy.remove();
|
|
this.result = out.check || this.result;
|
|
this.render();
|
|
this.app.doctrine?.loadMigrations?.();
|
|
const failed = (out.steps || []).find((s) => !s.ok);
|
|
if (out.error || failed) {
|
|
dialog({ title: t('It failed: see the output in the Migrations tab.'), wide: true, body: el('pre.dc-out.err', {}, out.error || failed.output) });
|
|
} else {
|
|
notify(out.ok ? t('Done.') : t('Done, but the database still differs: see the banner.'), out.ok ? 'success' : 'warning', { title: t('Database schema') });
|
|
}
|
|
}
|
|
|
|
/** a production database: its name to be typed; null = cancelled */
|
|
askName(name) {
|
|
return new Promise((resolve) => {
|
|
const input = el('input.input', { placeholder: name });
|
|
let done = false;
|
|
dialog({ title: t('Production: confirm the changes'),
|
|
body: el('div.db-form', {}, el('div.db-prodbar', {}, icon('warning'), t('{name} is a production database.', { name })),
|
|
el('label.db-field', {}, el('span', {}, t('Type {name} to go on', { name })), input)),
|
|
buttons: [{ label: t('Cancel') }, { label: t('Run on production'), danger: true, action: () => {
|
|
if (input.value.trim() !== name) { notify(t('The name does not match.'), 'warning'); return false; }
|
|
done = true; resolve(name);
|
|
} }], onClose: () => { if (!done) resolve(null); } });
|
|
});
|
|
}
|
|
|
|
registerActions(a) {
|
|
a.add('checkDatabaseSchema', 'Check the database schema', () => this.check(), { icon: 'database' });
|
|
}
|
|
}
|