#!/usr/bin/env python
# -*- encoding: utf8 -*-
import calendar
import time
import os
import sys
from datetime import datetime,timedelta
import mysql.connector # 定义一些全局变量
work_dir = "G:/"
argv_var_name=sys.argv
argv_database_name =""
argv_table_name = "" getdbconfig_0_152 = {
'host': '127.0.0.1',
'user': 'root',
'password': 'root',
'port': '3306'
} # 此函数用来打印输出制定内容,第一个变量是输出内容,第二个变量是文件前缀,第三个变量是后缀名
def print_log(print_content,db_name,filename,suffix):
my_log_day = time.strftime("%Y%m%d")
my_log_file="%s%s_%s_%s.%s" %(work_dir,db_name,filename,my_log_day,suffix)
#with open(my_log_file, "aw") as f:
with open(my_log_file, "a") as f:
f.write( print_content+"\n")
f.close() # 此函数用来指定某个月增加一个月
def add_month(srcDate, addMonths):
if not srcDate or not srcDate:
pass
if addMonths < 1:
return srcDate
month = srcDate.month - 1 + addMonths
year = srcDate.year + month / 12
month = month % 12 + 1
day = min(srcDate.day, calendar.monthrange(year, month)[1])
return srcDate.replace(year=year, month=month, day=day) def select_any_sql(getdbconfig,select_sql):
select_any_sql_cnn = mysql.connector.connect(host=getdbconfig['host'] , user=getdbconfig['user'], password=getdbconfig['password'],port=getdbconfig['port'])
select_any_sql_cursor = select_any_sql_cnn.cursor()
select_any_sql_cursor.execute(select_sql)
select_any_sql_data = select_any_sql_cursor.fetchone()
select_any_sql_cnn.close
return select_any_sql_data # 此函数用来导入数据
def import_dump_sql(getdbconfig, dumpsql):
import_host = getdbconfig['host']
import_db = getdbconfig['database']
import_user = getdbconfig['user']
import_password = getdbconfig['password']
import_port = getdbconfig['port']
mysqlimport = " mysql -h%s -u%s -p%s -P%s %s < %s " % (import_host, import_user, import_password, import_port, import_db, dumpsql)
os.system(mysqlimport) def select_partition(getdbconfig):
if len(argv_var_name) == 1:
argv_database_name = ""
argv_table_name = ""
elif len(argv_var_name) == 2:
argv_database_name = argv_var_name[1]
argv_table_name = ""
elif len(argv_var_name) == 3:
argv_database_name = argv_var_name[1]
argv_table_name = argv_var_name[2]
else:
print "Warning!!!! up to three parameters can be specified!!!!" if argv_database_name == "":
partition_info_table_schema = ""
else:
partition_info_table_schema = "and table_schema='%s'" % (argv_database_name) if argv_table_name == "":
partition_info_table_name = ""
else:
partition_info_table_name = "and table_name='%s'" %(argv_table_name) select_partition_sql = "SELECT table_schema,table_name,PARTITION_NAME,PARTITION_ORDINAL_POSITION,PARTITION_METHOD,PARTITION_EXPRESSION,PARTITION_DESCRIPTION FROM information_schema.`PARTITIONS` " \
"WHERE table_schema not in ('information_schema','performance_schema','mysql','test','sqlexe') %s %s and PARTITION_NAME is not null order by table_schema,PARTITION_NAME, PARTITION_ORDINAL_POSITION;" %(partition_info_table_schema,partition_info_table_name)
partition_contain_before = ()
partition_info = []
auto_create_partition_filename=[]
partition_info_not_in_range=[]
cnn = mysql.connector.connect(**getdbconfig)
cursor = cnn.cursor()
cursor.execute(select_partition_sql)
for (table_schema, table_name, PARTITION_NAME, PARTITION_ORDINAL_POSITION, PARTITION_METHOD, PARTITION_EXPRESSION,PARTITION_DESCRIPTION) in cursor:
PARTITION_ORDINAL_POSITION_NUM = int(PARTITION_ORDINAL_POSITION) - 1
if PARTITION_DESCRIPTION == 'MAXVALUE':
select_paritition_description_sql = "SELECT PARTITION_NAME AS PARTITION_NAME_BEFORE,PARTITION_DESCRIPTION AS PARTITION_DESCRIPTION_BEFORE FROM information_schema.`PARTITIONS` " \
"WHERE table_schema='%s' and table_name='%s' and PARTITION_ORDINAL_POSITION='%s' ;" % (table_schema, table_name, PARTITION_ORDINAL_POSITION_NUM)
mysql_select_cnn = mysql.connector.connect(**getdbconfig)
mysql_select_cursor = mysql_select_cnn.cursor()
mysql_select_cursor.execute(select_paritition_description_sql)
for (PARTITION_NAME_BEFORE, PARTITION_DESCRIPTION_BEFORE) in mysql_select_cursor: partition_contain_before = (table_schema, table_name, PARTITION_NAME, PARTITION_NAME_BEFORE, PARTITION_METHOD,PARTITION_DESCRIPTION, PARTITION_DESCRIPTION_BEFORE)
partition_info.append(partition_contain_before)
mysql_select_cnn.close
if len(partition_info) == 0:
print "PARTITION in the Database does not exist!!!"
else:
for i in partition_info:
#print "database:", i[0]
if i[4] == 'RANGE':
# 此部分数据用于生成alter table drop 语句 alter table t_lgj_log add PARTITION(PARTITION p201804 VALUES LESS THAN (737150));
select_max_partition_name_sql = "select count(*) as COUNT from %s.%s partition(%s)" % (i[0], i[1], i[2])
max_partition_name_count = select_any_sql(getdbconfig, select_max_partition_name_sql)[0]
if max_partition_name_count==0:
prompt_database="-- database : %s" %(i[0])
print_log(prompt_database,i[0], "auto_create_partition", "sql")
print_user_database="use %s;" %(i[0])
print_log(print_user_database, i[0],"auto_create_partition", "sql")
sql_alter_table_drop_partition = "alter table %s.%s drop PARTITION %s;" % (i[0], i[1], i[2])
# auto_create_partition 指的是用于自动删除最大分区并添加分区的脚本
print_log(sql_alter_table_drop_partition,i[0], "auto_create_partition", "sql")
else:
return_info_can_not_drop_max_partition = "%s.%s.%s" % (i[0], i[1], i[2])
# can_not_drop_max_partition 指的是最大分区中有数据,然后这部分数据并不能直接删除,把这些分区信息记录下来
print_log(return_info_can_not_drop_max_partition, i[0],"can_not_drop_max_partition", "sql") # 此部分数据用于生成alter table add 语句 alter table t_lgj_log add PARTITION(PARTITION p201804 VALUES LESS THAN (737150));
select_next_paritition_description_name="select from_days(%s);" %(i[6])
maxvalue_date=add_month(select_any_sql(getdbconfig, select_next_paritition_description_name)[0],1).strftime('%Y-%m-%d')
maxvalue_date_year_month=add_month(select_any_sql(getdbconfig, select_next_paritition_description_name)[0],1).strftime('%Y%m')
add_partition_name_01="p%s" %(maxvalue_date_year_month)
# 此部分用于判断生成的 PARTITION_NAME 是否已经存在,如果存在,则需要在月份上加1
if add_partition_name_01 == i[3]:
add_partition_name=add_partition_name_01
else:
maxvalue_date_year_month_01=add_month(add_month(select_any_sql(getdbconfig, select_next_paritition_description_name)[0],1),1).strftime('%Y%m')
add_partition_name = "p%s" % (maxvalue_date_year_month_01)
less_than_sql="select to_days('%s')" %(maxvalue_date)
less_than_name=select_any_sql(getdbconfig,less_than_sql)[0]
sql_alter_table_add_partition="alter table %s.%s add PARTITION(PARTITION %s VALUES LESS THAN (%s));" %(i[0], i[1],add_partition_name,less_than_name)
print_log(sql_alter_table_add_partition, i[0],"auto_create_partition", "sql")
else:
# can_not_drop_max_partition 指的是分区规则并不是range分区,把这些分区信息记录下来
print_log(i, i[0],"partition_info_not_in_range", "sql")
# 记录保存的文件名到auto_create_partition_filename 列表
my_log_day = time.strftime("%Y%m%d")
partition_filename="%s_%s_%s.sql" %(i[0],"auto_create_partition",my_log_day)
auto_create_partition_filename.append(partition_filename)
cnn.close
# auto_create_partition_filename 列表记录了保存可以执行SQL文件名,如果列表不为空,直接调用 import_dump_sql 函数执行里面的SQL ,其中不同的库保存在不同的文件
if len(auto_create_partition_filename)>0:
for i in auto_create_partition_filename:
filename="%s%s" %(work_dir,i)
#import_dump_sql(getdbconfig,filename)
print "Done!!!", filename else:
print "PARTITION in the Database does not exist!!!" select_partition(getdbconfig_0_152)

  

auto_create_partition的更多相关文章

  1. MYSQL定时创建表分区

    MYSQL定时创建表分区 一.存储过程-表分区-----------------------------------------------------------------需求: 每月创建一个分区 ...

随机推荐

  1. actionBarTab-actionBarTab自定义 布局没法改变其中字体相对中间的位置

    我们经常遇到对actionBarTab 进行操作的情况.现在记录修改它的样式的方法,已经如何自定义tab的显示布局 1.在你的theme主题中添加<item name="android ...

  2. 3.JPA开发

    转自:https://blog.csdn.net/aspnetandjava/article/details/7034779 1. 什么是JPA 1. JPA(Java Persistence API ...

  3. POJ 1113 Wall 求凸包

    http://poj.org/problem?id=1113 不多说...凸包网上解法很多,这个是用graham的极角排序,也就是算导上的那个解法 其实其他方法随便乱搞都行...我只是测一下模板... ...

  4. SSO单点登录学习总结(2)——基于Cookie+fliter单点登录实例

    1.使用Cookie解决单点登录 技术点: 1.设置Cookie的路径为setPath("/").即Tomcat的目录下都有效 2.设置Cookie的域setDomain(&quo ...

  5. 洛谷 P1755 斐波那契的拆分

    P1755 斐波那契的拆分 题目背景 无 题目描述 已知任意一个正整数都可以拆分为若干个斐波纳契数,现在,让你求出n的拆分方法 输入输出格式 输入格式: 一个数t,表示有t组数据 接下来t行,每行一个 ...

  6. Node.js笔记(九)Nodejs与shell

    众所周知.Nodejs是单进程异步执行的.但不光是单进程,Nodejs也提供了多进程方面的支持 其创始人应该还是比較重视这一块的,最有力的证据就是child_process是Nodejs核心模块之中的 ...

  7. Apache中PHP5.3 php5.4如何使用ZEND

    Apache中PHP5.3 php5.4如何使用ZEND 有一套zend加密程序,需要安装ZEND,经过多次尝试,结果如下 由于PHP有安全线程(TS)和非安全线程(NTS)区分,PHP官方网站上说, ...

  8. Docker---(3)Docker常用命令

    原文:Docker---(3)Docker常用命令 版权声明:欢迎转载,请标明出处,如有问题,欢迎指正!谢谢!微信:w1186355422 https://blog.csdn.net/weixin_3 ...

  9. 【习题 6-9 UVA - 127】"Accordian" Patience

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 链表模拟即可. 1pile不能加s... [代码] #include <bits/stdc++.h> using nam ...

  10. nginx源代码分析--事件模块 &amp; 琐碎

    通过HUP信息使得NGINX实现又一次读取配置文件,使用USR2信号使得NGINX实现平滑升级. 在nginx中有模块这么一说,对外全部的模块都是ngx_module_t类型,这个结构体作为全部模块的 ...