Skip to content
This repository was archived by the owner on Sep 26, 2025. It is now read-only.

Commit 4d226c5

Browse files
authored
adds a flag for specifying the db name for migrations (#185)
## Problem Currently, it seems that the name of the target database for migrations is fixed to "default." This produces the following error when there is no database named "default": ``` problem applying Hasura metadata: problem adding metadata for the buckets table: status_code: 400\nresponse: {"error":"source with name \"default\" does not exist","path":"$.args","code":"not-exists"} ``` (see #184 (comment)) ## Solution To address this issue and provide more flexibility in configuring the target database for migrations, we propose the following solution: - **Environment Variable**: Introduce a new environment variable named `HASURA_DB_NAME` to allow users to specify the target database name for migrations. - **Configuration Update**: Modify the code in `hasura-storage/migrations/hasura.go` to use the value of `HASURA_DB_NAME` as the target database name. This ensures that users can configure the target database dynamically. - **Fallback to "default"**: If `HASURA_DB_NAME` is not set, the code should default to "default" to maintain backward compatibility. ## Notes - Users can set the `HASURA_DB_NAME` environment variable to configure the target database for migrations to their desired database name. - This change enhances the flexibility of the migration process, especially in cases where the database name is not "default." - Reviewers are encouraged to verify that the documentation has been updated to reflect the new configuration option. With this solution, users can customize the target database for migrations by setting the `HASURA_DB_NAME` environment variable, which eliminates the error associated with the fixed "default" database name.
1 parent 97a03dc commit 4d226c5

2 files changed

Lines changed: 13 additions & 8 deletions

File tree

cmd/serve.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const (
4242
corsAllowOriginsFlag = "cors-allow-origins"
4343
corsAllowCredentialsFlag = "cors-allow-credentials" //nolint: gosec
4444
clamavServerFlag = "clamav-server"
45+
hasuraDbNameFlag = "hasura-db-name"
4546
)
4647

4748
func ginLogger(logger *logrus.Logger) gin.HandlerFunc {
@@ -158,6 +159,7 @@ func applymigrations(
158159
hasuraMetadata bool,
159160
hasuraEndpoint string,
160161
hasuraSecret string,
162+
hasuraDbName string,
161163
logger *logrus.Logger,
162164
) {
163165
if postgresMigrations {
@@ -174,7 +176,7 @@ func applymigrations(
174176

175177
if hasuraMetadata {
176178
logger.Info("applying hasura metadata")
177-
if err := migrations.ApplyHasuraMetadata(hasuraEndpoint, hasuraSecret); err != nil {
179+
if err := migrations.ApplyHasuraMetadata(hasuraEndpoint, hasuraSecret, hasuraDbName); err != nil {
178180
logger.Errorf("problem applying hasura metadata: %s", err.Error())
179181
os.Exit(1)
180182
}
@@ -222,6 +224,7 @@ func init() {
222224
"",
223225
"postgres connection, i.e. postgres://user@pass:localhost:5432/mydb",
224226
)
227+
addStringFlag(serveCmd.Flags(), hasuraDbNameFlag, "default", "Hasura database name")
225228
}
226229

227230
{
@@ -275,6 +278,7 @@ var serveCmd = &cobra.Command{
275278
s3BucketFlag: viper.GetString(s3BucketFlag),
276279
s3RootFolderFlag: viper.GetString(s3RootFolderFlag),
277280
clamavServerFlag: viper.GetString(clamavServerFlag),
281+
hasuraDbNameFlag: viper.GetString(hasuraDbNameFlag),
278282
},
279283
).Debug("parameters")
280284

@@ -294,6 +298,7 @@ var serveCmd = &cobra.Command{
294298
viper.GetBool(hasuraMetadataFlag),
295299
viper.GetString(hasuraEndpointFlag),
296300
viper.GetString(hasuraAdminSecretFlag),
301+
viper.GetString(hasuraDbNameFlag),
297302
logger,
298303
)
299304

migrations/hasura.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,11 @@ type DropRelationshipArgs struct {
142142
Relationship string `json:"relationship"`
143143
}
144144

145-
func ApplyHasuraMetadata(url, hasuraSecret string) error { //nolint: funlen
145+
func ApplyHasuraMetadata(url, hasuraSecret, hasuraDbName string) error { //nolint: funlen
146146
bucketsTable := TrackTable{
147147
Type: "pg_track_table",
148148
Args: PgTrackTableArgs{
149-
Source: "default",
149+
Source: hasuraDbName,
150150
Table: Table{
151151
Schema: "storage",
152152
Name: "buckets",
@@ -185,7 +185,7 @@ func ApplyHasuraMetadata(url, hasuraSecret string) error { //nolint: funlen
185185
filesTable := TrackTable{
186186
Type: "pg_track_table",
187187
Args: PgTrackTableArgs{
188-
Source: "default",
188+
Source: hasuraDbName,
189189
Table: Table{
190190
Schema: "storage",
191191
Name: "files",
@@ -227,7 +227,7 @@ func ApplyHasuraMetadata(url, hasuraSecret string) error { //nolint: funlen
227227
virusTable := TrackTable{
228228
Type: "pg_track_table",
229229
Args: PgTrackTableArgs{
230-
Source: "default",
230+
Source: hasuraDbName,
231231
Table: Table{
232232
Schema: "storage",
233233
Name: "virus",
@@ -270,7 +270,7 @@ func ApplyHasuraMetadata(url, hasuraSecret string) error { //nolint: funlen
270270
Name: "files",
271271
},
272272
Name: "bucket",
273-
Source: "default",
273+
Source: hasuraDbName,
274274
Using: CreateObjectRelationshipUsing{
275275
ForeignKeyConstraintOn: []string{"bucket_id"},
276276
},
@@ -289,7 +289,7 @@ func ApplyHasuraMetadata(url, hasuraSecret string) error { //nolint: funlen
289289
Name: "buckets",
290290
},
291291
Name: "files",
292-
Source: "default",
292+
Source: hasuraDbName,
293293
Using: CreateArrayRelationshipUsing{
294294
ForeignKeyConstraintOn: ForeignKeyConstraintOn{
295295
Table: Table{
@@ -314,7 +314,7 @@ func ApplyHasuraMetadata(url, hasuraSecret string) error { //nolint: funlen
314314
Name: "virus",
315315
},
316316
Name: "file",
317-
Source: "default",
317+
Source: hasuraDbName,
318318
Using: CreateObjectRelationshipUsing{
319319
ForeignKeyConstraintOn: []string{"file_id"},
320320
},

0 commit comments

Comments
 (0)