modify scripts
This commit is contained in:
147
src/sqlalchemy/alembic.ini
Normal file
147
src/sqlalchemy/alembic.ini
Normal file
@ -0,0 +1,147 @@
|
||||
# A generic, single database configuration.
|
||||
|
||||
[alembic]
|
||||
# path to migration scripts.
|
||||
# this is typically a path given in POSIX (e.g. forward slashes)
|
||||
# format, relative to the token %(here)s which refers to the location of this
|
||||
# ini file
|
||||
script_location = %(here)s
|
||||
|
||||
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
|
||||
# Uncomment the line below if you want the files to be prepended with date and time
|
||||
# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file
|
||||
# for all available tokens
|
||||
# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
|
||||
|
||||
# sys.path path, will be prepended to sys.path if present.
|
||||
# defaults to the current working directory. for multiple paths, the path separator
|
||||
# is defined by "path_separator" below.
|
||||
prepend_sys_path = .
|
||||
|
||||
|
||||
# timezone to use when rendering the date within the migration file
|
||||
# as well as the filename.
|
||||
# If specified, requires the python>=3.9 or backports.zoneinfo library and tzdata library.
|
||||
# Any required deps can installed by adding `alembic[tz]` to the pip requirements
|
||||
# string value is passed to ZoneInfo()
|
||||
# leave blank for localtime
|
||||
# timezone =
|
||||
|
||||
# max length of characters to apply to the "slug" field
|
||||
# truncate_slug_length = 40
|
||||
|
||||
# set to 'true' to run the environment during
|
||||
# the 'revision' command, regardless of autogenerate
|
||||
# revision_environment = false
|
||||
|
||||
# set to 'true' to allow .pyc and .pyo files without
|
||||
# a source .py file to be detected as revisions in the
|
||||
# versions/ directory
|
||||
# sourceless = false
|
||||
|
||||
# version location specification; This defaults
|
||||
# to <script_location>/versions. When using multiple version
|
||||
# directories, initial revisions must be specified with --version-path.
|
||||
# The path separator used here should be the separator specified by "path_separator"
|
||||
# below.
|
||||
# version_locations = %(here)s/bar:%(here)s/bat:%(here)s/alembic/versions
|
||||
|
||||
# path_separator; This indicates what character is used to split lists of file
|
||||
# paths, including version_locations and prepend_sys_path within configparser
|
||||
# files such as alembic.ini.
|
||||
# The default rendered in new alembic.ini files is "os", which uses os.pathsep
|
||||
# to provide os-dependent path splitting.
|
||||
#
|
||||
# Note that in order to support legacy alembic.ini files, this default does NOT
|
||||
# take place if path_separator is not present in alembic.ini. If this
|
||||
# option is omitted entirely, fallback logic is as follows:
|
||||
#
|
||||
# 1. Parsing of the version_locations option falls back to using the legacy
|
||||
# "version_path_separator" key, which if absent then falls back to the legacy
|
||||
# behavior of splitting on spaces and/or commas.
|
||||
# 2. Parsing of the prepend_sys_path option falls back to the legacy
|
||||
# behavior of splitting on spaces, commas, or colons.
|
||||
#
|
||||
# Valid values for path_separator are:
|
||||
#
|
||||
# path_separator = :
|
||||
# path_separator = ;
|
||||
# path_separator = space
|
||||
# path_separator = newline
|
||||
#
|
||||
# Use os.pathsep. Default configuration used for new projects.
|
||||
path_separator = os
|
||||
|
||||
# set to 'true' to search source files recursively
|
||||
# in each "version_locations" directory
|
||||
# new in Alembic version 1.10
|
||||
# recursive_version_locations = false
|
||||
|
||||
# the output encoding used when revision files
|
||||
# are written from script.py.mako
|
||||
# output_encoding = utf-8
|
||||
|
||||
# database URL. This is consumed by the user-maintained env.py script only.
|
||||
# other means of configuring database URLs may be customized within the env.py
|
||||
# file.
|
||||
sqlalchemy.url = driver://user:pass@localhost/dbname
|
||||
|
||||
|
||||
[post_write_hooks]
|
||||
# post_write_hooks defines scripts or Python functions that are run
|
||||
# on newly generated revision scripts. See the documentation for further
|
||||
# detail and examples
|
||||
|
||||
# format using "black" - use the console_scripts runner, against the "black" entrypoint
|
||||
# hooks = black
|
||||
# black.type = console_scripts
|
||||
# black.entrypoint = black
|
||||
# black.options = -l 79 REVISION_SCRIPT_FILENAME
|
||||
|
||||
# lint with attempts to fix using "ruff" - use the module runner, against the "ruff" module
|
||||
# hooks = ruff
|
||||
# ruff.type = module
|
||||
# ruff.module = ruff
|
||||
# ruff.options = check --fix REVISION_SCRIPT_FILENAME
|
||||
|
||||
# Alternatively, use the exec runner to execute a binary found on your PATH
|
||||
# hooks = ruff
|
||||
# ruff.type = exec
|
||||
# ruff.executable = ruff
|
||||
# ruff.options = check --fix REVISION_SCRIPT_FILENAME
|
||||
|
||||
# Logging configuration. This is also consumed by the user-maintained
|
||||
# env.py script only.
|
||||
[loggers]
|
||||
keys = root,sqlalchemy,alembic
|
||||
|
||||
[handlers]
|
||||
keys = console
|
||||
|
||||
[formatters]
|
||||
keys = generic
|
||||
|
||||
[logger_root]
|
||||
level = WARNING
|
||||
handlers = console
|
||||
qualname =
|
||||
|
||||
[logger_sqlalchemy]
|
||||
level = WARNING
|
||||
handlers =
|
||||
qualname = sqlalchemy.engine
|
||||
|
||||
[logger_alembic]
|
||||
level = INFO
|
||||
handlers =
|
||||
qualname = alembic
|
||||
|
||||
[handler_console]
|
||||
class = StreamHandler
|
||||
args = (sys.stderr,)
|
||||
level = NOTSET
|
||||
formatter = generic
|
||||
|
||||
[formatter_generic]
|
||||
format = %(levelname)-5.5s [%(name)s] %(message)s
|
||||
datefmt = %H:%M:%S
|
||||
12
src/sqlalchemy/config.py
Normal file
12
src/sqlalchemy/config.py
Normal file
@ -0,0 +1,12 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
# MySQL 配置
|
||||
db_config = {
|
||||
'host': 'testdb',
|
||||
'user': 'root',
|
||||
'password': 'mysqlpw',
|
||||
'database': 'stockdb'
|
||||
}
|
||||
|
||||
global_db_url = f"mysql+pymysql://{db_config['user']}:{db_config['password']}@{db_config['host']}:3306/{db_config['database']}?charset=utf8mb4"
|
||||
1
src/sqlalchemy/migrations/stockdb/README
Normal file
1
src/sqlalchemy/migrations/stockdb/README
Normal file
@ -0,0 +1 @@
|
||||
Generic single-database configuration.
|
||||
147
src/sqlalchemy/migrations/stockdb/alembic.ini
Normal file
147
src/sqlalchemy/migrations/stockdb/alembic.ini
Normal file
@ -0,0 +1,147 @@
|
||||
# A generic, single database configuration.
|
||||
|
||||
[alembic]
|
||||
# path to migration scripts.
|
||||
# this is typically a path given in POSIX (e.g. forward slashes)
|
||||
# format, relative to the token %(here)s which refers to the location of this
|
||||
# ini file
|
||||
script_location = %(here)s
|
||||
|
||||
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
|
||||
# Uncomment the line below if you want the files to be prepended with date and time
|
||||
# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file
|
||||
# for all available tokens
|
||||
# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
|
||||
|
||||
# sys.path path, will be prepended to sys.path if present.
|
||||
# defaults to the current working directory. for multiple paths, the path separator
|
||||
# is defined by "path_separator" below.
|
||||
prepend_sys_path = .
|
||||
|
||||
|
||||
# timezone to use when rendering the date within the migration file
|
||||
# as well as the filename.
|
||||
# If specified, requires the python>=3.9 or backports.zoneinfo library and tzdata library.
|
||||
# Any required deps can installed by adding `alembic[tz]` to the pip requirements
|
||||
# string value is passed to ZoneInfo()
|
||||
# leave blank for localtime
|
||||
# timezone =
|
||||
|
||||
# max length of characters to apply to the "slug" field
|
||||
# truncate_slug_length = 40
|
||||
|
||||
# set to 'true' to run the environment during
|
||||
# the 'revision' command, regardless of autogenerate
|
||||
# revision_environment = false
|
||||
|
||||
# set to 'true' to allow .pyc and .pyo files without
|
||||
# a source .py file to be detected as revisions in the
|
||||
# versions/ directory
|
||||
# sourceless = false
|
||||
|
||||
# version location specification; This defaults
|
||||
# to <script_location>/versions. When using multiple version
|
||||
# directories, initial revisions must be specified with --version-path.
|
||||
# The path separator used here should be the separator specified by "path_separator"
|
||||
# below.
|
||||
# version_locations = %(here)s/bar:%(here)s/bat:%(here)s/alembic/versions
|
||||
|
||||
# path_separator; This indicates what character is used to split lists of file
|
||||
# paths, including version_locations and prepend_sys_path within configparser
|
||||
# files such as alembic.ini.
|
||||
# The default rendered in new alembic.ini files is "os", which uses os.pathsep
|
||||
# to provide os-dependent path splitting.
|
||||
#
|
||||
# Note that in order to support legacy alembic.ini files, this default does NOT
|
||||
# take place if path_separator is not present in alembic.ini. If this
|
||||
# option is omitted entirely, fallback logic is as follows:
|
||||
#
|
||||
# 1. Parsing of the version_locations option falls back to using the legacy
|
||||
# "version_path_separator" key, which if absent then falls back to the legacy
|
||||
# behavior of splitting on spaces and/or commas.
|
||||
# 2. Parsing of the prepend_sys_path option falls back to the legacy
|
||||
# behavior of splitting on spaces, commas, or colons.
|
||||
#
|
||||
# Valid values for path_separator are:
|
||||
#
|
||||
# path_separator = :
|
||||
# path_separator = ;
|
||||
# path_separator = space
|
||||
# path_separator = newline
|
||||
#
|
||||
# Use os.pathsep. Default configuration used for new projects.
|
||||
path_separator = os
|
||||
|
||||
# set to 'true' to search source files recursively
|
||||
# in each "version_locations" directory
|
||||
# new in Alembic version 1.10
|
||||
# recursive_version_locations = false
|
||||
|
||||
# the output encoding used when revision files
|
||||
# are written from script.py.mako
|
||||
# output_encoding = utf-8
|
||||
|
||||
# database URL. This is consumed by the user-maintained env.py script only.
|
||||
# other means of configuring database URLs may be customized within the env.py
|
||||
# file.
|
||||
sqlalchemy.url = driver://user:pass@localhost/dbname
|
||||
|
||||
|
||||
[post_write_hooks]
|
||||
# post_write_hooks defines scripts or Python functions that are run
|
||||
# on newly generated revision scripts. See the documentation for further
|
||||
# detail and examples
|
||||
|
||||
# format using "black" - use the console_scripts runner, against the "black" entrypoint
|
||||
# hooks = black
|
||||
# black.type = console_scripts
|
||||
# black.entrypoint = black
|
||||
# black.options = -l 79 REVISION_SCRIPT_FILENAME
|
||||
|
||||
# lint with attempts to fix using "ruff" - use the module runner, against the "ruff" module
|
||||
# hooks = ruff
|
||||
# ruff.type = module
|
||||
# ruff.module = ruff
|
||||
# ruff.options = check --fix REVISION_SCRIPT_FILENAME
|
||||
|
||||
# Alternatively, use the exec runner to execute a binary found on your PATH
|
||||
# hooks = ruff
|
||||
# ruff.type = exec
|
||||
# ruff.executable = ruff
|
||||
# ruff.options = check --fix REVISION_SCRIPT_FILENAME
|
||||
|
||||
# Logging configuration. This is also consumed by the user-maintained
|
||||
# env.py script only.
|
||||
[loggers]
|
||||
keys = root,sqlalchemy,alembic
|
||||
|
||||
[handlers]
|
||||
keys = console
|
||||
|
||||
[formatters]
|
||||
keys = generic
|
||||
|
||||
[logger_root]
|
||||
level = WARNING
|
||||
handlers = console
|
||||
qualname =
|
||||
|
||||
[logger_sqlalchemy]
|
||||
level = WARNING
|
||||
handlers =
|
||||
qualname = sqlalchemy.engine
|
||||
|
||||
[logger_alembic]
|
||||
level = INFO
|
||||
handlers =
|
||||
qualname = alembic
|
||||
|
||||
[handler_console]
|
||||
class = StreamHandler
|
||||
args = (sys.stderr,)
|
||||
level = NOTSET
|
||||
formatter = generic
|
||||
|
||||
[formatter_generic]
|
||||
format = %(levelname)-5.5s [%(name)s] %(message)s
|
||||
datefmt = %H:%M:%S
|
||||
99
src/sqlalchemy/migrations/stockdb/env.py
Normal file
99
src/sqlalchemy/migrations/stockdb/env.py
Normal file
@ -0,0 +1,99 @@
|
||||
from logging.config import fileConfig
|
||||
|
||||
from sqlalchemy import engine_from_config
|
||||
from sqlalchemy import pool
|
||||
|
||||
from alembic import context
|
||||
|
||||
# this is the Alembic Config object, which provides
|
||||
# access to the values within the .ini file in use.
|
||||
config = context.config
|
||||
|
||||
# Interpret the config file for Python logging.
|
||||
# This line sets up loggers basically.
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
# add your model's MetaData object here
|
||||
# for 'autogenerate' support
|
||||
# from myapp import mymodel
|
||||
# target_metadata = mymodel.Base.metadata
|
||||
target_metadata = None
|
||||
|
||||
# other values from the config, defined by the needs of env.py,
|
||||
# can be acquired:
|
||||
# my_important_option = config.get_main_option("my_important_option")
|
||||
# ... etc.
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
"""Run migrations in 'offline' mode.
|
||||
|
||||
This configures the context with just a URL
|
||||
and not an Engine, though an Engine is acceptable
|
||||
here as well. By skipping the Engine creation
|
||||
we don't even need a DBAPI to be available.
|
||||
|
||||
Calls to context.execute() here emit the given string to the
|
||||
script output.
|
||||
|
||||
"""
|
||||
url = config.get_main_option("sqlalchemy.url")
|
||||
context.configure(
|
||||
url=url,
|
||||
target_metadata=target_metadata,
|
||||
literal_binds=True,
|
||||
dialect_opts={"paramstyle": "named"},
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
''' 修改点
|
||||
from models.modelclass_b import Base
|
||||
target_metadata = Base.metadata
|
||||
|
||||
def run_migrations_online():
|
||||
url = "sqlite:///../databases/db_b.db"
|
||||
connectable = create_engine(url)
|
||||
# 保持其他代码不变
|
||||
'''
|
||||
|
||||
import os
|
||||
from alembic import context
|
||||
from sqlalchemy import create_engine
|
||||
from logging.config import fileConfig
|
||||
from models.stockdb import Base
|
||||
target_metadata = Base.metadata
|
||||
from config import db_config
|
||||
|
||||
def run_migrations_online() -> None:
|
||||
"""Run migrations in 'online' mode.
|
||||
|
||||
In this scenario we need to create an Engine
|
||||
and associate a connection with the context.
|
||||
|
||||
connectable = engine_from_config(
|
||||
config.get_section(config.config_ini_section, {}),
|
||||
prefix="sqlalchemy.",
|
||||
poolclass=pool.NullPool,
|
||||
)
|
||||
"""
|
||||
|
||||
url = f"mysql+pymysql://{db_config['user']}:{db_config['password']}@{db_config['host']}:3306/{db_config['database']}?charset=utf8mb4"
|
||||
connectable = create_engine(url)
|
||||
|
||||
with connectable.connect() as connection:
|
||||
context.configure(
|
||||
connection=connection, target_metadata=target_metadata
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
28
src/sqlalchemy/migrations/stockdb/script.py.mako
Normal file
28
src/sqlalchemy/migrations/stockdb/script.py.mako
Normal file
@ -0,0 +1,28 @@
|
||||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = ${repr(up_revision)}
|
||||
down_revision: Union[str, Sequence[str], None] = ${repr(down_revision)}
|
||||
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
|
||||
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Upgrade schema."""
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
${downgrades if downgrades else "pass"}
|
||||
@ -0,0 +1,79 @@
|
||||
"""Auto update from stockdb
|
||||
|
||||
Revision ID: 0a39f1baab38
|
||||
Revises: df56ac7669f2
|
||||
Create Date: 2025-07-31 16:05:20.579725
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '0a39f1baab38'
|
||||
down_revision: Union[str, Sequence[str], None] = 'df56ac7669f2'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Upgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table('daily_snap',
|
||||
sa.Column('code', sa.String(length=20), nullable=False, comment='股票代码'),
|
||||
sa.Column('curr_date', sa.String(length=20), nullable=False, comment='交易日期'),
|
||||
sa.Column('name', sa.String(length=50), nullable=True, comment='股票名称'),
|
||||
sa.Column('market_id', sa.String(length=50), nullable=True, comment='市场名称'),
|
||||
sa.Column('code_prefix', sa.Integer(), nullable=True, comment='代码前缀'),
|
||||
sa.Column('industry', sa.String(length=50), nullable=True, comment='所处行业'),
|
||||
sa.Column('listing_date', sa.Date(), nullable=True, comment='上市时间'),
|
||||
sa.Column('latest_price', sa.Float(), nullable=True, comment='最新价'),
|
||||
sa.Column('price_change_percent', sa.Float(), nullable=True, comment='涨跌幅(%)'),
|
||||
sa.Column('price_change', sa.Float(), nullable=True, comment='涨跌额'),
|
||||
sa.Column('volume', sa.Float(), nullable=True, comment='成交量'),
|
||||
sa.Column('turnover', sa.Float(), nullable=True, comment='成交额'),
|
||||
sa.Column('amplitude', sa.Float(), nullable=True, comment='振幅(%)'),
|
||||
sa.Column('turnover_rate', sa.Float(), nullable=True, comment='换手率(%)'),
|
||||
sa.Column('pe_dynamic', sa.Float(), nullable=True, comment='市盈率动'),
|
||||
sa.Column('volume_ratio', sa.Float(), nullable=True, comment='量比'),
|
||||
sa.Column('change_5min', sa.Float(), nullable=True, comment='5分钟涨跌(%)'),
|
||||
sa.Column('highest', sa.Float(), nullable=True, comment='最高'),
|
||||
sa.Column('lowest', sa.Float(), nullable=True, comment='最低'),
|
||||
sa.Column('opening', sa.Float(), nullable=True, comment='今开'),
|
||||
sa.Column('previous_close', sa.Float(), nullable=True, comment='昨收'),
|
||||
sa.Column('price_speed', sa.Float(), nullable=True, comment='涨速(%)'),
|
||||
sa.Column('total_market_cap', sa.Float(), nullable=True, comment='总市值'),
|
||||
sa.Column('circulating_market_cap', sa.Float(), nullable=True, comment='流通市值'),
|
||||
sa.Column('pb_ratio', sa.Float(), nullable=True, comment='市净率'),
|
||||
sa.Column('change_60d', sa.Float(), nullable=True, comment='60日涨跌幅(%)'),
|
||||
sa.Column('change_ytd', sa.Float(), nullable=True, comment='年初至今涨跌幅(%)'),
|
||||
sa.Column('weighted_roe', sa.Float(), nullable=True, comment='加权净资产收益率(%)'),
|
||||
sa.Column('total_shares', sa.Float(), nullable=True, comment='总股本'),
|
||||
sa.Column('circulating_shares', sa.Float(), nullable=True, comment='已流通股份'),
|
||||
sa.Column('operating_revenue', sa.Float(), nullable=True, comment='营业收入'),
|
||||
sa.Column('revenue_growth', sa.Float(), nullable=True, comment='营业收入同比增长(%)'),
|
||||
sa.Column('net_profit', sa.Float(), nullable=True, comment='归属净利润'),
|
||||
sa.Column('net_profit_growth', sa.Float(), nullable=True, comment='归属净利润同比增长(%)'),
|
||||
sa.Column('undistributed_profit_per_share', sa.Float(), nullable=True, comment='每股未分配利润'),
|
||||
sa.Column('gross_margin', sa.Float(), nullable=True, comment='毛利率(%)'),
|
||||
sa.Column('asset_liability_ratio', sa.Float(), nullable=True, comment='资产负债率(%)'),
|
||||
sa.Column('reserve_per_share', sa.Float(), nullable=True, comment='每股公积金'),
|
||||
sa.Column('earnings_per_share', sa.Float(), nullable=True, comment='每股收益'),
|
||||
sa.Column('net_asset_per_share', sa.Float(), nullable=True, comment='每股净资产'),
|
||||
sa.Column('pe_static', sa.Float(), nullable=True, comment='市盈率静'),
|
||||
sa.Column('pe_ttm', sa.Float(), nullable=True, comment='市盈率TTM'),
|
||||
sa.Column('report_period', sa.String(length=20), nullable=True, comment='报告期'),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False, comment='记录创建时间'),
|
||||
sa.PrimaryKeyConstraint('code', 'curr_date'),
|
||||
comment='股票交易数据表'
|
||||
)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_table('daily_snap')
|
||||
# ### end Alembic commands ###
|
||||
@ -0,0 +1,38 @@
|
||||
"""Auto update from stockdb
|
||||
|
||||
Revision ID: 838eec3adc23
|
||||
Revises: 0a39f1baab38
|
||||
Create Date: 2025-07-31 16:56:37.635401
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '838eec3adc23'
|
||||
down_revision: Union[str, Sequence[str], None] = '0a39f1baab38'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Upgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table('futu_trading_days',
|
||||
sa.Column('market', sa.String(length=20), nullable=False, comment='市场标识'),
|
||||
sa.Column('trade_date', sa.Date(), nullable=False, comment='交易日日期'),
|
||||
sa.Column('trade_date_type', sa.String(length=20), nullable=False, comment='交易日类型'),
|
||||
sa.PrimaryKeyConstraint('market', 'trade_date'),
|
||||
comment='富途证券交易日历表'
|
||||
)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_table('futu_trading_days')
|
||||
# ### end Alembic commands ###
|
||||
@ -0,0 +1,87 @@
|
||||
"""Auto update from stockdb
|
||||
|
||||
Revision ID: 9680e7b8e29b
|
||||
Revises: 838eec3adc23
|
||||
Create Date: 2025-07-31 17:35:37.108765
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '9680e7b8e29b'
|
||||
down_revision: Union[str, Sequence[str], None] = '838eec3adc23'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Upgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table('daily_snap',
|
||||
sa.Column('code', sa.String(length=20), nullable=False, comment='股票代码'),
|
||||
sa.Column('curr_date', sa.String(length=20), nullable=False, comment='交易日期'),
|
||||
sa.Column('name', sa.String(length=50), nullable=True, comment='股票名称'),
|
||||
sa.Column('market_id', sa.String(length=50), nullable=True, comment='市场名称'),
|
||||
sa.Column('code_prefix', sa.Integer(), nullable=True, comment='代码前缀'),
|
||||
sa.Column('industry', sa.String(length=50), nullable=True, comment='所处行业'),
|
||||
sa.Column('listing_date', sa.Date(), nullable=True, comment='上市时间'),
|
||||
sa.Column('latest_price', sa.Float(), nullable=True, comment='最新价'),
|
||||
sa.Column('price_change_percent', sa.Float(), nullable=True, comment='涨跌幅(%)'),
|
||||
sa.Column('price_change', sa.Float(), nullable=True, comment='涨跌额'),
|
||||
sa.Column('volume', sa.Float(), nullable=True, comment='成交量'),
|
||||
sa.Column('turnover', sa.Float(), nullable=True, comment='成交额'),
|
||||
sa.Column('amplitude', sa.Float(), nullable=True, comment='振幅(%)'),
|
||||
sa.Column('turnover_rate', sa.Float(), nullable=True, comment='换手率(%)'),
|
||||
sa.Column('pe_dynamic', sa.Float(), nullable=True, comment='市盈率动'),
|
||||
sa.Column('volume_ratio', sa.Float(), nullable=True, comment='量比'),
|
||||
sa.Column('change_5min', sa.Float(), nullable=True, comment='5分钟涨跌(%)'),
|
||||
sa.Column('highest', sa.Float(), nullable=True, comment='最高'),
|
||||
sa.Column('lowest', sa.Float(), nullable=True, comment='最低'),
|
||||
sa.Column('opening', sa.Float(), nullable=True, comment='今开'),
|
||||
sa.Column('previous_close', sa.Float(), nullable=True, comment='昨收'),
|
||||
sa.Column('price_speed', sa.Float(), nullable=True, comment='涨速(%)'),
|
||||
sa.Column('total_market_cap', sa.Float(), nullable=True, comment='总市值'),
|
||||
sa.Column('circulating_market_cap', sa.Float(), nullable=True, comment='流通市值'),
|
||||
sa.Column('pb_ratio', sa.Float(), nullable=True, comment='市净率'),
|
||||
sa.Column('change_60d', sa.Float(), nullable=True, comment='60日涨跌幅(%)'),
|
||||
sa.Column('change_ytd', sa.Float(), nullable=True, comment='年初至今涨跌幅(%)'),
|
||||
sa.Column('weighted_roe', sa.Float(), nullable=True, comment='加权净资产收益率(%)'),
|
||||
sa.Column('total_shares', sa.Float(), nullable=True, comment='总股本'),
|
||||
sa.Column('circulating_shares', sa.Float(), nullable=True, comment='已流通股份'),
|
||||
sa.Column('operating_revenue', sa.Float(), nullable=True, comment='营业收入'),
|
||||
sa.Column('revenue_growth', sa.Float(), nullable=True, comment='营业收入同比增长(%)'),
|
||||
sa.Column('net_profit', sa.Float(), nullable=True, comment='归属净利润'),
|
||||
sa.Column('net_profit_growth', sa.Float(), nullable=True, comment='归属净利润同比增长(%)'),
|
||||
sa.Column('undistributed_profit_per_share', sa.Float(), nullable=True, comment='每股未分配利润'),
|
||||
sa.Column('gross_margin', sa.Float(), nullable=True, comment='毛利率(%)'),
|
||||
sa.Column('asset_liability_ratio', sa.Float(), nullable=True, comment='资产负债率(%)'),
|
||||
sa.Column('reserve_per_share', sa.Float(), nullable=True, comment='每股公积金'),
|
||||
sa.Column('earnings_per_share', sa.Float(), nullable=True, comment='每股收益'),
|
||||
sa.Column('net_asset_per_share', sa.Float(), nullable=True, comment='每股净资产'),
|
||||
sa.Column('pe_static', sa.Float(), nullable=True, comment='市盈率静'),
|
||||
sa.Column('pe_ttm', sa.Float(), nullable=True, comment='市盈率TTM'),
|
||||
sa.Column('report_period', sa.String(length=20), nullable=True, comment='报告期'),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False, comment='记录创建时间'),
|
||||
sa.PrimaryKeyConstraint('code', 'curr_date'),
|
||||
comment='股票交易数据表'
|
||||
)
|
||||
op.create_table('futu_trading_days',
|
||||
sa.Column('market', sa.String(length=20), nullable=False, comment='市场标识'),
|
||||
sa.Column('trade_date', sa.Date(), nullable=False, comment='交易日日期'),
|
||||
sa.Column('trade_date_type', sa.String(length=20), nullable=False, comment='交易日类型'),
|
||||
sa.PrimaryKeyConstraint('market', 'trade_date'),
|
||||
comment='富途证券交易日历表'
|
||||
)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_table('futu_trading_days')
|
||||
op.drop_table('daily_snap')
|
||||
# ### end Alembic commands ###
|
||||
@ -0,0 +1,32 @@
|
||||
"""Auto update from stockdb
|
||||
|
||||
Revision ID: df56ac7669f2
|
||||
Revises:
|
||||
Create Date: 2025-07-31 16:04:32.216988
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = 'df56ac7669f2'
|
||||
down_revision: Union[str, Sequence[str], None] = None
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Upgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.add_column('daily_snap', sa.Column('market_id', sa.String(length=50), nullable=True, comment='市场名称'))
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_column('daily_snap', 'market_id')
|
||||
# ### end Alembic commands ###
|
||||
23
src/sqlalchemy/models/base.py
Normal file
23
src/sqlalchemy/models/base.py
Normal file
@ -0,0 +1,23 @@
|
||||
from sqlalchemy import Column, Integer, DateTime
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from datetime import datetime
|
||||
|
||||
# 所有模型的基类
|
||||
Base = declarative_base()
|
||||
|
||||
class BaseModel(Base):
|
||||
__abstract__ = True # 抽象类,不生成实际表
|
||||
|
||||
# 通用字段
|
||||
id = Column(Integer, primary_key=True, autoincrement=True, comment="主键ID")
|
||||
created_at = Column(DateTime, default=datetime.now, comment="创建时间")
|
||||
updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment="更新时间")
|
||||
|
||||
# 通用方法:将模型实例转为字典(方便JSON序列化)
|
||||
def to_dict(self, exclude=None):
|
||||
exclude = exclude or []
|
||||
return {
|
||||
c.name: getattr(self, c.name)
|
||||
for c in self.__table__.columns
|
||||
if c.name not in exclude
|
||||
}
|
||||
448
src/sqlalchemy/models/stockdb.py
Normal file
448
src/sqlalchemy/models/stockdb.py
Normal file
@ -0,0 +1,448 @@
|
||||
from sqlalchemy import BigInteger, Date, DateTime, Double, Float, Integer, String, text, Numeric
|
||||
from sqlalchemy.dialects.mysql import TINYINT, VARCHAR
|
||||
from typing import Optional
|
||||
|
||||
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
||||
import datetime
|
||||
import decimal
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
|
||||
|
||||
class FutuMarketSnapshot(Base):
|
||||
__tablename__ = 'futu_market_snapshot'
|
||||
|
||||
code: Mapped[str] = mapped_column(String(100), primary_key=True, server_default=text("''"), comment='股票代码')
|
||||
name: Mapped[str] = mapped_column(String(255), server_default=text("''"), comment='股票名称')
|
||||
update_time: Mapped[datetime.datetime] = mapped_column(DateTime, primary_key=True, comment='当前价更新时间')
|
||||
last_price: Mapped[float] = mapped_column(Float, server_default=text("'0'"), comment='最新价格')
|
||||
open_price: Mapped[float] = mapped_column(Float, server_default=text("'0'"), comment='今日开盘价')
|
||||
high_price: Mapped[float] = mapped_column(Float, server_default=text("'0'"), comment='最高价格')
|
||||
low_price: Mapped[float] = mapped_column(Float, server_default=text("'0'"), comment='最低价格')
|
||||
prev_close_price: Mapped[float] = mapped_column(Float, server_default=text("'0'"), comment='昨收盘价格')
|
||||
volume: Mapped[int] = mapped_column(BigInteger, server_default=text("'0'"), comment='成交数量')
|
||||
turnover: Mapped[float] = mapped_column(Float, server_default=text("'0'"), comment='成交金额')
|
||||
turnover_rate: Mapped[float] = mapped_column(Float, server_default=text("'0'"), comment='换手率')
|
||||
suspension: Mapped[int] = mapped_column(TINYINT(1), server_default=text("'0'"), comment='是否停牌')
|
||||
listing_date: Mapped[datetime.date] = mapped_column(Date, server_default=text("'1970-01-01'"), comment='上市日期')
|
||||
equity_valid: Mapped[int] = mapped_column(TINYINT(1), server_default=text("'0'"), comment='是否正股')
|
||||
issued_shares: Mapped[int] = mapped_column(BigInteger, server_default=text("'0'"), comment='总股本')
|
||||
total_market_val: Mapped[float] = mapped_column(Float, server_default=text("'0'"), comment='总市值')
|
||||
net_asset: Mapped[int] = mapped_column(BigInteger, server_default=text("'0'"), comment='资产净值')
|
||||
net_profit: Mapped[int] = mapped_column(BigInteger, server_default=text("'0'"), comment='净利润')
|
||||
earning_per_share: Mapped[float] = mapped_column(Float, server_default=text("'0'"), comment='每股盈利')
|
||||
outstanding_shares: Mapped[int] = mapped_column(BigInteger, server_default=text("'0'"), comment='流通股本')
|
||||
net_asset_per_share: Mapped[float] = mapped_column(Float, server_default=text("'0'"), comment='每股净资产')
|
||||
circular_market_val: Mapped[float] = mapped_column(Float, server_default=text("'0'"), comment='流通市值')
|
||||
ey_ratio: Mapped[float] = mapped_column(Float, server_default=text("'0'"), comment='收益率')
|
||||
pe_ratio: Mapped[float] = mapped_column(Float, server_default=text("'0'"), comment='市盈率')
|
||||
pb_ratio: Mapped[float] = mapped_column(Float, server_default=text("'0'"), comment='市净率')
|
||||
pe_ttm_ratio: Mapped[float] = mapped_column(Float, server_default=text("'0'"), comment='市盈率 TTM')
|
||||
dividend_ttm: Mapped[float] = mapped_column(Float, server_default=text("'0'"), comment='股息 TTM')
|
||||
dividend_ratio_ttm: Mapped[float] = mapped_column(Float, server_default=text("'0'"), comment='股息率 TTM')
|
||||
dividend_lfy: Mapped[float] = mapped_column(Float, server_default=text("'0'"), comment='股息 LFY')
|
||||
dividend_lfy_ratio: Mapped[float] = mapped_column(Float, server_default=text("'0'"), comment='股息率 LFY')
|
||||
|
||||
|
||||
class FutuPlatList(Base):
|
||||
__tablename__ = 'futu_plat_list'
|
||||
|
||||
up_date: Mapped[datetime.date] = mapped_column(Date, primary_key=True)
|
||||
market: Mapped[str] = mapped_column(String(50))
|
||||
plat: Mapped[str] = mapped_column(String(50), primary_key=True, server_default=text("'INDUSTRY'"))
|
||||
code: Mapped[str] = mapped_column(String(50), primary_key=True)
|
||||
plate_id: Mapped[str] = mapped_column(String(50))
|
||||
plate_name: Mapped[str] = mapped_column(String(255))
|
||||
|
||||
|
||||
class FutuRehab(Base):
|
||||
__tablename__ = 'futu_rehab'
|
||||
|
||||
code: Mapped[str] = mapped_column(String(100), primary_key=True)
|
||||
name: Mapped[str] = mapped_column(String(100))
|
||||
ex_div_date: Mapped[datetime.date] = mapped_column(Date, primary_key=True)
|
||||
forward_adj_factorA: Mapped[float] = mapped_column(Float)
|
||||
forward_adj_factorB: Mapped[float] = mapped_column(Float)
|
||||
backward_adj_factorA: Mapped[float] = mapped_column(Float)
|
||||
backward_adj_factorB: Mapped[float] = mapped_column(Float)
|
||||
|
||||
|
||||
class Hs300(Base):
|
||||
__tablename__ = 'hs300'
|
||||
|
||||
up_date: Mapped[str] = mapped_column(String(100), primary_key=True, server_default=text("''"))
|
||||
index_code: Mapped[str] = mapped_column(String(100), server_default=text("'000300'"))
|
||||
index_name: Mapped[str] = mapped_column(String(100), server_default=text("''"))
|
||||
index_name_eng: Mapped[str] = mapped_column(String(100), server_default=text("''"))
|
||||
code_inner: Mapped[str] = mapped_column(String(100), primary_key=True, server_default=text("''"))
|
||||
code_name: Mapped[str] = mapped_column(String(100), server_default=text("''"))
|
||||
code_name_eng: Mapped[str] = mapped_column(String(100), server_default=text("''"))
|
||||
exchange: Mapped[str] = mapped_column(String(100), server_default=text("''"))
|
||||
exchange_eng: Mapped[str] = mapped_column(String(100), server_default=text("''"))
|
||||
code: Mapped[str] = mapped_column(String(100), server_default=text("''"))
|
||||
|
||||
|
||||
class Hs3003yearsYieldStats2410(Base):
|
||||
__tablename__ = 'hs300_3years_yield_stats_2410'
|
||||
|
||||
code: Mapped[str] = mapped_column(String(100), primary_key=True)
|
||||
name: Mapped[str] = mapped_column(String(100))
|
||||
year_diff: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||
max_yield_rate: Mapped[float] = mapped_column(Float)
|
||||
max_yield_rate_start: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
max_yield_rate_end: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
min_yield_rate: Mapped[float] = mapped_column(Float)
|
||||
min_yield_rate_start: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
min_yield_rate_end: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
avg_yield_rate: Mapped[float] = mapped_column(Float)
|
||||
median_yield_rate: Mapped[float] = mapped_column(Float)
|
||||
win_rate: Mapped[float] = mapped_column(Float)
|
||||
annual_max_yield_rate: Mapped[float] = mapped_column(Float)
|
||||
annual_max_yield_rate_start: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
annual_max_yield_rate_end: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
annual_min_yield_rate: Mapped[float] = mapped_column(Float)
|
||||
annual_min_yield_rate_start: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
annual_min_yield_rate_end: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
annual_avg_yield_rate: Mapped[float] = mapped_column(Float)
|
||||
annual_median_yield_rate: Mapped[float] = mapped_column(Float)
|
||||
max_deficit_days: Mapped[int] = mapped_column(Integer)
|
||||
max_deficit_start: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
max_deficit_end: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
annual_yield_variance: Mapped[float] = mapped_column(Float)
|
||||
|
||||
|
||||
class Hs3005yearsYieldStats2410(Base):
|
||||
__tablename__ = 'hs300_5years_yield_stats_2410'
|
||||
|
||||
code: Mapped[str] = mapped_column(String(100), primary_key=True)
|
||||
name: Mapped[str] = mapped_column(String(100))
|
||||
year_diff: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||
max_yield_rate: Mapped[float] = mapped_column(Float)
|
||||
max_yield_rate_start: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
max_yield_rate_end: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
min_yield_rate: Mapped[float] = mapped_column(Float)
|
||||
min_yield_rate_start: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
min_yield_rate_end: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
avg_yield_rate: Mapped[float] = mapped_column(Float)
|
||||
median_yield_rate: Mapped[float] = mapped_column(Float)
|
||||
win_rate: Mapped[float] = mapped_column(Float)
|
||||
annual_max_yield_rate: Mapped[float] = mapped_column(Float)
|
||||
annual_max_yield_rate_start: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
annual_max_yield_rate_end: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
annual_min_yield_rate: Mapped[float] = mapped_column(Float)
|
||||
annual_min_yield_rate_start: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
annual_min_yield_rate_end: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
annual_avg_yield_rate: Mapped[float] = mapped_column(Float)
|
||||
annual_median_yield_rate: Mapped[float] = mapped_column(Float)
|
||||
max_deficit_days: Mapped[int] = mapped_column(Integer)
|
||||
max_deficit_start: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
max_deficit_end: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
annual_yield_variance: Mapped[float] = mapped_column(Float)
|
||||
|
||||
|
||||
class Hs300AjustKline202410(Base):
|
||||
__tablename__ = 'hs300_ajust_kline_202410'
|
||||
|
||||
code: Mapped[str] = mapped_column(String(100), primary_key=True)
|
||||
name: Mapped[str] = mapped_column(String(100))
|
||||
time_key: Mapped[datetime.datetime] = mapped_column(DateTime, primary_key=True)
|
||||
hfq_open: Mapped[float] = mapped_column(Float)
|
||||
hfq_close: Mapped[float] = mapped_column(Float)
|
||||
qfq_open: Mapped[float] = mapped_column(Float)
|
||||
qfq_close: Mapped[float] = mapped_column(Float)
|
||||
none_open: Mapped[float] = mapped_column(Float)
|
||||
none_close: Mapped[float] = mapped_column(Float)
|
||||
|
||||
|
||||
class Hs300HisKlineHfq(Base):
|
||||
__tablename__ = 'hs300_his_kline_hfq'
|
||||
|
||||
code: Mapped[str] = mapped_column(String(100), primary_key=True, server_default=text("''"))
|
||||
name: Mapped[str] = mapped_column(String(100), server_default=text("''"))
|
||||
time_key: Mapped[datetime.datetime] = mapped_column(DateTime, primary_key=True)
|
||||
open: Mapped[float] = mapped_column(Float)
|
||||
close: Mapped[float] = mapped_column(Float)
|
||||
high: Mapped[float] = mapped_column(Float)
|
||||
low: Mapped[float] = mapped_column(Float)
|
||||
pe_ratio: Mapped[Optional[float]] = mapped_column(Float)
|
||||
turnover_rate: Mapped[Optional[float]] = mapped_column(Float)
|
||||
volume: Mapped[Optional[int]] = mapped_column(BigInteger)
|
||||
turnover: Mapped[Optional[decimal.Decimal]] = mapped_column(Double(asdecimal=True))
|
||||
change_rate: Mapped[Optional[float]] = mapped_column(Float)
|
||||
last_close: Mapped[Optional[float]] = mapped_column(Float)
|
||||
|
||||
|
||||
class Hs300HisKlineNone(Base):
|
||||
__tablename__ = 'hs300_his_kline_none'
|
||||
|
||||
code: Mapped[str] = mapped_column(String(100), primary_key=True, server_default=text("''"))
|
||||
name: Mapped[str] = mapped_column(String(100), server_default=text("''"))
|
||||
time_key: Mapped[datetime.datetime] = mapped_column(DateTime, primary_key=True)
|
||||
open: Mapped[float] = mapped_column(Float)
|
||||
close: Mapped[float] = mapped_column(Float)
|
||||
high: Mapped[float] = mapped_column(Float)
|
||||
low: Mapped[float] = mapped_column(Float)
|
||||
pe_ratio: Mapped[Optional[float]] = mapped_column(Float)
|
||||
turnover_rate: Mapped[Optional[float]] = mapped_column(Float)
|
||||
volume: Mapped[Optional[int]] = mapped_column(BigInteger)
|
||||
turnover: Mapped[Optional[decimal.Decimal]] = mapped_column(Double(asdecimal=True))
|
||||
change_rate: Mapped[Optional[float]] = mapped_column(Float)
|
||||
last_close: Mapped[Optional[float]] = mapped_column(Float)
|
||||
|
||||
|
||||
class Hs300QfqHis(Base):
|
||||
__tablename__ = 'hs300_qfq_his'
|
||||
|
||||
code: Mapped[str] = mapped_column(String(100), primary_key=True, server_default=text("''"))
|
||||
name: Mapped[str] = mapped_column(String(100), server_default=text("''"))
|
||||
time_key: Mapped[datetime.datetime] = mapped_column(DateTime, primary_key=True)
|
||||
open: Mapped[float] = mapped_column(Float)
|
||||
close: Mapped[float] = mapped_column(Float)
|
||||
high: Mapped[float] = mapped_column(Float)
|
||||
low: Mapped[float] = mapped_column(Float)
|
||||
pe_ratio: Mapped[Optional[float]] = mapped_column(Float)
|
||||
turnover_rate: Mapped[Optional[float]] = mapped_column(Float)
|
||||
volume: Mapped[Optional[int]] = mapped_column(BigInteger)
|
||||
turnover: Mapped[Optional[decimal.Decimal]] = mapped_column(Double(asdecimal=True))
|
||||
change_rate: Mapped[Optional[float]] = mapped_column(Float)
|
||||
last_close: Mapped[Optional[float]] = mapped_column(Float)
|
||||
|
||||
|
||||
class IndexHk(Base):
|
||||
__tablename__ = 'index_hk'
|
||||
|
||||
up_date: Mapped[str] = mapped_column(String(100), primary_key=True, server_default=text("''"))
|
||||
index_code: Mapped[str] = mapped_column(String(100), primary_key=True, server_default=text("'000300'"))
|
||||
index_name: Mapped[str] = mapped_column(String(100), server_default=text("''"))
|
||||
code_inner: Mapped[str] = mapped_column(String(100), primary_key=True, server_default=text("''"))
|
||||
code_name: Mapped[str] = mapped_column(String(100), server_default=text("''"))
|
||||
weight: Mapped[str] = mapped_column(String(100), server_default=text("''"))
|
||||
code: Mapped[str] = mapped_column(String(100), server_default=text("''"))
|
||||
|
||||
|
||||
class IndexHs(Base):
|
||||
__tablename__ = 'index_hs'
|
||||
|
||||
up_date: Mapped[str] = mapped_column(String(100), primary_key=True, server_default=text("''"))
|
||||
index_code: Mapped[str] = mapped_column(String(100), primary_key=True, server_default=text("'000300'"))
|
||||
index_name: Mapped[str] = mapped_column(String(100), server_default=text("''"))
|
||||
index_name_eng: Mapped[str] = mapped_column(String(100), server_default=text("''"))
|
||||
code_inner: Mapped[str] = mapped_column(String(100), primary_key=True, server_default=text("''"))
|
||||
code_name: Mapped[str] = mapped_column(String(100), server_default=text("''"))
|
||||
code_name_eng: Mapped[str] = mapped_column(String(100), server_default=text("''"))
|
||||
exchange: Mapped[str] = mapped_column(String(100), server_default=text("''"))
|
||||
exchange_eng: Mapped[str] = mapped_column(String(100), server_default=text("''"))
|
||||
code: Mapped[str] = mapped_column(String(100), server_default=text("''"))
|
||||
|
||||
|
||||
class IndexUs(Base):
|
||||
__tablename__ = 'index_us'
|
||||
|
||||
up_date: Mapped[str] = mapped_column(String(100), primary_key=True, server_default=text("''"))
|
||||
index_code: Mapped[str] = mapped_column(String(100), primary_key=True, server_default=text("'000300'"))
|
||||
index_name: Mapped[str] = mapped_column(String(100), server_default=text("''"))
|
||||
code_inner: Mapped[str] = mapped_column(String(100), primary_key=True, server_default=text("''"))
|
||||
code_name: Mapped[str] = mapped_column(String(100), server_default=text("''"))
|
||||
weight: Mapped[str] = mapped_column(String(100), server_default=text("''"))
|
||||
code: Mapped[str] = mapped_column(String(100), server_default=text("''"))
|
||||
|
||||
|
||||
class Sp500(Base):
|
||||
__tablename__ = 'sp500'
|
||||
|
||||
sp_no: Mapped[int] = mapped_column(Integer, server_default=text("'0'"))
|
||||
code_name: Mapped[str] = mapped_column(VARCHAR(100), server_default=text("''"))
|
||||
code_inner: Mapped[str] = mapped_column(String(100), primary_key=True, server_default=text("''"))
|
||||
sector: Mapped[str] = mapped_column(String(100), server_default=text("''"))
|
||||
code: Mapped[str] = mapped_column(String(100), server_default=text("''"))
|
||||
up_date: Mapped[str] = mapped_column(String(100), primary_key=True, server_default=text("'2024-10-02'"))
|
||||
|
||||
|
||||
class Sp5003yearsYieldStats2410(Base):
|
||||
__tablename__ = 'sp500_3years_yield_stats_2410'
|
||||
|
||||
code: Mapped[str] = mapped_column(String(100), primary_key=True)
|
||||
name: Mapped[str] = mapped_column(String(100))
|
||||
year_diff: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||
max_yield_rate: Mapped[float] = mapped_column(Float)
|
||||
max_yield_rate_start: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
max_yield_rate_end: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
min_yield_rate: Mapped[float] = mapped_column(Float)
|
||||
min_yield_rate_start: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
min_yield_rate_end: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
avg_yield_rate: Mapped[float] = mapped_column(Float)
|
||||
median_yield_rate: Mapped[float] = mapped_column(Float)
|
||||
win_rate: Mapped[float] = mapped_column(Float)
|
||||
annual_max_yield_rate: Mapped[float] = mapped_column(Float)
|
||||
annual_max_yield_rate_start: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
annual_max_yield_rate_end: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
annual_min_yield_rate: Mapped[float] = mapped_column(Float)
|
||||
annual_min_yield_rate_start: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
annual_min_yield_rate_end: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
annual_avg_yield_rate: Mapped[float] = mapped_column(Float)
|
||||
annual_median_yield_rate: Mapped[float] = mapped_column(Float)
|
||||
max_deficit_days: Mapped[int] = mapped_column(Integer)
|
||||
max_deficit_start: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
max_deficit_end: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
annual_yield_variance: Mapped[float] = mapped_column(Float)
|
||||
|
||||
|
||||
class Sp5005yearsYieldStats2410(Base):
|
||||
__tablename__ = 'sp500_5years_yield_stats_2410'
|
||||
|
||||
code: Mapped[str] = mapped_column(String(100), primary_key=True)
|
||||
name: Mapped[str] = mapped_column(String(100))
|
||||
year_diff: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||
max_yield_rate: Mapped[float] = mapped_column(Float)
|
||||
max_yield_rate_start: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
max_yield_rate_end: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
min_yield_rate: Mapped[float] = mapped_column(Float)
|
||||
min_yield_rate_start: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
min_yield_rate_end: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
avg_yield_rate: Mapped[float] = mapped_column(Float)
|
||||
median_yield_rate: Mapped[float] = mapped_column(Float)
|
||||
win_rate: Mapped[float] = mapped_column(Float)
|
||||
annual_max_yield_rate: Mapped[float] = mapped_column(Float)
|
||||
annual_max_yield_rate_start: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
annual_max_yield_rate_end: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
annual_min_yield_rate: Mapped[float] = mapped_column(Float)
|
||||
annual_min_yield_rate_start: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
annual_min_yield_rate_end: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
annual_avg_yield_rate: Mapped[float] = mapped_column(Float)
|
||||
annual_median_yield_rate: Mapped[float] = mapped_column(Float)
|
||||
max_deficit_days: Mapped[int] = mapped_column(Integer)
|
||||
max_deficit_start: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
max_deficit_end: Mapped[datetime.datetime] = mapped_column(DateTime)
|
||||
annual_yield_variance: Mapped[float] = mapped_column(Float)
|
||||
|
||||
|
||||
class Sp500AjustKline202410(Base):
|
||||
__tablename__ = 'sp500_ajust_kline_202410'
|
||||
|
||||
code: Mapped[str] = mapped_column(String(100), primary_key=True)
|
||||
name: Mapped[str] = mapped_column(String(100))
|
||||
time_key: Mapped[datetime.datetime] = mapped_column(DateTime, primary_key=True)
|
||||
hfq_open: Mapped[float] = mapped_column(Float)
|
||||
hfq_close: Mapped[float] = mapped_column(Float)
|
||||
qfq_open: Mapped[float] = mapped_column(Float)
|
||||
qfq_close: Mapped[float] = mapped_column(Float)
|
||||
none_open: Mapped[float] = mapped_column(Float)
|
||||
none_close: Mapped[float] = mapped_column(Float)
|
||||
|
||||
|
||||
class Sp500HisKlineNone(Base):
|
||||
__tablename__ = 'sp500_his_kline_none'
|
||||
|
||||
code: Mapped[str] = mapped_column(String(100), primary_key=True)
|
||||
name: Mapped[str] = mapped_column(String(100))
|
||||
time_key: Mapped[datetime.datetime] = mapped_column(DateTime, primary_key=True)
|
||||
open: Mapped[float] = mapped_column(Float)
|
||||
high: Mapped[float] = mapped_column(Float)
|
||||
low: Mapped[float] = mapped_column(Float)
|
||||
close: Mapped[float] = mapped_column(Float)
|
||||
volume: Mapped[int] = mapped_column(BigInteger)
|
||||
dividends: Mapped[float] = mapped_column(Float)
|
||||
stock_splits: Mapped[float] = mapped_column(Float)
|
||||
adj_close: Mapped[float] = mapped_column(Float)
|
||||
|
||||
|
||||
class Sp500QfqHis202410(Base):
|
||||
__tablename__ = 'sp500_qfq_his_202410'
|
||||
|
||||
code: Mapped[str] = mapped_column(String(100), primary_key=True)
|
||||
name: Mapped[str] = mapped_column(String(100))
|
||||
time_key: Mapped[datetime.datetime] = mapped_column(DateTime, primary_key=True)
|
||||
open: Mapped[float] = mapped_column(Float)
|
||||
high: Mapped[float] = mapped_column(Float)
|
||||
low: Mapped[float] = mapped_column(Float)
|
||||
close: Mapped[float] = mapped_column(Float)
|
||||
volume: Mapped[int] = mapped_column(BigInteger)
|
||||
dividends: Mapped[float] = mapped_column(Float)
|
||||
stock_splits: Mapped[float] = mapped_column(Float)
|
||||
adj_close: Mapped[float] = mapped_column(Float)
|
||||
|
||||
|
||||
|
||||
class DailySanpModel(Base):
|
||||
"""股票数据模型类(使用mapped_column风格)"""
|
||||
__tablename__ = 'daily_snap'
|
||||
__table_args__ = {"comment": "股票交易数据表"}
|
||||
|
||||
# 联合主键:股票代码+报告期
|
||||
code: Mapped[str] = mapped_column(String(20), primary_key=True, comment='股票代码')
|
||||
curr_date: Mapped[str] = mapped_column(String(20), primary_key=True, comment='交易日期')
|
||||
|
||||
# 基本信息字段
|
||||
name: Mapped[str | None] = mapped_column(String(50), comment='股票名称')
|
||||
market_id: Mapped[str | None] = mapped_column(String(50), comment='市场名称')
|
||||
code_prefix: Mapped[int | None] = mapped_column(Integer, comment='代码前缀')
|
||||
industry: Mapped[str | None] = mapped_column(String(50), comment='所处行业')
|
||||
listing_date: Mapped[datetime.date | None] = mapped_column(Date, comment='上市时间')
|
||||
|
||||
# 交易数据字段
|
||||
latest_price: Mapped[float | None] = mapped_column(Float, comment='最新价')
|
||||
price_change_percent: Mapped[float | None] = mapped_column(Float, comment='涨跌幅(%)')
|
||||
price_change: Mapped[float | None] = mapped_column(Float, comment='涨跌额')
|
||||
volume: Mapped[float | None] = mapped_column(Float, comment='成交量')
|
||||
turnover: Mapped[float | None] = mapped_column(Float, comment='成交额')
|
||||
amplitude: Mapped[float | None] = mapped_column(Float, comment='振幅(%)')
|
||||
turnover_rate: Mapped[float | None] = mapped_column(Float, comment='换手率(%)')
|
||||
pe_dynamic: Mapped[float | None] = mapped_column(Float, comment='市盈率动')
|
||||
volume_ratio: Mapped[float | None] = mapped_column(Float, comment='量比')
|
||||
change_5min: Mapped[float | None] = mapped_column(Float, comment='5分钟涨跌(%)')
|
||||
highest: Mapped[float | None] = mapped_column(Float, comment='最高')
|
||||
lowest: Mapped[float | None] = mapped_column(Float, comment='最低')
|
||||
opening: Mapped[float | None] = mapped_column(Float, comment='今开')
|
||||
previous_close: Mapped[float | None] = mapped_column(Float, comment='昨收')
|
||||
price_speed: Mapped[float | None] = mapped_column(Float, comment='涨速(%)')
|
||||
|
||||
# 市值数据字段
|
||||
total_market_cap: Mapped[float | None] = mapped_column(Float, comment='总市值')
|
||||
circulating_market_cap: Mapped[float | None] = mapped_column(Float, comment='流通市值')
|
||||
pb_ratio: Mapped[float | None] = mapped_column(Float, comment='市净率')
|
||||
change_60d: Mapped[float | None] = mapped_column(Float, comment='60日涨跌幅(%)')
|
||||
change_ytd: Mapped[float | None] = mapped_column(Float, comment='年初至今涨跌幅(%)')
|
||||
|
||||
# 财务数据字段
|
||||
weighted_roe: Mapped[float | None] = mapped_column(Float, comment='加权净资产收益率(%)')
|
||||
total_shares: Mapped[float | None] = mapped_column(Float, comment='总股本')
|
||||
circulating_shares: Mapped[float | None] = mapped_column(Float, comment='已流通股份')
|
||||
operating_revenue: Mapped[float | None] = mapped_column(Float, comment='营业收入')
|
||||
revenue_growth: Mapped[float | None] = mapped_column(Float, comment='营业收入同比增长(%)')
|
||||
net_profit: Mapped[float | None] = mapped_column(Float, comment='归属净利润')
|
||||
net_profit_growth: Mapped[float | None] = mapped_column(Float, comment='归属净利润同比增长(%)')
|
||||
undistributed_profit_per_share: Mapped[float | None] = mapped_column(Float, comment='每股未分配利润')
|
||||
gross_margin: Mapped[float | None] = mapped_column(Float, comment='毛利率(%)')
|
||||
asset_liability_ratio: Mapped[float | None] = mapped_column(Float, comment='资产负债率(%)')
|
||||
reserve_per_share: Mapped[float | None] = mapped_column(Float, comment='每股公积金')
|
||||
earnings_per_share: Mapped[float | None] = mapped_column(Float, comment='每股收益')
|
||||
net_asset_per_share: Mapped[float | None] = mapped_column(Float, comment='每股净资产')
|
||||
pe_static: Mapped[float | None] = mapped_column(Float, comment='市盈率静')
|
||||
pe_ttm: Mapped[float | None] = mapped_column(Float, comment='市盈率TTM')
|
||||
report_period: Mapped[str | None] = mapped_column(String(20), comment='报告期')
|
||||
|
||||
# 记录创建时间
|
||||
created_at: Mapped[datetime.datetime] = mapped_column(DateTime, default=datetime.datetime.now, comment='记录创建时间')
|
||||
|
||||
|
||||
class FutuTradingDayModel(Base):
|
||||
"""富途交易日历模型类"""
|
||||
__tablename__ = "futu_trading_days"
|
||||
__table_args__ = {"comment": "富途证券交易日历表"}
|
||||
|
||||
# 联合主键:market + trade_date
|
||||
market: Mapped[str] = mapped_column(
|
||||
String(20),
|
||||
primary_key=True,
|
||||
comment="市场标识"
|
||||
)
|
||||
trade_date: Mapped[datetime.date] = mapped_column(
|
||||
Date,
|
||||
primary_key=True,
|
||||
comment="交易日日期"
|
||||
)
|
||||
trade_date_type: Mapped[str] = mapped_column(
|
||||
String(20),
|
||||
comment="交易日类型"
|
||||
)
|
||||
13
src/sqlalchemy/readme.txt
Normal file
13
src/sqlalchemy/readme.txt
Normal file
@ -0,0 +1,13 @@
|
||||
|
||||
# 从数据库生成模型类
|
||||
sqlacodegen --outfile=models/stockdb.py "mysql+pymysql://root:mysqlpw@testdb:3306/stockdb?charset=utf8mb4"
|
||||
|
||||
# 初始化
|
||||
mkdir migrations
|
||||
alembic init migrations/stockdb
|
||||
# 修改 alembic.ini 脚本,以及 env.py 导入 models
|
||||
|
||||
# 同步修改到数据库(读取 models/shared.py )
|
||||
./scripts/sync_stockdb.sh
|
||||
|
||||
### 对视图支持不好,主要是视图的字段没有类型,所以在导入导出时会出错,慎用!
|
||||
7
src/sqlalchemy/scripts/sync_stockdb.sh
Executable file
7
src/sqlalchemy/scripts/sync_stockdb.sh
Executable file
@ -0,0 +1,7 @@
|
||||
#!/bin/bash
|
||||
cd $(dirname $0)/..
|
||||
|
||||
alembic -c migrations/stockdb/alembic.ini revision --autogenerate -m "Auto update from stockdb"
|
||||
alembic -c migrations/stockdb/alembic.ini upgrade head
|
||||
|
||||
echo "数据库 stockdb 同步完成"
|
||||
Reference in New Issue
Block a user