/** Drop order = reverse of schema create dependency order. */ export const TABLES = [ 'deliverysummary', 'shipmentsummary', 'quotesummary', 'ordersummary', 'package', 'shipping', 'payment', 'account', 'articleattributevalue', 'articleproperty', 'attributevaluepic', 'attributevalue', 'attributename', 'propertyname', 'manifestattachment', 'labeltemplate', 'file', 'modelmessage', 'modelconversation', 'companyuser', 'companysetting', 'posterminalcategory', 'posterminal', 'printer', 'numbers', 'ledger', 'warehouse', 'articlesupplier', 'manifestarticle', 'manifest', 'price', 'articletext', 'categorytext', 'categorypic', 'categoryarticle', 'optionslistarticle', 'articlelistarticle', 'brandpic', 'articlepic', 'articleean', 'articlehistory', 'entityhistory', 'companyhistory', '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 — drops all foreign keys so all tables can be purged safely. */ export const CONSTRAINTS = [ `DECLARE @dropFkSql NVARCHAR(MAX) = N''; SELECT @dropFkSql += N'ALTER TABLE ' + QUOTENAME(OBJECT_SCHEMA_NAME(parent_object_id)) + N'.' + QUOTENAME(OBJECT_NAME(parent_object_id)) + N' DROP CONSTRAINT ' + QUOTENAME(name) + N';' FROM sys.foreign_keys; IF @dropFkSql <> N'' EXEC sp_executesql @dropFkSql;`, ]; 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'); }