/** Drop order = reverse of schema create dependency order. */ export const TABLES = [ 'companyuser', 'syncpos', 'companysetting', 'posterminalcategory', 'posterminal', 'numbers', 'ledger', 'manifestarticle', 'manifest', 'price', 'articletext', 'categorytext', 'categorypic', 'categoryarticle', 'optionslistarticle', 'articlelistarticle', 'brandpic', 'articlepic', 'articleean', 'articlehistory', 'entityhistory', 'entityaddress', 'entitycontact', 'article', 'brand', 'articlelist', 'optionslist', 'unittext', 'unit', 'entity', 'customergroup', 'google_auth', 'company', 'pic', 'category', 'tax', 'language', 'meta', ]; export const PROCEDURES = [ 'getnextnumberwithprefixandpadding', 'peeknextnumberwithprefixandpadding', ]; export const VIEWS = ['v_entity_company_number']; /** Drop before tables — breaks company ↔ customergroup cycle for purge. */ export const CONSTRAINTS = [ `IF EXISTS (SELECT 1 FROM sys.foreign_keys WHERE name = N'FK_company_poscustomergroup') ALTER TABLE company DROP CONSTRAINT FK_company_poscustomergroup;`, ]; export function buildPurgeSql() { const drops = [ ...PROCEDURES.map( (name) => `IF OBJECT_ID(N'dbo.${name}', N'P') IS NOT NULL DROP PROCEDURE dbo.[${name}];`, ), ...VIEWS.map( (name) => `IF OBJECT_ID(N'dbo.${name}', N'V') IS NOT NULL DROP VIEW dbo.[${name}];`, ), ...CONSTRAINTS, ...TABLES.map( (name) => `IF OBJECT_ID(N'dbo.${name}', N'U') IS NOT NULL DROP TABLE dbo.[${name}];`, ), ]; return drops.join('\n'); }