From 06a5070a9260fb0f40fce0c076fa48d1b45247f9 Mon Sep 17 00:00:00 2001 From: Scott Pouliot Date: Sat, 20 Jun 2026 18:09:40 -0400 Subject: [PATCH] Add idempotent SQL script for AddCompanyDefaultGlAccounts migration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Migrations are not auto-applied on startup (Program.cs:856 is commented), so dev/prod DBs need this applied manually. Idempotent (guarded by __EFMigrationsHistory checks) — safe to run regardless of current state. Adds CompanyPreferences.Default{Revenue,Cogs,Inventory}AccountId + indexes + FKs. Co-Authored-By: Claude Opus 4.8 --- scripts/sql/AddCompanyDefaultGlAccounts.sql | 130 ++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 scripts/sql/AddCompanyDefaultGlAccounts.sql diff --git a/scripts/sql/AddCompanyDefaultGlAccounts.sql b/scripts/sql/AddCompanyDefaultGlAccounts.sql new file mode 100644 index 0000000..8d77a56 --- /dev/null +++ b/scripts/sql/AddCompanyDefaultGlAccounts.sql @@ -0,0 +1,130 @@ +BEGIN TRANSACTION; +GO + +IF NOT EXISTS ( + SELECT * FROM [__EFMigrationsHistory] + WHERE [MigrationId] = N'20260620134918_AddCompanyDefaultGlAccounts' +) +BEGIN + ALTER TABLE [CompanyPreferences] ADD [DefaultCogsAccountId] int NULL; +END; +GO + +IF NOT EXISTS ( + SELECT * FROM [__EFMigrationsHistory] + WHERE [MigrationId] = N'20260620134918_AddCompanyDefaultGlAccounts' +) +BEGIN + ALTER TABLE [CompanyPreferences] ADD [DefaultInventoryAccountId] int NULL; +END; +GO + +IF NOT EXISTS ( + SELECT * FROM [__EFMigrationsHistory] + WHERE [MigrationId] = N'20260620134918_AddCompanyDefaultGlAccounts' +) +BEGIN + ALTER TABLE [CompanyPreferences] ADD [DefaultRevenueAccountId] int NULL; +END; +GO + +IF NOT EXISTS ( + SELECT * FROM [__EFMigrationsHistory] + WHERE [MigrationId] = N'20260620134918_AddCompanyDefaultGlAccounts' +) +BEGIN + EXEC(N'UPDATE [PricingTiers] SET [CreatedAt] = ''2026-06-20T13:49:14.5644507Z'' + WHERE [Id] = 1; + SELECT @@ROWCOUNT'); +END; +GO + +IF NOT EXISTS ( + SELECT * FROM [__EFMigrationsHistory] + WHERE [MigrationId] = N'20260620134918_AddCompanyDefaultGlAccounts' +) +BEGIN + EXEC(N'UPDATE [PricingTiers] SET [CreatedAt] = ''2026-06-20T13:49:14.5644514Z'' + WHERE [Id] = 2; + SELECT @@ROWCOUNT'); +END; +GO + +IF NOT EXISTS ( + SELECT * FROM [__EFMigrationsHistory] + WHERE [MigrationId] = N'20260620134918_AddCompanyDefaultGlAccounts' +) +BEGIN + EXEC(N'UPDATE [PricingTiers] SET [CreatedAt] = ''2026-06-20T13:49:14.5644515Z'' + WHERE [Id] = 3; + SELECT @@ROWCOUNT'); +END; +GO + +IF NOT EXISTS ( + SELECT * FROM [__EFMigrationsHistory] + WHERE [MigrationId] = N'20260620134918_AddCompanyDefaultGlAccounts' +) +BEGIN + CREATE INDEX [IX_CompanyPreferences_DefaultCogsAccountId] ON [CompanyPreferences] ([DefaultCogsAccountId]); +END; +GO + +IF NOT EXISTS ( + SELECT * FROM [__EFMigrationsHistory] + WHERE [MigrationId] = N'20260620134918_AddCompanyDefaultGlAccounts' +) +BEGIN + CREATE INDEX [IX_CompanyPreferences_DefaultInventoryAccountId] ON [CompanyPreferences] ([DefaultInventoryAccountId]); +END; +GO + +IF NOT EXISTS ( + SELECT * FROM [__EFMigrationsHistory] + WHERE [MigrationId] = N'20260620134918_AddCompanyDefaultGlAccounts' +) +BEGIN + CREATE INDEX [IX_CompanyPreferences_DefaultRevenueAccountId] ON [CompanyPreferences] ([DefaultRevenueAccountId]); +END; +GO + +IF NOT EXISTS ( + SELECT * FROM [__EFMigrationsHistory] + WHERE [MigrationId] = N'20260620134918_AddCompanyDefaultGlAccounts' +) +BEGIN + ALTER TABLE [CompanyPreferences] ADD CONSTRAINT [FK_CompanyPreferences_Accounts_DefaultCogsAccountId] FOREIGN KEY ([DefaultCogsAccountId]) REFERENCES [Accounts] ([Id]); +END; +GO + +IF NOT EXISTS ( + SELECT * FROM [__EFMigrationsHistory] + WHERE [MigrationId] = N'20260620134918_AddCompanyDefaultGlAccounts' +) +BEGIN + ALTER TABLE [CompanyPreferences] ADD CONSTRAINT [FK_CompanyPreferences_Accounts_DefaultInventoryAccountId] FOREIGN KEY ([DefaultInventoryAccountId]) REFERENCES [Accounts] ([Id]); +END; +GO + +IF NOT EXISTS ( + SELECT * FROM [__EFMigrationsHistory] + WHERE [MigrationId] = N'20260620134918_AddCompanyDefaultGlAccounts' +) +BEGIN + ALTER TABLE [CompanyPreferences] ADD CONSTRAINT [FK_CompanyPreferences_Accounts_DefaultRevenueAccountId] FOREIGN KEY ([DefaultRevenueAccountId]) REFERENCES [Accounts] ([Id]); +END; +GO + +IF NOT EXISTS ( + SELECT * FROM [__EFMigrationsHistory] + WHERE [MigrationId] = N'20260620134918_AddCompanyDefaultGlAccounts' +) +BEGIN + INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion]) + VALUES (N'20260620134918_AddCompanyDefaultGlAccounts', N'8.0.11'); +END; +GO + +COMMIT; +GO +