46 lines
3.2 KiB
Python
46 lines
3.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Emit a reviewable migration for explicitly named isolated or production schemas.
|
|
Run mysql separately after backup. Native products are disabled until compatibility review.
|
|
"""
|
|
import argparse,re
|
|
from pathlib import Path
|
|
p=argparse.ArgumentParser();p.add_argument('--player',required=True);p.add_argument('--account',required=True);p.add_argument('--output',type=Path,required=True);a=p.parse_args()
|
|
for name in (a.player,a.account):
|
|
if not re.fullmatch('[A-Za-z0-9_]+',name):p.error('invalid schema')
|
|
sql=f'''USE `{a.player}`;
|
|
CREATE TABLE IF NOT EXISTS ishop_data (
|
|
id INT UNSIGNED PRIMARY KEY, categoryType INT NOT NULL, categorySubType INT NOT NULL,
|
|
itemVnum INT UNSIGNED NOT NULL,itemPrice BIGINT UNSIGNED NOT NULL,discount INT NOT NULL DEFAULT 0,
|
|
offerTime DATETIME NULL,addedTime DATETIME NULL,sellCount BIGINT UNSIGNED NOT NULL DEFAULT 0,
|
|
week_limit INT NOT NULL DEFAULT 0,month_limit INT NOT NULL DEFAULT 0,maxSellCount INT NOT NULL DEFAULT -1,
|
|
enabled TINYINT NOT NULL DEFAULT 0
|
|
) ENGINE=InnoDB;
|
|
CREATE TABLE IF NOT EXISTS port_itemshop_orders (
|
|
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,account_id INT UNSIGNED NOT NULL,
|
|
token CHAR(32) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,product INT UNSIGNED NOT NULL,vnum INT UNSIGNED NOT NULL,
|
|
quantity INT UNSIGNED NOT NULL,paid BIGINT UNSIGNED NOT NULL,player_name VARCHAR(24) NOT NULL,ip VARCHAR(45) NOT NULL,
|
|
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
UNIQUE KEY request_once(account_id,token), KEY limits_by_account(account_id,product,created_at)
|
|
) ENGINE=InnoDB;
|
|
ALTER TABLE ishop_data ENGINE=InnoDB;
|
|
ALTER TABLE item_award ENGINE=InnoDB;
|
|
ALTER TABLE `{a.account}`.account ENGINE=InnoDB;
|
|
'''
|
|
# Upgrade an existing native catalogue without replacing its products or sales history.
|
|
sql+="""SET @enabled_exists=(SELECT COUNT(*) FROM information_schema.columns WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='ishop_data' AND COLUMN_NAME='enabled');
|
|
SET @ddl=IF(@enabled_exists=0,'ALTER TABLE ishop_data ADD enabled TINYINT NOT NULL DEFAULT 0','SELECT 1');
|
|
PREPARE shop_ddl FROM @ddl; EXECUTE shop_ddl; DEALLOCATE PREPARE shop_ddl;
|
|
"""
|
|
# Coins are added by a separate conditional step, since MySQL 5.6 lacks ADD IF NOT EXISTS.
|
|
sql+=f'''SET @column_exists=(SELECT COUNT(*) FROM information_schema.columns WHERE TABLE_SCHEMA='{a.account}' AND TABLE_NAME='account' AND COLUMN_NAME='coins');
|
|
SET @ddl=IF(@column_exists=0,'ALTER TABLE `{a.account}`.account ADD coins BIGINT UNSIGNED NOT NULL DEFAULT 0','SELECT 1');
|
|
PREPARE shop_ddl FROM @ddl; EXECUTE shop_ddl; DEALLOCATE PREPARE shop_ddl;
|
|
'''
|
|
source=Path(__file__).resolve().parents[2]/'data/ava2-itemshop/original-products.tsv'
|
|
for line in source.read_text().splitlines():
|
|
row=line.split('\t');assert len(row)==12
|
|
values=[str(int(v)) if i not in (6,7) else ('NULL' if v.startswith('0000') else "'"+v+"'") for i,v in enumerate(row)]
|
|
sql+='INSERT IGNORE INTO ishop_data(id,categoryType,categorySubType,itemVnum,itemPrice,discount,offerTime,addedTime,sellCount,week_limit,month_limit,maxSellCount) VALUES('+','.join(values)+');\n'
|
|
a.output.parent.mkdir(parents=True,exist_ok=True);a.output.write_text(sql)
|
|
print('217 native products imported disabled; review compatibility before enabling')
|