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); }
protected bool GetDatabaseVersion(out int versionMajor, out int versionMinor, bool useBackupTable = false) { versionMajor = 0; versionMinor = 0; ISQLDatabase database = ServiceRegistration.Get <ISQLDatabase>(false); if (!database.TableExists(MediaPortal_Basis_Schema.DATABASE_VERSION_TABLE_NAME + (useBackupTable ? BACKUP_TABLE_SUFFIX : ""))) { return(false); } ITransaction transaction = database.BeginTransaction(); try { int versionIndex; IDbCommand command; if (useBackupTable) { command = SelectBackupDatabaseVersionByCommand(transaction, out versionIndex); } else { command = MediaPortal_Basis_Schema.SelectDatabaseVersionByCommand(transaction, out versionIndex); } using (command) using (IDataReader reader = command.ExecuteReader(CommandBehavior.SingleRow)) { if (reader.Read()) { if (Version.TryParse(reader.GetString(versionIndex), out Version version)) { versionMajor = version.Major; versionMinor = version.Minor; } return(true); } return(false); } } finally { transaction.Dispose(); } }
public void Startup() { ISQLDatabase database = ServiceRegistration.Get <ISQLDatabase>(false); if (database == null) { throw new IllegalCallException("There is no database present in the system"); } if (!_migrationScriptPlaceholders.ContainsKey(SQL_CONCAT_PLACEHOLDER)) { _migrationScriptPlaceholders.Add(SQL_CONCAT_PLACEHOLDER, database.ConcatOperator); } if (!_migrationScriptPlaceholders.ContainsKey(SQL_LEN_PLACEHOLDER)) { _migrationScriptPlaceholders.Add(SQL_LEN_PLACEHOLDER, database.LengthFunction); } // 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 (ITransaction transaction = database.BeginTransaction(IsolationLevel.Serializable)) { using (TextReader reader = new SqlScriptPreprocessor(MediaPortal_Basis_Schema.SubSchemaCreateScriptPath)) ExecuteBatch(transaction, new InstructionList(reader)); transaction.Commit(); } //A newly created database will be of the latest version SetDatabaseVersion(DATABASE_VERSION_MAJOR, DATABASE_VERSION_MINOR); } // 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); }
protected bool SetDatabaseVersion(int versionMajor, int versionMinor, bool useBackupTable = false) { Version newVersion = new Version(versionMajor, versionMinor); ISQLDatabase database = ServiceRegistration.Get <ISQLDatabase>(false); if (!database.TableExists(MediaPortal_Basis_Schema.DATABASE_VERSION_TABLE_NAME + (useBackupTable ? BACKUP_TABLE_SUFFIX : ""))) { return(false); } ITransaction transaction = database.BeginTransaction(); try { IDbCommand command; if (useBackupTable) { command = UpdateBackupDatabaseVersionCommand(transaction, newVersion.ToString(2)); } else { command = MediaPortal_Basis_Schema.UpdateDatabaseVersionCommand(transaction, newVersion.ToString(2)); } using (command) { command.ExecuteNonQuery(); } transaction.Commit(); return(true); } catch (Exception e) { ServiceRegistration.Get <ILogger>().Error("DatabaseManager: Error updating database version", e); transaction.Rollback(); throw; } }
protected DatabaseMigrationManager GetMiaMigrationManager(Guid miaId, string miaName) { if (_miaManagement == null) { _miaManagement = new MIA_Management(); } //Check if table for mia can be found var metaData = _miaManagement.GetMediaItemAspectMetadata(miaId); string tableName = _miaManagement.GetMIATableName(metaData); if (string.IsNullOrEmpty(tableName)) { return(null); } //Check if backup table exists ISQLDatabase database = ServiceRegistration.Get <ISQLDatabase>(false); if (!database.TableExists($"{tableName}{BACKUP_TABLE_SUFFIX}")) { return(null); } //Add main table IDictionary <string, IList <string> > defaultScriptPlaceholderTables = new Dictionary <string, IList <string> >(); defaultScriptPlaceholderTables.Add(MIA_TABLE_PLACEHOLDER, new List <string> { tableName }); //Add collection tables foreach (var attribute in metaData.AttributeSpecifications) { if (attribute.Value.IsCollectionAttribute) { if (attribute.Value.Cardinality == Common.MediaManagement.Cardinality.OneToMany || attribute.Value.Cardinality == Common.MediaManagement.Cardinality.ManyToMany) { tableName = _miaManagement.GetMIACollectionAttributeTableName(attribute.Value); if (string.IsNullOrEmpty(tableName)) { continue; } if (!database.TableExists($"{tableName}{BACKUP_TABLE_SUFFIX}")) { continue; } if (!defaultScriptPlaceholderTables.ContainsKey(MIA_V_TABLE_PLACEHOLDER)) { defaultScriptPlaceholderTables.Add(MIA_V_TABLE_PLACEHOLDER, new List <string>()); } defaultScriptPlaceholderTables[MIA_V_TABLE_PLACEHOLDER].Add(tableName); } if (attribute.Value.Cardinality == Common.MediaManagement.Cardinality.ManyToMany) { tableName = _miaManagement.GetMIACollectionAttributeNMTableName(attribute.Value); if (string.IsNullOrEmpty(tableName)) { continue; } if (!database.TableExists($"{tableName}{BACKUP_TABLE_SUFFIX}")) { continue; } if (!defaultScriptPlaceholderTables.ContainsKey(MIA_NM_TABLE_PLACEHOLDER)) { defaultScriptPlaceholderTables.Add(MIA_NM_TABLE_PLACEHOLDER, new List <string>()); } defaultScriptPlaceholderTables[MIA_NM_TABLE_PLACEHOLDER].Add(tableName); } } } DatabaseMigrationManager manager = new DatabaseMigrationManager(miaName, "DefaultAspect", defaultScriptPlaceholderTables); manager.AddDirectory(MediaPortal_Basis_Schema.DatabaseUpgradeScriptDirectory); return(manager); }