-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
16e5c75
commit 79ab598
Showing
55 changed files
with
1,388 additions
and
79 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,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" |
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,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() |
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,5 @@ | ||
nim c -d:reset ./migrations/migrate.nim | ||
nim c ./seeder/develop | ||
|
||
./migrations/migrate | ||
./seeder/develop |
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,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) | ||
]), | ||
]) |
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,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(), | ||
]) | ||
]) |
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,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() |
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,5 @@ | ||
nim c ./migrations/migrate.nim | ||
nim c database/seeder/production | ||
|
||
./migrations/migrate | ||
APP_ENV=production ./database/seeder/production |
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,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 |
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,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 |
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,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 |
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,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() |
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,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() |
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,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() |
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,5 @@ | ||
nim c -d:reset ./migrations/migrate.nim | ||
nim c database/seeder/staging | ||
|
||
./migrations/migrate | ||
APP_ENV=staging ./database/seeder/staging |
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
Oops, something went wrong.