/* Vote & Reward — Nexora Edition. Idempotent migration for existing WEBENGINE vote tables. */
IF COL_LENGTH('dbo.WEBENGINE_VOTE_SITES','realm') IS NULL ALTER TABLE dbo.WEBENGINE_VOTE_SITES ADD realm varchar(20) NOT NULL CONSTRAINT DF_WEBENGINE_VOTE_SITES_realm DEFAULT('legacy') WITH VALUES;
IF COL_LENGTH('dbo.WEBENGINE_VOTE_SITES','enabled') IS NULL ALTER TABLE dbo.WEBENGINE_VOTE_SITES ADD enabled bit NOT NULL CONSTRAINT DF_WEBENGINE_VOTE_SITES_enabled DEFAULT(1) WITH VALUES;
IF COL_LENGTH('dbo.WEBENGINE_VOTES','realm') IS NULL ALTER TABLE dbo.WEBENGINE_VOTES ADD realm varchar(20) NOT NULL CONSTRAINT DF_WEBENGINE_VOTES_realm DEFAULT('legacy') WITH VALUES;
IF COL_LENGTH('dbo.WEBENGINE_VOTE_LOGS','realm') IS NULL ALTER TABLE dbo.WEBENGINE_VOTE_LOGS ADD realm varchar(20) NOT NULL CONSTRAINT DF_WEBENGINE_VOTE_LOGS_realm DEFAULT('legacy') WITH VALUES;
IF COL_LENGTH('dbo.WEBENGINE_VOTE_LOGS','credits_reward') IS NULL ALTER TABLE dbo.WEBENGINE_VOTE_LOGS ADD credits_reward int NULL;
IF COL_LENGTH('dbo.WEBENGINE_VOTE_LOGS','user_ip') IS NULL ALTER TABLE dbo.WEBENGINE_VOTE_LOGS ADD user_ip varchar(50) NULL;
IF COL_LENGTH('dbo.WEBENGINE_VOTE_LOGS','account_username') IS NULL ALTER TABLE dbo.WEBENGINE_VOTE_LOGS ADD account_username varchar(50) NULL;
IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name='IX_WEBENGINE_VOTES_realm_user_site' AND object_id=OBJECT_ID('dbo.WEBENGINE_VOTES')) CREATE NONCLUSTERED INDEX IX_WEBENGINE_VOTES_realm_user_site ON dbo.WEBENGINE_VOTES(realm,user_id,vote_site_id);
IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name='IX_WEBENGINE_VOTES_realm_ip_site' AND object_id=OBJECT_ID('dbo.WEBENGINE_VOTES')) CREATE NONCLUSTERED INDEX IX_WEBENGINE_VOTES_realm_ip_site ON dbo.WEBENGINE_VOTES(realm,user_ip,vote_site_id);
