Skip to content

Commit

Permalink
update test
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl committed Nov 9, 2024
1 parent 0fbd8d9 commit 2cf94e0
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 20 deletions.
25 changes: 25 additions & 0 deletions database/schema/grammars/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,31 @@ func (s *PostgresSuite) TestCompilePrimary() {
}))
}

func (s *PostgresSuite) TestGetColumns() {
mockColumn1 := mocksschema.NewColumnDefinition(s.T())
mockColumn2 := mocksschema.NewColumnDefinition(s.T())
mockBlueprint := mocksschema.NewBlueprint(s.T())

mockBlueprint.EXPECT().GetAddedColumns().Return([]contractsschema.ColumnDefinition{
mockColumn1, mockColumn2,
}).Once()
mockBlueprint.EXPECT().HasCommand("primary").Return(false).Twice()

mockColumn1.EXPECT().GetName().Return("id").Once()
mockColumn1.EXPECT().GetType().Return("integer").Twice()
mockColumn1.EXPECT().GetDefault().Return(nil).Once()
mockColumn1.EXPECT().GetNullable().Return(false).Once()
mockColumn1.EXPECT().GetAutoIncrement().Return(true).Twice()

mockColumn2.EXPECT().GetName().Return("name").Once()
mockColumn2.EXPECT().GetType().Return("string").Twice()
mockColumn2.EXPECT().GetDefault().Return("goravel").Twice()
mockColumn2.EXPECT().GetNullable().Return(true).Once()
mockColumn2.EXPECT().GetLength().Return(10).Once()

s.Equal([]string{"\"id\" serial primary key not null", "\"name\" varchar(10) default 'goravel' null"}, s.grammar.getColumns(mockBlueprint))
}

func (s *PostgresSuite) TestEscapeNames() {
// SingleName
names := []string{"username"}
Expand Down
51 changes: 37 additions & 14 deletions database/schema/grammars/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ type Sqlite struct {
attributeCommands []string
modifiers []func(schema.Blueprint, schema.ColumnDefinition) string
serials []string
tablePrefix string
wrap *Wrap
}

func NewSqlite() *Sqlite {
func NewSqlite(tablePrefix string) *Sqlite {
sqlite := &Sqlite{
attributeCommands: []string{},
serials: []string{"bigInteger", "integer", "mediumInteger", "smallInteger", "tinyInteger"},
tablePrefix: tablePrefix,
wrap: NewWrap(tablePrefix),
}
sqlite.modifiers = []func(schema.Blueprint, schema.ColumnDefinition) string{
sqlite.ModifyDefault,
Expand All @@ -29,13 +33,13 @@ func NewSqlite() *Sqlite {
}

func (r *Sqlite) CompileAdd(blueprint schema.Blueprint, command *schema.Command) string {
return fmt.Sprintf("alter table %s add column %s", blueprint.GetTableName(), getColumn(r, blueprint, command.Column))
return fmt.Sprintf("alter table %s add column %s", r.wrap.Table(blueprint.GetTableName()), r.getColumn(blueprint, command.Column))
}

func (r *Sqlite) CompileCreate(blueprint schema.Blueprint) string {
return fmt.Sprintf("create table %s (%s%s%s)",
blueprint.GetTableName(),
strings.Join(getColumns(r, blueprint), ","),
r.wrap.Table(blueprint.GetTableName()),
strings.Join(r.getColumns(blueprint), ", "),
r.addForeignKeys(getCommandsByName(blueprint.GetCommands(), "foreign")),
r.addPrimaryKeys(getCommandByName(blueprint.GetCommands(), "primary")))
}
Expand All @@ -61,7 +65,7 @@ func (r *Sqlite) CompileDropAllViews(views []string) string {
}

func (r *Sqlite) CompileDropIfExists(blueprint schema.Blueprint) string {
return fmt.Sprintf("drop table if exists %s", blueprint.GetTableName())
return fmt.Sprintf("drop table if exists %s", r.wrap.Table(blueprint.GetTableName()))
}

func (r *Sqlite) CompileEnableWriteableSchema() string {
Expand All @@ -74,14 +78,14 @@ func (r *Sqlite) CompileForeign(blueprint schema.Blueprint, command *schema.Comm

func (r *Sqlite) CompileIndex(blueprint schema.Blueprint, command *schema.Command) string {
return fmt.Sprintf("create index %s on %s (%s)",
command.Index,
blueprint.GetTableName(),
strings.Join(command.Columns, ", "),
r.wrap.Column(command.Index),
r.wrap.Table(blueprint.GetTableName()),
r.wrap.Columns(command.Columns),
)
}

func (r *Sqlite) CompileIndexes(schema, table string) string {
quotedTable := quoteString(strings.ReplaceAll(table, ".", "__"))
quotedTable := r.wrap.Quote(strings.ReplaceAll(table, ".", "__"))

return fmt.Sprintf(
`select 'primary' as name, group_concat(col) as columns, 1 as "unique", 1 as "primary" `+
Expand All @@ -90,7 +94,7 @@ func (r *Sqlite) CompileIndexes(schema, table string) string {
`from (select il.*, ii.name as col from pragma_index_list(%s) il, pragma_index_info(il.name) ii order by il.seq, ii.seqno) `+
`group by name, "unique", "primary"`,
quotedTable,
quoteString(table),
r.wrap.Quote(table),
)
}

Expand Down Expand Up @@ -173,14 +177,33 @@ func (r *Sqlite) addPrimaryKeys(command *schema.Command) string {
return ""
}

return fmt.Sprintf(", primary key (%s)", strings.Join(command.Columns, ", "))
return fmt.Sprintf(", primary key (%s)", r.wrap.Columns(command.Columns))
}

func (r *Sqlite) getColumns(blueprint schema.Blueprint) []string {
var columns []string
for _, column := range blueprint.GetAddedColumns() {
columns = append(columns, r.getColumn(blueprint, column))
}

return columns
}

func (r *Sqlite) getColumn(blueprint schema.Blueprint, column schema.ColumnDefinition) string {
sql := fmt.Sprintf("%s %s", r.wrap.Column(column.GetName()), getType(r, column))

for _, modifier := range r.modifiers {
sql += modifier(blueprint, column)
}

return sql
}

func (r *Sqlite) getForeignKey(command *schema.Command) string {
sql := fmt.Sprintf(", foreign key(%s) references %s(%s)",
strings.Join(command.Columns, ","),
command.On,
strings.Join(command.References, ","))
r.wrap.Columns(command.Columns),
r.wrap.Table(command.On),
r.wrap.Columns(command.References))

if command.OnDelete != "" {
sql += " on delete " + command.OnDelete
Expand Down
65 changes: 59 additions & 6 deletions database/schema/grammars/sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,26 @@ func TestSqliteSuite(t *testing.T) {
}

func (s *SqliteSuite) SetupTest() {
s.grammar = NewSqlite()
s.grammar = NewSqlite("goravel_")
}

func (s *SqliteSuite) TestAddForeignKeys() {
commands := []*contractsschema.Command{
{
Columns: []string{"role_id", "permission_id"},
On: "roles",
References: []string{"id", "user_id"},
OnDelete: "cascade",
OnUpdate: "restrict",
},
{
Columns: []string{"permission_id", "role_id"},
On: "permissions",
References: []string{"id", "user_id"},
},
}

s.Equal(`, foreign key("role_id", "permission_id") references "goravel_roles"("id", "user_id") on delete cascade on update restrict, foreign key("permission_id", "role_id") references "goravel_permissions"("id", "user_id")`, s.grammar.addForeignKeys(commands))
}

func (s *SqliteSuite) TestCompileAdd() {
Expand All @@ -36,7 +55,7 @@ func (s *SqliteSuite) TestCompileAdd() {
Column: mockColumn,
})

s.Equal("alter table users add column name varchar default 'goravel' not null", sql)
s.Equal(`alter table "goravel_users" add column "name" varchar default 'goravel' not null`, sql)
}

func (s *SqliteSuite) TestCompileCreate() {
Expand Down Expand Up @@ -82,31 +101,65 @@ func (s *SqliteSuite) TestCompileCreate() {
},
{
Name: "foreign",
Columns: []string{"role_id"},
Columns: []string{"role_id", "permission_id"},
On: "roles",
References: []string{"id"},
OnDelete: "cascade",
OnUpdate: "restrict",
},
{
Name: "foreign",
Columns: []string{"permission_id"},
Columns: []string{"permission_id", "role_id"},
On: "permissions",
References: []string{"id"},
OnDelete: "cascade",
OnUpdate: "restrict",
},
}).Twice()

s.Equal("create table users (id integer primary key autoincrement not null,name varchar null, foreign key(role_id) references roles(id) on delete cascade on update restrict, foreign key(permission_id) references permissions(id) on delete cascade on update restrict, primary key (id))",
s.Equal(`create table "goravel_users" ("id" integer primary key autoincrement not null, "name" varchar null, foreign key("role_id", "permission_id") references "goravel_roles"("id") on delete cascade on update restrict, foreign key("permission_id", "role_id") references "goravel_permissions"("id") on delete cascade on update restrict, primary key ("id"))`,
s.grammar.CompileCreate(mockBlueprint))
}

func (s *SqliteSuite) TestCompileDropIfExists() {
mockBlueprint := mocksschema.NewBlueprint(s.T())
mockBlueprint.EXPECT().GetTableName().Return("users").Once()

s.Equal("drop table if exists users", s.grammar.CompileDropIfExists(mockBlueprint))
s.Equal(`drop table if exists "goravel_users"`, s.grammar.CompileDropIfExists(mockBlueprint))
}

func (s *SqliteSuite) TestCompileIndex() {
mockBlueprint := mocksschema.NewBlueprint(s.T())
mockBlueprint.EXPECT().GetTableName().Return("users").Once()
command := &contractsschema.Command{
Index: "users",
Columns: []string{"role_id", "permission_id"},
}

s.Equal(`create index "users" on "goravel_users" ("role_id", "permission_id")`, s.grammar.CompileIndex(mockBlueprint, command))
}

func (s *SqliteSuite) TestGetColumns() {
mockColumn1 := mocksschema.NewColumnDefinition(s.T())
mockColumn2 := mocksschema.NewColumnDefinition(s.T())
mockBlueprint := mocksschema.NewBlueprint(s.T())

mockBlueprint.EXPECT().GetAddedColumns().Return([]contractsschema.ColumnDefinition{
mockColumn1, mockColumn2,
}).Once()

mockColumn1.EXPECT().GetName().Return("id").Once()
mockColumn1.EXPECT().GetType().Return("integer").Twice()
mockColumn1.EXPECT().GetDefault().Return(nil).Once()
mockColumn1.EXPECT().GetNullable().Return(false).Once()
mockColumn1.EXPECT().GetAutoIncrement().Return(true).Once()

mockColumn2.EXPECT().GetName().Return("name").Once()
mockColumn2.EXPECT().GetType().Return("string").Twice()
mockColumn2.EXPECT().GetDefault().Return("goravel").Twice()
mockColumn2.EXPECT().GetNullable().Return(true).Once()

s.Equal([]string{"\"id\" integer primary key autoincrement not null", "\"name\" varchar default 'goravel' null"}, s.grammar.getColumns(mockBlueprint))
}

func (s *SqliteSuite) TestModifyDefault() {
Expand Down

0 comments on commit 2cf94e0

Please sign in to comment.