Esempio n. 1
0
        public static void Initialize(String connectionString)
        {
            String[] datasourceParts = connectionString.Split(new char[] { '=' });

            if (datasourceParts.Length == 2)
            {
                List <String> versions = new List <String>();

                if (!File.Exists(datasourceParts[1]))
                {
                    SQLiteConnection.CreateFile(datasourceParts[1]);
                }

                SQLiteConnection connection = new SQLiteConnection(connectionString);
                connection.Open();

                SQLiteCommand schemaCommand = new SQLiteCommand(connection);
                schemaCommand.CommandText = "SELECT version FROM _Schema ORDER BY version";

                try
                {
                    SQLiteDataReader schemaReader = schemaCommand.ExecuteReader();

                    DataTable dataTable = new DataTable();
                    dataTable.Load(schemaReader);

                    foreach (DataRow row in dataTable.Rows)
                    {
                        versions.Add((String)row["version"]);
                    }
                }
                catch (Exception)
                {
                    SQLiteCommand writeSchemaCommand = new SQLiteCommand(connection);
                    writeSchemaCommand.CommandText = "CREATE TABLE _Schema (version NVARCHAR(250) NOT NULL)";

                    writeSchemaCommand.ExecuteNonQuery();
                }

                String lastMigration = String.Empty;
                if (versions.Count > 0)
                {
                    lastMigration = versions.Last();
                }

                MigrationManager.AddMigrations(connection, lastMigration);
            }
        }