Skip to main content

dialect.go

dialect.go - Overview

  1. Overview The dialect.go file defines a dialect struct and implements methods for performing database schema migrations and checks specific to SQLite. It includes functionalities such as altering column types, renaming columns/tables, checking for the existence of tables/columns, adding constraints and updating primary keys.

  2. Detailed Documentation

Identity

  • Purpose: Defines the string constant "id", likely used as a standard primary key column name.

Integer

  • Purpose: Defines the string constant "INTEGER", representing the integer data type in SQLite.

Text

  • Purpose: Defines the string constant "TEXT", representing the text data type in SQLite.

Org

  • Purpose: Defines the string constant "org", likely used as a standard organization column name.

User

  • Purpose: Defines the string constant "user", likely used as a standard user column name.

OrgReference

  • Purpose: Defines a string constant representing a foreign key constraint referencing the organizations table.

UserReference

  • Purpose: Defines a string constant representing a foreign key constraint referencing the users table with ON DELETE CASCADE ON UPDATE CASCADE options.

dialect struct

  • Purpose: Represents the SQLite dialect. It doesn't have any fields.

(*dialect) MigrateIntToTimestamp(ctx context.Context, bun bun.IDB, table string, column string) error

  • Purpose: Migrates an integer column to a timestamp column in the specified table.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • bun (bun.IDB): The bun database instance.
    • table (string): The name of the table.
    • column (string): The name of the column to migrate.
  • Returns: error: An error if the migration fails, or nil if successful.

(*dialect) MigrateIntToBoolean(ctx context.Context, bun bun.IDB, table string, column string) error

  • Purpose: Migrates an integer column to a boolean column in the specified table.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • bun (bun.IDB): The bun database instance.
    • table (string): The name of the table.
    • column (string): The name of the column to migrate.
  • Returns: error: An error if the migration fails, or nil if successful.

(*dialect) GetColumnType(ctx context.Context, bun bun.IDB, table string, column string) (string, error)

  • Purpose: Retrieves the data type of a column in the specified table.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • bun (bun.IDB): The bun database instance.
    • table (string): The name of the table.
    • column (string): The name of the column.
  • Returns:
    • string: The data type of the column.
    • error: An error if the retrieval fails, or nil if successful.

(*dialect) ColumnExists(ctx context.Context, bun bun.IDB, table string, column string) (bool, error)

  • Purpose: Checks if a column exists in the specified table.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • bun (bun.IDB): The bun database instance.
    • table (string): The name of the table.
    • column (string): The name of the column.
  • Returns:
    • bool: true if the column exists, false otherwise.
    • error: An error if the check fails, or nil if successful.

(*dialect) RenameColumn(ctx context.Context, bun bun.IDB, table string, oldColumnName string, newColumnName string) (bool, error)

  • Purpose: Renames a column in the specified table.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • bun (bun.IDB): The bun database instance.
    • table (string): The name of the table.
    • oldColumnName (string): The current name of the column.
    • newColumnName (string): The new name for the column.
  • Returns:
    • bool: Returns true if the column has been renamed successfully or new column exists already, false otherwise
    • error: An error if the renaming fails, or nil if successful.

(*dialect) TableExists(ctx context.Context, bun bun.IDB, table interface{}) (bool, error)

  • Purpose: Checks if a table exists in the database.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • bun (bun.IDB): The bun database instance.
    • table (interface<{}>): The table model.
  • Returns:
    • bool: true if the table exists, false otherwise.
    • error: An error if the check fails, or nil if successful.

(*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 the model, creating a new table based on newModel, copying data, and dropping the old table. Adds foreign key constraints based on references.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • bun (bun.IDB): The bun database instance.
    • oldModel (interface<{}>): The old table model.
    • newModel (interface<{}>): The new table model.
    • references ([]string): A slice of strings indicating which foreign key references to add ("org", "user").
    • cb (func(context.Context) error): A callback function to execute after creating the new table, but before dropping the old one.
  • Returns: error: An error if the operation fails, or nil if successful.

(*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 in the specified table.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • bun (bun.IDB): The bun database instance.
    • table (string): The name of the table.
    • column (string): The name of the column.
    • columnType (string): The data type of the column.
    • defaultValue (string): The default value for the column.
  • Returns: error: An error if the operation fails, or nil if successful.

(*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, creating a new table based on newModel, copying data, dropping the old table, and renaming the new table to the old table's name.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • bun (bun.IDB): The bun database instance.
    • oldModel (interface<{}>): The old table model.
    • newModel (interface<{}>): The new table model.
    • reference (string): A string indicating which foreign key references to add ("org", "user").
    • cb (func(context.Context) error): A callback function to execute after creating the new table, but before dropping the old one.
  • Returns: error: An error if the operation fails, or nil if successful.

(*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, creating a new table based on newModel, copying data, dropping the old table, and renaming the new table to the old table's name.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • bun (bun.IDB): The bun database instance.
    • oldModel (interface<{}>): The old table model.
    • newModel (interface<{}>): The new table model.
    • reference (string): A string indicating which foreign key references to add ("org", "user").
    • cb (func(context.Context) error): A callback function to execute after creating the new table, but before dropping the old one.
  • Returns: error: An error if the operation fails, or nil if successful.
  1. Code Examples N/A

  2. Clarity and Accuracy The documentation is based on the code provided.

Include in Getting Started: NO