Skip to content

Commit

Permalink
fix: sqlite processor (#792)
Browse files Browse the repository at this point in the history
* fix: sqlite processor

* fix test
  • Loading branch information
hwbrzzl authored Dec 26, 2024
1 parent 38c26f0 commit 4172c66
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
18 changes: 17 additions & 1 deletion database/schema/processors/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,31 @@ func NewSqlite() Sqlite {
}

func (r Sqlite) ProcessColumns(dbColumns []schema.DBColumn) []schema.Column {
var primaryKeyNum int
collect.Map(dbColumns, func(dbColumn schema.DBColumn, _ int) bool {
if dbColumn.Primary {
primaryKeyNum++
}

return true
})

var columns []schema.Column
for _, dbColumn := range dbColumns {
ttype := strings.ToLower(dbColumn.Type)
typeNameParts := strings.SplitN(ttype, "(", 2)
typeName := ""
if len(typeNameParts) > 0 {
typeName = typeNameParts[0]
}

columns = append(columns, schema.Column{
Autoincrement: dbColumn.Primary && ttype == "integer",
Autoincrement: primaryKeyNum == 1 && dbColumn.Primary && ttype == "integer",
Default: dbColumn.Default,
Name: dbColumn.Name,
Nullable: cast.ToBool(dbColumn.Nullable),
Type: ttype,
TypeName: typeName,
})
}

Expand Down
8 changes: 4 additions & 4 deletions database/schema/processors/sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func (s *SqliteTestSuite) TestProcessColumns() {
{Name: "name", Type: "varchar", Nullable: "true", Default: "default_name"},
},
expected: []schema.Column{
{Autoincrement: true, Default: "1", Name: "id", Nullable: false, Type: "integer"},
{Autoincrement: false, Default: "default_name", Name: "name", Nullable: true, Type: "varchar"},
{Autoincrement: true, Default: "1", Name: "id", Nullable: false, Type: "integer", TypeName: "integer"},
{Autoincrement: false, Default: "default_name", Name: "name", Nullable: true, Type: "varchar", TypeName: "varchar"},
},
},
{
Expand All @@ -48,7 +48,7 @@ func (s *SqliteTestSuite) TestProcessColumns() {
{Name: "description", Type: "text", Nullable: "true", Default: "default_description"},
},
expected: []schema.Column{
{Autoincrement: false, Default: "default_description", Name: "description", Nullable: true, Type: "text"},
{Autoincrement: false, Default: "default_description", Name: "description", Nullable: true, Type: "text", TypeName: "text"},
},
},
{
Expand All @@ -57,7 +57,7 @@ func (s *SqliteTestSuite) TestProcessColumns() {
{Name: "created_at", Type: "timestamp", Nullable: "false", Default: "CURRENT_TIMESTAMP"},
},
expected: []schema.Column{
{Autoincrement: false, Default: "CURRENT_TIMESTAMP", Name: "created_at", Nullable: false, Type: "timestamp"},
{Autoincrement: false, Default: "CURRENT_TIMESTAMP", Name: "created_at", Nullable: false, Type: "timestamp", TypeName: "timestamp"},
},
},
}
Expand Down
33 changes: 33 additions & 0 deletions database/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,230 +503,263 @@ func (s *SchemaSuite) TestColumnTypes_Sqlite() {
s.Empty(column.Default)
s.True(column.Nullable)
s.Equal("datetime", column.Type)
s.Equal("datetime", column.TypeName)
}
if column.Name == "big_integer" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
s.Empty(column.Default)
s.False(column.Nullable)
s.Equal("integer", column.Type)
s.Equal("integer", column.TypeName)
}
if column.Name == "char" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
s.Empty(column.Default)
s.False(column.Nullable)
s.Equal("varchar", column.Type)
s.Equal("varchar", column.TypeName)
}
if column.Name == "created_at" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
s.Empty(column.Default)
s.True(column.Nullable)
s.Equal("datetime", column.Type)
s.Equal("datetime", column.TypeName)
}
if column.Name == "date" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
s.Empty(column.Default)
s.False(column.Nullable)
s.Equal("date", column.Type)
s.Equal("date", column.TypeName)
}
if column.Name == "date_time" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
s.Empty(column.Default)
s.False(column.Nullable)
s.Equal("datetime", column.Type)
s.Equal("datetime", column.TypeName)
}
if column.Name == "date_time_tz" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
s.Empty(column.Default)
s.False(column.Nullable)
s.Equal("datetime", column.Type)
s.Equal("datetime", column.TypeName)
}
if column.Name == "decimal" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
s.Empty(column.Default)
s.False(column.Nullable)
s.Equal("numeric", column.Type)
s.Equal("numeric", column.TypeName)
}
if column.Name == "deleted_at" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
s.Empty(column.Default)
s.True(column.Nullable)
s.Equal("datetime", column.Type)
s.Equal("datetime", column.TypeName)
}
if column.Name == "double" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
s.Empty(column.Default)
s.False(column.Nullable)
s.Equal("double", column.Type)
s.Equal("double", column.TypeName)
}
if column.Name == "enum" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
s.Empty(column.Default)
s.False(column.Nullable)
s.Equal("varchar", column.Type)
s.Equal("varchar", column.TypeName)
}
if column.Name == "enum_int" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
s.Empty(column.Default)
s.False(column.Nullable)
s.Equal("varchar", column.Type)
s.Equal("varchar", column.TypeName)
}
if column.Name == "float" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
s.Empty(column.Default)
s.False(column.Nullable)
s.Equal("float", column.Type)
s.Equal("float", column.TypeName)
}
if column.Name == "id" {
s.True(column.Autoincrement)
s.Empty(column.Comment)
s.Empty(column.Default)
s.False(column.Nullable)
s.Equal("integer", column.Type)
s.Equal("integer", column.TypeName)
}
if column.Name == "integer" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
s.Empty(column.Default)
s.False(column.Nullable)
s.Equal("integer", column.Type)
s.Equal("integer", column.TypeName)
}
if column.Name == "integer_default" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
s.Equal("'1'", column.Default)
s.False(column.Nullable)
s.Equal("integer", column.Type)
s.Equal("integer", column.TypeName)
}
if column.Name == "json" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
s.Empty(column.Default)
s.False(column.Nullable)
s.Equal("text", column.Type)
s.Equal("text", column.TypeName)
}
if column.Name == "jsonb" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
s.Empty(column.Default)
s.False(column.Nullable)
s.Equal("text", column.Type)
s.Equal("text", column.TypeName)
}
if column.Name == "long_text" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
s.Empty(column.Default)
s.False(column.Nullable)
s.Equal("text", column.Type)
s.Equal("text", column.TypeName)
}
if column.Name == "medium_text" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
s.Empty(column.Default)
s.False(column.Nullable)
s.Equal("text", column.Type)
s.Equal("text", column.TypeName)
}
if column.Name == "string" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
s.Empty(column.Default)
s.False(column.Nullable)
s.Equal("varchar", column.Type)
s.Equal("varchar", column.TypeName)
}
if column.Name == "string_default" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
s.Equal("'goravel'", column.Default)
s.False(column.Nullable)
s.Equal("varchar", column.Type)
s.Equal("varchar", column.TypeName)
}
if column.Name == "text" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
s.Empty(column.Default)
s.False(column.Nullable)
s.Equal("text", column.Type)
s.Equal("text", column.TypeName)
}
if column.Name == "tiny_text" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
s.Empty(column.Default)
s.False(column.Nullable)
s.Equal("text", column.Type)
s.Equal("text", column.TypeName)
}
if column.Name == "time" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
s.Empty(column.Default)
s.False(column.Nullable)
s.Equal("time", column.Type)
s.Equal("time", column.TypeName)
}
if column.Name == "time_tz" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
s.Empty(column.Default)
s.False(column.Nullable)
s.Equal("time", column.Type)
s.Equal("time", column.TypeName)
}
if column.Name == "timestamp" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
s.Empty(column.Default)
s.False(column.Nullable)
s.Equal("datetime", column.Type)
s.Equal("datetime", column.TypeName)
}
if column.Name == "timestamp_tz" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
s.Empty(column.Default)
s.False(column.Nullable)
s.Equal("datetime", column.Type)
s.Equal("datetime", column.TypeName)
}
if column.Name == "timestamp_use_current" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
s.Equal("CURRENT_TIMESTAMP", column.Default)
s.False(column.Nullable)
s.Equal("datetime", column.Type)
s.Equal("datetime", column.TypeName)
}
if column.Name == "timestamp_use_current_on_update" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
s.Equal("CURRENT_TIMESTAMP", column.Default)
s.False(column.Nullable)
s.Equal("datetime", column.Type)
s.Equal("datetime", column.TypeName)
}
if column.Name == "updated_at" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
s.Empty(column.Default)
s.True(column.Nullable)
s.Equal("datetime", column.Type)
s.Equal("datetime", column.TypeName)
}
if column.Name == "unsigned_integer" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
s.Empty(column.Default)
s.False(column.Nullable)
s.Equal("integer", column.Type)
s.Equal("integer", column.TypeName)
}
if column.Name == "unsigned_big_integer" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
s.Empty(column.Default)
s.False(column.Nullable)
s.Equal("integer", column.Type)
s.Equal("integer", column.TypeName)
}
}
}
Expand Down

0 comments on commit 4172c66

Please sign in to comment.