-
-
Notifications
You must be signed in to change notification settings - Fork 2
Automatic Schema Versioning
Oxford Harrison edited this page Nov 8, 2024
·
17 revisions
✨ Alter your database and get back a reference to a "savepoint" automatically created for you
// Alter schema
const savepoint = await client.query(
`CREATE TABLE public.users (
id int,
name varchar
)`,
{ desc: 'Create users table' }
);
✨ Or obtain said savepoint on-demand
const savepoint = await client.database('public').savepoint();
✨ Inspect savepoint
// Some details
console.log(savepoint.versionTag()); // 1
console.log(savepoint.commitDesc()); // Create users table
console.log(savepoint.commitDate()); // 2024-07-17T22:40:56.786Z
// Everything...
console.log(savepoint.jsonfy());
Console
{ // Internal parameters... master_savepoint: null, id: '952cc6ae-5b5b-4534-8b03-dc38ee8658ac', database_tag: 'db.1730978107426', // Schema snapshot... name: 'public', '$name': null, tables: [ { name: 'users', columns: [Array], constraints: [], indexes: [], status: 'new' } ], status: null, // Version details... version_tag: 1, version_tags: [ 1 ], version_state: 'commit', commit_date: '2024-07-17T22:40:56.786Z', commit_desc: 'Create users table', commit_ref: null, commit_pid: '72776', rollback_date: null, rollback_desc: null, rollback_ref: null, rollback_pid: null, // Cascades... cascades: [] }
✨ Roll back savepoint
// SQL preview
console.log(savepoint.restorePreview()); // DROP TABLE public.users CASCADE
// Execute now (drops "users" table)
await savepoint.rollback({
desc: 'Users table unnecessary'
});
✨ Roll forward savepoint
// SQL preview
console.log(savepoint.restorePreview()); // CREATE TABLE public.users (...)
// Execute now (recreates "users" table)
await savepoint.recommit({
desc: 'Users table necessary again'
});