-
Notifications
You must be signed in to change notification settings - Fork 111
feat: [#280] Manage migrations table #664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d312096
feat: [#280] Manage migrations table
hwbrzzl 5032bf7
optimize structure
hwbrzzl c469c2a
chore: update mocks
hwbrzzl 62f9828
fix test
hwbrzzl e94a92f
add error
hwbrzzl 1961a55
chore: update mocks
hwbrzzl bc30279
Merge branch 'master' into bowen/#280-5
hwbrzzl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, error) | ||
| // 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() bool | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| package migration | ||
|
|
||
| import ( | ||
| "github.com/goravel/framework/contracts/database/migration" | ||
| "github.com/goravel/framework/contracts/database/orm" | ||
| ) | ||
|
|
||
| type Repository struct { | ||
| query orm.Query | ||
| schema migration.Schema | ||
| table string | ||
| } | ||
|
|
||
| func NewRepository(query orm.Query, schema migration.Schema, table string) *Repository { | ||
| return &Repository{ | ||
| query: query, | ||
| schema: schema, | ||
| table: table, | ||
| } | ||
| } | ||
|
|
||
| func (r *Repository) CreateRepository() error { | ||
| return r.schema.Create(r.table, func(table migration.Blueprint) { | ||
| table.ID() | ||
| table.String("migration") | ||
| table.Integer("batch") | ||
| }) | ||
| } | ||
hwbrzzl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| func (r *Repository) Delete(migration string) error { | ||
| _, err := r.query.Table(r.table).Where("migration", migration).Delete() | ||
|
|
||
| return err | ||
| } | ||
|
|
||
| func (r *Repository) DeleteRepository() error { | ||
| return r.schema.DropIfExists(r.table) | ||
| } | ||
|
|
||
| func (r *Repository) GetLast() ([]migration.File, error) { | ||
| var files []migration.File | ||
| lastBatchNumber, err := r.getLastBatchNumber() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if err := r.query.Table(r.table).Where("batch", lastBatchNumber).OrderByDesc("migration").Get(&files); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return files, nil | ||
| } | ||
|
|
||
| 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) ([]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() (int, error) { | ||
| lastBatchNumber, err := r.getLastBatchNumber() | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
|
|
||
| return lastBatchNumber + 1, nil | ||
| } | ||
|
|
||
| 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 string, batch int) error { | ||
| return r.query.Table(r.table).Create(map[string]any{ | ||
| "migration": file, | ||
| "batch": batch, | ||
| }) | ||
| } | ||
|
|
||
| func (r *Repository) RepositoryExists() bool { | ||
| return r.schema.HasTable(r.table) | ||
| } | ||
|
|
||
| func (r *Repository) getLastBatchNumber() (int, error) { | ||
| var batch int | ||
| if err := r.query.Table(r.table).OrderByDesc("batch").Limit(1).Pluck("batch", &batch); err != nil { | ||
| return 0, err | ||
| } | ||
|
|
||
| return batch, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| package migration | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/suite" | ||
|
|
||
| "github.com/goravel/framework/contracts/database" | ||
| "github.com/goravel/framework/database/gorm" | ||
| mocksorm "github.com/goravel/framework/mocks/database/orm" | ||
| "github.com/goravel/framework/support/docker" | ||
| "github.com/goravel/framework/support/env" | ||
| ) | ||
|
|
||
| type RepositoryTestSuite struct { | ||
| suite.Suite | ||
| driverToTestQuery map[database.Driver]*gorm.TestQuery | ||
| } | ||
|
|
||
| func TestRepositoryTestSuite(t *testing.T) { | ||
| if env.IsWindows() { | ||
| t.Skip("Skipping tests of using docker") | ||
| } | ||
|
|
||
| suite.Run(t, &RepositoryTestSuite{}) | ||
| } | ||
|
|
||
| func (s *RepositoryTestSuite) SetupTest() { | ||
| postgresDocker := docker.Postgres() | ||
| postgresQuery := gorm.NewTestQuery(postgresDocker, true) | ||
| s.driverToTestQuery = map[database.Driver]*gorm.TestQuery{ | ||
| database.DriverPostgres: postgresQuery, | ||
| } | ||
| } | ||
hwbrzzl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| func (s *RepositoryTestSuite) TestCreate_Delete_Exists() { | ||
| for driver, testQuery := range s.driverToTestQuery { | ||
| s.Run(driver.String(), func() { | ||
| repository, mockOrm := s.initRepository(testQuery) | ||
|
|
||
| mockOrm.EXPECT().Connection(driver.String()).Return(mockOrm).Once() | ||
| mockOrm.EXPECT().Query().Return(repository.query).Once() | ||
|
|
||
| err := repository.CreateRepository() | ||
| s.NoError(err) | ||
|
|
||
| mockOrm.EXPECT().Query().Return(repository.query).Once() | ||
|
|
||
| s.True(repository.RepositoryExists()) | ||
|
|
||
| mockOrm.EXPECT().Connection(driver.String()).Return(mockOrm).Once() | ||
| mockOrm.EXPECT().Query().Return(repository.query).Once() | ||
|
|
||
| err = repository.DeleteRepository() | ||
| s.NoError(err) | ||
|
|
||
| mockOrm.EXPECT().Query().Return(repository.query).Once() | ||
|
|
||
| s.False(repository.RepositoryExists()) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func (s *RepositoryTestSuite) TestRecord() { | ||
| for driver, testQuery := range s.driverToTestQuery { | ||
| s.Run(driver.String(), func() { | ||
| repository, mockOrm := s.initRepository(testQuery) | ||
|
|
||
| mockOrm.EXPECT().Query().Return(repository.query).Once() | ||
|
|
||
| if !repository.RepositoryExists() { | ||
| mockOrm.EXPECT().Connection(driver.String()).Return(mockOrm).Once() | ||
| mockOrm.EXPECT().Query().Return(repository.query).Once() | ||
|
|
||
| s.NoError(repository.CreateRepository()) | ||
| } | ||
|
|
||
| err := repository.Log("migration1", 1) | ||
| s.NoError(err) | ||
|
|
||
| err = repository.Log("migration2", 1) | ||
| s.NoError(err) | ||
|
|
||
| err = repository.Log("migration3", 2) | ||
| s.NoError(err) | ||
|
|
||
| lastBatchNumber, err := repository.getLastBatchNumber() | ||
| s.NoError(err) | ||
| s.Equal(2, lastBatchNumber) | ||
|
|
||
| nextBatchNumber, err := repository.GetNextBatchNumber() | ||
| s.NoError(err) | ||
| s.Equal(3, nextBatchNumber) | ||
|
|
||
| ranMigrations, err := repository.GetRan() | ||
| s.NoError(err) | ||
| s.ElementsMatch([]string{"migration1", "migration2", "migration3"}, ranMigrations) | ||
|
|
||
| migrations, err := repository.GetMigrations(2) | ||
| s.NoError(err) | ||
| s.Len(migrations, 2) | ||
| s.Equal("migration3", migrations[0].Migration) | ||
| s.Equal(2, migrations[0].Batch) | ||
| s.Equal("migration2", migrations[1].Migration) | ||
| s.Equal(1, migrations[1].Batch) | ||
|
|
||
| migrations, err = repository.GetMigrationsByBatch(1) | ||
| s.NoError(err) | ||
| s.Len(migrations, 2) | ||
| s.Equal("migration2", migrations[0].Migration) | ||
| s.Equal(1, migrations[0].Batch) | ||
| s.Equal("migration1", migrations[1].Migration) | ||
| s.Equal(1, migrations[1].Batch) | ||
|
|
||
| migrations, err = repository.GetLast() | ||
| s.NoError(err) | ||
| s.Len(migrations, 1) | ||
| s.Equal("migration3", migrations[0].Migration) | ||
| s.Equal(2, migrations[0].Batch) | ||
|
|
||
| err = repository.Delete("migration1") | ||
| s.NoError(err) | ||
|
|
||
| ranMigrations, err = repository.GetRan() | ||
| s.NoError(err) | ||
| s.ElementsMatch([]string{"migration2", "migration3"}, ranMigrations) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func (s *RepositoryTestSuite) initRepository(testQuery *gorm.TestQuery) (*Repository, *mocksorm.Orm) { | ||
| schema, mockOrm := initSchema(s.T(), testQuery) | ||
|
|
||
| return NewRepository(testQuery.Query(), schema, "migrations"), mockOrm | ||
| } | ||
hwbrzzl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.