Skip to content

Commit

Permalink
feat: [#280] Add index methods (#741)
Browse files Browse the repository at this point in the history
* feat: [#280] Add index methods

* chore: update mocks

* Add logic

* chore: update mocks

* Rename

* Add test

* Add test

* Update test

* fix test

* add foreign keys logic

* chore: update mocks

* Add foreign keys logic

* Add test cases

* chore: update mocks

* Optimize test cases

* chore: update mocks

* Fix test cases

* Optimize test cases

---------

Co-authored-by: hwbrzzl <[email protected]>
  • Loading branch information
hwbrzzl and hwbrzzl authored Dec 5, 2024
1 parent a171c80 commit 814ad23
Show file tree
Hide file tree
Showing 40 changed files with 3,305 additions and 328 deletions.
34 changes: 34 additions & 0 deletions contracts/database/schema/blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,44 @@ type Blueprint interface {
Decimal(column string) ColumnDefinition
// Double Create a new double column on the table.
Double(column string) ColumnDefinition
// Drop Indicate that the table should be dropped.
Drop()
// DropColumn Indicate that the given columns should be dropped.
DropColumn(column ...string)
// DropForeign Indicate that the given foreign key should be dropped.
DropForeign(column ...string)
// DropForeignByName Indicate that the given foreign key should be dropped.
DropForeignByName(name string)
// DropFullText Indicate that the given fulltext index should be dropped.
DropFullText(column ...string)
// DropFullTextByName Indicate that the given fulltext index should be dropped.
DropFullTextByName(name string)
// DropIfExists Indicate that the table should be dropped if it exists.
DropIfExists()
// DropIndex Indicate that the given index should be dropped.
DropIndex(column ...string)
// DropIndexByName Indicate that the given index should be dropped.
DropIndexByName(name string)
// DropPrimary Indicate that the given primary key should be dropped.
DropPrimary(column ...string)
// DropSoftDeletes Indicate that the soft delete column should be dropped.
DropSoftDeletes(column ...string)
// DropSoftDeletesTz Indicate that the soft delete column should be dropped.
DropSoftDeletesTz(column ...string)
// DropTimestamps Indicate that the timestamp columns should be dropped.
DropTimestamps()
// DropTimestampsTz Indicate that the timestamp columns should be dropped.
DropTimestampsTz()
// DropUnique Indicate that the given unique key should be dropped.
DropUnique(column ...string)
// Enum Create a new enum column on the table.
Enum(column string, array []string) ColumnDefinition
// Float Create a new float column on the table.
Float(column string, precision ...int) ColumnDefinition
// Foreign Specify a foreign key for the table.
Foreign(column ...string) ForeignKeyDefinition
// FullText Specify a fulltext for the table.
FullText(column ...string) IndexDefinition
// GetAddedColumns Get the added columns.
GetAddedColumns() []ColumnDefinition
// GetCommands Get the commands.
Expand Down Expand Up @@ -65,6 +95,8 @@ type Blueprint interface {
MediumText(column string) ColumnDefinition
// Primary Specify the primary key(s) for the table.
Primary(column ...string)
// RenameIndex Indicate that the given indexes should be renamed.
RenameIndex(from, to string)
// SetTable Set the table that the blueprint operates on.
SetTable(name string)
// SmallIncrements Create a new auto-incrementing small integer (2-byte) column on the table.
Expand Down Expand Up @@ -99,6 +131,8 @@ type Blueprint interface {
TinyText(column string) ColumnDefinition
// ToSql Get the raw SQL statements for the blueprint.
ToSql(grammar Grammar) []string
// Unique Specify a unique index for the table.
Unique(column ...string) IndexDefinition
// UnsignedBigInteger Create a new unsigned big integer (8-byte) column on the table.
UnsignedBigInteger(column string) ColumnDefinition
// UnsignedInteger Create a new unsigned integer (4-byte) column on the table.
Expand Down
22 changes: 22 additions & 0 deletions contracts/database/schema/grammar.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ type Grammar interface {
CompileComment(blueprint Blueprint, command *Command) string
// CompileCreate Compile a create table command.
CompileCreate(blueprint Blueprint) string
// CompileDrop Compile a drop table command.
CompileDrop(blueprint Blueprint) string
// CompileDropAllDomains Compile the SQL needed to drop all domains.
CompileDropAllDomains(domains []string) string
// CompileDropAllTables Compile the SQL needed to drop all tables.
Expand All @@ -17,20 +19,40 @@ type Grammar interface {
CompileDropAllTypes(types []string) string
// CompileDropAllViews Compile the SQL needed to drop all views.
CompileDropAllViews(views []string) string
// CompileDropColumn Compile a drop column command.
CompileDropColumn(blueprint Blueprint, command *Command) []string
// CompileDropForeign Compile a drop foreign key command.
CompileDropForeign(blueprint Blueprint, command *Command) string
// CompileDropFullText Compile a drop fulltext index command.
CompileDropFullText(blueprint Blueprint, command *Command) string
// CompileDropIfExists Compile a drop table (if exists) command.
CompileDropIfExists(blueprint Blueprint) string
// CompileDropIndex Compile a drop index command.
CompileDropIndex(blueprint Blueprint, command *Command) string
// CompileDropPrimary Compile a drop primary key command.
CompileDropPrimary(blueprint Blueprint, command *Command) string
// CompileDropUnique Compile a drop unique key command.
CompileDropUnique(blueprint Blueprint, command *Command) string
// CompileForeign Compile a foreign key command.
CompileForeign(blueprint Blueprint, command *Command) string
// CompileForeignKeys Compile the query to determine the foreign keys.
CompileForeignKeys(schema, table string) string
// CompileFullText Compile a fulltext index key command.
CompileFullText(blueprint Blueprint, command *Command) string
// CompileIndex Compile a plain index key command.
CompileIndex(blueprint Blueprint, command *Command) string
// CompileIndexes Compile the query to determine the indexes.
CompileIndexes(schema, table string) string
// CompilePrimary Compile a primary key command.
CompilePrimary(blueprint Blueprint, command *Command) string
// CompileRenameIndex Compile a rename index command.
CompileRenameIndex(schema Schema, blueprint Blueprint, command *Command) []string
// CompileTables Compile the query to determine the tables.
CompileTables(database string) string
// CompileTypes Compile the query to determine the types.
CompileTypes() string
// CompileUnique Compile a unique key command.
CompileUnique(blueprint Blueprint, command *Command) string
// CompileViews Compile the query to determine the views.
CompileViews(database string) string
// GetAttributeCommands Get the commands for the schema build.
Expand Down
4 changes: 4 additions & 0 deletions contracts/database/schema/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ type ForeignKeyDefinition interface {

type IndexDefinition interface {
Algorithm(algorithm string) IndexDefinition
Deferrable() IndexDefinition
InitiallyImmediate() IndexDefinition
Language(name string) IndexDefinition
Name(name string) IndexDefinition
}

type IndexConfig struct {
Algorithm string
Name string
Language string
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
package processors
package schema

type Processor interface {
ProcessColumns(dbColumns []DBColumn) []Column
ProcessForeignKeys(dbIndexes []DBForeignKey) []ForeignKey
ProcessIndexes(dbIndexes []DBIndex) []Index
}

type DBColumn struct {
Autoincrement bool
Expand All @@ -16,6 +22,16 @@ type DBColumn struct {
TypeName string
}

type DBForeignKey struct {
Name string
Columns string
ForeignSchema string
ForeignTable string
ForeignColumns string
OnUpdate string
OnDelete string
}

type DBIndex struct {
Columns string
Name string
Expand Down
43 changes: 29 additions & 14 deletions contracts/database/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type Schema interface {
GetColumnListing(table string) []string
// GetConnection Get the connection of the schema.
GetConnection() string
// GetForeignKeys Get the foreign keys for a given table.
GetForeignKeys(table string) ([]ForeignKey, error)
// GetIndexListing Get the names of the indexes for a given table.
GetIndexListing(table string) []string
// HasColumn Determine if the given table has a given column.
Expand All @@ -38,7 +40,7 @@ type Schema interface {
// Sql Execute a sql directly.
Sql(sql string)
// Table Modify a table on the schema.
Table(table string, callback func(table Blueprint))
Table(table string, callback func(table Blueprint)) error
}

type CommonSchema interface {
Expand Down Expand Up @@ -78,19 +80,32 @@ type Connection interface {
}

type Command struct {
Algorithm string
Column ColumnDefinition
Columns []string
From string
Index string
On string
OnDelete string
OnUpdate string
Name string
To string
References []string
ShouldBeSkipped bool
Value string
Algorithm string
Column ColumnDefinition
Columns []string
Deferrable *bool
From string
Index string
InitiallyImmediate *bool
Language string
Name string
On string
OnDelete string
OnUpdate string
References []string
ShouldBeSkipped bool
To string
Value string
}

type ForeignKey struct {
Name string
Columns []string
ForeignSchema string
ForeignTable string
ForeignColumns []string
OnUpdate string
OnDelete string
}

type Index struct {
Expand Down
Loading

0 comments on commit 814ad23

Please sign in to comment.