lahirujayathilake commented on code in PR #474:
URL: https://github.com/apache/airavata-custos/pull/474#discussion_r3279787829


##########
internal/db/migrate.go:
##########
@@ -53,3 +54,32 @@ func MigrateEmbedded(database *sqlx.DB) error {
        slog.Info("database migrations applied successfully")
        return nil
 }
+
+// MigrateConnectorFS applies migrations from the supplied embed.FS to the
+// supplied database, tracking version state in a per-connector table named
+// schema_migrations_<name>. Connectors invoke this via the host so version
+// sequences never collide across connectors or with core.
+func MigrateConnectorFS(database *sqlx.DB, src fs.FS, dir, name string) error {
+       driver, err := mysql.WithInstance(database.DB, &mysql.Config{
+               MigrationsTable: "schema_migrations_" + name,
+       })
+       if err != nil {
+               return fmt.Errorf("create migration driver for %s: %w", name, 
err)
+       }
+
+       source, err := iofs.New(src, dir)
+       if err != nil {
+               return fmt.Errorf("create migration source for %s: %w", name, 
err)
+       }
+
+       m, err := migrate.NewWithInstance("iofs", source, "mysql", driver)
+       if err != nil {
+               return fmt.Errorf("create migrator for %s: %w", name, err)
+       }
+
+       if err := m.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) {
+               return fmt.Errorf("run migrations for %s: %w", name, err)
+       }
+       slog.Info("connector migrations applied", "connector", name)
+       return nil
+}

Review Comment:
   
https://github.com/apache/airavata-custos/pull/476/changes#diff-311f48b0a3cbb726ac3f19ee0eb90bdeb9ad2014003685c87ef46b6ee920ff86
 (updated file in the new PR) this is the migration wrapper to apply core level 
schema migrations (MigrateEmbedded) and connector level  schema migrations 
(MigrateConnectorFS) at startup
   
   and to isolate core level and connector level tables,
   
   1. location -> core level migrations reside in `internal/db/migrations/` and 
if each connector needs its own schemas (like AMIE) they would be inside the 
connector's  <>/db/migrations directory
   
   2. to track core will have `schema_migrations` and connectors will have 
`schema_migrations_<name>` (eg, `schema_migrations_amie`)
   
   3. connector tables are prefixed --> eg, `amie_*`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to