-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
99 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
''' | ||
Desc: | ||
File: /__init__.py | ||
Project: crud | ||
File Created: Sunday, 28th August 2022 3:00:19 pm | ||
Author: [email protected] | ||
----- | ||
Copyright (c) 2022 Camel Lu | ||
''' | ||
import sys | ||
sys.path.append('./src') | ||
|
||
from models.manager import ManagerAssoc |
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,33 @@ | ||
''' | ||
Desc: | ||
File: /ddl.py | ||
Project: crud | ||
File Created: Monday, 29th August 2022 10:23:08 pm | ||
Author: [email protected] | ||
----- | ||
Copyright (c) 2022 Camel Lu | ||
''' | ||
import sys | ||
sys.path.append('./src') | ||
from sqlalchemy import text | ||
from models.var import prefix, ORM_Base, engine | ||
|
||
|
||
def alter_foreign_quarter(): | ||
with engine.connect() as conn: | ||
table = 'fund_morning_quarter' | ||
source_table = 'quarter' | ||
table_data = { | ||
"table": table, | ||
"target_key": 'quarter_index', | ||
'source_table': source_table, | ||
'foreign_name': table + '_fk_' + source_table, | ||
'source_key': 'quarter_index' | ||
} | ||
sql = "ALTER TABLE {table} ADD CONSTRAINT {foreign_name} FOREIGN KEY ({target_key}) REFERENCES {source_table}({source_key})".format(**table_data) | ||
conn.execute(text(sql)) | ||
conn.commit() | ||
|
||
if __name__ == '__main__': | ||
alter_foreign_quarter() | ||
|
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,53 @@ | ||
''' | ||
Desc: 季度时间 | ||
File: /quarter.py | ||
Project: models | ||
File Created: Sunday, 28th August 2022 9:21:25 pm | ||
Author: [email protected] | ||
----- | ||
Copyright (c) 2022 Camel Lu | ||
''' | ||
import sys | ||
sys.path.append('./src') | ||
from datetime import datetime | ||
from sqlalchemy import UniqueConstraint, CheckConstraint, MetaData, Table, Column, text, Integer, String, Date, DateTime, Enum, func | ||
from sqlalchemy.orm import validates | ||
from models.var import ORM_Base, engine | ||
|
||
class Quarter(ORM_Base): | ||
__tablename__ = 'quarter' | ||
id = Column(Integer, primary_key=True) | ||
quarter_index = Column(String(12), nullable=False, unique=True) | ||
start_time = Column(Date(), nullable=False, unique=True) | ||
end_time = Column(Date(), nullable=False, unique=True) | ||
updated_at = Column(DateTime, server_default=text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'), onupdate=func.now()) | ||
created_at = Column(DateTime, server_default=text('CURRENT_TIMESTAMP'), comment='创建时间') | ||
# created_at = Column(DateTime, server_default=func.now(), comment='创建时间') | ||
UniqueConstraint(quarter_index, name='uix_1') | ||
|
||
def __init__(self, *args, **kwargs) -> None: | ||
super().__init__(*args, **kwargs) | ||
|
||
@validates('end_time') | ||
def validate_start_time(self, key, end_time): | ||
# end_time_stamp = time.mktime(end_time) | ||
end_time_stamp = datetime.strptime (end_time, '%Y-%m-%d') | ||
start_time_stamp = datetime.strptime (self.start_time, '%Y-%m-%d') | ||
if end_time_stamp > start_time_stamp: | ||
return end_time | ||
else: | ||
assert 'end_time cannot less than start_time' in end_time | ||
return end_time | ||
|
||
def __repr__(self): | ||
return f"Quarter(id={self.id!r}, name={self.quarter_index!r})" | ||
|
||
def create(): | ||
ORM_Base.metadata.create_all(engine) | ||
|
||
def drop(): | ||
Quarter.__table__.drop(engine) | ||
|
||
if __name__ == '__main__': | ||
# drop() | ||
create() |