Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
DataSmith
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
apps
DataSmith
Commits
e3e96441
Commit
e3e96441
authored
9 months ago
by
Sigmund, Dominik
Browse files
Options
Downloads
Patches
Plain Diff
Added dbml addtion
parent
70699ed5
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#82670
failed
9 months ago
Stage: build
Stage: scan
Stage: release
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
commands/add.go
+54
-1
54 additions, 1 deletion
commands/add.go
with
54 additions
and
1 deletion
commands/add.go
+
54
−
1
View file @
e3e96441
...
...
@@ -67,7 +67,6 @@ func Add(tableName string, model string) {
/*
- Generate Test Data for the new table if wanted
- Add Table to import-sql.sh
- Add Description to the DBML file
- Add Description and mermaid and DBML to the README.md file
- Add Test to gitlab-ci.yml
- Add to CHANGELOG.md
...
...
@@ -90,6 +89,12 @@ func Add(tableName string, model string) {
// Create the table file under sql directory
createTableFile
(
projectDir
,
tableName
,
config
.
DbType
,
tableModel
)
// Append table description to database.dbml
if
err
:=
appendToDBMLFile
(
projectDir
,
tableName
,
tableModel
);
err
!=
nil
{
fmt
.
Printf
(
"Error appending to database.dbml: %v
\n
"
,
err
)
}
else
{
fmt
.
Printf
(
"Appended table '%s' to database.dbml
\n
"
,
tableName
)
}
fmt
.
Printf
(
"Added new table '%s' to the project
\n
"
,
slug
)
}
...
...
@@ -241,6 +246,54 @@ func generatePostgreSQLSQL(file *os.File, tableName string, tableModel TableMode
return
err
}
func
generateDBML
(
tableName
string
,
tableModel
TableModel
)
string
{
var
dbml
strings
.
Builder
dbml
.
WriteString
(
fmt
.
Sprintf
(
"Table %s {
\n
"
,
tableName
))
for
_
,
field
:=
range
tableModel
.
Fields
{
dbml
.
WriteString
(
fmt
.
Sprintf
(
" %s %s"
,
field
.
Name
,
field
.
Type
))
if
field
.
PrimaryKey
{
dbml
.
WriteString
(
" [pk]"
)
}
if
field
.
Unique
{
dbml
.
WriteString
(
" [unique]"
)
}
if
field
.
NotNull
{
dbml
.
WriteString
(
" [not null]"
)
}
if
field
.
AutoIncrement
{
dbml
.
WriteString
(
" [autoincrement]"
)
}
if
field
.
DefaultValue
!=
nil
{
dbml
.
WriteString
(
fmt
.
Sprintf
(
" [default: '%v']"
,
field
.
DefaultValue
))
}
dbml
.
WriteString
(
"
\n
"
)
}
dbml
.
WriteString
(
"}
\n
"
)
return
dbml
.
String
()
}
func
appendToDBMLFile
(
projectDir
,
tableName
string
,
tableModel
TableModel
)
error
{
dbmlFilePath
:=
filepath
.
Join
(
projectDir
,
"database.dbml"
)
file
,
err
:=
os
.
OpenFile
(
dbmlFilePath
,
os
.
O_APPEND
|
os
.
O_WRONLY
,
os
.
ModeAppend
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"error opening database.dbml file: %v"
,
err
)
}
defer
file
.
Close
()
dbmlContent
:=
generateDBML
(
tableName
,
tableModel
)
if
_
,
err
:=
file
.
WriteString
(
dbmlContent
);
err
!=
nil
{
return
fmt
.
Errorf
(
"error writing to database.dbml file: %v"
,
err
)
}
return
nil
}
func
validateModel
(
model
TableModel
)
error
{
fieldNames
:=
make
(
map
[
string
]
bool
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment