Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
E
elp
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
崔为之
elp
Commits
da1f8aca
Commit
da1f8aca
authored
Nov 11, 2023
by
崔为之
💪🏽
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update project
parent
4f9e6b48
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
41 additions
and
9 deletions
+41
-9
application/config/config.yaml
application/config/config.yaml
+6
-5
application/extensions/init_sqlalchemy.py
application/extensions/init_sqlalchemy.py
+13
-1
application/libs/tasks/task.py
application/libs/tasks/task.py
+4
-3
application/models/user.py
application/models/user.py
+18
-0
No files found.
application/config/config.yaml
View file @
da1f8aca
...
...
@@ -2,13 +2,14 @@ System:
Env
:
public
Database
:
Type
:
my
sql
Driver
:
p
ymysql
Username
:
root
Type
:
postgre
sql
Driver
:
p
sycopg2
Username
:
postgres
Password
:
localhost123
Host
:
localhost
Port
:
3306
Port
:
5432
DB
:
elp
TableName
:
user
Elasticsearch
:
Host
:
172.28.5.39
...
...
@@ -39,6 +40,6 @@ Redis:
DB
:
13
Scheduler
:
Start
:
2023-11-11 1
5:34
:00
Start
:
2023-11-11 1
8:20
:00
End
:
2099-11-11 16:00:00
Timezone
:
Asia/Shanghai
application/extensions/init_sqlalchemy.py
View file @
da1f8aca
...
...
@@ -12,6 +12,7 @@
from
flask
import
Flask
from
flask_sqlalchemy
import
SQLAlchemy
from
sqlalchemy
import
text
from
application.utils
import
DatabaseUri
...
...
@@ -39,4 +40,15 @@ def init_database(app: Flask) -> None:
db
.
app
=
app
db
.
init_app
(
app
)
with
app
.
app_context
():
db
.
create_all
()
db
.
session
.
execute
(
text
(
"""
CREATE TABLE IF NOT EXISTS users (
id SERIAL NOT NULL,
username VARCHAR(80) NOT NULL,
email VARCHAR(120) NOT NULL,
password VARCHAR(255) NOT NULL,
active BOOLEAN,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
PRIMARY KEY (id, created_at)
) PARTITION BY RANGE (created_at);
"""
))
db
.
session
.
commit
()
application/libs/tasks/task.py
View file @
da1f8aca
...
...
@@ -10,6 +10,7 @@
# @Description :
"""
import
datetime
from
application.extensions.init_sqlalchemy
import
db
from
application.models
import
User
...
...
@@ -22,7 +23,7 @@ def task():
"""
with
db
.
app
.
app_context
():
print
(
"create user"
)
user
=
User
(
username
=
"CW
Z"
,
email
=
"Stu@outlook.com"
,
password
=
"qwe!2345"
,
active
=
True
)
db
.
session
.
add
(
user
)
db
.
session
.
commit
()
user
=
User
(
username
=
"CW
zz"
,
email
=
"Stuu@outlook.com"
,
password
=
"qwe!2345"
,
active
=
True
,
created_at
=
datetime
.
datetime
.
utcnow
()
)
user
.
save
()
print
(
"created user admin"
)
application/models/user.py
View file @
da1f8aca
...
...
@@ -10,6 +10,8 @@
# @Description :
"""
from
datetime
import
datetime
from
sqlalchemy
import
text
from
sqlalchemy.ext.hybrid
import
hybrid_property
from
application.extensions.init_bcrypt
import
bcrypt
...
...
@@ -18,12 +20,14 @@ from application.extensions.init_sqlalchemy import db
class
User
(
db
.
Model
):
"""Basic user model"""
__tablename__
=
'users'
id
=
db
.
Column
(
db
.
Integer
,
primary_key
=
True
)
username
=
db
.
Column
(
db
.
String
(
80
),
unique
=
True
,
nullable
=
False
)
email
=
db
.
Column
(
db
.
String
(
80
),
unique
=
True
,
nullable
=
False
)
_password
=
db
.
Column
(
"password"
,
db
.
String
(
255
),
nullable
=
False
)
active
=
db
.
Column
(
db
.
Boolean
,
default
=
True
)
created_at
=
db
.
Column
(
db
.
DateTime
,
nullable
=
False
)
@
hybrid_property
def
password
(
self
):
...
...
@@ -47,3 +51,17 @@ class User(db.Model):
def
__repr__
(
self
)
->
str
:
return
f
'<User
{
self
.
username
}
>'
def
save
(
self
):
partition_date
=
self
.
created_at
.
strftime
(
'%Y_%m'
)
partition_name
=
f
'users_
{
partition_date
}
'
with
db
.
session
.
begin_nested
():
db
.
session
.
execute
(
text
(
f
"""
CREATE TABLE IF NOT EXISTS
{
partition_name
}
PARTITION OF users
FOR VALUES FROM ('
{
self
.
created_at
.
strftime
(
'%Y-%m-01'
)
}
') TO
('
{
self
.
created_at
.
strftime
(
'%Y-%m-01'
)
}
'::date + interval '1 month');
"""
))
db
.
session
.
add
(
self
)
db
.
session
.
commit
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment