db

package
v0.0.0-...-128919f Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateMigrationFiles

func CreateMigrationFiles(filename string, t Type) error

CreateMigrationFiles creates migration files for the specified database type. It generates both the up and down migration files with a timestamp and the provided filename. The file extensions are determined based on the database type (SQL for Postgres, JSON for MongoDB). If the filename is empty, it returns an error indicating that the migration filename is not found. If the database type is invalid, it returns an error indicating that the database type is invalid. If there is an error creating the up migration file, it returns the error. If there is an error creating the down migration file, it removes the up migration file and returns the error.

Parameters: - filename: The name of the migration file. - t: The type of the database (Postgres or MongoDB).

Returns: - error: Any error that occurred during the file creation process.

func NewMongoInstance

func NewMongoInstance(client *mongo.Client) (*migrate.Migrate, error)

NewMongoInstance creates a new migration instance for MongoDB. It initializes a MongoDB driver with the provided MongoDB client and then creates a new migration instance using this driver.

Parameters: - client: A pointer to a mongo.Client instance representing the MongoDB client.

Returns: - *migrate.Migrate: A pointer to the created migration instance. - error: An error if the driver initialization or migration instance creation fails.

func NewPostgresInstance

func NewPostgresInstance(db *sql.DB) (*migrate.Migrate, error)

NewPostgresInstance creates a new migration instance for Postgres. It initializes a Postgres driver with the provided database connection and then creates a new migration instance using this driver.

Parameters: - db: A pointer to sql.DB instance representing the database connection.

Returns: - *migrate.Migrate: A pointer to the created migration instance. - error: An error if the driver initialization or migration instance creation fails.

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 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 migrate.Migrate instance that will be run.

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

Types

type Type

type Type string
const (
	PostgresDB Type = "postgres"
	MongoDB    Type = "mongodb"
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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