Skip to content

Instantly share code, notes, and snippets.

@nathan-osman
Created March 9, 2021 04:34
Show Gist options
  • Save nathan-osman/6cadc8bc53dda8867b45e35176c8cb74 to your computer and use it in GitHub Desktop.
Save nathan-osman/6cadc8bc53dda8867b45e35176c8cb74 to your computer and use it in GitHub Desktop.

Revisions

  1. nathan-osman created this gist Mar 9, 2021.
    48 changes: 48 additions & 0 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    package main

    import (
    "gorm.io/driver/sqlite"
    "gorm.io/gorm"
    "gorm.io/gorm/schema"
    )

    // CustomDialector allows for customization of the type affinity for booleans.
    type CustomDialector struct {
    sqlite.Dialector
    }

    // CustomMigrator overrides behavior of the SQLite migrator.
    type CustomMigrator struct {
    sqlite.Migrator
    }

    // DataTypeOf returns the data type for the supplied field.
    func (d CustomDialector) DataTypeOf(field *schema.Field) string {
    if field.DataType == schema.Bool {
    return "integer"
    }
    return d.Dialector.DataTypeOf(field)
    }

    // Migrator returns the migrator for the dialector.
    func (d CustomDialector) Migrator(db *gorm.DB) gorm.Migrator {
    m := d.Dialector.Migrator(db).(sqlite.Migrator)
    m.Migrator.Config.Dialector = d
    return m
    }

    func main() {
    c, err := gorm.Open(
    CustomDialector{
    sqlite.Dialector{
    DSN: "filename.db",
    },
    },
    &gorm.Config{},
    )
    if err != nil {
    panic(err)
    }

    // TODO: do something with c
    }