2023-12-16 07:12:20 +00:00
|
|
|
package migrations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/uptrace/bun"
|
2024-01-06 09:26:56 +00:00
|
|
|
"github.com/uptrace/bun/dialect/feature"
|
2023-12-16 07:12:20 +00:00
|
|
|
"github.com/uptrace/bun/migrate"
|
2024-01-06 09:26:56 +00:00
|
|
|
"github.com/uptrace/bun/schema"
|
2023-12-16 07:12:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var migrations = migrate.NewMigrations()
|
|
|
|
|
|
|
|
func NewMigrator(db *bun.DB, opts ...migrate.MigratorOption) *migrate.Migrator {
|
2024-01-14 10:04:17 +00:00
|
|
|
// db isn't required in all cases (e.g. for creating migrations)
|
|
|
|
if db != nil {
|
|
|
|
db = db.WithNamedArg("ID_COL", autoincrementIDColumn(db))
|
|
|
|
}
|
|
|
|
return migrate.NewMigrator(db, migrations, opts...)
|
2024-01-06 09:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type hasFeaturer interface {
|
|
|
|
HasFeature(feat feature.Feature) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func autoincrementIDColumn(f hasFeaturer) schema.QueryAppender {
|
|
|
|
if f.HasFeature(feature.GeneratedIdentity) {
|
|
|
|
// postgres
|
|
|
|
return bun.Safe("id bigint GENERATED BY DEFAULT AS IDENTITY primary key")
|
|
|
|
}
|
|
|
|
|
|
|
|
// sqlite
|
|
|
|
return bun.Safe("id INTEGER PRIMARY KEY")
|
2023-12-16 07:12:20 +00:00
|
|
|
}
|