示例#1
0
        public void Migrate()
        {
            var database = DependencyService.Get <ISQLite> ();

            SQLite.Net.SQLiteConnection connection = null;

            if (!database.Exists())
            {
                connection = database.GetConnection();

                connection.BeginTransaction();
                connection.Execute(@"CREATE TABLE DBVersion (Version INT NOT NULL,
										ExecutionTime TEXT NOT NULL)"                                        );
                connection.Execute("INSERT INTO DBVersion (Version, ExecutionTime) VALUES (?,?)", new object[] { 0, DateTime.Now });
                connection.Commit();
            }

            if (connection == null)
            {
                connection = database.GetConnection();
            }

            foreach (var migration in Migrations)
            {
                int currentVersion = connection.ExecuteScalar <int> ("SELECT Version FROM DBVersion ORDER BY Version DESC LIMIT 1");

                if (migration.Key == (currentVersion + 1))
                {
                    connection.BeginTransaction();

                    foreach (var step in migration.Value)
                    {
                        connection.Execute(step);
                    }

                    connection.Execute("INSERT INTO DBVersion (Version, ExecutionTime) VALUES (?,?)", new object[] { migration.Key, DateTime.Now });
                    connection.Commit();
                }
            }
        }