modify_datetime.go
011_modify_datetime.go - Overview
-
Overview This file defines a SQL migration to modify the datetime columns in several tables for SQLite databases. It renames existing datetime columns, adds new timestamp columns, copies data from the old columns to the new ones, and then drops the old columns. This migration is intended to update the schema of older SQLite databases.
-
Detailed Documentation
modifyDatetime
- Purpose: Represents a SQL migration to modify datetime columns.
- Parameters: None
- Returns: None
NewModifyDatetimeFactory
- Purpose: Creates a new factory for the
modifyDatetime
SQL migration. - Parameters: None
- Returns: A
factory.ProviderFactory[SQLMigration, Config]
that createsmodifyDatetime
instances.
newModifyDatetime
- Purpose: Creates a new
modifyDatetime
instance. - Parameters:
_
:context.Context
- Context (not used)._
:factory.ProviderSettings
- Factory provider settings (not used)._
:Config
- Configuration (not used).
- Returns:
SQLMigration
: A newmodifyDatetime
instance.error
: An error, if any. Returns nil in this case.
(*modifyDatetime) Register
- Purpose: Registers the Up and Down migration functions with the provided
migrate.Migrations
instance. - Parameters:
migrations
:*migrate.Migrations
- The migration registry.
- Returns:
error
: An error, if any.
(*modifyDatetime) Up
- Purpose: Executes the migration to modify datetime columns. This function only executes if the database dialect is SQLite. It iterates through a predefined list of tables and columns, calling the
modifyColumn
function for each. - Parameters:
ctx
:context.Context
- The context for the operation.db
:*bun.DB
- The database connection.
- Returns:
error
: An error, if any.
modifyColumn
- Purpose: Modifies a specific column in a table by renaming the original column, adding a new column with the correct type (TIMESTAMP), copying the data, and then dropping the original column.
- Parameters:
ctx
:context.Context
- The context for the operation.tx
:bun.Tx
- The database transaction.table
:string
- The name of the table.column
:string
- The name of the column.
- Returns:
error
: An error, if any.
(*modifyDatetime) Down
- Purpose: Defines the rollback operation for the migration, which is currently empty (does nothing).
- Parameters:
ctx
:context.Context
- The context for the operation.db
:*bun.DB
- The database connection.
- Returns:
error
: An error, if any. Always returns nil.
- Code Examples
None
Include in Getting Started: NO