Skip to content

Commit

Permalink
288 create schema file (#289)
Browse files Browse the repository at this point in the history
* fix other example

* .

* fix archetecture

* postgres create schema

* sqlite create schema

* mariadb create scema

* mysql create scema

* surreal create schema

* delete echo

* postgres create scema test

* maiadb test create schema

* postgres test create schema

* surreal test create schema

* fix table name

* fix create schema

* データベース接続とスキーマの更新

- PostgreSQL接続の設定を有効化し、SQLite3の接続をコメントアウトしました。
- スキーマのオブジェクト名を変更し、新しいフィールドを追加しました(例: UserTable, PostTable)。
- シーダーのロジックを改善し、非同期処理を適切に使用しました。
- 不要なコメントを削除し、コードの可読性を向上させました。

* fix

* test_schema.nimの文字列カラムサイズを255から256に更新

- 'char'カラムのサイズをさまざまなテーブル定義およびチェックで拡張し、より大きな文字列を扱えるようにしました。
- 新しいサイズ制限を反映するために、テスト内の関連するアサーションを更新しました。
- テストを直接コンパイルして実行するコマンドを追加しました。

* refactor: テストコードのテーブルクリア処理を共通化

テストコードの最後でテーブルをクリアする処理を各テストファイルから clear_tables.nim に移動し、共通化しました。
これにより、テストファイル内のテーブルクリア処理が簡潔になり、コードの重複も解消されました。
各データベースエンジン(MySQL, MariaDB, PostgreSQL, SQLite)のテストで clear_tables モジュールをインポートし、テスト終了時に clearTables() を呼び出すように変更しています。

* fix test

* .
  • Loading branch information
itsumura-h authored Jan 14, 2025
1 parent 16e5c75 commit 79ab598
Show file tree
Hide file tree
Showing 55 changed files with 1,388 additions and 79 deletions.
2 changes: 1 addition & 1 deletion compose.test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ services:
image: mysql:8
tty: true
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: database
MYSQL_USER: user
MYSQL_PASSWORD: pass
MYSQL_ROOT_PASSWORD: pass

mariadb:
image: mariadb
Expand Down
3 changes: 2 additions & 1 deletion compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ services:
image: mysql:8
tty: true
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: database
MYSQL_USER: user
MYSQL_PASSWORD: pass
MYSQL_ROOT_PASSWORD: pass

mariadb:
image: mariadb
Expand All @@ -32,6 +32,7 @@ services:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: database
MYSQL_USER: user
MYSQL_PASSWORD: pass

postgres:
image: postgres:alpine
Expand Down
3 changes: 2 additions & 1 deletion docker/ubuntu/test.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ RUN apt install -y \
# ca-certificates... for https
# libpcre3-dev... for nim regex

WORKDIR /root

# Nim
ARG NIM_VERSION="2.0.0"
WORKDIR /root
RUN curl https://nim-lang.org/choosenim/init.sh -o init.sh
RUN sh init.sh -y
RUN rm -f init.sh
Expand Down
2 changes: 2 additions & 0 deletions example/benchmark.nim
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ proc query():Future[seq[JsonNode]] {.async.} =
)
return response


proc queryRaw():Future[seq[JsonNode]] {.async.} =
var futures = newSeq[Future[seq[string]]](countNum)
for i in 1..countNum:
Expand Down Expand Up @@ -182,6 +183,7 @@ proc timeProcess[T](name:string, cb:proc():Future[T]) {.async.}=
echo fmt"|Avg|{sumTime / times}|"
echo ""


proc main() =
migrate().waitFor

Expand Down
17 changes: 17 additions & 0 deletions example/database/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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
DB_MAX_CONNECTION=95
DB_TIMEOUT=30

# Logging
LOG_IS_DISPLAY=true
LOG_IS_FILE=false
LOG_DIR="/root/project/example/logs"
21 changes: 21 additions & 0 deletions example/database/connection.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import std/asyncdispatch
import std/os
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
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(SurrealDB, "test", "test", "user", "pass", "http://surreal", 8000, 5, 30, shouldDisplayLog=true).waitFor()
5 changes: 5 additions & 0 deletions example/database/develop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
nim c -d:reset ./migrations/migrate.nim
nim c ./seeder/develop

./migrations/migrate
./seeder/develop
42 changes: 42 additions & 0 deletions example/database/migrations/create_all_type_table.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import std/json
import ../../../src/allographer/schema_builder
import ../connection

proc createAllTypeTable*() =
## 0002
rdb.create([
table("IntRelation", [
Column.increments("id")
]),
table("StrRelation", [
Column.uuid("uuid")
]),
table("Types", [
Column.increments("id"),
Column.integer("integer"),
Column.smallInteger("smallInteger"),
Column.mediumInteger("mediumInteger"),
Column.bigInteger("bigInteger"),
Column.decimal("decimal", 10, 3),
Column.double("double", 10, 3),
Column.float("float"),
Column.uuid("uuid"),
Column.char("char", 255),
Column.string("string"),
Column.text("text"),
Column.mediumText("mediumText"),
Column.longText("longText"),
Column.date("date"),
Column.datetime("datetime"),
Column.time("time"),
Column.timestamp("timestamp"),
Column.timestamps(),
Column.softDelete(),
Column.binary("binary"),
Column.boolean("boolean"),
Column.enumField("enumField", ["A", "B", "C"]),
Column.json("json"),
Column.foreign("int_relation_id").reference("id").onTable("IntRelation").onDelete(SET_NULL),
Column.strForeign("str_relation_id").reference("uuid").onTable("StrRelation").onDelete(SET_NULL)
]),
])
23 changes: 23 additions & 0 deletions example/database/migrations/init_databaase.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import ../../../src/allographer/schema_builder
import ../connection

proc init_databaase*() =
## 0001
rdb.create([
table("user", [
Column.uuid("id").index(),
Column.string("name"),
Column.string("email"),
Column.string("password"),
Column.integer("created_at").index(),
Column.integer("updated_at").index(),
]),
table("post", [
Column.uuid("id").index(),
Column.string("title"),
Column.string("content"),
Column.strForeign("user_id").reference("id").onTable("user"),
Column.integer("created_at").index(),
Column.integer("updated_at").index(),
])
])
12 changes: 12 additions & 0 deletions example/database/migrations/migrate.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import std/asyncdispatch
import ../../../src/allographer/schema_builder
import ../connection
import ./init_databaase
import ./create_all_type_table

proc migrate*() =
init_databaase()
createAllTypeTable()

migrate()
createSchema(rdb).waitFor()
5 changes: 5 additions & 0 deletions example/database/production.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
nim c ./migrations/migrate.nim
nim c database/seeder/production

./migrations/migrate
APP_ENV=production ./database/seeder/production
61 changes: 61 additions & 0 deletions example/database/schema.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import std/json

type IntRelationTable* = object
## IntRelation
id*: int


type StrRelationTable* = object
## StrRelation
uuid*: string


type UserTable* = object
## user
id*: string
name*: string
email*: string
password*: string
created_at*: int
updated_at*: int


type TypesTable* = object
## Types
id*: int
integer*: int
smallInteger*: int
mediumInteger*: int
bigInteger*: int
decimal*: float
double*: float
float*: float
uuid*: string
char*: string
string*: string
text*: string
mediumText*: string
longText*: string
date*: string
datetime*: string
time*: string
timestamp*: string
created_at*: string
updated_at*: string
deleted_at*: string
binary*: string
boolean*: bool
enumField*: string
json*: JsonNode
int_relation_id*: int
str_relation_id*: string


type PostTable* = object
## post
id*: string
title*: string
content*: string
user_id*: string
created_at*: int
updated_at*: int
32 changes: 32 additions & 0 deletions example/database/seeder/data/post_seeder.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import std/json
import std/strformat
import std/asyncdispatch
import std/oids
import std/times
import ../../../../src/allographer/query_builder
import ../../connection
import ../../schema

proc postSeeder*() {.async.} =
seeder(rdb, "post"):
let postCount = rdb.table("post").count().await
if postCount > 0:
return

let users = rdb.table("user").get().orm(UserTable).await
if users.len == 0:
raise newException(ValueError, "No users found in database")

var postList: seq[JsonNode]
for i in 1..users.len:
let row = PostTable(
id: $genOid(),
title: &"post {i}",
content: &"content {i}",
userId: users[i-1].id,
createdAt: now().toTime().toUnix(),
updatedAt: now().toTime().toUnix()
)
postList.add(%row)

rdb.table("post").insert(postList).await
27 changes: 27 additions & 0 deletions example/database/seeder/data/user_seeder.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import std/json
import std/strformat
import std/asyncdispatch
import std/oids
import std/times
import bcrypt
import ../../../../src/allographer/query_builder
import ../../connection
import ../../schema

proc userSeeder*() {.async.} =
seeder(rdb, "user"):
let salt = genSalt(10)

var userList: seq[JsonNode]
for i in 1..10:
let row = UserTable(
id: $genOid(),
name: &"user {i}",
email: &"user{i}@example.com",
password: hash(&"password{i}", salt),
createdAt: now().toTime().toUnix(),
updatedAt: now().toTime().toUnix(),
)
userList.add(%row)

rdb.table("user").insert(userList).await
15 changes: 15 additions & 0 deletions example/database/seeder/develop.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import std/asyncdispatch
import std/os
import ./data/user_seeder
import ./data/post_seeder

proc seed() {.async.} =
let env = getEnv("APP_ENV")
if env != "develop":
raise newException(CatchableError, "This command is only available in the develop environment")

userSeeder().await
postSeeder().await


seed().waitFor()
11 changes: 11 additions & 0 deletions example/database/seeder/production.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import std/asyncdispatch
import std/os
import ./data/user_seeder
import ./data/post_seeder

proc seed() =
let env = getEnv("APP_ENV")
if env != "production":
raise newException(Exception, "This command is only available in the production environment")

seed()
14 changes: 14 additions & 0 deletions example/database/seeder/staging.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import std/asyncdispatch
import std/os
import ./data/user_seeder
import ./data/post_seeder

proc seed() =
let env = getEnv("APP_ENV")
if env != "staging":
raise newException(Exception, "This command is only available in the staging environment")

userSeeder().waitFor()
postSeeder().waitFor()

seed()
5 changes: 5 additions & 0 deletions example/database/staging.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
nim c -d:reset ./migrations/migrate.nim
nim c database/seeder/staging

./migrations/migrate
APP_ENV=staging ./database/seeder/staging
6 changes: 3 additions & 3 deletions example/rearchtect.nim
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ proc main() {.async.} =

rdb.table("test").insert(%*{"blob":binaryImage, "int": 1, "float": 1.1, "str": "alice"}).await

let res = rdb.table("test").select("id", "int", "float", "str").get().await
let res = rdb.select("id", "int", "float", "str").table("test").get().await
for row in res:
echo row

var row = rdb.table("test").select("id", "int", "float", "str").first().await
var row = rdb.select("id", "int", "float", "str").table("test").first().await
if row.isSome:
echo row.get

rdb.table("test").where("id", "=", 1).update(%*{"str": "bob"}).await

row = rdb.table("test").select("id", "int", "float", "str").find(1).await
row = rdb.select("id", "int", "float", "str").table("test").find(1).await
if row.isSome:
echo row.get

Expand Down
Loading

0 comments on commit 79ab598

Please sign in to comment.