Skip to content

Commit

Permalink
193 add comment to schema (#296)
Browse files Browse the repository at this point in the history
* .

* postgres

* mariadb mysql

* .

* .

* .

* .

* fix test only for create table

* Add new table creation and column comment functionality

- Introduced a new `table` procedure to create tables with specified names and default properties.
- Updated the `create` procedure to accept variable arguments for tables.
- Added test cases for creating tables with comments on columns in MariaDB, MySQL, and PostgreSQL.
  • Loading branch information
itsumura-h authored Jan 18, 2025
1 parent b873356 commit 522e052
Show file tree
Hide file tree
Showing 36 changed files with 656 additions and 76 deletions.
2 changes: 1 addition & 1 deletion docker/ubuntu/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ RUN choosenim ${NIM_VERSION}

# nimlangserver
# https://github.com/nim-lang/langserver/releases
RUN curl -o nimlangserver.tar.gz -L https://github.com/nim-lang/langserver/releases/download/v1.6.0/nimlangserver-1.6.0-linux-amd64.tar.gz
RUN curl -o nimlangserver.tar.gz -L https://github.com/nim-lang/langserver/releases/download/v1.8.0/nimlangserver-linux-amd64.tar.gz
RUN tar zxf nimlangserver.tar.gz
RUN rm -f nimlangserver.tar.gz
RUN mv nimlangserver /root/.nimble/bin/
Expand Down
1 change: 1 addition & 0 deletions documents/rdb/schema_builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ template seeder*(rdb:Rdb, tableName, column:string, body:untyped):untyped
|`.unsigned()`|Set INTEGER to UNSIGNED|
|`.unique()`|Adding an unique index|
|`.index()`|Adding an index|
|`.comment("value")`|Adding a comment to a column. Only for Postgres, Mariadb and Mysql|

## Foreign Key Constraints
For example, let's define a `user_id` column on the table that references the `id` column on a `users` table:
Expand Down
6 changes: 3 additions & 3 deletions example/database/.env
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
SQLITE_HOST="/root/project/example/db.sqlite3"
# SQLITE_HOST=":memory:"
PG_URL=postgres://user:pass@postgres:5432/database
MYSQL_URL=mysql://user:pass@mysql:3306/database
MARIA_URL=mariadb://user:pass@mariadb:3306/database
PG_URL="postgresql://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
4 changes: 2 additions & 2 deletions example/database/develop.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
nim c -d:reset ./migrations/migrate.nim
nim c ./seeder/develop
nim c -d:reset --threads:off ./migrations/migrate.nim
nim c --threads:off ./seeder/develop

./migrations/migrate
./seeder/develop
30 changes: 15 additions & 15 deletions example/database/migrations/init_databaase.nim
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import ../../../src/allographer/schema_builder
import ../connection

proc init_databaase*() =
proc initDatabaase*() =
## 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(),
]),
Column.uuid("id").index().comment("User ID"),
Column.string("name").comment("User name"),
Column.string("email").comment("User email address"),
Column.string("password").comment("User password"),
Column.integer("created_at").index().comment("Created at"),
Column.integer("updated_at").index().comment("Updated at"),
], "User table"),
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(),
])
Column.uuid("id").index().comment("Post ID"),
Column.string("title").comment("Post title"),
Column.string("content").comment("Post content"),
Column.strForeign("user_id").reference("id").onTable("user").comment("User ID"),
Column.integer("created_at").index().comment("Created at"),
Column.integer("updated_at").index().comment("Updated at"),
], "Post table"),
])
2 changes: 1 addition & 1 deletion example/database/migrations/migrate.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ./init_databaase
import ./create_all_type_table

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

migrate()
Expand Down
4 changes: 2 additions & 2 deletions example/database/production.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
nim c ./migrations/migrate.nim
nim c database/seeder/production
nim c --threads:off ./migrations/migrate.nim
nim c --threads:off database/seeder/production

./migrations/migrate
APP_ENV=production ./database/seeder/production
20 changes: 10 additions & 10 deletions example/database/schema.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ type UserTable* = object
updated_at*: int


type PostTable* = object
## post
id*: string
title*: string
content*: string
user_id*: string
created_at*: int
updated_at*: int


type TypesTable* = object
## Types
id*: int
Expand Down Expand Up @@ -49,13 +59,3 @@ type TypesTable* = object
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
4 changes: 2 additions & 2 deletions example/database/seeder/data/post_seeder.nim
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ proc postSeeder*() {.async.} =
title: &"post {i}",
content: &"content {i}",
userId: users[i-1].id,
createdAt: now().toTime().toUnix(),
updatedAt: now().toTime().toUnix()
createdAt: now().toTime().toUnix().int,
updatedAt: now().toTime().toUnix().int
)
postList.add(%row)

Expand Down
4 changes: 2 additions & 2 deletions example/database/seeder/data/user_seeder.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ proc userSeeder*() {.async.} =
name: &"user {i}",
email: &"user{i}@example.com",
password: hash(&"password{i}", salt),
createdAt: now().toTime().toUnix(),
updatedAt: now().toTime().toUnix(),
createdAt: now().toTime().toUnix().int,
updatedAt: now().toTime().toUnix().int,
)
userList.add(%row)

Expand Down
4 changes: 2 additions & 2 deletions example/database/staging.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
nim c -d:reset ./migrations/migrate.nim
nim c database/seeder/staging
nim c -d:reset --threads:off./migrations/migrate.nim
nim c --threads:off database/seeder/staging

./migrations/migrate
APP_ENV=staging ./database/seeder/staging
25 changes: 23 additions & 2 deletions src/allographer/connection.nim
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,26 @@ elif NimMajor == 1:
when isExistsSurrealdb:
import ./v1/query_builder/models/surreal/surreal_types; export SurrealDB, SurrealConnections
import ./v1/query_builder/models/surreal/surreal_open; export surreal_open
else:
discard


# ==================================================

# when isExistsSqlite:
# import ./v2/query_builder/models/sqlite/sqlite_types; export SQLite3, SqliteConnections
# import ./v2/query_builder/models/sqlite/sqlite_open; export sqlite_open

# when isExistsPostgres:
# import ./v2/query_builder/models/postgres/postgres_types; export PostgreSQL, PostgresConnections
# import ./v2/query_builder/models/postgres/postgres_open; export postgres_open

# when isExistsMariadb:
# import ./v2/query_builder/models/mariadb/mariadb_types; export MariaDB, MariadbConnections
# import ./v2/query_builder/models/mariadb/mariadb_open; export mariadb_open

# when isExistsMysql:
# import ./v2/query_builder/models/mysql/mysql_types; export MySql, MysqlConnections
# import ./v2/query_builder/models/mysql/mysql_open; export mysql_open

# when isExistsSurrealdb:
# import ./v2/query_builder/models/surreal/surreal_types; export SurrealDB, SurrealConnections
# import ./v2/query_builder/models/surreal/surreal_open; export surreal_open
36 changes: 36 additions & 0 deletions src/allographer/query_builder.nim
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,39 @@ elif NimMajor == 1:
import ./v1/query_builder/models/surreal/surreal_types; export surreal_types
import ./v1/query_builder/models/surreal/surreal_query; export surreal_query
import ./v1/query_builder/models/surreal/surreal_exec; export surreal_exec


# ==================================================

# import ./v2/query_builder/enums; export enums
# import ./v2/query_builder/error; export error
# import ./v2/query_builder/models/orm; export orm

# when isExistsSqlite:
# import ./v2/query_builder/models/sqlite/sqlite_types; export sqlite_types
# import ./v2/query_builder/models/sqlite/sqlite_query; export sqlite_query
# import ./v2/query_builder/models/sqlite/sqlite_exec; export sqlite_exec
# import ./v2/query_builder/models/sqlite/sqlite_transaction; export sqlite_transaction

# when isExistsPostgres:
# import ./v2/query_builder/models/postgres/postgres_types; export postgres_types
# import ./v2/query_builder/models/postgres/postgres_query; export postgres_query
# import ./v2/query_builder/models/postgres/postgres_exec; export postgres_exec
# import ./v2/query_builder/models/postgres/poatgres_transaction; export poatgres_transaction

# when isExistsMariadb:
# import ./v2/query_builder/models/mariadb/mariadb_types; export mariadb_types
# import ./v2/query_builder/models/mariadb/mariadb_query; export mariadb_query
# import ./v2/query_builder/models/mariadb/mariadb_exec; export mariadb_exec
# import ./v2/query_builder/models/mariadb/mariadb_transaction; export mariadb_transaction

# when isExistsMysql:
# import ./v2/query_builder/models/mysql/mysql_types; export mysql_types
# import ./v2/query_builder/models/mysql/mysql_query; export mysql_query
# import ./v2/query_builder/models/mysql/mysql_exec; export mysql_exec
# import ./v2/query_builder/models/mysql/mysql_transaction; export mysql_transaction

# when isExistsSurrealdb:
# import ./v2/query_builder/models/surreal/surreal_types; export surreal_types
# import ./v2/query_builder/models/surreal/surreal_query; export surreal_query
# import ./v2/query_builder/models/surreal/surreal_exec; export surreal_exec
62 changes: 52 additions & 10 deletions src/allographer/schema_builder.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,35 @@ when NimMajor == 2:

when isExistsSqlite:
import ./v2/query_builder/models/sqlite/sqlite_types; export SQLite3, SqliteConnections
import ./v2/schema_builder/usecases/sqlite/create as sqlite_crate; export sqlite_crate
import ./v2/schema_builder/usecases/sqlite/create as sqlite_create; export sqlite_create
import ./v2/schema_builder/usecases/sqlite/alter as sqlite_alter; export sqlite_alter
import ./v2/schema_builder/usecases/sqlite/drop as sqlite_drop; export sqlite_drop
import ./v2/schema_builder/usecases/sqlite/create_schema as sqlite_create_schema; export sqlite_create_schema

when isExistsPostgres:
import ./v2/query_builder/models/postgres/postgres_types; export PostgreSQL, PostgresConnections
import ./v2/schema_builder/usecases/postgres/create as postgres_crate; export postgres_crate
import ./v2/schema_builder/usecases/postgres/create as postgres_create; export postgres_create
import ./v2/schema_builder/usecases/postgres/alter as postgres_alter; export postgres_alter
import ./v2/schema_builder/usecases/postgres/drop as postgres_drop; export postgres_drop
import ./v2/schema_builder/usecases/postgres/create_schema as postgres_create_schema; export postgres_create_schema

when isExistsMariadb:
import ./v2/query_builder/models/mariadb/mariadb_types; export MariaDB, MariadbConnections
import ./v2/schema_builder/usecases/mariadb/create as mariadb_crate; export mariadb_crate
import ./v2/schema_builder/usecases/mariadb/create as mariadb_create; export mariadb_create
import ./v2/schema_builder/usecases/mariadb/alter as mariadb_alter; export mariadb_alter
import ./v2/schema_builder/usecases/mariadb/drop as mariadb_drop; export mariadb_drop
import ./v2/schema_builder/usecases/mariadb/create_schema as mariadb_create_schema; export mariadb_create_schema

when isExistsMysql:
import ./v2/query_builder/models/mysql/mysql_types; export MySql, MysqlConnections
import ./v2/schema_builder/usecases/mysql/create as mysql_crate; export mysql_crate
import ./v2/schema_builder/usecases/mysql/create as mysql_create; export mysql_create
import ./v2/schema_builder/usecases/mysql/alter as mysql_alter; export mysql_alter
import ./v2/schema_builder/usecases/mysql/drop as mysql_drop; export mysql_drop
import ./v2/schema_builder/usecases/mysql/create_schema as mysql_create_schema; export mysql_create_schema

when isExistsSurrealdb:
import ./v2/query_builder/models/surreal/surreal_types; export SurrealDB, SurrealConnections
import ./v2/schema_builder/usecases/surreal/create as surreal_crate; export surreal_crate
import ./v2/schema_builder/usecases/surreal/create as surreal_create; export surreal_create
import ./v2/schema_builder/usecases/surreal/alter as surreal_alter; export surreal_alter
import ./v2/schema_builder/usecases/surreal/drop as surreal_drop; export surreal_drop
import ./v2/schema_builder/usecases/surreal/create_schema as surreal_create_schema; export surreal_create_schema
Expand All @@ -46,30 +46,72 @@ elif NimMajor == 1:

when isExistsSqlite:
import ./v1/query_builder/models/sqlite/sqlite_types; export SQLite3, SqliteConnections
import ./v1/schema_builder/usecases/sqlite/create as sqlite_crate; export sqlite_crate
import ./v1/schema_builder/usecases/sqlite/create as sqlite_create; export sqlite_create
import ./v1/schema_builder/usecases/sqlite/alter as sqlite_alter; export sqlite_alter
import ./v1/schema_builder/usecases/sqlite/drop as sqlite_drop; export sqlite_drop

when isExistsPostgres:
import ./v1/query_builder/models/postgres/postgres_types; export PostgreSQL, PostgresConnections
import ./v1/schema_builder/usecases/postgres/create as postgres_crate; export postgres_crate
import ./v1/schema_builder/usecases/postgres/create as postgres_create; export postgres_create
import ./v1/schema_builder/usecases/postgres/alter as postgres_alter; export postgres_alter
import ./v1/schema_builder/usecases/postgres/drop as postgres_drop; export postgres_drop

when isExistsMariadb:
import ./v1/query_builder/models/mariadb/mariadb_types; export MariaDB, MariadbConnections
import ./v1/schema_builder/usecases/mariadb/create as mariadb_crate; export mariadb_crate
import ./v1/schema_builder/usecases/mariadb/create as mariadb_create; export mariadb_create
import ./v1/schema_builder/usecases/mariadb/alter as mariadb_alter; export mariadb_alter
import ./v1/schema_builder/usecases/mariadb/drop as mariadb_drop; export mariadb_drop

when isExistsMysql:
import ./v1/query_builder/models/mysql/mysql_types; export MySql, MysqlConnections
import ./v1/schema_builder/usecases/mysql/create as mysql_crate; export mysql_crate
import ./v1/schema_builder/usecases/mysql/create as mysql_create; export mysql_create
import ./v1/schema_builder/usecases/mysql/alter as mysql_alter; export mysql_alter
import ./v1/schema_builder/usecases/mysql/drop as mysql_drop; export mysql_drop

when isExistsSurrealdb:
import ./v1/query_builder/models/surreal/surreal_types; export SurrealDB, SurrealConnections
import ./v1/schema_builder/usecases/surreal/create as surreal_crate; export surreal_crate
import ./v1/schema_builder/usecases/surreal/create as surreal_create; export surreal_create
import ./v1/schema_builder/usecases/surreal/alter as surreal_alter; export surreal_alter
import ./v1/schema_builder/usecases/surreal/drop as surreal_drop; export surreal_drop


# ==================================================

# import ./v2/schema_builder/enums; export enums
# import ./v2/schema_builder/models/table; export table
# import ./v2/schema_builder/models/column; export column

# when isExistsSqlite:
# import ./v2/query_builder/models/sqlite/sqlite_types; export SQLite3, SqliteConnections
# import ./v2/schema_builder/usecases/sqlite/create as sqlite_create; export sqlite_create
# import ./v2/schema_builder/usecases/sqlite/alter as sqlite_alter; export sqlite_alter
# import ./v2/schema_builder/usecases/sqlite/drop as sqlite_drop; export sqlite_drop
# import ./v2/schema_builder/usecases/sqlite/create_schema as sqlite_create_schema; export sqlite_create_schema

# when isExistsPostgres:
# import ./v2/query_builder/models/postgres/postgres_types; export PostgreSQL, PostgresConnections
# import ./v2/schema_builder/usecases/postgres/create as postgres_create; export postgres_create
# import ./v2/schema_builder/usecases/postgres/alter as postgres_alter; export postgres_alter
# import ./v2/schema_builder/usecases/postgres/drop as postgres_drop; export postgres_drop
# import ./v2/schema_builder/usecases/postgres/create_schema as postgres_create_schema; export postgres_create_schema

# when isExistsMariadb:
# import ./v2/query_builder/models/mariadb/mariadb_types; export MariaDB, MariadbConnections
# import ./v2/schema_builder/usecases/mariadb/create as mariadb_create; export mariadb_create
# import ./v2/schema_builder/usecases/mariadb/alter as mariadb_alter; export mariadb_alter
# import ./v2/schema_builder/usecases/mariadb/drop as mariadb_drop; export mariadb_drop
# import ./v2/schema_builder/usecases/mariadb/create_schema as mariadb_create_schema; export mariadb_create_schema

# when isExistsMysql:
# import ./v2/query_builder/models/mysql/mysql_types; export MySql, MysqlConnections
# import ./v2/schema_builder/usecases/mysql/create as mysql_create; export mysql_create
# import ./v2/schema_builder/usecases/mysql/alter as mysql_alter; export mysql_alter
# import ./v2/schema_builder/usecases/mysql/drop as mysql_drop; export mysql_drop
# import ./v2/schema_builder/usecases/mysql/create_schema as mysql_create_schema; export mysql_create_schema

# when isExistsSurrealdb:
# import ./v2/query_builder/models/surreal/surreal_types; export SurrealDB, SurrealConnections
# import ./v2/schema_builder/usecases/surreal/create as surreal_create; export surreal_create
# import ./v2/schema_builder/usecases/surreal/alter as surreal_alter; export surreal_alter
# import ./v2/schema_builder/usecases/surreal/drop as surreal_drop; export surreal_drop
# import ./v2/schema_builder/usecases/surreal/create_schema as surreal_create_schema; export surreal_create_schema
9 changes: 8 additions & 1 deletion src/allographer/v1/schema_builder/models/column.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Column* = ref object
defaultJson*: JsonNode
defaultDatetime*: DefaultDateTime
foreignOnDelete*: ForeignOnDelete
commentContent*:string
info*: JsonNode
checksum*:string
# alter table
Expand Down Expand Up @@ -49,6 +50,7 @@ proc toSchema*(self:Column):JsonNode =
"defaultString": self.defaultString,
"defaultJson": self.defaultJson,
"foreignOnDelete": self.foreignOnDelete,
"comment": self.commentContent,
"info": self.info,
"previousName":self.previousName,
"migrationType":self.migrationType,
Expand Down Expand Up @@ -283,7 +285,7 @@ proc reference*(self:Column, column:string):Column =
return self


proc on*(self:Column, table:string):Column {.deprecated: "Use `onTable` instead after Nim v2".} =
proc on*(self:Column, table:string):Column {.deprecated: "Use `onTable` instead".} =
self.info["table"] = %*table
return self

Expand Down Expand Up @@ -378,6 +380,11 @@ proc unsigned*(c: Column): Column =
return c


proc comment*(c: Column, value:string): Column =
c.commentContent = value
return c


# =============================================================================
# alter table
# =============================================================================
Expand Down
Loading

0 comments on commit 522e052

Please sign in to comment.