Skip to content

Commit 68cdafb

Browse files
authored
handle "postgresql" during DB migration (#104)
Handle `postgresql` when setting the driver for DB migration
1 parent 239c1a7 commit 68cdafb

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

dbos/system_database.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,28 +140,35 @@ const (
140140

141141
func runMigrations(databaseURL string) error {
142142
// Change the driver to pgx5
143-
databaseURL = "pgx5://" + strings.TrimPrefix(databaseURL, "postgres://")
144-
145-
// Create migration source from embedded files
146-
d, err := iofs.New(migrationFiles, "migrations")
147-
if err != nil {
148-
return newInitializationError(fmt.Sprintf("failed to create migration source: %v", err))
149-
}
150-
151-
// Add custom migration table name to avoid conflicts with user migrations
152-
// Parse the URL to properly determine where to add the query parameter
153143
parsedURL, err := url.Parse(databaseURL)
154144
if err != nil {
155145
return newInitializationError(fmt.Sprintf("failed to parse database URL: %v", err))
156146
}
147+
// Handle various PostgreSQL URL schemes
148+
switch parsedURL.Scheme {
149+
case "postgres", "postgresql":
150+
parsedURL.Scheme = "pgx5"
151+
case "pgx5":
152+
// Already in correct format
153+
default:
154+
return newInitializationError(fmt.Sprintf("unsupported database URL scheme: %s", parsedURL.Scheme))
155+
}
156+
databaseURL = parsedURL.String()
157157

158+
// Add custom migration table name to avoid conflicts with user migrations
158159
// Check if query parameters already exist
159160
separator := "?"
160161
if parsedURL.RawQuery != "" {
161162
separator = "&"
162163
}
163164
databaseURL += separator + "x-migrations-table=" + _DBOS_MIGRATION_TABLE
164165

166+
// Create migration source from embedded files
167+
d, err := iofs.New(migrationFiles, "migrations")
168+
if err != nil {
169+
return newInitializationError(fmt.Sprintf("failed to create migration source: %v", err))
170+
}
171+
165172
// Create migrator
166173
m, err := migrate.NewWithSourceInstance("iofs", d, databaseURL)
167174
if err != nil {

0 commit comments

Comments
 (0)