Skip to content

Commit

Permalink
feat: [#280] Implement repository
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl committed Sep 22, 2024
1 parent a7f1994 commit 8dae8bf
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 64 deletions.
48 changes: 26 additions & 22 deletions contracts/database/migration/repository.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
package migration

type File struct {
ID uint
Migration string
Batch int
}

type Repository interface {
//// CreateRepository Create the migration repository data store.
//CreateRepository()
//// Delete Remove a migration from the log.
//Delete(migration string)
//// DeleteRepository Delete the migration repository data store.
//DeleteRepository()
//// GetLast Get the last migration batch.
//GetLast()
//// GetMigrationBatches Get the completed migrations with their batch numbers.
//GetMigrationBatches()
//// GetMigrations Get the list of migrations.
//GetMigrations(steps int)
//// GetMigrationsByBatch Get the list of the migrations by batch.
//GetMigrationsByBatch(batch int)
//// GetNextBatchNumber Get the next migration batch number.
//GetNextBatchNumber()
//// GetRan Get the completed migrations.
//GetRan()
//// Log that a migration was run.
//Log(file, batch string)
//// RepositoryExists Determine if the migration repository exists.
//RepositoryExists()
// CreateRepository Create the migration repository data store.
CreateRepository() error
// Delete Remove a migration from the log.
Delete(migration string) error
// DeleteRepository Delete the migration repository data store.
DeleteRepository() error
// GetLast Get the last migration batch.
GetLast() ([]File, error)
// GetMigrations Get the list of migrations.
GetMigrations(steps int) ([]File, error)
// GetMigrationsByBatch Get the list of the migrations by batch.
GetMigrationsByBatch(batch int) ([]File, error)
// GetNextBatchNumber Get the next migration batch number.
GetNextBatchNumber() int
// GetRan Get the completed migrations.
GetRan() ([]string, error)
// Log that a migration was run.
Log(file string, batch int) error
// RepositoryExists Determine if the migration repository exists.
RepositoryExists()
}
4 changes: 2 additions & 2 deletions contracts/database/migration/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package migration

type Schema interface {
// Create a new table on the schema.
Create(table string, callback func(table Blueprint))
Create(table string, callback func(table Blueprint)) error
// Connection Get the connection for the schema.
Connection(name string) Schema
// DropIfExists Drop a table from the schema if exists.
DropIfExists(table string)
DropIfExists(table string) error
// Register migrations.
Register([]Migration)
// Sql Execute a sql directly.
Expand Down
94 changes: 59 additions & 35 deletions database/migration/respository.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,70 +6,94 @@ import (
)

type Repository struct {
orm orm.Orm
query orm.Query
schema migration.Schema
table string
}

func NewRepository(orm orm.Orm, schema migration.Schema, table string) *Repository {
func NewRepository(query orm.Query, schema migration.Schema, table string) *Repository {
return &Repository{
orm: orm,
query: query,
schema: schema,
table: table,
}
}

func (r *Repository) CreateRepository() {
//TODO implement me
panic("implement me")
func (r *Repository) CreateRepository() error {
return r.schema.Create(r.table, func(table migration.Blueprint) {
table.ID()
table.String("migration")
table.Integer("batch")
})
}

func (r *Repository) Delete(migration string) {
//TODO implement me
panic("implement me")
}
func (r *Repository) Delete(migration string) error {
_, err := r.query.Exec("DELETE FROM ? WHERE migration = ?", r.table, migration)

func (r *Repository) DeleteRepository() {
//TODO implement me
panic("implement me")
return err
}

func (r *Repository) GetLast() {
//TODO implement me
panic("implement me")
func (r *Repository) DeleteRepository() error {
return r.schema.DropIfExists(r.table)
}

func (r *Repository) GetMigrationBatches() {
//TODO implement me
panic("implement me")
func (r *Repository) GetLast() ([]migration.File, error) {
var files []migration.File
if err := r.query.Table(r.table).Where("batch", r.getLastBatchNumber()).OrderByDesc("migration").Get(&files); err != nil {
return nil, err
}

return files, nil
}

func (r *Repository) GetMigrations(steps int) {
//TODO implement me
panic("implement me")
func (r *Repository) GetMigrations(steps int) ([]migration.File, error) {
var files []migration.File
if err := r.query.Table(r.table).Where("batch >= 1").OrderByDesc("batch").OrderByDesc("migration").Limit(steps).Get(&files); err != nil {
return nil, err
}

return files, nil
}

func (r *Repository) GetMigrationsByBatch(batch int) {
//TODO implement me
panic("implement me")
func (r *Repository) GetMigrationsByBatch(batch int) ([]migration.File, error) {
var files []migration.File
if err := r.query.Table(r.table).Where("batch", batch).OrderByDesc("migration").Get(&files); err != nil {
return nil, err
}

return files, nil
}

func (r *Repository) GetNextBatchNumber() {
//TODO implement me
panic("implement me")
func (r *Repository) GetNextBatchNumber() int {
return r.getLastBatchNumber() + 1
}

func (r *Repository) GetRan() {
//TODO implement me
panic("implement me")
func (r *Repository) GetRan() ([]string, error) {
var migrations []string
if err := r.query.Table(r.table).OrderBy("batch").OrderBy("migration").Pluck("migration", &migrations); err != nil {
return nil, err
}

return migrations, nil
}

func (r *Repository) Log(file, batch string) {
//TODO implement me
panic("implement me")
func (r *Repository) Log(file string, batch int) error {
return r.query.Table(r.table).Create(map[string]any{
"migration": file,
"batch": batch,
})
}

func (r *Repository) RepositoryExists() {
//TODO implement me
//TODO implement me when schema.HasTable is implemented
panic("implement me")
}

func (r *Repository) getLastBatchNumber() int {
var batch int
if err := r.query.Table(r.table).OrderBy("batch", "desc").Pluck("batch", &batch); err != nil {
return 0
}

return batch
}
10 changes: 5 additions & 5 deletions database/migration/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,21 @@ func (r *Schema) Connection(name string) migration.Schema {
return schema
}

func (r *Schema) Create(table string, callback func(table migration.Blueprint)) {
func (r *Schema) Create(table string, callback func(table migration.Blueprint)) error {
r.blueprint.SetTable(table)
r.blueprint.Create()
callback(r.blueprint)

// TODO catch error and rollback
_ = r.blueprint.Build(r.orm.Connection(r.connection).Query(), r.grammar)
return r.blueprint.Build(r.orm.Connection(r.connection).Query(), r.grammar)
}

func (r *Schema) DropIfExists(table string) {
func (r *Schema) DropIfExists(table string) error {
r.blueprint.SetTable(table)
r.blueprint.DropIfExists()

// TODO catch error
_ = r.blueprint.Build(r.orm.Connection(r.connection).Query(), r.grammar)
// TODO catch error when run migrate command
return r.blueprint.Build(r.orm.Connection(r.connection).Query(), r.grammar)
}

func (r *Schema) Register(migrations []migration.Migration) {
Expand Down

0 comments on commit 8dae8bf

Please sign in to comment.