Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

280 use database url #293

Merged
merged 11 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions .env
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
SQLITE_HOST="/root/project/db.sqlite3"
# SQLITE_HOST=":memory:"
MY_HOST=mysql
MARIA_HOST=mariadb
MY_PORT=3306
PG_HOST=postgres
PG_PORT=5432
# SQLITE_HOST="/root/project/db.sqlite3"
SQLITE_HOST=":memory:"
PG_URL="postgresql://user:pass@postgres:5432/database"
MARIA_URL="mariadb://user:pass@mariadb:3306/database"
MY_URL="mysql://user:pass@mysql:3306/database"
SURREAL_HOST="http://surreal"
SURREAL_PORT=8000
DB_USER=user
Expand Down
5 changes: 3 additions & 2 deletions allographer.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ when (NimMajor, NimMinor) > (1, 6):
import strformat, os

task test, "run testament test":
exec &"testament p 'tests/v{NimMajor}/*/test_*.nim'"
exec &"testament p 'tests/v2/test_*.nim'"
exec &"testament p tests/v{NimMajor}/test*"
exec &"testament p tests/v{NimMajor}/*/test*"

for kind, path in walkDir(getCurrentDir() / "tests"):
if not path.contains(".") and path.fileExists():
exec "rm -f " & path
Expand Down
11 changes: 3 additions & 8 deletions example/database/.env
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
SQLITE_HOST="/root/project/example/db.sqlite3"
# SQLITE_HOST=":memory:"
MY_HOST=mysql
MARIA_HOST=mariadb
MY_PORT=3306
PG_HOST=postgres
PG_PORT=5432
DB_USER=user
DB_PASSWORD=pass
DB_DATABASE=database
PG_URL=postgres://user:pass@postgres:5432/database
MYSQL_URL=mysql://user:pass@mysql:3306/database
MARIA_URL=mariadb://user:pass@mariadb:3306/database
DB_MAX_CONNECTION=95
DB_TIMEOUT=30

Expand Down
16 changes: 6 additions & 10 deletions example/database/connection.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@ import std/strutils
import ../../src/allographer/connection

let
database = getEnv("DB_DATABASE")
user = getEnv("DB_USER")
password = getEnv("DB_PASSWORD")
pgHost = getEnv("PG_HOST")
pgPort = getEnv("PG_PORT").parseInt
mariaHost = getEnv("MARIA_HOST")
myPort = getEnv("MY_PORT").parseInt
pgUrl = getEnv("PG_URL")
mariaUrl = getEnv("MARIA_URL")
mysqlUrl = getEnv("MYSQL_URL")
maxConnections = getEnv("DB_MAX_CONNECTION").parseInt
timeout = getEnv("DB_TIMEOUT").parseInt

# let rdb* = dbOpen(SQLite3, "./db.sqlite3", shouldDisplayLog=true)
# let rdb* = dbOpen(PostgreSQL, database, user, password, pgHost, pgPort, maxConnections, timeout, shouldDisplayLog=true)
# let rdb* = dbOpen(Mariadb, database, user, password, mariaHost, myPort, maxConnections, timeout, shouldDisplayLog=true)
let rdb* = dbOpen(MySQL, database, user, password, "mysql", myPort, maxConnections, timeout, shouldDisplayLog=true)
let rdb* = dbOpen(PostgreSQL, pgUrl, maxConnections, timeout, shouldDisplayLog=true)
# let rdb* = dbOpen(Mariadb, mariaUrl, maxConnections, timeout, shouldDisplayLog=true)
# let rdb* = dbOpen(MySQL, mysqlUrl, maxConnections, timeout, shouldDisplayLog=true)
# let rdb* = dbOpen(SurrealDB, "test", "test", "user", "pass", "http://surreal", 8000, 5, 30, shouldDisplayLog=true).waitFor()
21 changes: 19 additions & 2 deletions src/allographer/v1/query_builder/models/mariadb/mariadb_open.nim
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import std/times
import std/strutils
import ../../libs/mariadb/mariadb_rdb
import ../../error
import ../../log
import ./mariadb_types


proc dbOpen*(_: type MariaDB, database: string = "", user: string = "", password: string = "",
host: string = "", port: int = 0, maxConnections: int = 1, timeout=30,
proc dbOpen*(_: type MariaDB, database: string, user: string, password: string,
host: string, port: int, maxConnections=1, timeout=30,
shouldDisplayLog=false, shouldOutputLogFile=false, logDir=""): MariadbConnections =
var conns = newSeq[Connection](maxConnections)
for i in 0..<maxConnections:
Expand Down Expand Up @@ -39,3 +40,19 @@ proc dbOpen*(_: type MariaDB, database: string = "", user: string = "", password
info: info,
log: LogSetting(shouldDisplayLog:shouldDisplayLog, shouldOutputLogFile:shouldOutputLogFile, logDir:logDir)
)


proc dbOpen*(_:type MariaDB, url: string, maxConnections=1, timeout=30,
shouldDisplayLog=false, shouldOutputLogFile=false, logDir=""): MariadbConnections =
## url: "mariadb://username:password@localhost:3306/DB_Name"
let isMariadb = url.startsWith("mariadb://")
if not isMariadb:
raise newException(ValueError, "Invalid URL format. Expected a MariaDB URL starting with 'mariadb://'.")

let user = url.split("://")[1].split(":")[0]
let password = url.split(":")[2].split("@")[0]
let host = url.split("@")[1].split(":")[0]
let port = url.split(":")[3].split("/")[0]
let database = url.split("/")[^1]

return dbOpen(Mariadb, database, user, password, host, port.parseInt, maxConnections, timeout, shouldDisplayLog, shouldOutputLogFile, logDir)
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,7 @@ type RawMariadbQuery* = ref object

proc `$`*(self:MariadbConnections|MariadbQuery|RawMariadbQuery):string =
return "MariaDB"


proc isConnected*(self:MariadbConnections|MariadbQuery|RawMariadbQuery):bool =
return self.pools.conns.len > 0
21 changes: 19 additions & 2 deletions src/allographer/v1/query_builder/models/mysql/mysql_open.nim
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import std/times
import std/strutils
import ../../libs/mysql/mysql_rdb
import ../../error
import ../../log
import ./mysql_types


proc dbOpen*(_:type MySQL, database: string = "", user: string = "", password: string = "",
host: string = "", port: int = 0, maxConnections: int = 1, timeout=30,
proc dbOpen*(_:type MySQL, database: string, user: string, password: string,
host: string, port: int, maxConnections: int = 1, timeout=30,
shouldDisplayLog=false, shouldOutputLogFile=false, logDir=""): MysqlConnections =
var conns = newSeq[Connection](maxConnections)
for i in 0..<maxConnections:
Expand Down Expand Up @@ -39,3 +40,19 @@ proc dbOpen*(_:type MySQL, database: string = "", user: string = "", password: s
info: info,
log: LogSetting(shouldDisplayLog:shouldDisplayLog, shouldOutputLogFile:shouldOutputLogFile, logDir:logDir)
)


proc dbOpen*(_:type MySQL, url: string, maxConnections=1, timeout=30,
shouldDisplayLog=false, shouldOutputLogFile=false, logDir=""): MysqlConnections =
## url: "mysql://username:password@localhost:3306/DB_Name"
let isMariadb = url.startsWith("mysql://")
if not isMariadb:
raise newException(ValueError, "Invalid URL format. Expected a MariaDB URL starting with 'mariadb://'.")

let user = url.split("://")[1].split(":")[0]
let password = url.split(":")[2].split("@")[0]
let host = url.split("@")[1].split(":")[0]
let port = url.split(":")[3].split("/")[0]
let database = url.split("/")[^1]

return dbOpen(MySQL, database, user, password, host, port.parseInt, maxConnections, timeout, shouldDisplayLog, shouldOutputLogFile, logDir)
4 changes: 4 additions & 0 deletions src/allographer/v1/query_builder/models/mysql/mysql_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,7 @@ type RawMysqlQuery* = ref object

proc `$`*(self:MysqlConnections|MysqlQuery|RawMysqlQuery):string =
return "MySQL"


proc isConnected*(self:MysqlConnections|MysqlQuery|RawMysqlQuery):bool =
return self.pools.conns.len > 0
23 changes: 20 additions & 3 deletions src/allographer/v1/query_builder/models/postgres/postgres_open.nim
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import std/times
import std/strutils
import ../../libs/postgres/postgres_rdb
import ../../libs/postgres/postgres_lib
import ../../log
import ./postgres_types


proc dbOpen*(_:type PostgreSQL, database: string = "", user: string = "", password: string = "",
host: string = "", port: int = 0, maxConnections: int = 1, timeout=30,
shouldDisplayLog=false, shouldOutputLogFile=false, logDir=""): PostgresConnections =
proc dbOpen*(_:type PostgreSQL, database: string, user: string, password: string,
host: string, port: int, maxConnections=1, timeout=30,
shouldDisplayLog=false, shouldOutputLogFile=false, logDir=""): PostgresConnections =
var conns = newSeq[Connection](maxConnections)
for i in 0..<maxConnections:
let conn = postgres_rdb.pqsetdbLogin(host, port.`$`.cstring, nil, nil, database, user, password)
Expand All @@ -25,3 +26,19 @@ proc dbOpen*(_:type PostgreSQL, database: string = "", user: string = "", passwo
pools: pools,
log: LogSetting(shouldDisplayLog:shouldDisplayLog, shouldOutputLogFile:shouldOutputLogFile, logDir:logDir)
)


proc dbOpen*(_:type PostgreSQL, url: string, maxConnections: int = 1, timeout=30,
shouldDisplayLog=false, shouldOutputLogFile=false, logDir=""): PostgresConnections =
## url: "postgresql://user:pass@host:port/database"
let isPostgres = url.startsWith("postgresql://")
if not isPostgres:
raise newException(ValueError, "Invalid URL format. Expected a PostgreSQL URL starting with 'postgresql://'.")

let user = url.split("://")[1].split(":")[0]
let password = url.split(":")[2].split("@")[0]
let host = url.split("@")[1].split(":")[0]
let port = url.split(":")[3].split("/")[0]
let database = url.split("/")[^1]

return dbOpen(PostgreSQL, database, user, password, host, port.parseInt, maxConnections, timeout, shouldDisplayLog, shouldOutputLogFile, logDir)
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ type RawPostgresQuery* = ref object

proc `$`*(self:PostgresConnections|PostgresQuery|RawPostgresQuery):string =
return "PostgreSQL"


proc isConnected*(self:PostgresConnections|PostgresQuery|RawPostgresQuery):bool =
return self.pools.conns.len > 0
21 changes: 19 additions & 2 deletions src/allographer/v2/query_builder/models/mariadb/mariadb_open.nim
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import std/times
import std/strutils
import ../../libs/mariadb/mariadb_rdb
import ../../error
import ../../log
import ./mariadb_types


proc dbOpen*(_: type MariaDB, database: string = "", user: string = "", password: string = "",
host: string = "", port: int = 0, maxConnections: int = 1, timeout=30,
proc dbOpen*(_: type MariaDB, database: string, user: string, password: string,
host: string, port: int, maxConnections=1, timeout=30,
shouldDisplayLog=false, shouldOutputLogFile=false, logDir=""): MariadbConnections =
var conns = newSeq[Connection](maxConnections)
for i in 0..<maxConnections:
Expand Down Expand Up @@ -39,3 +40,19 @@ proc dbOpen*(_: type MariaDB, database: string = "", user: string = "", password
info: info,
log: LogSetting(shouldDisplayLog:shouldDisplayLog, shouldOutputLogFile:shouldOutputLogFile, logDir:logDir)
)


proc dbOpen*(_:type MariaDB, url: string, maxConnections: int = 1, timeout=30,
shouldDisplayLog=false, shouldOutputLogFile=false, logDir=""): MariadbConnections =
## url: "mariadb://username:password@localhost:3306/DB_Name"
let isMariadb = url.startsWith("mariadb://")
if not isMariadb:
raise newException(ValueError, "Invalid URL format. Expected a MariaDB URL starting with 'mariadb://'.")

let user = url.split("://")[1].split(":")[0]
let password = url.split(":")[2].split("@")[0]
let host = url.split("@")[1].split(":")[0]
let port = url.split(":")[3].split("/")[0]
let database = url.split("/")[^1]

return dbOpen(Mariadb, database, user, password, host, port.parseInt, maxConnections, timeout, shouldDisplayLog, shouldOutputLogFile, logDir)
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,7 @@ type RawMariadbQuery* = ref object

proc `$`*(self:MariadbConnections|MariadbQuery|RawMariadbQuery):string =
return "MariaDB"


proc isConnected*(self:MariadbConnections|MariadbQuery|RawMariadbQuery):bool =
return self.pools.conns.len > 0
21 changes: 19 additions & 2 deletions src/allographer/v2/query_builder/models/mysql/mysql_open.nim
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import std/times
import std/strutils
import ../../libs/mysql/mysql_rdb
import ../../error
import ../../log
import ./mysql_types


proc dbOpen*(_:type MySQL, database: string = "", user: string = "", password: string = "",
host: string = "", port: int = 0, maxConnections: int = 1, timeout=30,
proc dbOpen*(_:type MySQL, database: string, user: string, password: string,
host: string, port: int, maxConnections: int = 1, timeout=30,
shouldDisplayLog=false, shouldOutputLogFile=false, logDir=""): MysqlConnections =
var conns = newSeq[Connection](maxConnections)
for i in 0..<maxConnections:
Expand Down Expand Up @@ -39,3 +40,19 @@ proc dbOpen*(_:type MySQL, database: string = "", user: string = "", password: s
info: info,
log: LogSetting(shouldDisplayLog:shouldDisplayLog, shouldOutputLogFile:shouldOutputLogFile, logDir:logDir)
)


proc dbOpen*(_:type MySQL, url: string, maxConnections=1, timeout=30,
shouldDisplayLog=false, shouldOutputLogFile=false, logDir=""): MysqlConnections =
## url: "mysql://username:password@localhost:3306/DB_Name"
let isMariadb = url.startsWith("mysql://")
if not isMariadb:
raise newException(ValueError, "Invalid URL format. Expected a MariaDB URL starting with 'mariadb://'.")

let user = url.split("://")[1].split(":")[0]
let password = url.split(":")[2].split("@")[0]
let host = url.split("@")[1].split(":")[0]
let port = url.split(":")[3].split("/")[0]
let database = url.split("/")[^1]

return dbOpen(MySQL, database, user, password, host, port.parseInt, maxConnections, timeout, shouldDisplayLog, shouldOutputLogFile, logDir)
4 changes: 4 additions & 0 deletions src/allographer/v2/query_builder/models/mysql/mysql_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,7 @@ type RawMysqlQuery* = ref object

proc `$`*(self:MysqlConnections|MysqlQuery|RawMysqlQuery):string =
return "MySQL"


proc isConnected*(self:MysqlConnections|MysqlQuery|RawMysqlQuery):bool =
return self.pools.conns.len > 0
24 changes: 20 additions & 4 deletions src/allographer/v2/query_builder/models/postgres/postgres_open.nim
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import std/times
import std/strutils
import ../../libs/postgres/postgres_rdb
import ../../libs/postgres/postgres_lib
import ../../log
import ./postgres_types


proc dbOpen*(_:type PostgreSQL, database: string = "", user: string = "", password: string = "",
host: string = "", port:int = 0, maxConnections: int = 1, timeout=30,
shouldDisplayLog=false, shouldOutputLogFile=false, logDir=""): PostgresConnections =
proc dbOpen*(_:type PostgreSQL, database: string, user: string, password: string,
host: string, port: int, maxConnections=1, timeout=30,
shouldDisplayLog=false, shouldOutputLogFile=false, logDir=""): PostgresConnections =
var conns = newSeq[Connection](maxConnections)
for i in 0..<maxConnections:
let conn = postgres_rdb.pqsetdbLogin(host, port.`$`.cstring, nil, nil, database, user, password)
Expand All @@ -25,3 +25,19 @@ proc dbOpen*(_:type PostgreSQL, database: string = "", user: string = "", passwo
pools: pools,
log: LogSetting(shouldDisplayLog:shouldDisplayLog, shouldOutputLogFile:shouldOutputLogFile, logDir:logDir)
)


proc dbOpen*(_:type PostgreSQL, url: string, maxConnections: int = 1, timeout=30,
shouldDisplayLog=false, shouldOutputLogFile=false, logDir=""): PostgresConnections =
## url: "postgresql://user:pass@host:port/database"
let isPostgres = url.startsWith("postgresql://")
if not isPostgres:
raise newException(ValueError, "Invalid URL format. Expected a PostgreSQL URL starting with 'postgresql://'.")

let user = url.split("://")[1].split(":")[0]
let password = url.split(":")[2].split("@")[0]
let host = url.split("@")[1].split(":")[0]
let port = url.split(":")[3].split("/")[0]
let database = url.split("/")[^1]

return dbOpen(PostgreSQL, database, user, password, host, port.parseInt, maxConnections, timeout, shouldDisplayLog, shouldOutputLogFile, logDir)
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ type RawPostgresQuery* = ref object

proc `$`*(self:PostgresConnections|PostgresQuery|RawPostgresQuery):string =
return "PostgreSQL"


proc isConnected*(self:PostgresConnections|PostgresQuery|RawPostgresQuery):bool =
return self.pools.conns.len > 0
59 changes: 59 additions & 0 deletions tests/clear_tables.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import std/asyncdispatch
import std/json
import std/strformat
import std/strutils
import ../src/allographer/query_builder


proc clearTables*(rdb:PostgresConnections) {.async.} =
let tables = rdb.table("_allographer_migrations").orderBy("id", Desc) .get().await
for table in tables:
let tableName = table["name"].getStr()
if not tableName.startsWith("_"):
rdb.raw(&"DROP TABLE IF EXISTS \"{tableName}\"").exec().await

rdb.raw("DROP TABLE IF EXISTS \"_allographer_migrations\"").exec().await


proc clearTables*(rdb:MariaDBConnections) {.async.} =
let tables = rdb.table("_allographer_migrations").orderBy("id", Desc) .get().await
for table in tables:
let tableName = table["name"].getStr()
if not tableName.startsWith("_"):
rdb.raw(&"DROP TABLE IF EXISTS `{tableName}`").exec().await

rdb.raw("DROP TABLE IF EXISTS `_allographer_migrations`").exec().await


proc clearTables*(rdb:MySQLConnections) {.async.} =
let tables = rdb.table("_allographer_migrations").orderBy("id", Desc) .get().await
for table in tables:
let tableName = table["name"].getStr()
if not tableName.startsWith("_"):
rdb.raw(&"DROP TABLE IF EXISTS `{tableName}`").exec().await

rdb.raw("DROP TABLE IF EXISTS `_allographer_migrations`").exec().await


proc clearTables*(rdb:SqliteConnections) {.async.} =
let tables = rdb.table("_allographer_migrations").orderBy("id", Desc) .get().await
for table in tables:
let tableName = table["name"].getStr()
if not tableName.startsWith("_"):
rdb.raw(&"DROP TABLE IF EXISTS \"{tableName}\"").exec().await

rdb.raw("DROP TABLE IF EXISTS \"_allographer_migrations\"").exec().await


proc clearTables*(rdb:SurrealConnections) {.async.} =
let dbInfo = rdb.raw("INFO FOR DB").info().await
echo "=".repeat(30)
echo "dbInfo: ", dbInfo

let tables = dbInfo[0]["result"]["tb"]
for (table, _) in tables.pairs:
if not table.startsWith("_"):
rdb.raw(&"REMOVE TABLE {table}").exec().await

rdb.raw(&"REMOVE TABLE _allographer_migrations").exec().await
rdb.raw(&"REMOVE TABLE _autoincrement_sequences").exec().await
Loading
Loading