The Migrations service provides read-only access to the database migration status and history. It’s useful for monitoring the current state of your SnackBase deployment and tracking schema changes.
Overview
import { SnackBaseClient } from "@snackbase/sdk";
const client = new SnackBaseClient({
baseUrl: "https://api.example.com",
});
// Access the migrations service
const migrations = client.migrations;
Migration operations require superadmin authentication. This service only provides
read access to migration information. Actual migrations are run server-side.
List Migrations
Get all available Alembic migrations with their application status:
const response = await migrations.list();
// Returns: { items: Migration[], total: number }
Each migration includes:
{
revision: string; // Migration revision ID
name: string; // Human-readable name
is_applied: boolean; // Whether this migration has been applied
created_at: string; // ISO timestamp
}
Get Current Migration
Get the currently applied database revision:
const current = await migrations.getCurrent();
if (current) {
console.log("Current revision:", current.revision);
console.log("Applied at:", current.created_at);
} else {
console.log("No migrations applied yet");
}
Returns null if no migrations have been applied yet.
Get Migration History
Get the complete migration history in chronological order:
const history = await migrations.getHistory();
// Returns: { items: Migration[], total: number }
This shows all migrations that have been applied to the database, in the order they were applied.
Use Cases
Version Check
Check if the database is up to date:
async function isDatabaseUpToDate() {
const [allMigrations, current] = await Promise.all([
migrations.list(),
migrations.getCurrent()
]);
const latestRevision = allMigrations.items[allMigrations.items.length - 1].revision;
if (!current) {
return false;
}
return current.revision === latestRevision;
}
Health Monitoring
Include migration status in health checks:
async function getHealthStatus() {
const [current, history] = await Promise.all([
migrations.getCurrent(),
migrations.getHistory()
]);
return {
hasMigrations: !!current,
migrationCount: history.total,
currentRevision: current?.revision || null,
lastMigrationAt: history.items[history.items.length - 1]?.created_at
};
}
Display Status
Show migration information in an admin dashboard:
import { useQuery } from "@snackbase/sdk/react";
function MigrationStatus() {
const { data: current, isLoading } = useQuery({
queryKey: ["current-migration"],
queryFn: () => migrations.getCurrent()
});
if (isLoading) return <div>Loading...</div>;
if (!current) {
return <div>Warning: No migrations applied</div>;
}
return (
<div>
<h3>Database Migration Status</h3>
<p>Revision: {current.revision}</p>
<p>Applied: {new Date(current.created_at).toLocaleString()}</p>
</div>
);
}
Next Steps