-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This allows users to connect to a postgresql backend for our ent library. The ent client creation was broken into it's own function so we can use the enttest library to pass in the test client for unit testing in the future.
- Loading branch information
1 parent
658b3f6
commit 56a98e9
Showing
7 changed files
with
111 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Copyright 2023 The Archivista Contributors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
services: | ||
psql: | ||
image: postgres:16 | ||
restart: always | ||
environment: | ||
POSTGRES_USER: testify | ||
POSTGRES_PASSWORD: example | ||
|
||
archivista: | ||
environment: | ||
ARCHIVISTA_SQL_STORE_BACKEND: PSQL | ||
ARCHIVISTA_SQL_STORE_CONNECTION_STRING: postgresql://testify:example@psql?sslmode=disable |
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,72 @@ | ||
// Copyright 2023 The Archivista Contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package sqlstore | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"ariga.io/sqlcomment" | ||
"entgo.io/ent/dialect" | ||
"entgo.io/ent/dialect/sql" | ||
"github.com/go-sql-driver/mysql" | ||
"github.com/testifysec/archivista/ent" | ||
|
||
_ "github.com/lib/pq" | ||
) | ||
|
||
// NewEntClient creates an ent client for use in the sqlmetadata store. | ||
// Valid backends are MYSQL and PSQL. | ||
func NewEntClient(sqlBackend string, connectionString string) (*ent.Client, error) { | ||
var entDialect string | ||
switch strings.ToUpper(sqlBackend) { | ||
case "MYSQL": | ||
dbConfig, err := mysql.ParseDSN(connectionString) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not parse mysql connection string: %w", err) | ||
} | ||
|
||
// this tells the go-sql-driver to parse times from mysql to go's time.Time | ||
// see https://github.com/go-sql-driver/mysql#timetime-support for details | ||
dbConfig.ParseTime = true | ||
entDialect = dialect.MySQL | ||
connectionString = dbConfig.FormatDSN() | ||
case "PSQL": | ||
entDialect = dialect.Postgres | ||
default: | ||
return nil, fmt.Errorf("unknown sql backend: %s", sqlBackend) | ||
} | ||
|
||
drv, err := sql.Open(entDialect, connectionString) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not open sql connection: %w", err) | ||
} | ||
|
||
db := drv.DB() | ||
db.SetMaxIdleConns(10) | ||
db.SetMaxOpenConns(100) | ||
db.SetConnMaxLifetime(3 * time.Minute) | ||
sqlcommentDrv := sqlcomment.NewDriver(drv, | ||
sqlcomment.WithDriverVerTag(), | ||
sqlcomment.WithTags(sqlcomment.Tags{ | ||
sqlcomment.KeyApplication: "archivista", | ||
sqlcomment.KeyFramework: "net/http", | ||
}), | ||
) | ||
|
||
client := ent.NewClient(ent.Driver(sqlcommentDrv)) | ||
return client, nil | ||
} |
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