Skip to content

SQL migration

A blueprint that creates a pair of migration files: one that creates a table and one that drops it. The migration number and the table name are entered when the blueprint runs.

blueprintor.config.js
module.exports = {
blueprints: [
{
title: "SQL migration",
variables: [
{ key: "version", prompt: "Migration number (e.g. 001)" },
{ key: "table", prompt: "Table name (e.g. users)" },
],
snippets: {
up: `
CREATE TABLE {table.snake} (
id BIGINT PRIMARY KEY,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
`,
down: `
DROP TABLE IF EXISTS {table.snake};
`,
},
structure: {
files: [
{ name: "{version.raw}_create_{table.snake}.up.sql", snippet: "up" },
{ name: "{version.raw}_create_{table.snake}.down.sql", snippet: "down" },
],
},
},
],
};
  1. Right-click your migrations folder (for example db/migrations) and choose Run Blueprintor….

  2. Select SQL migration.

  3. Enter 001 as the number, then users as the table name.

  • Directorydb
    • Directorymigrations
      • 001_create_users.up.sql
      • 001_create_users.down.sql
CREATE TABLE users (
id BIGINT PRIMARY KEY,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

{table.snake} turns a name such as user profile or UserProfile into user_profile, which is the usual style for table names.