microsoft / microsoft/sqlmanagementobjects

SMO, CREATE DATABASE: doesn't always replicate file/filegroups accurately.

Offen
#144 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
C#
Sterne
143
Forks
28
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

Imagine a database on server A, with several files: PRIMARY DATA, LOG, and additional DATA files belonging to a filegroup. Now imagine you have server B. You want to swap the DATA/LOG drives from server A, to server B, for example, to do a "quick" OS/SQL upgrade (obviously, in a virtual/cloud environment). So: you prepare a new server, flip the drives, and voila.

Before moving the volumes you need to pre-create the database on server B, such that when you flip volumes and boot the SQL Server B, the database is known, and goes ONLINE (yes, I am aware of other db-scoped attributes, login mappings, etc.), but bear with me. :-)

Then, you use SMO/Database to script the database create. It builds the CREATE DATABASE and adds all of the necessary files.

Then, you swap the volumes, boot server B -- and sometimes . . . . you get an error, something like:

2023-05-25 10:30:18.060 spid27s An unexpected file id was encountered. File id 3 was expected but 7 was read from "E:\MSSQL\DATA\blah_5_new.mdf". Verify that files are mapped correctly in sys.master_files. ALTER DATABASE can be used to correct the mappings.

What we found, is that SMO isn't "accurately" building the CREATE DATABASE statement. It adds all files into one statement. That is incorrect (in this case, at least). What it should be doing, is building CREATE DATABASE for the primary DATA and LOG files, and then using a combination of ALTER DATABASE {database} ADD FILE (or REMOVE FILE), to match the fileId values, and more importantly to introduce required "gaps" into the fileId values.

We wrote such code to solve this problem. Was wondering if SMO should be "fixed" too, if deemed a bug? Admittedly, if you're just moving random databases between machines this logic is necessary, but if you're moving ALL databases on a machine, then it comes into play.

Thanks!

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginnen Sie mit dem SMO Database-Scripting-Pfad, der CREATE DATABASE-Anweisungen erzeugt, und reproduzieren Sie dann den Fall mit Dateien der primären, der Protokoll- und zusätzlicher Dateigruppen, die Lücken in ihren fileId-Werten enthalten. Vergleichen Sie das generierte Skript mit den erforderlichen CREATE DATABASE- sowie ALTER DATABASE ADD FILE- oder REMOVE FILE-Operationen. Die Aufgabe ist abgeschlossen, wenn die geskriptete Datenbank die ursprünglichen Dateizuordnungen beibehält, nachdem die Volumes verschoben wurden und der Server startet.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
csharp, sql
Bereich
databases
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.