fix world
This commit is contained in:
43
db.py
43
db.py
@@ -218,25 +218,58 @@ def init_db():
|
||||
)
|
||||
''')
|
||||
|
||||
# Clan members — links Grepolis player_ids to a clan
|
||||
# Clan members — links Grepolis player_ids to a clan.
|
||||
# UNIQUE on (clan_id, player_id, world_id) so the same player
|
||||
# appearing in multiple worlds creates separate rows.
|
||||
c.execute('''
|
||||
CREATE TABLE IF NOT EXISTS clan_members (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
clan_id INTEGER NOT NULL REFERENCES clans(id),
|
||||
player_id TEXT NOT NULL,
|
||||
player_name TEXT,
|
||||
world_id TEXT,
|
||||
world_id TEXT NOT NULL DEFAULT '',
|
||||
features TEXT NOT NULL DEFAULT 'farm,admin',
|
||||
joined_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
UNIQUE(clan_id, player_id)
|
||||
UNIQUE(clan_id, player_id, world_id)
|
||||
)
|
||||
''')
|
||||
|
||||
# Migration: if clan_members still has the old UNIQUE(clan_id, player_id) constraint
|
||||
# (without world_id), recreate the table with the correct 3-column constraint.
|
||||
try:
|
||||
tbl_sql = c.execute(
|
||||
"SELECT sql FROM sqlite_master WHERE type='table' AND name='clan_members'"
|
||||
).fetchone()
|
||||
if tbl_sql and 'player_id, world_id' not in (tbl_sql['sql'] or ''):
|
||||
c.execute('''
|
||||
CREATE TABLE IF NOT EXISTS _clan_members_new (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
clan_id INTEGER NOT NULL REFERENCES clans(id),
|
||||
player_id TEXT NOT NULL,
|
||||
player_name TEXT,
|
||||
world_id TEXT NOT NULL DEFAULT '',
|
||||
features TEXT NOT NULL DEFAULT 'farm,admin',
|
||||
joined_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
UNIQUE(clan_id, player_id, world_id)
|
||||
)
|
||||
''')
|
||||
c.execute('''
|
||||
INSERT OR IGNORE INTO _clan_members_new
|
||||
(id, clan_id, player_id, player_name, world_id, features, joined_at)
|
||||
SELECT id, clan_id, player_id, player_name,
|
||||
COALESCE(world_id, ''), features, joined_at
|
||||
FROM clan_members
|
||||
''')
|
||||
c.execute('DROP TABLE clan_members')
|
||||
c.execute('ALTER TABLE _clan_members_new RENAME TO clan_members')
|
||||
except Exception as _e:
|
||||
print(f'clan_members migration skipped: {_e}')
|
||||
|
||||
# Migration: Auto-assign existing users to their clan_id if they are the owner
|
||||
try:
|
||||
c.execute('''
|
||||
UPDATE users
|
||||
SET clan_id = (SELECT id FROM clans WHERE owner_id = users.id)
|
||||
UPDATE users
|
||||
SET clan_id = (SELECT id FROM clans WHERE owner_id = users.id)
|
||||
WHERE clan_id IS NULL AND EXISTS (SELECT 1 FROM clans WHERE owner_id = users.id)
|
||||
''')
|
||||
except Exception:
|
||||
|
||||
Reference in New Issue
Block a user