Documentation
¶
Index ¶
- func QuoteIdent(name string) string
- func QuoteValue(v interface{}) string
- type PostgresTableDef
- func (p *PostgresTableDef) AfterTableCreate() []func()
- func (p *PostgresTableDef) BigInt(columnName string, opts ...schema.ColumnOptions)
- func (p *PostgresTableDef) Binary(columnName string, opts ...schema.ColumnOptions)
- func (p *PostgresTableDef) Boolean(columnName string, opts ...schema.ColumnOptions)
- func (p *PostgresTableDef) Column(columnName string, columnType schema.ColumnType, opts ...schema.ColumnOptions)
- func (p *PostgresTableDef) Columns() []schema.ColumnOptions
- func (p *PostgresTableDef) Date(columnName string, opts ...schema.ColumnOptions)
- func (p *PostgresTableDef) DateTime(columnName string, opts ...schema.ColumnOptions)
- func (p *PostgresTableDef) Decimal(columnName string, opts ...schema.ColumnOptions)
- func (p *PostgresTableDef) Float(columnName string, opts ...schema.ColumnOptions)
- func (p *PostgresTableDef) ForeignKey(toTable schema.TableName, opts ...schema.AddForeignKeyConstraintOptions)
- func (p *PostgresTableDef) Hstore(columnName string, opts ...schema.ColumnOptions)
- func (p *PostgresTableDef) Index(columnNames []string, opts ...schema.IndexOptions)
- func (p *PostgresTableDef) Integer(columnName string, opts ...schema.ColumnOptions)
- func (p *PostgresTableDef) JSON(columnName string, opts ...schema.ColumnOptions)
- func (p *PostgresTableDef) Serial(columnName string, opts ...schema.ColumnOptions)
- func (p *PostgresTableDef) String(columnName string, opts ...schema.ColumnOptions)
- func (p *PostgresTableDef) Text(columnName string, opts ...schema.ColumnOptions)
- func (p *PostgresTableDef) Time(columnName string, opts ...schema.ColumnOptions)
- func (p *PostgresTableDef) Timestamps()
- func (p *PostgresTableDef) UUID(columnName string, opts ...schema.ColumnOptions)
- type Schema
- func (p *Schema) AddCheckConstraint(tableName schema.TableName, constraintName string, expression string, ...)
- func (p *Schema) AddColumn(tableName schema.TableName, columnName string, columnType schema.ColumnType, ...)
- func (p *Schema) AddColumnComment(tableName schema.TableName, columnName string, comment *string, ...)
- func (s *Schema) AddEnumValue(name string, value string, opts ...schema.AddEnumValueOptions)
- func (p *Schema) AddExtension(name string, option ...schema.ExtensionOptions)
- func (p *Schema) AddForeignKeyConstraint(fromTable, toTable schema.TableName, ...)
- func (p *Schema) AddIndex(table schema.TableName, columns []string, option ...schema.IndexOptions)
- func (p *Schema) AddPrimaryKeyConstraint(tableName schema.TableName, columns []string, ...)
- func (p *Schema) AddTableComment(tableName schema.TableName, comment *string, ...)
- func (p *Schema) AddTimestamps(tableName schema.TableName)
- func (p *Schema) ChangeColumnDefault(tableName schema.TableName, columnName, defaultValue string, ...)
- func (p *Schema) ChangeColumnType(tableName schema.TableName, columnName string, columnType schema.ColumnType, ...)
- func (p *Schema) ColumnExist(tableName schema.TableName, columnName string) bool
- func (p *Schema) ConstraintExist(tableName schema.TableName, constraintName string) bool
- func (s *Schema) CreateEnum(name string, values []string, opts ...schema.CreateEnumOptions)
- func (p *Schema) CreateTable(tableName schema.TableName, f func(*PostgresTableDef), ...)
- func (p *Schema) DropCheckConstraint(tableName schema.TableName, constraintName string, ...)
- func (p *Schema) DropColumn(tableName schema.TableName, columnName string, opt ...schema.DropColumnOptions)
- func (s *Schema) DropEnum(name string, opts ...schema.DropEnumOptions)
- func (p *Schema) DropExtension(name string, opt ...schema.DropExtensionOptions)
- func (p *Schema) DropForeignKeyConstraint(from, to schema.TableName, opt ...schema.DropForeignKeyConstraintOptions)
- func (p *Schema) DropIndex(table schema.TableName, columns []string, opt ...schema.DropIndexOptions)
- func (p *Schema) DropPrimaryKeyConstraint(tableName schema.TableName, opts ...schema.DropPrimaryKeyConstraintOptions)
- func (p *Schema) DropTable(tableName schema.TableName, opts ...schema.DropTableOptions)
- func (p *Schema) Exec(query string, args ...interface{})
- func (s *Schema) FindEnumUsage(name string, schemaName *string) []schema.EnumUsage
- func (p *Schema) IndexExist(tableName schema.TableName, indexName string) bool
- func (s *Schema) ListEnumValues(name string, schemaName *string) []string
- func (p *Schema) PrimaryKeyExist(tableName schema.TableName) bool
- func (p *Schema) RenameColumn(tableName schema.TableName, oldColumnName, newColumnName string)
- func (s *Schema) RenameEnum(oldName, newName string, opts ...schema.RenameEnumOptions)
- func (s *Schema) RenameEnumValue(name, oldName, newName string, opts ...schema.RenameEnumValueOptions)
- func (p *Schema) RenameTable(oldTableName, newTableName schema.TableName)
- func (p *Schema) TableExist(tableName schema.TableName) bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func QuoteIdent ¶
func QuoteValue ¶
func QuoteValue(v interface{}) string
Types ¶
type PostgresTableDef ¶
type PostgresTableDef struct {
// contains filtered or unexported fields
}
PostgresTableDef holds the definition in the table creation. create table articles ( <inner_table> )
func (*PostgresTableDef) AfterTableCreate ¶
func (p *PostgresTableDef) AfterTableCreate() []func()
func (*PostgresTableDef) BigInt ¶
func (p *PostgresTableDef) BigInt(columnName string, opts ...schema.ColumnOptions)
func (*PostgresTableDef) Binary ¶
func (p *PostgresTableDef) Binary(columnName string, opts ...schema.ColumnOptions)
func (*PostgresTableDef) Boolean ¶
func (p *PostgresTableDef) Boolean(columnName string, opts ...schema.ColumnOptions)
func (*PostgresTableDef) Column ¶
func (p *PostgresTableDef) Column(columnName string, columnType schema.ColumnType, opts ...schema.ColumnOptions)
func (*PostgresTableDef) Columns ¶
func (p *PostgresTableDef) Columns() []schema.ColumnOptions
func (*PostgresTableDef) Date ¶
func (p *PostgresTableDef) Date(columnName string, opts ...schema.ColumnOptions)
func (*PostgresTableDef) DateTime ¶
func (p *PostgresTableDef) DateTime(columnName string, opts ...schema.ColumnOptions)
func (*PostgresTableDef) Decimal ¶
func (p *PostgresTableDef) Decimal(columnName string, opts ...schema.ColumnOptions)
func (*PostgresTableDef) Float ¶
func (p *PostgresTableDef) Float(columnName string, opts ...schema.ColumnOptions)
func (*PostgresTableDef) ForeignKey ¶
func (p *PostgresTableDef) ForeignKey(toTable schema.TableName, opts ...schema.AddForeignKeyConstraintOptions)
func (*PostgresTableDef) Hstore ¶
func (p *PostgresTableDef) Hstore(columnName string, opts ...schema.ColumnOptions)
func (*PostgresTableDef) Index ¶
func (p *PostgresTableDef) Index(columnNames []string, opts ...schema.IndexOptions)
func (*PostgresTableDef) Integer ¶
func (p *PostgresTableDef) Integer(columnName string, opts ...schema.ColumnOptions)
func (*PostgresTableDef) JSON ¶
func (p *PostgresTableDef) JSON(columnName string, opts ...schema.ColumnOptions)
func (*PostgresTableDef) Serial ¶
func (p *PostgresTableDef) Serial(columnName string, opts ...schema.ColumnOptions)
func (*PostgresTableDef) String ¶
func (p *PostgresTableDef) String(columnName string, opts ...schema.ColumnOptions)
func (*PostgresTableDef) Text ¶
func (p *PostgresTableDef) Text(columnName string, opts ...schema.ColumnOptions)
func (*PostgresTableDef) Time ¶
func (p *PostgresTableDef) Time(columnName string, opts ...schema.ColumnOptions)
func (*PostgresTableDef) Timestamps ¶
func (p *PostgresTableDef) Timestamps()
Timestamps adds created_at, updated_at Columns to the table.
func (*PostgresTableDef) UUID ¶
func (p *PostgresTableDef) UUID(columnName string, opts ...schema.ColumnOptions)
type Schema ¶
type Schema struct { // TX is the transaction to execute the queries. TX schema.DB // DB is a database connection but not in a transaction. DB schema.DB Context *schema.MigratorContext *base.Schema // ReversibleMigrationExecutor is a helper to execute reversible migrations in change method. *schema.ReversibleMigrationExecutor }
func NewPostgres ¶
func (*Schema) AddCheckConstraint ¶
func (p *Schema) AddCheckConstraint(tableName schema.TableName, constraintName string, expression string, opts ...schema.CheckConstraintOptions)
AddCheckConstraint Adds a new check constraint to the Table. expression parameter is a FormatRecords representation of verifiable boolean condition.
Example:
p.AddCheckConstraint("products", "price_check", "price > 0")
Generates:
ALTER TABLE "products" ADD CONSTRAINT price_check CHECK (price > 0)
func (*Schema) AddColumn ¶
func (p *Schema) AddColumn(tableName schema.TableName, columnName string, columnType schema.ColumnType, opts ...schema.ColumnOptions)
AddColumn adds a new column to the Table.
Example:
p.Column("users", "picture", schema.ColumnTypeBinary)
Generates:
ALTER TABLE "users" ADD "picture" BYTEA
Adding a column with a limit, default value and null constraint:
p.Column("articles", "status", schema.ColumnTypeString, schema.ColumnOptions{Limit: 20, Default: "draft", NotNull: false})
Generates:
ALTER TABLE "articles" ADD "status" VARCHAR(20) DEFAULT 'draft' NOT NULL
Adding a column with precision and scale:
p.Column("answers", "bill_gates_money", schema.ColumnTypeDecimal, schema.ColumnOptions{Precision: 15, Scale: 2})
Generates:
ALTER TABLE "answers" ADD "bill_gates_money" DECIMAL(15,2)
Adding a column with an array type:
p.Column("users", "skills", schema.ColumnTypeText, schema.ColumnOptions{Array: true})
Generates:
ALTER TABLE "users" ADD "skills" TEXT[]
Adding a column with a custom type:
p.Column("shapes", "triangle", "polygon")
Generates:
ALTER TABLE "shapes" ADD "triangle" POLYGON
Adding a column if it does not exist:
p.Column("shapes", "triangle", "polygon", schema.ColumnOptions{IfNotExists: true})
Generates:
ALTER TABLE "shapes" ADD "triangle" IF NOT EXISTS POLYGON
func (*Schema) AddColumnComment ¶
func (p *Schema) AddColumnComment(tableName schema.TableName, columnName string, comment *string, opts ...schema.ColumnCommentOptions)
AddColumnComment adds a comment to the column.
Example:
p.AddColumnComment("users", "name", utils.Ptr("The name of the User"))
Generates:
COMMENT ON COLUMN "users"."name" IS 'The name of the User'
To set a null comment:
p.AddColumnComment("users", "name", nil)
Generates:
COMMENT ON COLUMN "users"."name" IS NULL
To be able to rollbackMode the operation you must provide the Reversible parameter
func (*Schema) AddEnumValue ¶
func (s *Schema) AddEnumValue(name string, value string, opts ...schema.AddEnumValueOptions)
AddEnumValue add a new value to an existing enum type This operation is not reversible. https://www.postgresql.org/message-id/21012.1459434338%40sss.pgh.pa.us
WARNINGS: This operation is not running in a transaction. So it can't be rolled back.
Example:
schema.AddEnumValue("my_enum", "d")
Generates:
ALTER TYPE my_enum ADD VALUE 'd';
func (*Schema) AddExtension ¶
func (p *Schema) AddExtension(name string, option ...schema.ExtensionOptions)
AddExtension adds a new extension to the database.
Example:
p.AddExtension("uuid", ExtensionOptions{})
Generates:
CREATE EXTENSION IF NOT EXISTS "uuid-ossp"
func (*Schema) AddForeignKeyConstraint ¶
func (p *Schema) AddForeignKeyConstraint(fromTable, toTable schema.TableName, opts ...schema.AddForeignKeyConstraintOptions)
AddForeignKeyConstraint Adds a new foreign key. FromTable is the Table with the key column, ToTable contains the referenced primary key.
The foreign key will be named after the following pattern: fk_[from_table]_[to_table].
Creating a simple foreign key:
p.AddForeignKeyConstraint("articles", "authors", AddForeignKeyConstraintOptions{})
Generates:
ALTER TABLE "articles" ADD CONSTRAINT fk_articles_authors FOREIGN KEY (author_id) REFERENCES "authors" (id)
Creating a foreign key, ignoring method call if the foreign key exists:
p.AddForeignKeyConstraint("articles", "authors", AddForeignKeyConstraintOptions{IfNotExists: true})
Creating a foreign key on a specific column:
p.AddForeignKeyConstraint("articles", "users", AddForeignKeyConstraintOptions{Column: "author_id", PrimaryKey: "lng_id"})
Generates:
ALTER TABLE "articles" ADD CONSTRAINT fk_articles_users FOREIGN KEY (author_id) REFERENCES "users" (lng_id)
Creating a composite foreign key:
Assuming "carts" Table has "(shop_id, user_id)" as a primary key. p.AddForeignKeyConstraint("orders", "carts", AddForeignKeyConstraintOptions{PrimaryKey: []string{"shop_id", "user_id"}})
Generates:
ALTER TABLE "orders" ADD CONSTRAINT fk_orders_carts FOREIGN KEY (cart_shop_id, cart_user_id) REFERENCES "carts" (shop_id, user_id)
Creating a cascading foreign key:
p.AddForeignKeyConstraint("articles", "authors", AddForeignKeyConstraintOptions{OnDelete: "cascade"})
Generates:
ALTER TABLE "articles" ADD CONSTRAINT fk_articles_authors FOREIGN KEY (author_id) REFERENCES "authors" (id) ON DELETE CASCADE
func (*Schema) AddIndex ¶
AddIndex adds a new index to the Table. Columns is a list of column names to index.
Creating a simple index:
p.AddIndex("products", []string{"name"}, IndexOptions{})
Generates:
CREATE INDEX idx_products_name ON "products" (name)
Creating a unique index:
p.AddIndex("products", []string{"name"}, IndexOptions{Unique: true})
Generates:
CREATE UNIQUE INDEX idx_products_name ON "products" (name)
Creating an index with a custom name:
p.AddIndex("products", []string{"name"}, IndexOptions{IndexNameBuilder: func(Table schema.TableName, Columns []string) string { return "index_products_on_name" }})
Generates:
CREATE INDEX index_products_on_name ON "products" (name)
Creating an index if it does not exist:
p.AddIndex("products", []string{"name"}, IndexOptions{IfNotExists: true})
Generates:
CREATE INDEX IF NOT EXISTS idx_products_name ON "products" (name)
Creating an index with a method:
p.AddIndex("products", []string{"name"}, IndexOptions{Method: "btree"})
Generates:
CREATE INDEX idx_products_name ON "products" USING btree (name)
Creating an index concurrently:
p.AddIndex("products", []string{"name"}, IndexOptions{Concurrent: true})
Generates:
CREATE INDEX CONCURRENTLY idx_products_name ON "products" (name)
Creating an index with a custom order:
p.AddIndex("products", []string{"name"}, IndexOptions{Order: "DESC"})
Generates:
CREATE INDEX idx_products_name ON "products" (name DESC)
Creating an index with custom order per column:
p.AddIndex("products", []string{"name", "price"}, IndexOptions{OrderPerColumn: map[string]string{"name": "DESC"}})
Generates:
CREATE INDEX idx_products_name_price ON "products" (name DESC, price)
Creating an index with a predicate:
p.AddIndex("products", []string{"name"}, IndexOptions{Predicate: "name IS NOT NULL"})
Generates:
CREATE INDEX idx_products_name ON "products" (name) WHERE name IS NOT NULL
func (*Schema) AddPrimaryKeyConstraint ¶
func (p *Schema) AddPrimaryKeyConstraint(tableName schema.TableName, columns []string, opts ...schema.PrimaryKeyConstraintOptions)
AddPrimaryKeyConstraint adds a new primary key to the Table.
Example:
p.AddPrimaryKeyConstraint("users", []string{"id"}, PrimaryKeyConstraintOptions{})
Generates:
ALTER TABLE "users" ADD CONSTRAINT PRIMARY KEY (id)
Adding a composite primary key:
p.AddPrimaryKeyConstraint("users", []string{"id", "name"}, PrimaryKeyConstraintOptions{})
Generates:
ALTER TABLE "users" ADD CONSTRAINT PRIMARY KEY (id, name)
Adding a primary key if it does not exist:
p.AddPrimaryKeyConstraint("users", []string{"id"}, PrimaryKeyConstraintOptions{IfNotExists: true})
Generates:
ALTER TABLE "users" ADD CONSTRAINT IF NOT EXISTS PRIMARY KEY (id)
func (*Schema) AddTableComment ¶
func (p *Schema) AddTableComment(tableName schema.TableName, comment *string, opts ...schema.TableCommentOptions)
AddTableComment adds a comment to a table in the database.
Example:
p.AddTableComment("users", utils.Ptr("This table contains the users"))
Generates:
COMMENT ON TABLE "users" IS 'This table contains the users'
To remove a comment from a table:
p.AddTableComment("users", nil)
Generates:
COMMENT ON TABLE "users" IS NULL
func (*Schema) AddTimestamps ¶
AddTimestamps adds the timestamps columns created_at and updated_at to the table. It's a shortcut for adding two columns with the current timestamp as default value.
Example:
p.AddTimestamps("users")
Generates:
ALTER TABLE "users" ADD "created_at" TIMESTAMP NOT NULL DEFAULT NOW() ALTER TABLE "users" ADD "updated_at" TIMESTAMP NOT NULL DEFAULT NOW()
func (*Schema) ChangeColumnDefault ¶
func (p *Schema) ChangeColumnDefault(tableName schema.TableName, columnName, defaultValue string, opts ...schema.ChangeColumnDefaultOptions)
ChangeColumnDefault changes the default value of a column.
Example:
p.ChangeColumnDefault("users", "status", "'draft'")
Generates:
ALTER TABLE "users" ALTER COLUMN "status" SET DEFAULT 'draft'
func (*Schema) ChangeColumnType ¶
func (p *Schema) ChangeColumnType(tableName schema.TableName, columnName string, columnType schema.ColumnType, opts ...schema.ChangeColumnTypeOptions)
ChangeColumnType changes the column type and options of a column.
Example:
p.ChangeColumnType("users", "name", schema.ColumnTypeString, schema.ColumnOptions{Limit: 20})
Generates:
ALTER TABLE "users" ALTER COLUMN "name" TYPE VARCHAR(20)
Changing a column type with a custom cast: For a table like:
CREATE TABLE users (invoice_no TEXT); p.ChangeColumnType("users", "invoice_no", schema.ColumnTypeInteger, schema.ChangeColumnTypeOptions{Using: "invoice_no::integer"})
Generates:
ALTER TABLE "users" ALTER COLUMN "invoice_no" TYPE INTEGER USING invoice_no::integer
func (*Schema) ColumnExist ¶
ColumnExist checks if a column exists in the Table.
func (*Schema) ConstraintExist ¶
ConstraintExist checks if a constraint exists in the Table.
func (*Schema) CreateEnum ¶
func (s *Schema) CreateEnum(name string, values []string, opts ...schema.CreateEnumOptions)
CreateEnum create a new enum type Is auto reversible Be careful because you can't remove an enum value easily.
Example:
schema.CreateEnum("my_enum", []string{"a", "b", "c"})
Generates:
CREATE TYPE my_enum AS ENUM ('a', 'b', 'c');
func (*Schema) CreateTable ¶
func (p *Schema) CreateTable(tableName schema.TableName, f func(*PostgresTableDef), opts ...schema.TableOptions)
CreateTable creates a new table in the database.
Example:
p.CreateTable("users", func(t *pg.PostgresTableDef) { t.Serial("id") t.FormatRecords("name") t.Integer("age") })
Generates:
CREATE TABLE "users" ( "id" serial PRIMARY KEY, "name" text, "age" integer )
note: the primary key column must be defined in the table.
To create a table without a primary key:
p.CreateTable("users", func(t *pg.PostgresTableDef) { t.FormatRecords("name") }, schema.TableOptions{ WithoutPrimaryKey: true })
Generates:
CREATE TABLE "users" ( "name" text )
To create a table with a composite primary key:
p.CreateTable("users", func(t *pg.PostgresTableDef) { t.FormatRecords("name") t.Integer("age") }, schema.TableOptions{ PrimaryKeys: []string{"name", "age"} })
Generates:
CREATE TABLE "users" ( "name" text, "age" integer ) ALTER TABLE "users" ADD PRIMARY KEY ("name", "age")
note: You can use PrimaryKeys to specify the primary key name (without creating a composite primary key).
To add index to the table:
p.CreateTable("users", func(t *pg.PostgresTableDef) { t.FormatRecords("name") t.Index([]string{"name"}) })
Generates:
CREATE TABLE "users" ( "name" text ) CREATE INDEX idx_users_name ON "users" (name)
To add foreign key to the table:
p.CreateTable("users", func(t *pg.PostgresTableDef) { t.FormatRecords("name") t.Integer("article_id") t.ForeignKey("articles") })
Generates:
CREATE TABLE "users" ( "name" text, "article_id" integer ) ALTER TABLE "users" ADD CONSTRAINT fk_users_articles FOREIGN KEY (article_id) REFERENCES "articles" (id)
To add created_at, updated_at Columns to the table:
p.CreateTable("users", func(t *pg.PostgresTableDef) { t.FormatRecords("name") t.Timestamps() })
Generates:
CREATE TABLE "users" ( "name" text, "created_at" TIMESTAMP(6) DEFAULT 'now()', "updated_at" TIMESTAMP(6) DEFAULT 'now()' )
func (*Schema) DropCheckConstraint ¶
func (p *Schema) DropCheckConstraint(tableName schema.TableName, constraintName string, opts ...schema.DropCheckConstraintOptions)
DropCheckConstraint drops a check constraint from the Table.
Example:
p.DropCheckConstraint("products", "price_check")
Generates:
ALTER TABLE "products" DROP CONSTRAINT price_check
Dropping a check constraint if it exists:
p.DropCheckConstraint("products", "price_check", schema.DropCheckConstraintOptions{IfExists: true})
Generates:
ALTER TABLE "products" DROP CONSTRAINT IF EXISTS price_check
Dropping a check constraint with a reversible expression:
p.DropCheckConstraint("products", "price_check", schema.DropCheckConstraintOptions{ Reversible: schema.CheckConstraintOptions{Expression: "price > 0"}, })
Generates:
ALTER TABLE "products" ADD CONSTRAINT price_check CHECK (price > 0)
func (*Schema) DropColumn ¶
func (p *Schema) DropColumn(tableName schema.TableName, columnName string, opt ...schema.DropColumnOptions)
DropColumn drops a column from the table.
Example:
p.DropColumn("users", "name")
Generates:
ALTER TABLE "users" DROP COLUMN "name"
Dropping a column if it exists:
p.DropColumn("users", "name", schema.DropColumnOptions{IfExists: true})
Generates:
ALTER TABLE "users" DROP COLUMN IF EXISTS "name"
To be able to reverse the operation you must provide the Reversible parameter:
p.DropColumn("users", "name", schema.DropColumnOptions{Reversible: &schema.ReversibleColumn{ColumnType: "VARCHAR(255)"}})
Generates:
ALTER TABLE "users" ADD "name" VARCHAR(255)
func (*Schema) DropEnum ¶
func (s *Schema) DropEnum(name string, opts ...schema.DropEnumOptions)
DropEnum drop an enum type Must precise options.Reversible to be able to reverse the operation
Example:
schema.DropEnum("my_enum")
Generates:
DROP TYPE my_enum;
func (*Schema) DropExtension ¶
func (p *Schema) DropExtension(name string, opt ...schema.DropExtensionOptions)
DropExtension drops an extension from the database.
Example:
p.DropExtension("uuid", DropExtensionOptions{})
Generates:
DROP EXTENSION IF EXISTS "uuid-ossp"
Dropping an extension if it exists:
p.DropExtension("uuid", DropExtensionOptions{IfExists: true})
Generates:
DROP EXTENSION IF EXISTS "uuid-ossp"
To reverse the operation, you can use the reversible option:
p.DropExtension("uuid", DropExtensionOptions{ Reversible: &schema.ExtensionOptions{} })
Generates:
CREATE EXTENSION "uuid-ossp"
func (*Schema) DropForeignKeyConstraint ¶
func (p *Schema) DropForeignKeyConstraint(from, to schema.TableName, opt ...schema.DropForeignKeyConstraintOptions)
DropForeignKeyConstraint drops a foreign key from the Table.
Example:
p.DropForeignKeyConstraint("articles", "authors")
Generates:
ALTER TABLE "articles" DROP CONSTRAINT fk_articles_authors
Dropping a foreign key if it exists:
p.DropForeignKeyConstraint("articles", "authors", schema.DropForeignKeyConstraintOptions{IfExists: true})
Generates:
ALTER TABLE "articles" DROP CONSTRAINT IF EXISTS fk_articles_authors
Dropping a foreign key with a reversible expression:
p.DropForeignKeyConstraint("articles", "authors", schema.DropForeignKeyConstraintOptions{ Reversible: schema.AddForeignKeyConstraintOptions{Column: "author_id", PrimaryKey: "lng_id"}, })
Generates:
ALTER TABLE "articles" ADD CONSTRAINT fk_articles_authors FOREIGN KEY (author_id) REFERENCES "authors" (lng_id)
func (*Schema) DropIndex ¶
func (p *Schema) DropIndex(table schema.TableName, columns []string, opt ...schema.DropIndexOptions)
DropIndex drops an index from the database.
Example:
p.DropIndex("products", []string{"name"}, DropIndexOptions{})
Generates:
DROP INDEX idx_products_name
Dropping an index if it exists:
p.DropIndex("products", []string{"name"}, DropIndexOptions{IfExists: true})
Generates:
DROP INDEX IF EXISTS idx_products_name
To reverse the operation, you can use the reversible option:
p.DropIndex("products", []string{"name"}, DropIndexOptions{ Reversible: &schema.IndexOptions{} })
Generates:
CREATE INDEX idx_products_name ON "products" (name)
func (*Schema) DropPrimaryKeyConstraint ¶
func (p *Schema) DropPrimaryKeyConstraint(tableName schema.TableName, opts ...schema.DropPrimaryKeyConstraintOptions)
DropPrimaryKeyConstraint drops a primary key from the Table.
Example:
p.DropPrimaryKeyConstraint("users")
Generates:
ALTER TABLE "users" DROP CONSTRAINT pk_users
Dropping a primary key if it exists:
p.DropPrimaryKeyConstraint("users", schema.DropPrimaryKeyConstraintOptions{IfExists: true})
Generates:
ALTER TABLE "users" DROP CONSTRAINT IF EXISTS pk_users
Dropping a primary key with a reversible expression:
p.DropPrimaryKeyConstraint("users", schema.DropPrimaryKeyConstraintOptions{ Reversible: schema.PrimaryKeyConstraintOptions{Columns: []string{"id"}}, })
Generates:
ALTER TABLE "users" ADD CONSTRAINT pk_users PRIMARY KEY (id)
func (*Schema) DropTable ¶
func (p *Schema) DropTable(tableName schema.TableName, opts ...schema.DropTableOptions)
DropTable drops a table from the database.
Example:
p.DropTable("users", schema.DropTableOptions{})
Generates:
DROP TABLE "users"
To drop a table if it exists:
p.DropTable("users", schema.DropTableOptions{IfExists: true})
Generates:
DROP TABLE IF EXISTS "users"
To make the drop table reversible:
p.DropTable("users", schema.DropTableOptions{Reversible: &TableOption{ schema.TableName: "users", TableDefinition: Innerschema.Tablefunc(t *PostgresTableDef) { t.Serial("id") t.FormatRecords("name") }), }})
Generates:
CREATE TABLE "users" ( "id" serial PRIMARY KEY, "name" text )
func (*Schema) FindEnumUsage ¶
FindEnumUsage find all tables and columns that use the enum type schemaName is optional, if provided, it will restrict the search to the specified schema
func (*Schema) IndexExist ¶
IndexExist checks if an index exists in the Table.
func (*Schema) ListEnumValues ¶
func (*Schema) RenameColumn ¶
RenameColumn renames a column in the table. The column is renamed from oldColumnName to newColumnName.
Example:
p.RenameColumn("users", "name", "full_name")
Generates:
ALTER TABLE "users" RENAME COLUMN "name" TO "full_name"
func (*Schema) RenameEnum ¶
func (s *Schema) RenameEnum(oldName, newName string, opts ...schema.RenameEnumOptions)
RenameEnum rename an enum type Example:
schema.RenameEnum("old_enum", "new_enum")
Generates:
ALTER TYPE old_enum RENAME TO new_enum
func (*Schema) RenameEnumValue ¶
func (s *Schema) RenameEnumValue(name, oldName, newName string, opts ...schema.RenameEnumValueOptions)
RenameEnumValue rename an enum value Is auto reversible
Example:
schema.RenameEnumValue("my_enum", "old_value", "new_value")
Generates:
ALTER TYPE my_enum RENAME VALUE 'old_value' TO 'new_value'
func (*Schema) RenameTable ¶
RenameTable renames a table in the database.
Example:
p.RenameTable("users", "people")
Generates:
ALTER TABLE "users" RENAME TO "people"