FluentMigrator create password protected SqlLite DB

113 views Asked by At

I use FluentMigrator to create a SqlLite DB in C# using FluentMigrator.Runner.MigrationRunner. I wonder is there any way to use the SetPassword command o the SqlConnection only when the DB needs to be created ? There's a SqLiteRunnerContextFactory object but it don't seem to be a property that I can use to specify password.

public MigrationRunner CreateMigrationRunner(string connectionString, string[] migrationTargets, Assembly assembly, long version)
    {
        var announcer = new TextWriterAnnouncer(Console.WriteLine) { ShowSql = true };
        var options = new ProcessorOptions { PreviewOnly = false, Timeout = 60 };
        var runnerContext = new SqLiteRunnerContextFactory().CreateRunnerContext(connectionString, migrationTargets, version, announcer);

        var sqlLiteConnection = new SQLiteConnection(connectionString);
        //If the DB has already been created, it crashes later on if I specify this
        sqlLiteConnection.SetPassword("ban4an4");

        return new MigrationRunner(assembly, 
                                   runnerContext, 
                                   new SQLiteProcessor(sqlLiteConnection, 
                                                       new SQLiteGenerator(), 
                                                       announcer, 
                                                       options, 
                                                       new SQLiteDbFactory()));
    }

I would like to avoid having to look if the file exists before setting password on connection.

1

There are 1 answers

0
Patrice Cote On

Well, finally the code below works perfectly by using SetPassword everytime you create de runner. No need to check if the file exists or not. First time it creates it with the password and second time it opens it with it seems to use it to open DB. Which is exactly what I was looking for.