Inheritance: SQLite.SQLiteConnection
Example #1
0
        public static WorkspaceDB Create(LocalDB localDB, string fullPath)
        {
            WorkspaceDB ws = new WorkspaceDB(fullPath, SQLite.SQLiteOpenFlags.Create | SQLite.SQLiteOpenFlags.ReadWrite | SQLite.SQLiteOpenFlags.NoMutex, localDB);

            ws.BeginTransaction();
            try
            {
                Objects.FormatInfo formatInfo = new FormatInfo();
                formatInfo.InternalFormat = InternalDBVersion;
                ws.InsertSafe(formatInfo);
                ws.Commit();
                return(ws);
            }
            catch (Exception e)
            {
                ws.Rollback();
                ws.Dispose();
                if (System.IO.File.Exists(fullPath))
                {
                    System.IO.File.Delete(fullPath);
                }
                throw new Exception("Couldn't create database!", e);
            }
        }
Example #2
0
        public static LocalDB Create(string fullPath)
        {
            LocalDB ldb = new LocalDB(fullPath, SQLite.SQLiteOpenFlags.Create | SQLite.SQLiteOpenFlags.ReadWrite | SQLite.SQLiteOpenFlags.FullMutex);

            ldb.BeginTransaction();
            try
            {
                LocalState.Configuration conf = new LocalState.Configuration();
                conf.Version = LocalDBVersion;
                ldb.InsertSafe(conf);
                ldb.Commit();
                return(ldb);
            }
            catch (Exception e)
            {
                ldb.Rollback();
                ldb.Dispose();
                if (System.IO.File.Exists(fullPath))
                {
                    System.IO.File.Delete(fullPath);
                }
                throw new Exception("Couldn't create database!", e);
            }
        }
Example #3
0
 public static LocalDB Create(string fullPath)
 {
     LocalDB ldb = new LocalDB(fullPath, SQLite.SQLiteOpenFlags.Create | SQLite.SQLiteOpenFlags.ReadWrite | SQLite.SQLiteOpenFlags.FullMutex);
     ldb.BeginTransaction();
     try
     {
         LocalState.Configuration conf = new LocalState.Configuration();
         conf.Version = LocalDBVersion;
         ldb.InsertSafe(conf);
         ldb.Commit();
         return ldb;
     }
     catch (Exception e)
     {
         ldb.Rollback();
         ldb.Dispose();
         if (System.IO.File.Exists(fullPath))
             System.IO.File.Delete(fullPath);
         throw new Exception("Couldn't create database!", e);
     }
 }
Example #4
0
 public static LocalDB Open(string fullPath)
 {
     LocalDB db = new LocalDB(fullPath, SQLite.SQLiteOpenFlags.ReadWrite | SQLite.SQLiteOpenFlags.FullMutex);
     if (!db.Upgrade())
         return null;
     return db;
 }
Example #5
0
 public static WorkspaceDB Create(LocalDB localDB, string fullPath)
 {
     WorkspaceDB ws = new WorkspaceDB(fullPath, SQLite.SQLiteOpenFlags.Create | SQLite.SQLiteOpenFlags.ReadWrite | SQLite.SQLiteOpenFlags.NoMutex, localDB);
     ws.BeginTransaction();
     try
     {
         Objects.FormatInfo formatInfo = new FormatInfo();
         formatInfo.InternalFormat = InternalDBVersion;
         ws.InsertSafe(formatInfo);
         ws.Commit();
         return ws;
     }
     catch (Exception e)
     {
         ws.Rollback();
         ws.Dispose();
         if (System.IO.File.Exists(fullPath))
             System.IO.File.Delete(fullPath);
         throw new Exception("Couldn't create database!", e);
     }
 }
Example #6
0
 public static WorkspaceDB Open(LocalDB localDB, string fullPath)
 {
     return new WorkspaceDB(fullPath, SQLite.SQLiteOpenFlags.ReadWrite | SQLite.SQLiteOpenFlags.NoMutex, localDB);
 }
Example #7
0
        private WorkspaceDB(string path, SQLite.SQLiteOpenFlags flags, LocalDB localDB) : base(path, flags)
        {
            Printer.PrintDiagnostics("Metadata DB Open.");
            EnableWAL = true;
            LocalDatabase = localDB;
            
            CreateTable<Objects.FormatInfo>();
            if (flags.HasFlag(SQLite.SQLiteOpenFlags.Create))
            {
                ExecuteDirect("PRAGMA main.page_size = 4096;");
                ExecuteDirect("PRAGMA main.cache_size = 10240;");
                ExecuteDirect("PRAGMA temp_store = MEMORY;");
                EnableWAL = true;
                PrepareTables();
                return;
            }

            if (!ValidForUpgrade)
                return;

            if (Format.InternalFormat < InternalDBVersion)
            {
                try
                {
                    var fmt = Format;
					int priorFormat = fmt.InternalFormat;
                    if (priorFormat < 17)
                    {
                        ExecuteDirect("PRAGMA main.page_size = 4096;");
                        ExecuteDirect("PRAGMA main.cache_size = 10240;");
                        ExecuteDirect("PRAGMA temp_store = MEMORY;");
                        EnableWAL = false;
                        ExecuteDirect("VACUUM");
                        EnableWAL = true;
                    }
                    BeginExclusive(true);
                    if (priorFormat <= 12)
                    {
                        var info = GetTableInfo("ObjectName");
                        if (info.Where(x => x.Name == "NameId").Count() == 0)
                        {
                            var objNames = Query<ObjectNameOld>("SELECT * FROM ObjectName").ToList();
                            Dictionary<long, long> nameMapping = new Dictionary<long, long>();
                            Dictionary<string, long> nameIndexes = new Dictionary<string, long>();
                            DropTable<ObjectName>();
                            Commit();
                            BeginExclusive();
                            CreateTable<ObjectName>();
                            foreach (var x in objNames)
                            {
                                if (nameIndexes.ContainsKey(x.CanonicalName))
                                {
                                    nameMapping[x.Id] = nameIndexes[x.CanonicalName];
                                }
                                else
                                {
                                    ObjectName oname = new ObjectName() { CanonicalName = x.CanonicalName };
                                    Insert(oname);
                                    nameMapping[x.Id] = oname.NameId;
                                    nameIndexes[x.CanonicalName] = oname.NameId;
                                }
                            }
                            foreach (var x in Table<Record>().ToList())
                            {
                                x.CanonicalNameId = nameMapping[x.CanonicalNameId];
                                Update(x);
                            }
                            Commit();
                        }
                    }
                    PrepareTables();
                    Printer.PrintMessage("Updating workspace database version from v{0} to v{1}", Format.InternalFormat, InternalDBVersion);

                    if (priorFormat < 28)
                    {
                        var tips = Query<BranchJournal>("SELECT * FROM BranchJournal WHERE NOT EXISTS (SELECT * FROM BranchJournalLink WHERE Parent = BranchJournal.ID)").ToList();
                        if (tips.Count > 1)
                            Printer.PrintError("#e#Database update encountered an error - multiple possible tips for branch journal data found. Selecting final revision.");
                        if (tips.Count != 0)
                            BranchJournalTip = tips[tips.Count - 1].ID;
                    }
                    if (priorFormat < 14)
                    {
                        ExecuteDirect("DROP TABLE RecordIndex;");
                    }
                    if (priorFormat < 32)
                    {
                        int count = 0;
                        foreach (var x in Table<Objects.Version>())
                        {
                            if (x.Message != null && x.Message.StartsWith("Automatic merge of"))
                            {
                                var mergeInfos = Table<MergeInfo>().Where(y => y.DestinationVersion == x.ID).ToList();
                                if (mergeInfos.Count == 1)
                                {
                                    mergeInfos[0].Type = MergeType.Automatic;
                                    Update(mergeInfos[0]);
                                    count++;
                                }
                            }
                        }
                        Printer.PrintMessage("Updated #b#{0}## merge info records.", count);
                    }
                    if (priorFormat < 31)
                    {
                        Dictionary<long, Record> recordMap = new Dictionary<long, Record>();
                        foreach (var x in Table<Record>().ToList())
                            recordMap[x.Id] = x;

                        foreach (var x in Table<Record>().ToList())
                        {
                            if (x.Parent.HasValue && !recordMap.ContainsKey(x.Parent.Value))
                            {
                                x.Parent = null;
                                Update(x);
                            }
                        }
                    }
                    if (priorFormat < 30)
                    {
                        RunConsistencyCheck();
                    }
                    if (priorFormat < 30)
                    {
                        foreach (var x in Table<Objects.Version>().ToList())
                        {
                            x.Snapshot = null;
                            Update(x);
                            var alterations = Table<Objects.Alteration>().Where(z => z.Owner == x.AlterationList);
                            Dictionary<long, bool> moveDeletes = new Dictionary<long, bool>();
                            HashSet<long> deletions = new HashSet<long>();
                            int counter = 0;
                            foreach (var s in alterations)
                            {
                                if (s.Type == AlterationType.Move)
                                {
                                    if (moveDeletes.ContainsKey(s.PriorRecord.Value))
                                        moveDeletes[s.PriorRecord.Value] = false;
                                    else
                                        moveDeletes[s.PriorRecord.Value] = true;
                                }
                            }
                            foreach (var s in alterations)
                            {
                                if (s.Type == AlterationType.Move)
                                {
                                    if (moveDeletes[s.PriorRecord.Value] == false)
                                    {
                                        s.Type = AlterationType.Copy;
                                        Update(s);
                                        deletions.Add(s.PriorRecord.Value);
                                        counter++;
                                    }
                                }
                            }
                            foreach (var s in deletions)
                            {
                                Alteration alt = new Alteration() { PriorRecord = s, Type = AlterationType.Delete, Owner = x.AlterationList };
                                Insert(alt);
                            }
                            if (counter > 0)
                                Printer.PrintDiagnostics("Version {0} had {1} multiple-moves that have been fixed.", x.ShortName, counter);
                        }
                    }
                    if (priorFormat < 30)
                    {
                        foreach (var x in Table<Objects.Version>().ToList())
                        {
                            x.Snapshot = null;
                            Update(x);
                            var alterations = Table<Objects.Alteration>().Where(z => z.Owner == x.AlterationList);
                            HashSet<Tuple<AlterationType, long?, long?>> duplicateAlterations = new HashSet<Tuple<AlterationType, long?, long?>>();
                            int counter = 0;
                            foreach (var s in alterations)
                            {
                                var key = new Tuple<AlterationType, long?, long?>(s.Type, s.NewRecord, s.PriorRecord);
                                if (duplicateAlterations.Contains(key))
                                {
                                    Delete(s);
                                    counter++;
                                }
                                else
                                    duplicateAlterations.Add(key);
                            }
                            if (counter > 0)
                                Printer.PrintDiagnostics("Version {0} had {1} duplicated alterations that have been fixed.", x.ShortName, counter);
                        }
                    }
                    else if (priorFormat == 7)
                    {
                        Printer.PrintMessage(" - Upgrading database - adding branch root version.");
                        foreach (var x in Table<Objects.Branch>().ToList())
                        {
                            var allVersions = Table<Objects.Version>().Where(y => y.Branch == x.ID);
                            Guid? rootVersion = null;
                            foreach (var y in allVersions)
                            {
                                if (y.Parent.HasValue)
                                {
                                    Objects.Version parent = Get<Objects.Version>(y.Parent);
                                    if (parent.Branch != x.ID)
                                    {
                                        rootVersion = parent.ID;
                                        break;
                                    }
                                }
                            }
                            x.RootVersion = rootVersion;
                            Update(x);
                        }
                    }
                    DropTable<Objects.FormatInfo>();
                    fmt.InternalFormat = InternalDBVersion;
                    CreateTable<Objects.FormatInfo>();
                    Insert(fmt);

                    Commit();

                    ExecuteDirect("VACUUM");
                }
                catch (Exception e)
                {
                    Rollback();
                    Printer.PrintError("Couldn't update DB: {0}", e.ToString());
                }
                PrepareTables();
            }
        }
Example #8
0
        private WorkspaceDB(string path, SQLite.SQLiteOpenFlags flags, LocalDB localDB) : base(path, flags)
        {
            Printer.PrintDiagnostics("Metadata DB Open.");
            EnableWAL     = true;
            LocalDatabase = localDB;

            if (ExecuteScalar <string>("SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'FormatInfo';") != "FormatInfo")
            {
                CreateTable <Objects.FormatInfo>();
            }
            if (flags.HasFlag(SQLite.SQLiteOpenFlags.Create))
            {
                ExecuteDirect("PRAGMA main.page_size = 4096;");
                ExecuteDirect("PRAGMA main.cache_size = 10240;");
                ExecuteDirect("PRAGMA temp_store = MEMORY;");
                ExecuteDirect("PRAGMA threads = 2;");
                EnableWAL = true;
                PrepareTables();
                return;
            }

            if (!ValidForUpgrade)
            {
                return;
            }

            if (Format.InternalFormat < InternalDBVersion)
            {
                try
                {
                    var fmt         = Format;
                    int priorFormat = fmt.InternalFormat;

                    if (priorFormat < 42)
                    {
                        ExecuteDirect("DROP TABLE Annotation");
                        ExecuteDirect("DROP TABLE AnnotationJournal");
                        ExecuteDirect("DROP TABLE TagJournal");
                        ExecuteDirect("DROP TABLE Tag");
                    }
                    BeginExclusive(true);
                    Printer.PrintMessage("Updating workspace database version from v{0} to v{1}", Format.InternalFormat, InternalDBVersion);
                    PrepareTables();
                    if (priorFormat < 42)
                    {
                        RunConsistencyCheck();
                    }
                    if (priorFormat < 33)
                    {
                        foreach (var x in Table <Record>().ToList())
                        {
                            if (x.Parent.HasValue)
                            {
                                Record rp = Find <Record>(x.Parent.Value);
                                if (rp == null)
                                {
                                    Printer.PrintMessage("Found missing parent for record ID {0} - parent ID: {1}, attempting to fix.", x.Id, x.Parent.Value);
                                    Printer.PrintMessage(" - Record {0} - {1}", Find <ObjectName>(x.CanonicalNameId).CanonicalName, x.Fingerprint);

                                    var alterations     = Query <Alteration>("SELECT * from Alteration where Alteration.NewRecord = ?", x.Id);
                                    var possibleParents = alterations.Where(z => z.PriorRecord.HasValue).Select(z => z.PriorRecord.Value).Distinct().ToList();

                                    Printer.PrintMessage(" - Found {0} possible parents for this record.", possibleParents.Count);
                                    if (possibleParents.Count == 1)
                                    {
                                        Printer.PrintMessage(" - Updating parent record to ID {0}", possibleParents[0]);
                                        x.Parent = possibleParents[0];
                                    }
                                    else
                                    {
                                        if (possibleParents.Count > 0)
                                        {
                                            Printer.PrintMessage(" - Data is ambiguous, removing parent record.");
                                        }
                                        else
                                        {
                                            Printer.PrintMessage(" - No parents found, removing parent record.");
                                        }
                                        x.Parent = null;
                                    }
                                    Update(x);
                                }
                            }
                        }
                    }
                    if (priorFormat < 32)
                    {
                        int count = 0;
                        foreach (var x in Table <Objects.Version>())
                        {
                            if (x.Message != null && x.Message.StartsWith("Automatic merge of"))
                            {
                                var mergeInfos = Table <MergeInfo>().Where(y => y.DestinationVersion == x.ID).ToList();
                                if (mergeInfos.Count == 1)
                                {
                                    mergeInfos[0].Type = MergeType.Automatic;
                                    Update(mergeInfos[0]);
                                    count++;
                                }
                            }
                        }
                        Printer.PrintMessage("Updated #b#{0}## merge info records.", count);
                    }
                    if (priorFormat < 31)
                    {
                        Dictionary <long, Record> recordMap = new Dictionary <long, Record>();
                        foreach (var x in Table <Record>().ToList())
                        {
                            recordMap[x.Id] = x;
                        }

                        foreach (var x in Table <Record>().ToList())
                        {
                            if (x.Parent.HasValue && !recordMap.ContainsKey(x.Parent.Value))
                            {
                                x.Parent = null;
                                Update(x);
                            }
                        }
                    }
                    if (priorFormat < 30)
                    {
                        foreach (var x in Table <Objects.Version>().ToList())
                        {
                            x.Snapshot = null;
                            Update(x);
                            var alterations = Table <Objects.Alteration>().Where(z => z.Owner == x.AlterationList);
                            Dictionary <long, bool> moveDeletes = new Dictionary <long, bool>();
                            HashSet <long>          deletions   = new HashSet <long>();
                            int counter = 0;
                            foreach (var s in alterations)
                            {
                                if (s.Type == AlterationType.Move)
                                {
                                    if (moveDeletes.ContainsKey(s.PriorRecord.Value))
                                    {
                                        moveDeletes[s.PriorRecord.Value] = false;
                                    }
                                    else
                                    {
                                        moveDeletes[s.PriorRecord.Value] = true;
                                    }
                                }
                            }
                            foreach (var s in alterations)
                            {
                                if (s.Type == AlterationType.Move)
                                {
                                    if (moveDeletes[s.PriorRecord.Value] == false)
                                    {
                                        s.Type = AlterationType.Copy;
                                        Update(s);
                                        deletions.Add(s.PriorRecord.Value);
                                        counter++;
                                    }
                                }
                            }
                            foreach (var s in deletions)
                            {
                                Alteration alt = new Alteration()
                                {
                                    PriorRecord = s, Type = AlterationType.Delete, Owner = x.AlterationList
                                };
                                Insert(alt);
                            }
                            if (counter > 0)
                            {
                                Printer.PrintDiagnostics("Version {0} had {1} multiple-moves that have been fixed.", x.ShortName, counter);
                            }
                        }
                    }
                    if (priorFormat < 30)
                    {
                        foreach (var x in Table <Objects.Version>().ToList())
                        {
                            x.Snapshot = null;
                            Update(x);
                            var alterations = Table <Objects.Alteration>().Where(z => z.Owner == x.AlterationList);
                            HashSet <Tuple <AlterationType, long?, long?> > duplicateAlterations = new HashSet <Tuple <AlterationType, long?, long?> >();
                            int counter = 0;
                            foreach (var s in alterations)
                            {
                                var key = new Tuple <AlterationType, long?, long?>(s.Type, s.NewRecord, s.PriorRecord);
                                if (duplicateAlterations.Contains(key))
                                {
                                    Delete(s);
                                    counter++;
                                }
                                else
                                {
                                    duplicateAlterations.Add(key);
                                }
                            }
                            if (counter > 0)
                            {
                                Printer.PrintDiagnostics("Version {0} had {1} duplicated alterations that have been fixed.", x.ShortName, counter);
                            }
                        }
                    }

                    DropTable <Objects.FormatInfo>();
                    fmt.InternalFormat = InternalDBVersion;
                    CreateTable <Objects.FormatInfo>();
                    Insert(fmt);

                    Commit();

                    Vacuum();
                }
                catch (Exception e)
                {
                    Rollback();
                    Printer.PrintError("Couldn't update DB: {0}", e.ToString());
                }
                PrepareTables();
            }
        }
Example #9
0
 public static WorkspaceDB Open(LocalDB localDB, string fullPath)
 {
     return(new WorkspaceDB(fullPath, SQLite.SQLiteOpenFlags.ReadWrite | SQLite.SQLiteOpenFlags.NoMutex, localDB));
 }