@@ -33,13 +33,16 @@ const (
3333 LatestSchemaFileName = "LATEST.sql"
3434)
3535
36- // Migrate applies the latest schema to the database.
36+ // Migrate migrates the database schema to the latest version.
37+ // It checks the current schema version and applies any necessary migrations.
38+ // It also seeds the database with initial data if in demo mode.
3739func (s * Store ) Migrate (ctx context.Context ) error {
3840 if err := s .preMigrate (ctx ); err != nil {
3941 return errors .Wrap (err , "failed to pre-migrate" )
4042 }
4143
42- if s .profile .Mode == "prod" {
44+ switch s .profile .Mode {
45+ case "prod" :
4346 workspaceBasicSetting , err := s .GetWorkspaceBasicSetting (ctx )
4447 if err != nil {
4548 return errors .Wrap (err , "failed to get workspace basic setting" )
@@ -94,7 +97,7 @@ func (s *Store) Migrate(ctx context.Context) error {
9497 return errors .Wrap (err , "failed to update current schema version" )
9598 }
9699 }
97- } else if s . profile . Mode == "demo" {
100+ case "demo" :
98101 // In demo mode, we should seed the database.
99102 if err := s .seed (ctx ); err != nil {
100103 return errors .Wrap (err , "failed to seed" )
@@ -103,6 +106,7 @@ func (s *Store) Migrate(ctx context.Context) error {
103106 return nil
104107}
105108
109+ // preMigrate checks if the database is initialized and applies the latest schema if not.
106110func (s * Store ) preMigrate (ctx context.Context ) error {
107111 initialized , err := s .driver .IsInitialized (ctx )
108112 if err != nil {
@@ -157,6 +161,9 @@ func (s *Store) getSeedBasePath() string {
157161 return fmt .Sprintf ("seed/%s/" , s .profile .Driver )
158162}
159163
164+ // seed seeds the database with initial data.
165+ // It reads all seed files from the embedded filesystem and executes them in order.
166+ // This is only supported for SQLite databases.
160167func (s * Store ) seed (ctx context.Context ) error {
161168 // Only seed for SQLite.
162169 if s .profile .Driver != "sqlite" {
@@ -205,6 +212,9 @@ func (s *Store) GetCurrentSchemaVersion() (string, error) {
205212 return s .getSchemaVersionOfMigrateScript (filePaths [len (filePaths )- 1 ])
206213}
207214
215+ // getSchemaVersionOfMigrateScript extracts the schema version from the migration script file path.
216+ // It returns the schema version in the format "major.minor.patch".
217+ // If the file is the latest schema file, it returns the current schema version.
208218func (s * Store ) getSchemaVersionOfMigrateScript (filePath string ) (string , error ) {
209219 // If the file is the latest schema file, return the current schema version.
210220 if strings .HasSuffix (filePath , LatestSchemaFileName ) {
@@ -225,14 +235,17 @@ func (s *Store) getSchemaVersionOfMigrateScript(filePath string) (string, error)
225235 return fmt .Sprintf ("%s.%d" , minorVersion , patchVersion + 1 ), nil
226236}
227237
228- // execute runs a single SQL statement within a transaction.
238+ // execute executes a SQL statement within a transaction context.
239+ // It returns an error if the execution fails.
229240func (* Store ) execute (ctx context.Context , tx * sql.Tx , stmt string ) error {
230241 if _ , err := tx .ExecContext (ctx , stmt ); err != nil {
231242 return errors .Wrap (err , "failed to execute statement" )
232243 }
233244 return nil
234245}
235246
247+ // updateCurrentSchemaVersion updates the current schema version in the workspace basic setting.
248+ // It retrieves the workspace basic setting, updates the schema version, and upserts the setting back to the database.
236249func (s * Store ) updateCurrentSchemaVersion (ctx context.Context , schemaVersion string ) error {
237250 workspaceBasicSetting , err := s .GetWorkspaceBasicSetting (ctx )
238251 if err != nil {
@@ -248,6 +261,8 @@ func (s *Store) updateCurrentSchemaVersion(ctx context.Context, schemaVersion st
248261 return nil
249262}
250263
264+ // normalizeMigrationHistoryList normalizes the migration history list.
265+ // It checks the existing migration history and updates it to the latest schema version if necessary.
251266func (s * Store ) normalizeMigrationHistoryList (ctx context.Context ) error {
252267 migrationHistoryList , err := s .driver .FindMigrationHistoryList (ctx , & FindMigrationHistory {})
253268 if err != nil {
@@ -299,6 +314,8 @@ func (s *Store) normalizeMigrationHistoryList(ctx context.Context) error {
299314 return nil
300315}
301316
317+ // migrateSchemaVersionToSetting migrates the schema version from the migration history to the workspace basic setting.
318+ // It retrieves the migration history, sorts the versions, and updates the workspace basic setting if necessary
302319func (s * Store ) migrateSchemaVersionToSetting (ctx context.Context ) error {
303320 migrationHistoryList , err := s .driver .FindMigrationHistoryList (ctx , & FindMigrationHistory {})
304321 if err != nil {
0 commit comments