Skip to content

Commit

Permalink
[COR-916] Check if database already exists before trying to create. (#5)
Browse files Browse the repository at this point in the history
* Testing check if database exists.

* [COR-916] Check if database already exists before trying to create casbin database.
  • Loading branch information
giacoman authored Jun 4, 2021
1 parent 1796bd0 commit a1e0877
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ func (a *Adapter) createDatabase() error {
}

if a.driverName == "postgres" || a.driverName == "pq-timeouts" {
//COR-916: If database already exists, just exit. Users without permissions to create database
//will fail on CREATE DATABASE call further on down.
casbindb := db.Exec("SELECT 1 FROM pg_database WHERE datname = 'casbin'")
if casbindb.Error == nil && casbindb.RowsAffected == 1 {
db.Close()
return nil
}

if err = db.Exec("CREATE DATABASE casbin").Error; err != nil {
// 42P04 is duplicate_database
if err.(*pq.Error).Code == "42P04" {
Expand Down

0 comments on commit a1e0877

Please sign in to comment.