mirror of
https://github.com/logos-messaging/nim-chat-sdk.git
synced 2026-01-08 17:13:07 +00:00
Merge branch 'main' into feat/rate-limit-no-storage
This commit is contained in:
commit
f8e2ace1df
11
.gitignore
vendored
11
.gitignore
vendored
@ -13,6 +13,15 @@ nimcache/
|
|||||||
*.a
|
*.a
|
||||||
|
|
||||||
# OS
|
# OS
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
|
||||||
|
# Temporary files
|
||||||
|
*.db
|
||||||
|
|
||||||
|
# Compiled files
|
||||||
|
chat_sdk/*
|
||||||
|
!*.nim
|
||||||
|
apps/*
|
||||||
|
!*.nim
|
||||||
nimble.develop
|
nimble.develop
|
||||||
nimble.paths
|
nimble.paths
|
||||||
|
|||||||
1
apps/nim.cfg
Normal file
1
apps/nim.cfg
Normal file
@ -0,0 +1 @@
|
|||||||
|
path = ".."
|
||||||
12
apps/run_migration.nim
Normal file
12
apps/run_migration.nim
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import db_connector/db_sqlite
|
||||||
|
import chat_sdk/migration
|
||||||
|
|
||||||
|
proc main() =
|
||||||
|
let db = open("test.db", "", "", "")
|
||||||
|
defer:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
runMigrations(db)
|
||||||
|
|
||||||
|
when isMainModule:
|
||||||
|
main()
|
||||||
@ -1,22 +1,19 @@
|
|||||||
# Package
|
# Package
|
||||||
|
|
||||||
version = "0.0.1"
|
version = "0.0.1"
|
||||||
author = "Waku Chat Team"
|
author = "Waku Chat Team"
|
||||||
description = "Chat features over Waku"
|
description = "Chat features over Waku"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
srcDir = "src"
|
srcDir = "src"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### Dependencies
|
### Dependencies
|
||||||
requires "nim >= 2.2.4",
|
requires "nim >= 2.2.4", "chronicles", "chronos", "db_connector", "waku"
|
||||||
"chronicles",
|
|
||||||
"chronos",
|
|
||||||
"db_connector",
|
|
||||||
"waku"
|
|
||||||
|
|
||||||
task buildSharedLib, "Build shared library for C bindings":
|
task buildSharedLib, "Build shared library for C bindings":
|
||||||
exec "nim c --mm:refc --app:lib --out:../library/c-bindings/libchatsdk.so chat_sdk/chat_sdk.nim"
|
exec "nim c --mm:refc --app:lib --out:../library/c-bindings/libchatsdk.so chat_sdk/chat_sdk.nim"
|
||||||
|
|
||||||
task buildStaticLib, "Build static library for C bindings":
|
task buildStaticLib, "Build static library for C bindings":
|
||||||
exec "nim c --mm:refc --app:staticLib --out:../library/c-bindings/libchatsdk.a chat_sdk/chat_sdk.nim"
|
exec "nim c --mm:refc --app:staticLib --out:../library/c-bindings/libchatsdk.a chat_sdk/chat_sdk.nim"
|
||||||
|
|
||||||
|
task migrate, "Run database migrations":
|
||||||
|
exec "nim c -r apps/run_migration.nim"
|
||||||
|
|||||||
32
chat_sdk/migration.nim
Normal file
32
chat_sdk/migration.nim
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import os, sequtils, algorithm
|
||||||
|
import db_connector/db_sqlite
|
||||||
|
import chronicles
|
||||||
|
|
||||||
|
proc ensureMigrationTable(db: DbConn) =
|
||||||
|
db.exec(sql"""
|
||||||
|
CREATE TABLE IF NOT EXISTS schema_migrations (
|
||||||
|
filename TEXT PRIMARY KEY,
|
||||||
|
applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
|
)
|
||||||
|
""")
|
||||||
|
|
||||||
|
proc hasMigrationRun(db: DbConn, filename: string): bool =
|
||||||
|
for row in db.fastRows(sql"SELECT 1 FROM schema_migrations WHERE filename = ?", filename):
|
||||||
|
return true
|
||||||
|
return false
|
||||||
|
|
||||||
|
proc markMigrationRun(db: DbConn, filename: string) =
|
||||||
|
db.exec(sql"INSERT INTO schema_migrations (filename) VALUES (?)", filename)
|
||||||
|
|
||||||
|
proc runMigrations*(db: DbConn, dir = "migrations") =
|
||||||
|
info "Migration process starting up"
|
||||||
|
ensureMigrationTable(db)
|
||||||
|
let files = walkFiles(dir / "*.sql").toSeq().sorted()
|
||||||
|
for file in files:
|
||||||
|
if hasMigrationRun(db, file):
|
||||||
|
info "Migration already applied", file
|
||||||
|
else:
|
||||||
|
info "Applying migration", file
|
||||||
|
let sql = readFile(file)
|
||||||
|
db.exec(sql(sql))
|
||||||
|
markMigrationRun(db, file)
|
||||||
9
migrations/README.md
Normal file
9
migrations/README.md
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
## Database Migrations
|
||||||
|
|
||||||
|
This directory contains SQL migration files for the Nim Chat SDK.
|
||||||
|
|
||||||
|
Each migration file should be named in the format `NNN_description.sql`, where `NNN` is a zero-padded number indicating the order of the migration. For example, `001_create_user_table.sql` is the first migration.
|
||||||
|
|
||||||
|
## Running Migrations
|
||||||
|
|
||||||
|
If you want to install a test sqlite database with tables setup, you can run `nimble migrate` from the command line. This will execute all migration files in order, ensuring that your database schema is up to date.
|
||||||
Loading…
x
Reference in New Issue
Block a user