Documentation
¶
Index ¶
- func CreateMigrationFiles(filename string, t Type) error
- func NewMongoInstance(client *mongo.Client) (*migrate.Migrate, error)
- func NewPostgresInstance(db *sql.DB) (*migrate.Migrate, error)
- func RollbackMigration(migration *migrate.Migrate) error
- func RunMigration(migration *migrate.Migrate) error
- type Type
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CreateMigrationFiles ¶
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 ¶
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 ¶
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.