From 3264b823686d3483193b3da1e0cab53189dc6347 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 29 Apr 2014 15:39:06 +0800 Subject: [PATCH] Fix auto migration for sqlite --- README.md | 2 -- sqlite3.go | 12 +++--------- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 0c9e0cfa..71dad237 100644 --- a/README.md +++ b/README.md @@ -190,8 +190,6 @@ FYI, AutoMigrate will only add new columns, it won't change the current columns' If the table doesn't exist when AutoMigrate is called, gorm will create the table automatically. (the database first needs to be created manually though...). -(only postgres and mysql supported) - ```go db.AutoMigrate(User{}) ``` diff --git a/sqlite3.go b/sqlite3.go index 1b2fadc6..400d811e 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -63,18 +63,12 @@ func (s *sqlite3) Quote(key string) string { func (s *sqlite3) HasTable(scope *Scope, tableName string) bool { var count int newScope := scope.New(nil) - newScope.Raw(fmt.Sprintf("SELECT count(*) FROM INFORMATION_SCHEMA.tables where table_name = %v", newScope.AddToVars(tableName))) + newScope.Raw(fmt.Sprintf("SELECT count(*) FROM sqlite_master WHERE type='table' AND name=%v", newScope.AddToVars(tableName))) newScope.DB().QueryRow(newScope.Sql, newScope.SqlVars...).Scan(&count) return count > 0 } func (s *sqlite3) HasColumn(scope *Scope, tableName string, columnName string) bool { - var count int - newScope := scope.New(nil) - newScope.Raw(fmt.Sprintf("SELECT count(*) FROM information_schema.columns WHERE table_name = %v AND column_name = %v", - newScope.AddToVars(tableName), - newScope.AddToVars(columnName), - )) - newScope.DB().QueryRow(newScope.Sql, newScope.SqlVars...).Scan(&count) - return count > 0 + _, err := scope.DB().Exec(fmt.Sprintf("SELECT %v FROM %v LIMIT 1", columnName, tableName)) + return err == nil }