dialect.go
dialect.go - Overview
-
Overview The
dialect.go
file defines adialect
struct and associated methods for performing database schema migrations and checks specific to PostgreSQL. It includes functions for altering column types, renaming columns and tables, checking for the existence of columns and tables, and updating primary keys. -
Detailed Documentation
Variables
Identity
: String representing the name of the identity column (default: "id").Integer
: String representing the integer data type (default: "bigint").Text
: String representing the text data type (default: "text").Org
: String representing the organization reference (default: "org").User
: String representing the user reference (default: "user").OrgReference
: String representing the foreign key constraint for the organization (default:("org_id") REFERENCES "organizations" ("id")
).UserReference
: String representing the foreign key constraint for the user (default:("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE
).
type dialect struct {}
- Purpose: Represents the PostgreSQL dialect.
- Parameters: None
- Returns: None
func (dialect *dialect) MigrateIntToTimestamp(ctx context.Context, bun bun.IDB, table string, column string) error
- Purpose: Migrates an integer column to a timestamp column in a given table.
- Parameters:
ctx
(context.Context): Context for the operation.bun
(bun.IDB): Bun database instance.table
(string): Table name.column
(string): Column name to migrate.
- Returns:
error
: An error if the migration fails, nil otherwise.
func (dialect *dialect) MigrateIntToBoolean(ctx context.Context, bun bun.IDB, table string, column string) error
- Purpose: Migrates an integer column to a boolean column in a given table.
- Parameters:
ctx
(context.Context): Context for the operation.bun
(bun.IDB): Bun database instance.table
(string): Table name.column
(string): Column name to migrate.
- Returns:
error
: An error if the migration fails, nil otherwise.
func (dialect *dialect) GetColumnType(ctx context.Context, bun bun.IDB, table string, column string) (string, error)
- Purpose: Retrieves the data type of a specified column in a given table.
- Parameters:
ctx
(context.Context): Context for the operation.bun
(bun.IDB): Bun database instance.table
(string): Table name.column
(string): Column name.
- Returns:
string
: The data type of the column.error
: An error if the retrieval fails, nil otherwise.
func (dialect *dialect) ColumnExists(ctx context.Context, bun bun.IDB, table string, column string) (bool, error)
- Purpose: Checks if a specified column exists in a given table.
- Parameters:
ctx
(context.Context): Context for the operation.bun
(bun.IDB): Bun database instance.table
(string): Table name.column
(string): Column name.
- Returns:
bool
: True if the column exists, false otherwise.error
: An error if the check fails, nil otherwise.
func (dialect *dialect) RenameColumn(ctx context.Context, bun bun.IDB, table string, oldColumnName string, newColumnName string) (bool, error)
- Purpose: Renames a column in a given table.
- Parameters:
ctx
(context.Context): Context for the operation.bun
(bun.IDB): Bun database instance.table
(string): Table name.oldColumnName
(string): Current column name.newColumnName
(string): New column name.
- Returns:
bool
: Returns true if rename is successfulerror
: An error if the rename fails, nil otherwise.
func (dialect *dialect) TableExists(ctx context.Context, bun bun.IDB, table interface{}) (bool, error)
- Purpose: Checks if a table exists.
- Parameters:
ctx
(context.Context): Context for the operation.bun
(bun.IDB): Bun database instance.table
(interface<{}>): Table model.
- Returns:
bool
: True if the table exists, false otherwise.error
: An error if the check fails, nil otherwise.
func (dialect *dialect) RenameTableAndModifyModel(ctx context.Context, bun bun.IDB, oldModel interface{}, newModel interface{}, references []string, cb func(context.Context) error) error
- Purpose: Renames a table and modifies its model, creating foreign key relationships.
- Parameters:
ctx
(context.Context): Context for the operation.bun
(bun.IDB): Bun database instance.oldModel
(interface<{}>): Old table model.newModel
(interface<{}>): New table model.references
([]string): List of references ("org", "user").cb
(func(context.Context) error): Callback function to execute after table creation and before dropping the old table.
- Returns:
error
: An error if the operation fails, nil otherwise.
func (dialect *dialect) AddNotNullDefaultToColumn(ctx context.Context, bun bun.IDB, table string, column string, columnType string, defaultValue string) error
- Purpose: Adds a NOT NULL constraint with a default value to a column.
- Parameters:
ctx
(context.Context): Context for the operation.bun
(bun.IDB): Bun database instance.table
(string): Table name.column
(string): Column name.columnType
(string): Column type.defaultValue
(string): Default value for the column.
- Returns:
error
: An error if the operation fails, nil otherwise.
func (dialect *dialect) UpdatePrimaryKey(ctx context.Context, bun bun.IDB, oldModel interface{}, newModel interface{}, reference string, cb func(context.Context) error) error
- Purpose: Updates the primary key of a table by creating a new table with the desired primary key configuration, copying data, and dropping the old table.
- Parameters:
ctx
(context.Context): Context for the operation.bun
(bun.IDB): Bun database instance.oldModel
(interface<{}>): Old table model.newModel
(interface<{}>): New table model.reference
(string): Reference type ("org", "user").cb
(func(context.Context) error): Callback function to execute after table creation and before dropping the old table.
- Returns:
error
: An error if the operation fails, nil otherwise.
func (dialect *dialect) AddPrimaryKey(ctx context.Context, bun bun.IDB, oldModel interface{}, newModel interface{}, reference string, cb func(context.Context) error) error
- Purpose: Adds a primary key to a table by creating a new table with the primary key, copying data, and dropping the old table.
- Parameters:
ctx
(context.Context): Context for the operation.bun
(bun.IDB): Bun database instance.oldModel
(interface<{}>): Old table model.newModel
(interface<{}>): New table model.reference
(string): Reference type ("org", "user").cb
(func(context.Context) error): Callback function to execute after table creation and before dropping the old table.
- Returns:
error
: An error if the operation fails, nil otherwise.
-
Code Examples N/A
-
Clarity and Accuracy The documentation is based solely on the code provided.
Include in Getting Started: NO