Skip to content

Commit

Permalink
feat: [#280] Refactor Migration feature
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl committed Mar 19, 2024
1 parent 1f6b446 commit 2d2cf33
Show file tree
Hide file tree
Showing 126 changed files with 9,151 additions and 271 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/mockery.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ jobs:
uses: actions/checkout@v4
- name: Install Mockery
run: |
docker pull vektra/mockery:v2.42.1
go install github.com/vektra/mockery/v2@v2.39.1
- name: Generate Mocks
run: |
docker run -v "$PWD":/src -w /src vektra/mockery:v2.42.1
~/go/bin/mockery --all --keeptree --dir=contracts
- uses: stefanzweifel/git-auto-commit-action@v5
name: Commit changes
with:
Expand Down
98 changes: 98 additions & 0 deletions contracts/database/schema/blueprint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package schema

import (
ormcontract "github.com/goravel/framework/contracts/database/orm"
)

type Blueprint interface {
// Boolean Create a new boolean column on the table.
Boolean(column string) ColumnDefinition
// BigInteger Create a new big integer (8-byte) column on the table.
BigInteger(column string) ColumnDefinition
// Binary Create a new binary column on the table.
Binary(column string) ColumnDefinition
// Build Execute the blueprint to build / modify the table.
Build(query ormcontract.Query, grammar Grammar) error
// Char Create a new char column on the table.
Char(column string, length ...int) ColumnDefinition
// Comment Add a comment to the table.
Comment(comment string) error
// Create Indicate that the table needs to be created.
Create()
// Date Create a new date column on the table.
Date(column string) ColumnDefinition
// DateTime Create a new date-time column on the table.
DateTime(column string) ColumnDefinition
// DateTimeTz Create a new date-time column (with time zone) on the table.
DateTimeTz(column string) ColumnDefinition
// Decimal Create a new decimal column on the table.
Decimal(column string) ColumnDefinition
// Double Create a new double column on the table.
Double(column string) ColumnDefinition
// DropColumn Indicate that the given columns should be dropped.
DropColumn(column string) error
// DropForeign Indicate that the given foreign key should be dropped.
DropForeign(index string) error
// DropIndex Indicate that the given index should be dropped.
DropIndex(index string) error
// DropSoftDeletes Indicate that the soft delete column should be dropped.
DropSoftDeletes() error
// DropTimestamps Indicate that the timestamp columns should be dropped.
DropTimestamps() error
// Enum Create a new enum column on the table.
Enum(column string, array []any) ColumnDefinition
// Float Create a new float column on the table.
Float(column string) ColumnDefinition
// Foreign Specify a foreign key for the table.
Foreign(columns []string, name ...string) error
// GetAddedColumns Get the added columns.
GetAddedColumns() []ColumnDefinition
// GetChangedColumns Get the changed columns.
GetChangedColumns() []ColumnDefinition
// GetTableName Get the table name with prefix.
GetTableName() string
// ID Create a new auto-incrementing big integer (8-byte) column on the table.
ID() ColumnDefinition
// Index Specify an index for the table.
Index(columns []string, name string) error
// Integer Create a new integer (4-byte) column on the table.
Integer(column string) ColumnDefinition
// Json Create a new json column on the table.
Json(column string) ColumnDefinition
// Jsonb Create a new jsonb column on the table.
Jsonb(column string) ColumnDefinition
// Primary Specify the primary key(s) for the table.
Primary(columns []string, name string) error
// RenameColumn Indicate that the given columns should be renamed.
RenameColumn(from, to string) error
// RenameIndex Indicate that the given indexes should be renamed.
RenameIndex(from, to string) error
// SoftDeletes Add a "deleted at" timestamp for the table.
SoftDeletes(column ...string) ColumnDefinition
// SoftDeletesTz Add a "deleted at" timestampTz for the table.
SoftDeletesTz(column ...string) ColumnDefinition
// String Create a new string column on the table.
String(column string, length ...int) ColumnDefinition
// Text Create a new text column on the table.
Text(column string) ColumnDefinition
// Time Create a new time column on the table.
Time(column string) ColumnDefinition
// TimeTz Create a new time column (with time zone) on the table.
TimeTz(column string) ColumnDefinition
// Timestamp Create a new time column on the table.
Timestamp(column string) ColumnDefinition
// Timestamps Add nullable creation and update timestamps to the table.
Timestamps() ColumnDefinition
// TimestampsTz Add creation and update timestampTz columns to the table.
TimestampsTz() ColumnDefinition
// TimestampTz Create a new time column (with time zone) on the table.
TimestampTz(column string) ColumnDefinition
// ToSql Get the raw SQL statements for the blueprint.
ToSql(query ormcontract.Query, grammar Grammar) []string
// Unique Specify a unique index for the table.
Unique(columns []string, name string) error
// UnsignedInteger Create a new unsigned integer (4-byte) column on the table.
UnsignedInteger(column string) ColumnDefinition
// UnsignedBigInteger Create a new unsigned big integer (8-byte) column on the table.
UnsignedBigInteger(column string) ColumnDefinition
}
33 changes: 33 additions & 0 deletions contracts/database/schema/column.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package schema

type Column struct {
AutoIncrement bool
Collation string
Comment string
Default any
Name string
Nullable bool
TypeName string
Type string
}

type ColumnDefinition interface {
// Change the column
Change()
// GetAllowed returns the allowed value
GetAllowed() []string
// GetAutoIncrement returns the autoIncrement value
GetAutoIncrement() bool
// GetLength returns the length value
GetLength() int
// GetName returns the name value
GetName() string
// GetPlaces returns the places value
GetPlaces() int
// GetPrecision returns the precision value
GetPrecision() int
// GetTotal returns the total value
GetTotal() int
// GetType returns the type value
GetType() string
}
105 changes: 105 additions & 0 deletions contracts/database/schema/grammar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package schema

import (
ormcontract "github.com/goravel/framework/contracts/database/orm"
)

type Grammar interface {
// CompileAdd Compile an add column command.
CompileAdd(blueprint Blueprint, command string) string
// CompileAutoIncrementStartingValues Compile the auto-incrementing column starting values.
CompileAutoIncrementStartingValues(blueprint Blueprint, command string) string
// CompileChange Compile a change column command into a series of SQL statements.
CompileChange(blueprint Blueprint, command, connection string) string
// CompileColumns Compile the query to determine the columns.
// TODO check if the database is required
CompileColumns(database, table, schema string) string
// CompileCreate Compile a create table command.
CompileCreate(blueprint Blueprint, query ormcontract.Query) string
// CompileCreateEncoding Append the character set specifications to a command.
CompileCreateEncoding(sql, connection string, blueprint Blueprint) string
// CompileCreateEngine Append the engine specifications to a command.
CompileCreateEngine(sql, connection string, blueprint Blueprint) string
// CompileCreateTable Create the main create table clause.
CompileCreateTable(blueprint Blueprint, command, connection string) string
// CompileDrop Compile a drop table command.
CompileDrop(blueprint Blueprint, command string) string
// CompileDropAllTables Compile the SQL needed to drop all tables.
CompileDropAllTables(tables []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 string) string
// CompileDropIfExists Compile a drop table (if exists) command.
CompileDropIfExists(blueprint Blueprint, command string) string
// CompileDropIndex Compile a drop index command.
CompileDropIndex(blueprint Blueprint, command string) string
// CompileDropPrimary Compile a drop primary key command.
CompileDropPrimary(blueprint Blueprint, command string) string
// CompileDropUnique Compile a drop unique key command.
CompileDropUnique(blueprint Blueprint, command string) string
// CompilePrimary Compile a primary key command.
CompilePrimary(blueprint Blueprint, command string) string
// CompileIndex Compile a plain index key command.
CompileIndex(blueprint Blueprint, command string) string
// CompileIndexes Compile the query to determine the indexes.
CompileIndexes(database, table string) string
// CompileRename Compile a rename table command.
CompileRename(blueprint Blueprint, command string) string
// CompileRenameColumn Compile a rename column command.
CompileRenameColumn(blueprint Blueprint, command, connection string) string
// CompileRenameIndex Compile a rename index command.
CompileRenameIndex(blueprint Blueprint, command string) string
// CompileTableComment Compile a table comment command.
CompileTableComment(blueprint Blueprint, command string) string
// CompileTables Compile the query to determine the tables.
CompileTables(database string) string
// CompileUnique Compile a unique key command.
CompileUnique(blueprint Blueprint, command string) string
// CompileViews Compile the query to determine the views.
CompileViews(database string) string
// ModifyNullable Get the SQL for a nullable column modifier.
ModifyNullable(blueprint Blueprint, column string) string
// ModifyDefault Get the SQL for a default column modifier.
ModifyDefault(blueprint Blueprint, column string) string
// TypeBigInteger Create the column definition for a big integer type.
TypeBigInteger(column ColumnDefinition) string
// TypeBinary Create the column definition for a binary type.
TypeBinary(column ColumnDefinition) string
// TypeBoolean Create the column definition for a boolean type.
TypeBoolean(column ColumnDefinition) string
// TypeChar Create the column definition for a char type.
TypeChar(column ColumnDefinition) string
// TypeDate Create the column definition for a date type.
TypeDate(column ColumnDefinition) string
// TypeDateTime Create the column definition for a date-time type.
TypeDateTime(column ColumnDefinition) string
// TypeDateTimeTz Create the column definition for a date-time (with time zone) type.
TypeDateTimeTz(column ColumnDefinition) string
// TypeDecimal Create the column definition for a decimal type.
TypeDecimal(column ColumnDefinition) string
// TypeDouble Create the column definition for a double type.
TypeDouble(column ColumnDefinition) string
// TypeEnum Create the column definition for an enumeration type.
TypeEnum(column ColumnDefinition) string
// TypeFloat Create the column definition for a float type.
TypeFloat(column ColumnDefinition) string
// TypeInteger Create the column definition for an integer type.
TypeInteger(column ColumnDefinition) string
// TypeJson Create the column definition for a json type.
TypeJson(column ColumnDefinition) string
// TypeJsonb Create the column definition for a jsonb type.
TypeJsonb(column ColumnDefinition) string
// TypeString Create the column definition for a string type.
TypeString(column ColumnDefinition) string
// TypeText Create the column definition for a text type.
TypeText(column ColumnDefinition) string
// TypeTime Create the column definition for a time type.
TypeTime(column ColumnDefinition) string
// TypeTimeTz Create the column definition for a time (with time zone) type.
TypeTimeTz(column ColumnDefinition) string
// TypeTimestamp Create the column definition for a timestamp type.
TypeTimestamp(column ColumnDefinition) string
// TypeTimestampTz Create the column definition for a timestamp (with time zone) type.
TypeTimestampTz(column ColumnDefinition) string
}
6 changes: 6 additions & 0 deletions contracts/database/schema/processor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package schema

type Processor interface {
// ProcessColumns Process the results of a columns query.
ProcessColumns(columns []Column) []Column
}
71 changes: 71 additions & 0 deletions contracts/database/schema/schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package schema

type Schema interface {
Connection(name string) Schema
// Create a new table on the schema.
Create(table string, callback func(table Blueprint)) error
// Drop a table from the schema.
Drop(table string) error
// DropAllTables Drop all tables from the database.
DropAllTables() error
// DropAllViews Drop all views from the database.
DropAllViews() error
// DropColumns Drop columns from a table schema.
DropColumns(table string, columns []string) error
// DropIfExists Drop a table from the schema if it exists.
DropIfExists(table string) error
// GetColumns Get the columns for a given table.
GetColumns(table string) ([]Column, error)
// GetColumnListing Get the column listing for a given table.
GetColumnListing(table string) []string
// GetIndexes Get the indexes for a given table.
GetIndexes(table string) []Index
// GetIndexListing Get the names of the indexes for a given table.
GetIndexListing(table string) []string
// GetTableListing Get the names of the tables that belong to the database.
GetTableListing() []string
// GetTables Get the tables that belong to the database.
GetTables() ([]Table, error)
// GetViews Get the views that belong to the database.
GetViews() []View
// HasColumn Determine if the given table has a given column.
HasColumn(table, column string) bool
// HasColumns Determine if the given table has given columns.
HasColumns(table string, columns []string) bool
// HasIndex Determine if the given table has a given index.
HasIndex(table, index string)
// HasTable Determine if the given table exists.
HasTable(table string) bool
// HasView Determine if the given view exists.
HasView(view string) bool
// Register migrations.
Register([]Migration)
// Rename a table on the schema.
Rename(from, to string)
// Table Modify a table on the schema.
Table(table string, callback func(table Blueprint)) error
}

type Migration interface {
Signature() string
Up() error
Down() error
}

type Index struct {
Columns []string
Name string
Primary bool
Type string
Unique bool
}

type Table struct {
Name string
Size int
}

type View struct {
Definition string
Name string
}
7 changes: 5 additions & 2 deletions contracts/foundation/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/goravel/framework/contracts/console"
"github.com/goravel/framework/contracts/crypt"
"github.com/goravel/framework/contracts/database/orm"
"github.com/goravel/framework/contracts/database/schema"
"github.com/goravel/framework/contracts/database/seeder"
"github.com/goravel/framework/contracts/event"
"github.com/goravel/framework/contracts/filesystem"
Expand Down Expand Up @@ -70,6 +71,10 @@ type Container interface {
MakeRoute() route.Route
// MakeSchedule resolves the schedule instance.
MakeSchedule() schedule.Schedule
// MakeSchema resolves the schema instance.
MakeSchema() schema.Schema
// MakeSeeder resolves the seeder instance.
MakeSeeder() seeder.Facade
// MakeSession resolves the session instance.
MakeSession() session.Manager
// MakeStorage resolves the storage instance.
Expand All @@ -80,8 +85,6 @@ type Container interface {
MakeValidation() validation.Validation
// MakeView resolves the view instance.
MakeView() http.View
// MakeSeeder resolves the seeder instance.
MakeSeeder() seeder.Facade
// MakeWith resolves the given type with the given parameters from the container.
MakeWith(key any, parameters map[string]any) (any, error)
// Singleton registers a shared binding in the container.
Expand Down
4 changes: 2 additions & 2 deletions database/gorm/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2277,7 +2277,7 @@ func (s *QueryTestSuite) TestRefreshConnection() {
return product
}(),
setup: func() {
mockPostgresqlConnection(s.mysqlDocker.MockConfig, testDatabaseDocker.Postgresql.Config())
mockPostgresqlConnection(s.mysqlDocker.MockConfig, testDatabaseDocker.Postgres.Config())
},
expectConnection: "postgresql",
},
Expand Down Expand Up @@ -3057,7 +3057,7 @@ func TestCustomConnection(t *testing.T) {
assert.Nil(t, query.Where("body", "create_review").First(&review1))
assert.True(t, review1.ID > 0)

mockPostgresqlConnection(mysqlDocker.MockConfig, testDatabaseDocker.Postgresql.Config())
mockPostgresqlConnection(mysqlDocker.MockConfig, testDatabaseDocker.Postgres.Config())

product := Product{Name: "create_product"}
assert.Nil(t, query.Create(&product))
Expand Down
2 changes: 1 addition & 1 deletion database/gorm/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ type PostgresqlDocker struct {
}

func NewPostgresqlDocker(database *supportdocker.Database) *PostgresqlDocker {
config := database.Postgresql.Config()
config := database.Postgres.Config()

return &PostgresqlDocker{MockConfig: &mocksconfig.Config{}, Port: config.Port, user: config.Username, password: config.Password, database: config.Database}
}
Expand Down
Loading

0 comments on commit 2d2cf33

Please sign in to comment.