postgres

package
v0.0.0-...-64f6abd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 20, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateMigrationFiles

func CreateMigrationFiles(filename string) error

CreateMigrationFiles creates two new migration files with the provided filename. The function generates a timestamp and appends it to the filename to ensure uniqueness. It creates an 'up' migration file and a 'down' migration file. If the filename is not provided, it returns an error. If there is an error while creating the 'up' migration file, it returns the error. If there is an error while creating the 'down' migration file, it deletes the 'up' migration file and returns the error.

Parameters: filename: The base name for the migration files. The actual file names will be prepended with a timestamp and appended with '.up.sql' or '.down.sql'.

Returns: If successful, returns nil. If an error occurs, returns the error.

func IsForeignKeyViolation

func IsForeignKeyViolation(err error) bool

IsForeignKeyViolation checks if the provided error is a foreign key violation error in PostgresSQL. It returns true if the error is a foreign key violation error (error code "23503"), and false otherwise.

func IsInvalidTextRepresentation

func IsInvalidTextRepresentation(err error) bool

IsInvalidTextRepresentation checks if the provided error is an invalid text representation error in PostgresSQL. It returns true if the error is an invalid text representation error (error code "22P02"), and false otherwise.

func IsNotNullViolation

func IsNotNullViolation(err error) bool

IsNotNullViolation checks if the provided error is a not null violation error in PostgresSQL. It returns true if the error is a not null violation error (error code "23502"), and false otherwise.

func IsUniqueViolation

func IsUniqueViolation(err error) bool

IsUniqueViolation checks if the provided error is a unique violation error in PostgresSQL. It returns true if the error is a unique violation error (error code "23505"), and false otherwise.

func NewMigrationInstance

func NewMigrationInstance(data *gorm.DB) (*migrate.Migrate, error)

NewMigrationInstance creates a new migration instance using the provided gorm.DB data. It first retrieves the underlying sql.DB from the gorm.DB instance. If there is an error while retrieving the sql.DB, it returns the error. It then creates a new postgres database driver using the sql.DB and a default postgres.Config. If there is an error while creating the postgres driver, it returns the error. It then creates a new migration instance using the migration source path, the database name, and the postgres driver. If there is an error while creating the migration instance, it returns the error.

Parameters: data: A pointer to a gorm.DB instance that will be used to create the migration instance.

Returns: If successful, returns a pointer to the new migration instance and nil. If an error occurs, returns nil and the error.

func RollbackMigration

func RollbackMigration(migration *migrate.Migrate) error

RollbackMigration rolls back the last applied migration. It calls the Steps method on the migration instance with a parameter of -1, which rolls back the last migration. If there is an error while rolling back the migration, it returns the error. If there are no migrations to roll back, it returns nil.

Parameters: migration: A pointer to a migrate.Migrate instance that will be rolled back.

Returns: If successful, returns nil. If an error occurs, returns the error.

func RunMigration

func RunMigration(migration *migrate.Migrate) error

RunMigration runs the provided migration. It calls the Up method on the migration instance, which applies all up migrations. If there is an error while running the migrations, it returns the error. If there are no migrations to apply, it returns nil.

Parameters: migration: A pointer to a migrate.Migrate instance that will be run.

Returns: If successful, returns nil. If an error occurs, returns the error.

Types

type Config

type Config struct {
	Database, Host, Username, Password, Params, LogMode string
	Port, MaxIdleConn, MaxOpenConn                      int
	DebugEnabled                                        bool
}

Config is a struct that holds the configuration for the database connection. It contains the following fields: - Database: the name of the database - Host: the host of the database - Username: the username to connect to the database - Password: the password to connect to the database - Params: additional parameters for the database connection - LogMode: the log mode for the database connection - Port: the port number of the database - MaxIdleConn: the maximum number of idle connections for the database - MaxOpenConn: the maximum number of open connections for the database - DebugEnabled: a boolean indicating whether debug mode is enabled or not

func (Config) Client

func (c Config) Client() (*gorm.DB, error)

Client is a method on the Config struct that establishes a database connection using the configuration in the Config struct. It first constructs the data source name (dsn) using the configuration fields. Then it sets the log mode based on the LogMode field in the Config struct. It then attempts to open a connection to the database using the constructed dsn and log mode. If an error occurs during the connection process, it returns the error. If the DebugEnabled field in the Config struct is set to true, it enables debug mode on the database connection. It then retrieves the underlying sql.DB object from the gorm.DB object and sets the maximum idle and open connections. Finally, it returns the established database connection and any error that occurred during the process.

Returns: - *gorm.DB: The established database connection. - error: Any error that occurred during the process.

func (Config) Close

func (c Config) Close() error

Close is a method on the Config struct that closes the database connection. It first retrieves the underlying sql.DB object from the gorm.DB object. If an error occurs during this process, it returns the error. If the retrieval is successful, it calls the Close method on the sql.DB object to close the database connection. If an error occurs while closing the database connection, it returns the error. If the database connection is successfully closed, it returns nil.

func (Config) DB

func (c Config) DB() *gorm.DB

DB is a method on the Config struct that retrieves the current database connection. It does not take any parameters. It returns the *gorm.DB object representing the current database connection.

func (Config) Ping

func (c Config) Ping() error

Ping is a method on the Config struct that checks the database connection by sending a ping. If the database is reachable and responds to the ping, it returns nil. If the database is not reachable or does not respond to the ping, it returns an error.

func (Config) Reset

func (c Config) Reset() error

Reset is a method on the Config struct that resets the database connection. It first resets the once.Do function to allow the Client method to be called again. Then it calls the Client method to establish a new database connection. If an error occurs during this process, it returns the error. If the process is successful, it returns nil.

func (Config) SetDB

func (c Config) SetDB(conn *gorm.DB)

SetDB is a method on the Config struct that sets the database connection. It takes one parameter: - conn: The *gorm.DB object representing the database connection to be set. This method does not return any value.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL