Skip to content

Commit

Permalink
feat: add quarter model & ddl
Browse files Browse the repository at this point in the history
  • Loading branch information
jackluson committed Aug 29, 2022
1 parent e1eb7d2 commit d685a60
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/crud/__init__.py
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
33 changes: 33 additions & 0 deletions src/crud/ddl.py
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()

53 changes: 53 additions & 0 deletions src/models/quarter.py
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()

0 comments on commit d685a60

Please sign in to comment.