[longomatch] Add a migration service for DB and templates



commit 54749ec9d31a37ab954260a9d3f1faeb863561f6
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date:   Thu Jul 4 19:50:33 2013 +0200

    Add a migration service for DB and templates

 LongoMatch.Core/Common/Constants.cs                |    3 +
 LongoMatch.Core/Config.cs                          |    1 +
 LongoMatch.Core/Interfaces/IDatabase.cs            |    2 +
 .../Store/Templates/CategoriesTemplate.cs          |    7 +
 .../Store/Templates/SubCategoryTemplate.cs         |   15 ++-
 LongoMatch.Core/Store/Templates/TeamTemplate.cs    |    7 +
 LongoMatch.Services/LongoMatch.Services.mdp        |    1 +
 LongoMatch.Services/Makefile.am                    |    1 +
 LongoMatch.Services/Services/Core.cs               |    4 +
 LongoMatch.Services/Services/DataBase.cs           |   26 +++-
 LongoMatch.Services/Services/DataBaseManager.cs    |    5 +-
 LongoMatch.Services/Services/MigrationsManager.cs  |  158 ++++++++++++++++++++
 12 files changed, 220 insertions(+), 10 deletions(-)
---
diff --git a/LongoMatch.Core/Common/Constants.cs b/LongoMatch.Core/Common/Constants.cs
index a434132..7de7a61 100644
--- a/LongoMatch.Core/Common/Constants.cs
+++ b/LongoMatch.Core/Common/Constants.cs
@@ -103,5 +103,8 @@ Xavier Queralt Mateu (ca)";
                public const string FIELD_BACKGROUND = "field_background.svg";
                public const string HALF_FIELD_BACKGROUND = "half_field_background.svg";
                public const string GOAL_BACKGROUND = "goal_background.svg";
+               
+               public const int DB_MAYOR_VERSION = 2;
+               public const int DB_MINOR_VERSION = 1;
        }
 }
diff --git a/LongoMatch.Core/Config.cs b/LongoMatch.Core/Config.cs
index 5d649ad..4b12d01 100644
--- a/LongoMatch.Core/Config.cs
+++ b/LongoMatch.Core/Config.cs
@@ -322,6 +322,7 @@ namespace LongoMatch
                                Save ();
                        }
                }
+               
                #endregion
 
        }
diff --git a/LongoMatch.Core/Interfaces/IDatabase.cs b/LongoMatch.Core/Interfaces/IDatabase.cs
index 5307748..e02c0f0 100644
--- a/LongoMatch.Core/Interfaces/IDatabase.cs
+++ b/LongoMatch.Core/Interfaces/IDatabase.cs
@@ -44,6 +44,8 @@ namespace LongoMatch.Interfaces
                DateTime LastBackup {get;}
                
                int Count {get;}
+               
+               Version Version {get; set;}
        }
 }
 
diff --git a/LongoMatch.Core/Store/Templates/CategoriesTemplate.cs 
b/LongoMatch.Core/Store/Templates/CategoriesTemplate.cs
index bebc3da..76b4fbc 100644
--- a/LongoMatch.Core/Store/Templates/CategoriesTemplate.cs
+++ b/LongoMatch.Core/Store/Templates/CategoriesTemplate.cs
@@ -44,6 +44,7 @@ namespace LongoMatch.Store.Templates
        {
                /* Database additions */
                GameUnitsList gameUnits;
+               Version version;
 
                /// <summary>
                /// Creates a new template
@@ -56,6 +57,11 @@ namespace LongoMatch.Store.Templates
                        set;
                }
                
+               public Version Version {
+                       get;
+                       set;
+               }
+               
                public GameUnitsList GameUnits {
                        set {
                                gameUnits = value;
@@ -141,6 +147,7 @@ namespace LongoMatch.Store.Templates
                        periods.Add ("1");
                        periods.Add ("2");
                        defaultTemplate.GamePeriods = periods; 
+                       defaultTemplate.Version = new Version (Constants.DB_MAYOR_VERSION, 
Constants.DB_MINOR_VERSION);
                        return defaultTemplate;
                }
 
diff --git a/LongoMatch.Core/Store/Templates/SubCategoryTemplate.cs 
b/LongoMatch.Core/Store/Templates/SubCategoryTemplate.cs
index 0d007d5..4cabc20 100644
--- a/LongoMatch.Core/Store/Templates/SubCategoryTemplate.cs
+++ b/LongoMatch.Core/Store/Templates/SubCategoryTemplate.cs
@@ -29,10 +29,15 @@ namespace LongoMatch.Store.Templates
        public class SubCategoryTemplate: TagSubCategory, ITemplate<string>
        {
 
-               public SubCategoryTemplate() {}
-
-               public SubCategoryTemplate(IEnumerable<string> tags): base (tags) {}
+               public Version version;
                
+               public SubCategoryTemplate() {
+               }
+
+               public Version Version {
+                       get;
+                       set;
+               }
                public void AddDefaultItem (int index) {
                        throw new Exception("Not implemented yet");
                }
@@ -46,7 +51,9 @@ namespace LongoMatch.Store.Templates
                }
                
                public static SubCategoryTemplate DefaultTemplate (int not_used) {
-                       return new SubCategoryTemplate();
+                       SubCategoryTemplate template = new SubCategoryTemplate();
+                       template.Version = new Version (Constants.DB_MAYOR_VERSION, 
Constants.DB_MINOR_VERSION);
+                       return template;
                }
        }
 }
diff --git a/LongoMatch.Core/Store/Templates/TeamTemplate.cs b/LongoMatch.Core/Store/Templates/TeamTemplate.cs
index 6b0b9e7..42fcf29 100644
--- a/LongoMatch.Core/Store/Templates/TeamTemplate.cs
+++ b/LongoMatch.Core/Store/Templates/TeamTemplate.cs
@@ -33,6 +33,7 @@ namespace LongoMatch.Store.Templates
                private byte[] thumbnailBuf;
                private const int MAX_WIDTH=100;
                private const int MAX_HEIGHT=100;
+               Version version;
                
                public TeamTemplate() {
                        TeamName = Catalog.GetString("Team");
@@ -48,6 +49,11 @@ namespace LongoMatch.Store.Templates
                        set;
                }
                
+               public Version Version {
+                       get;
+                       set;
+               }
+               
                public Image Shield {
                        get {
                                if(thumbnailBuf != null)
@@ -94,6 +100,7 @@ namespace LongoMatch.Store.Templates
                        Clear();
                        for(int i=1; i<=playersCount; i++)
                                AddDefaultItem(i-1);
+                       version = new Version (Constants.DB_MAYOR_VERSION, Constants.DB_MINOR_VERSION);
                }
        }
 }
diff --git a/LongoMatch.Services/LongoMatch.Services.mdp b/LongoMatch.Services/LongoMatch.Services.mdp
index 0493736..aa92697 100644
--- a/LongoMatch.Services/LongoMatch.Services.mdp
+++ b/LongoMatch.Services/LongoMatch.Services.mdp
@@ -30,6 +30,7 @@
     <File subtype="Directory" buildaction="Compile" name=".." />
     <File subtype="Directory" buildaction="Compile" name="Services" />
     <File subtype="Code" buildaction="Compile" name="Services/DataBaseManager.cs" />
+    <File subtype="Code" buildaction="Compile" name="Services/MigrationsManager.cs" />
   </Contents>
   <References>
     <ProjectReference type="Gac" localcopy="True" refto="System, Version=4.0.0.0, Culture=neutral, 
PublicKeyToken=b77a5c561934e089" />
diff --git a/LongoMatch.Services/Makefile.am b/LongoMatch.Services/Makefile.am
index 23fcfb5..6712995 100644
--- a/LongoMatch.Services/Makefile.am
+++ b/LongoMatch.Services/Makefile.am
@@ -10,6 +10,7 @@ SOURCES = \
        Services/EventsManager.cs \
        Services/GameUnitsManager.cs \
        Services/HotKeysManager.cs \
+       Services/MigrationsManager.cs \
        Services/PlaylistManager.cs \
        Services/ProjectsManager.cs \
        Services/TemplatesService.cs \
diff --git a/LongoMatch.Services/Services/Core.cs b/LongoMatch.Services/Services/Core.cs
index 36a9421..0ba65a2 100644
--- a/LongoMatch.Services/Services/Core.cs
+++ b/LongoMatch.Services/Services/Core.cs
@@ -82,6 +82,10 @@ namespace LongoMatch.Services
                        dbManager = new DataBaseManager (Config.DBDir, guiToolkit);
                        dbManager.SetActiveByName (Config.CurrentDatabase);
                        
+                       /* Start Migration */
+                       MigrationsManager migration = new MigrationsManager(ts, dbManager);
+                       migration.StartMigration();
+                       
                        /* Start the rendering jobs manager */
                        videoRenderer = new RenderingJobsManager(multimediaToolkit, guiToolkit);
                        
diff --git a/LongoMatch.Services/Services/DataBase.cs b/LongoMatch.Services/Services/DataBase.cs
index cc0608c..e56fd6c 100644
--- a/LongoMatch.Services/Services/DataBase.cs
+++ b/LongoMatch.Services/Services/DataBase.cs
@@ -47,8 +47,6 @@ namespace LongoMatch.DB
                Version dbVersion;
                BackupDate lastBackup;
                int count;
-               const int MAYOR=2;
-               const int MINOR=0;
                TimeSpan maxDaysWithoutBackup = new TimeSpan(5, 0, 0, 0);
                
                
@@ -73,6 +71,9 @@ namespace LongoMatch.DB
                        get {
                                return dbVersion;
                        }
+                       set {
+                               UpdateVersion (value);
+                       }
                }
                
                public string Name {
@@ -347,7 +348,7 @@ namespace LongoMatch.DB
                        // Create new DB and add version and last backup date
                        IObjectContainer db = Db4oFactory.OpenFile(DBFile);
                        try {
-                               dbVersion= new Version(MAYOR,MINOR);
+                               dbVersion= new Version(Constants.DB_MAYOR_VERSION, 
Constants.DB_MINOR_VERSION);
                                lastBackup = new BackupDate { Date = DateTime.UtcNow};
                                db.Store(dbVersion);
                                db.Store(lastBackup);
@@ -361,7 +362,7 @@ namespace LongoMatch.DB
                private void GetDBVersion () {
                        dbVersion = GetObject<Version>();
                        if (dbVersion == null)
-                               dbVersion = new Version(MAYOR, MINOR);
+                               dbVersion = new Version(Constants.DB_MAYOR_VERSION, 
Constants.DB_MINOR_VERSION);
                        Log.Information("DB version: "+ dbVersion.ToString());
                }
                
@@ -390,6 +391,23 @@ namespace LongoMatch.DB
                        }
                }
                
+               private void UpdateVersion (Version version) {
+                       IObjectContainer db = Db4oFactory.OpenFile(DBFile);
+                       try     {
+                               IQuery query = db.Query();
+                               query.Constrain(typeof(Version));
+                               IObjectSet result = query.Execute();
+                               while (result.HasNext()) {
+                                       Version v = result.Next() as Version;
+                                       db.Delete (v);
+                               }
+                               db.Store(version);
+                               dbVersion = version;
+                       } finally {
+                               db.Close();
+                       }
+               }
+               
                private T GetObject<T>() {
                        T ret = default(T);
                        IObjectContainer db = Db4oFactory.OpenFile(DBFile);
diff --git a/LongoMatch.Services/Services/DataBaseManager.cs b/LongoMatch.Services/Services/DataBaseManager.cs
index 31ee9cf..389fbc6 100644
--- a/LongoMatch.Services/Services/DataBaseManager.cs
+++ b/LongoMatch.Services/Services/DataBaseManager.cs
@@ -24,6 +24,7 @@ using Mono.Unix;
 using LongoMatch.Interfaces;
 using LongoMatch.Interfaces.GUI;
 using LongoMatch.Store;
+using LongoMatch.Common;
 
 namespace LongoMatch.DB
 {
@@ -32,7 +33,7 @@ namespace LongoMatch.DB
                string DBDir;
                IGUIToolkit guiToolkit;
                IDatabase activeDB;
-               const int SUPPORTED_MAJOR_VERSION = 2;
+               const int SUPPORTED_MAJOR_VERSION = Constants.DB_MAYOR_VERSION;
                
                public DataBaseManager (string DBDir, IGUIToolkit guiToolkit)
                {
@@ -106,7 +107,7 @@ namespace LongoMatch.DB
                
                string Extension {
                        get {
-                               return SUPPORTED_MAJOR_VERSION -1 + ".db";
+                               return SUPPORTED_MAJOR_VERSION - 1 + ".db";
                        }
                }
 
diff --git a/LongoMatch.Services/Services/MigrationsManager.cs 
b/LongoMatch.Services/Services/MigrationsManager.cs
new file mode 100644
index 0000000..3b65a98
--- /dev/null
+++ b/LongoMatch.Services/Services/MigrationsManager.cs
@@ -0,0 +1,158 @@
+//
+//  Copyright (C) 2013 Andoni Morales Alastruey
+//
+//  This program is free software; you can redistribute it and/or modify
+//  it under the terms of the GNU General Public License as published by
+//  the Free Software Foundation; either version 2 of the License, or
+//  (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+//  GNU General Public License for more details.
+//
+//  You should have received a copy of the GNU General Public License
+//  along with this program; if not, write to the Free Software
+//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+//
+using System;
+
+using LongoMatch.Store.Templates;
+using LongoMatch.Interfaces;
+using LongoMatch.Store;
+using System.Collections.Generic;
+using Mono.Unix;
+using LongoMatch.Common;
+
+namespace LongoMatch.Services
+{
+       public class MigrationsManager
+       {
+               TemplatesService templates;
+               IDataBaseManager databaseManager;
+               Version currentVersion;
+               
+               public MigrationsManager (TemplatesService templates, IDataBaseManager databaseManager)
+               {
+                       this.templates = templates;
+                       this.databaseManager = databaseManager;
+                       currentVersion = new Version (Constants.DB_MAYOR_VERSION, Constants.DB_MINOR_VERSION);
+               }
+               
+               public void StartMigration () {
+                       MigrateAllDB();
+                       MigrateAllTemplates();
+               }
+               
+               void MigrateAllDB () {
+                       foreach (IDatabase db in databaseManager.Databases) {
+                               while (db.Version != currentVersion) {
+                                       MigrateDB (db);
+                               }
+                       }
+               }
+               
+               void MigrateAllTemplates () {
+                       foreach (Categories cat in templates.CategoriesTemplateProvider.Templates) {
+                               MigrateCategories (cat);
+                       }
+                       
+                       foreach (SubCategoryTemplate subcat in 
templates.SubCategoriesTemplateProvider.Templates) {
+                               MigrateSubCategories (subcat);
+                       }
+                       
+                       foreach (TeamTemplate team in templates.TeamTemplateProvider.Templates) {
+                               MigrateTeamTemplates (team);
+                       }
+               }
+               
+               void MigrateDB (IDatabase db) {
+                       Version version = db.Version;
+                       
+                       if (version == null || version.Major == 2 && version.Minor == 0) {
+                               MigrateDB_2_0 (db);
+                       }
+               }
+               
+               void MigrateCategories (Categories cats) {
+                       Version version = cats.Version;
+                       
+                       if (version == null || (version.Major == 2 && version.Minor == 0)) {
+                               MigrateCat_2_0 (cats);
+                       }
+               }
+               
+               void MigrateSubCategories (SubCategoryTemplate subcat) {
+                       Version version = subcat.Version;
+                       
+                       if (version == null || (version.Major == 2 && version.Minor == 0)) {
+                               MigrateSubCat_2_0 (subcat);
+                       }
+               }
+               
+               void MigrateTeamTemplates (TeamTemplate teamTemplate) {
+                       Version version = teamTemplate.Version;
+                       
+                       if (version == null || (version.Major == 2 && version.Minor == 0)) {
+                               MigrateTeamTeamplate_2_0 (teamTemplate);
+                       }
+               }
+               
+               void MigrateDB_2_0 (IDatabase db) {
+                       Log.Information ("Migrating db " + db.Name + " to 2.1");
+                       db.Version = new Version (2, 1);
+               }
+               
+               void MigrateSubCat_2_0 (SubCategoryTemplate template) {
+                       Log.Information ("Migrating sub category " + template.Name + " to 2.1");
+                       template.Version = new Version (2, 1);
+                       templates.SubCategoriesTemplateProvider.Update (template);
+               }
+               
+               void MigrateTeamTeamplate_2_0 (TeamTemplate template) {
+                       Log.Information ("Migrating team template " + template.Name + " to 2.1");
+                       template.Version = new Version (2, 1);
+                       templates.TeamTemplateProvider.Update (template);
+               }
+               
+               void MigrateCat_2_0 (Categories cats) {
+                       /*
+                       Migrate templates: in this version game periods are a common
+                       tag for all sub-categories. We need to remove the old Period
+                       string tag from all categories and add a GamePeriod string list
+                       with the same values 
+                       */
+                       List<string> periods = null;
+                       
+                       Log.Information ("Migrating categories template " + cats.Name + " to 2.1");
+                       foreach (Category cat in cats) {
+                               ISubCategory toDelete = null;
+                               
+                               foreach (ISubCategory subcat in cat.SubCategories) {
+                                       TagSubCategory tagSubcat = subcat as TagSubCategory;
+                                       
+                                       if (tagSubcat == null)
+                                               continue;
+                                       
+                                       if (subcat.Name == Catalog.GetString("Period")) {
+                                               if (periods == null) {
+                                                       periods = new List<string>();
+                                                               foreach (string tag in tagSubcat) {
+                                                               periods.Add (tag);
+                                                       }
+                                               }
+                                               toDelete = subcat;
+                                               Log.Debug ("Migrated Period for category " + cat.Name);
+                                               break;
+                                       }
+                               }
+                               if (toDelete != null)
+                                       cat.SubCategories.Remove (toDelete);
+                       } 
+                       cats.Version = new Version (2,1);
+                       templates.CategoriesTemplateProvider.Update (cats);
+                       Log.Information ("Migration to 2.1 done");
+               }
+       }
+}
+


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]