Skip to content

Commit 9c5ee1e

Browse files
committed
Allow to disable datetime precision, close #13
1 parent df4a436 commit 9c5ee1e

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

migrator.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -192,29 +192,31 @@ func (m Migrator) DropConstraint(value interface{}, name string) error {
192192
func (m Migrator) ColumnTypes(value interface{}) (columnTypes []gorm.ColumnType, err error) {
193193
columnTypes = make([]gorm.ColumnType, 0)
194194
err = m.RunWithValue(value, func(stmt *gorm.Statement) error {
195-
currentDatabase := m.DB.Migrator().CurrentDatabase()
196-
columns, err := m.DB.Raw(
197-
"SELECT column_name, is_nullable, data_type, character_maximum_length, "+
198-
"numeric_precision, numeric_scale, datetime_precision "+
199-
"FROM information_schema.columns WHERE table_schema = ? AND table_name = ?",
200-
currentDatabase, stmt.Table).Rows()
195+
var (
196+
currentDatabase = m.DB.Migrator().CurrentDatabase()
197+
columnTypeSQL = "SELECT column_name, is_nullable, data_type, character_maximum_length, numeric_precision, numeric_scale "
198+
)
199+
200+
if !m.DisableDatetimePrecision {
201+
columnTypeSQL += ", datetime_precision "
202+
}
203+
columnTypeSQL += "FROM information_schema.columns WHERE table_schema = ? AND table_name = ?"
204+
205+
columns, err := m.DB.Raw(columnTypeSQL, currentDatabase, stmt.Table).Rows()
201206
if err != nil {
202207
return err
203208
}
204209
defer columns.Close()
205210

206211
for columns.Next() {
207212
var column Column
208-
err = columns.Scan(
209-
&column.name,
210-
&column.nullable,
211-
&column.datatype,
212-
&column.maxlen,
213-
&column.precision,
214-
&column.scale,
215-
&column.datetimeprecision,
216-
)
217-
if err != nil {
213+
var values = []interface{}{&column.name, &column.nullable, &column.datatype, &column.maxlen, &column.precision, &column.scale}
214+
215+
if !m.DisableDatetimePrecision {
216+
values = append(values, &column.datetimeprecision)
217+
}
218+
219+
if err = columns.Scan(values...); err != nil {
218220
return err
219221
}
220222
columnTypes = append(columnTypes, column)

mysql.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type Config struct {
2222
Conn gorm.ConnPool
2323
SkipInitializeWithVersion bool
2424
DefaultStringSize uint
25+
DefaultDatetimePrecision *int
2526
DisableDatetimePrecision bool
2627
DontSupportRenameIndex bool
2728
DontSupportRenameColumn bool
@@ -56,6 +57,11 @@ func (dialector Dialector) Initialize(db *gorm.DB) (err error) {
5657
dialector.DriverName = "mysql"
5758
}
5859

60+
if dialector.DefaultDatetimePrecision == nil {
61+
var defaultDatetimePrecision = 3
62+
dialector.DefaultDatetimePrecision = &defaultDatetimePrecision
63+
}
64+
5965
if dialector.Conn != nil {
6066
db.ConnPool = dialector.Conn
6167
} else {
@@ -260,14 +266,12 @@ func (dialector Dialector) DataTypeOf(field *schema.Field) string {
260266
case schema.Time:
261267
precision := ""
262268

263-
if !dialector.DisableDatetimePrecision {
264-
if field.Precision == 0 {
265-
field.Precision = 3
266-
}
269+
if !dialector.DisableDatetimePrecision && field.Precision == 0 {
270+
field.Precision = *dialector.DefaultDatetimePrecision
271+
}
267272

268-
if field.Precision > 0 {
269-
precision = fmt.Sprintf("(%d)", field.Precision)
270-
}
273+
if field.Precision > 0 {
274+
precision = fmt.Sprintf("(%d)", field.Precision)
271275
}
272276

273277
if field.NotNull || field.PrimaryKey {

0 commit comments

Comments
 (0)