Test postgres autoincrement check

This commit is contained in:
Jinzhu 2022-10-08 17:16:32 +08:00
parent 983e96f142
commit e93dc3426e
2 changed files with 40 additions and 1 deletions

View File

@ -9,7 +9,7 @@ require (
github.com/lib/pq v1.10.7 github.com/lib/pq v1.10.7
golang.org/x/crypto v0.0.0-20221005025214-4161e89ecf1b // indirect golang.org/x/crypto v0.0.0-20221005025214-4161e89ecf1b // indirect
gorm.io/driver/mysql v1.4.0 gorm.io/driver/mysql v1.4.0
gorm.io/driver/postgres v1.4.3 gorm.io/driver/postgres v1.4.4
gorm.io/driver/sqlite v1.4.1 gorm.io/driver/sqlite v1.4.1
gorm.io/driver/sqlserver v1.4.0 gorm.io/driver/sqlserver v1.4.0
gorm.io/gorm v1.24.0 gorm.io/gorm v1.24.0

View File

@ -112,6 +112,45 @@ func TestPostgres(t *testing.T) {
if err := DB.First(&result, "id = ?", harumph.ID).Error; err != nil || harumph.Name != "jinzhu1" { if err := DB.First(&result, "id = ?", harumph.ID).Error; err != nil || harumph.Name != "jinzhu1" {
t.Errorf("No error should happen, but got %v", err) t.Errorf("No error should happen, but got %v", err)
} }
DB.Migrator().DropTable("log_usage")
if err := DB.Exec(`
CREATE TABLE public.log_usage (
log_id bigint NOT NULL
);
ALTER TABLE public.log_usage ALTER COLUMN log_id ADD GENERATED BY DEFAULT AS IDENTITY (
SEQUENCE NAME public.log_usage_log_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1
);
`).Error; err != nil {
t.Fatalf("failed to create table, got error %v", err)
}
columns, err := DB.Migrator().ColumnTypes("log_usage")
if err != nil {
t.Fatalf("failed to get columns, got error %v", err)
}
hasLogID := false
for _, column := range columns {
if column.Name() == "log_id" {
hasLogID = true
autoIncrement, ok := column.AutoIncrement()
if !ok || !autoIncrement {
t.Fatalf("column log_id should be auto incrementment")
}
}
}
if !hasLogID {
t.Fatalf("failed to found column log_id")
}
} }
type Post struct { type Post struct {