-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create artifact database if it does not exist (#4)
Because - In order to expose the tag list of an image in `repository`, we need to deploy `artifact-backend` and serve its API. This commit - Modifies the existing configuration and initialisation so `artifact-backend` can be built and run locally (`make build` / `make dev`) ## ⏭️ Next steps - [ ] `artifact-backend` will implement the endpoints added in instill-ai/protobufs#286 - [ ] `artifact-backend` should be built along with the other Instill services at [`instill-core`](https://github.com/instill-ai/instill-core) - The cloud versions of these repos should be updated, too.
- Loading branch information
Showing
8 changed files
with
112 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,62 @@ | ||
package main | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
|
||
_ "github.com/golang-migrate/migrate/v4/database/postgres" | ||
|
||
"github.com/instill-ai/artifact-backend/config" | ||
) | ||
|
||
func dbExistsOrCreate(databaseConfig config.DatabaseConfig) error { | ||
datasource := fmt.Sprintf("host=%s user=%s password=%s dbname=postgres port=%d sslmode=disable TimeZone=%s", | ||
databaseConfig.Host, | ||
databaseConfig.Username, | ||
databaseConfig.Password, | ||
databaseConfig.Port, | ||
databaseConfig.TimeZone, | ||
) | ||
|
||
db, err := sql.Open("postgres", datasource) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer db.Close() | ||
|
||
// Open() may just validate its arguments without creating a connection to the database. | ||
// To verify that the data source name is valid, call Ping(). | ||
if err = db.Ping(); err != nil { | ||
return err | ||
} | ||
|
||
var count int | ||
|
||
q := fmt.Sprintf("SELECT count(*) FROM pg_catalog.pg_database WHERE datname = '%s';", databaseConfig.Name) | ||
if err := db.QueryRow(q).Scan(&count); err != nil { | ||
return err | ||
} | ||
|
||
if count > 0 { | ||
return nil | ||
} | ||
|
||
fmt.Printf("Create database %s\n", databaseConfig.Name) | ||
if _, err := db.Exec(fmt.Sprintf("CREATE DATABASE %s;", databaseConfig.Name)); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
func main() { | ||
if err := config.Init(); err != nil { | ||
panic(err) | ||
} | ||
|
||
databaseConfig := config.Config.Database | ||
|
||
if err := dbExistsOrCreate(databaseConfig); err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains 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 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 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