first commit
This commit is contained in:
76
add_backlink_count_column.py
Normal file
76
add_backlink_count_column.py
Normal file
@@ -0,0 +1,76 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: UTF-8 -*-
|
||||
'''
|
||||
@Project :domainScanDemo
|
||||
@File :add_backlink_count_column.py
|
||||
@IDE :PyCharm
|
||||
@Author :梦伴
|
||||
@Date :2026/4/10 03:50
|
||||
@explain : 为domains表添加backlink_count字段
|
||||
'''
|
||||
|
||||
import psycopg2
|
||||
from app.config import config
|
||||
|
||||
|
||||
def add_backlink_count_column():
|
||||
"""
|
||||
为domains表添加backlink_count字段
|
||||
"""
|
||||
try:
|
||||
# 连接数据库
|
||||
conn = psycopg2.connect(
|
||||
host=config.DB_HOST,
|
||||
port=config.DB_PORT,
|
||||
database=config.DB_DATABASE,
|
||||
user=config.DB_USER,
|
||||
password=config.DB_PASSWORD
|
||||
)
|
||||
print(f"成功连接到数据库: {config.DB_HOST}:{config.DB_PORT}/{config.DB_DATABASE}")
|
||||
|
||||
# 创建游标
|
||||
cur = conn.cursor()
|
||||
|
||||
# 添加backlink_count字段
|
||||
add_column_sql = """
|
||||
ALTER TABLE domains ADD COLUMN IF NOT EXISTS backlink_count INTEGER DEFAULT 0
|
||||
"""
|
||||
cur.execute(add_column_sql)
|
||||
print("添加backlink_count字段成功")
|
||||
|
||||
# 添加字段注释
|
||||
add_comment_sql = """
|
||||
COMMENT ON COLUMN domains.backlink_count IS '友情链接数量'
|
||||
"""
|
||||
cur.execute(add_comment_sql)
|
||||
print("添加字段注释成功")
|
||||
|
||||
# 创建索引
|
||||
create_index_sql = """
|
||||
CREATE INDEX IF NOT EXISTS idx_domains_backlink_count ON domains(backlink_count)
|
||||
"""
|
||||
cur.execute(create_index_sql)
|
||||
print("创建索引成功")
|
||||
|
||||
# 提交事务
|
||||
conn.commit()
|
||||
print("事务提交成功")
|
||||
|
||||
# 关闭游标和连接
|
||||
cur.close()
|
||||
conn.close()
|
||||
print("连接关闭成功")
|
||||
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"添加backlink_count字段失败: {e}")
|
||||
return False
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("开始为domains表添加backlink_count字段...")
|
||||
success = add_backlink_count_column()
|
||||
if success:
|
||||
print("backlink_count字段添加成功!")
|
||||
else:
|
||||
print("backlink_count字段添加失败!")
|
||||
Reference in New Issue
Block a user