Forge

Schema

Describe tables and columns as Go values.

GitHub

You describe a table with dbswitch.Table and dbswitch.Column — plain Go structs, no struct tags. CreateTable emits CREATE TABLE IF NOT EXISTS for the configured dialect.

users := dbswitch.Table{
	Name: "users",
	Columns: []dbswitch.Column{
		{Name: "id", Type: dbswitch.TypeUUID, PrimaryKey: true, Default: dbswitch.DefaultGenerateUUID},
		{Name: "email", Type: dbswitch.TypeText, NotNull: true, Unique: true},
		{Name: "active", Type: dbswitch.TypeBool, NotNull: true, Default: dbswitch.DefaultTrue},
		{Name: "created_at", Type: dbswitch.TypeTimestamp, NotNull: true, Default: dbswitch.DefaultCurrentTime},
	},
}

Column fields

FieldTypeMeaning
NamestringColumn name
TypeColumnTypeAbstract type (see below)
PrimaryKeyboolMark as PRIMARY KEY
UniqueboolAdd a UNIQUE constraint
NotNullboolAdd NOT NULL
DefaultDefaultValueColumn default (see below)

Column types

Abstract ColumnType values map to each database's native type:

dbswitch typePostgreSQL
TypeUUIDUUID
TypeTextTEXT
TypeBoolBOOLEAN
TypeIntINTEGER
TypeTimestampTIMESTAMPTZ

Defaults

DefaultValuePostgreSQL
DefaultGenerateUUIDgen_random_uuid()
DefaultCurrentTimenow()
DefaultTrueTRUE
DefaultFalseFALSE

CreateTable is CREATE TABLE IF NOT EXISTSnot a migration system. It won't alter existing tables, add indexes beyond column constraints, or version your schema. Use a real migration tool for evolving schemas. See Limitations.

Column types and defaults above are SQL concepts and apply to the PostgreSQL backend. On MongoDB and DynamoDB (both schemaless) they're ignored — only Unique (→ unique index / conditional write) and the primary key (→ _id on Mongo, the partition key on DynamoDB) have an effect. On DynamoDB the PrimaryKey column must additionally be named "id", and its Type must be TypeUUID/TypeText (→ S) or TypeInt (→ N) — DynamoDB keys can't be TypeBool or TypeTimestamp. See Backends.

On this page