Ejemplo n.º 1
0
 public void Startup()
 {
   ISQLDatabase database = ServiceRegistration.Get<ISQLDatabase>(false);
   if (database == null)
     throw new IllegalCallException("There is no database present in the system");
   // Prepare schema
   if (!database.TableExists(MediaPortal_Basis_Schema.MEDIAPORTAL_BASIS_TABLE_NAME))
   {
     ServiceRegistration.Get<ILogger>().Info("DatabaseManager: Creating subschema '{0}'", MediaPortal_Basis_Schema.SUBSCHEMA_NAME);
     using (TextReader reader = new SqlScriptPreprocessor(MediaPortal_Basis_Schema.SubSchemaCreateScriptPath))
       ExecuteBatch(database, new InstructionList(reader));
   }
   // Hint: Table MEDIAPORTAL_BASIS contains a sub schema entry for "MEDIAPORTAL_BASIS" with version number 1.0
   int versionMajor;
   int versionMinor;
   if (!GetSubSchemaVersion(MediaPortal_Basis_Schema.SUBSCHEMA_NAME, out versionMajor, out versionMinor))
     throw new UnexpectedStateException("{0} schema is not present or corrupted", MediaPortal_Basis_Schema.SUBSCHEMA_NAME);
   ServiceRegistration.Get<ILogger>().Info("DatabaseManager: Subschema '{0}' present in version {1}.{2}",
       MediaPortal_Basis_Schema.SUBSCHEMA_NAME, versionMajor, versionMinor);
 }
Ejemplo n.º 2
0
 public void DeleteSubSchema(string subSchemaName, int currentVersionMajor, int currentVersionMinor, string deleteScriptFilePath)
 {
   ISQLDatabase database = ServiceRegistration.Get<ISQLDatabase>(false);
   int versionMajor;
   int versionMinor;
   bool schemaPresent = GetSubSchemaVersion(subSchemaName, out versionMajor, out versionMinor);
   if (!schemaPresent)
     return;
   if (currentVersionMajor == versionMajor && currentVersionMinor == versionMinor)
     using (TextReader reader = new SqlScriptPreprocessor(deleteScriptFilePath))
       ExecuteBatch(database, new InstructionList(reader));
   else
     throw new ArgumentException(string.Format("The current version of sub schema '{0}' is {1}.{2}, but the schema deletion script works for version {3}.{4}",
         subSchemaName, versionMajor, versionMinor, currentVersionMajor, currentVersionMajor));
   ITransaction transaction = database.BeginTransaction();
   try
   {
     using (IDbCommand command = MediaPortal_Basis_Schema.DeleteSubSchemaCommand(transaction, subSchemaName))
       command.ExecuteNonQuery();
   }
   finally
   {
     transaction.Dispose();
   }
 }
Ejemplo n.º 3
0
 public bool UpdateSubSchema(string subSchemaName, int? currentVersionMajor, int? currentVersionMinor,
     string updateScriptFilePath, int newVersionMajor, int newVersionMinor)
 {
   ISQLDatabase database = ServiceRegistration.Get<ISQLDatabase>(false);
   int versionMajor;
   int versionMinor;
   bool schemaPresent = GetSubSchemaVersion(subSchemaName, out versionMajor, out versionMinor);
   if (schemaPresent)
     if (currentVersionMajor.HasValue && currentVersionMajor.Value == versionMajor &&
         currentVersionMinor.HasValue && currentVersionMinor.Value == versionMinor)
     {
       ServiceRegistration.Get<ILogger>().Debug("DatabaseManager: Updating subschema '{0}' from version {1}.{2} to version {3}.{4}...",
           subSchemaName, versionMajor, versionMinor, newVersionMajor, newVersionMinor);
       using (TextReader reader = new SqlScriptPreprocessor(updateScriptFilePath))
         ExecuteBatch(database, new InstructionList(reader));
     }
     else
       throw new ArgumentException(string.Format(
           "The current version of sub schema '{0}' is {1}.{2}, but the schema update script is given for version {3}",
           subSchemaName, versionMajor, versionMinor, ExplicitVersionToString(currentVersionMajor, currentVersionMinor)));
   else // !schemaPresent
     if (!currentVersionMajor.HasValue && !currentVersionMinor.HasValue)
     {
       ServiceRegistration.Get<ILogger>().Debug("DatabaseManager: Creating subschema '{0}' version {1}.{2}...",
           subSchemaName, newVersionMajor, newVersionMinor);
       using (TextReader reader = new SqlScriptPreprocessor(updateScriptFilePath))
         ExecuteBatch(database, new InstructionList(reader));
     }
     else
       throw new ArgumentException(string.Format(
           "The sub schema '{0}' is not present yet, but the schema update script is given for version {1}",
           subSchemaName, ExplicitVersionToString(currentVersionMajor, currentVersionMinor)));
   ITransaction transaction = database.BeginTransaction();
   try
   {
     IDbCommand command;
     if (schemaPresent)
       command = MediaPortal_Basis_Schema.UpdateSubSchemaCommand(
           transaction, subSchemaName, newVersionMajor, newVersionMinor);
     else
       command = MediaPortal_Basis_Schema.InsertSubSchemaCommand(
           transaction, subSchemaName, newVersionMajor, newVersionMinor);
     try
     {
       command.ExecuteNonQuery();
     }
     finally
     {
       command.Dispose();
     }
     transaction.Commit();
     ServiceRegistration.Get<ILogger>().Info("DatabaseManager: Subschema '{0}' present in version {1}.{2}", subSchemaName,
         newVersionMajor, newVersionMinor);
     return true;
   }
   catch (Exception e)
   {
     ServiceRegistration.Get<ILogger>().Error("DatabaseManager: Error updating subschema '{0}'", e, subSchemaName);
     transaction.Rollback();
     throw;
   }
 }