Prevent sqlite db to overwrite while installing my ElectronNet application #852

Closed
opened 2026-01-29 16:50:09 +00:00 by claunia · 5 comments
Owner

Originally created by @krishrana17 on GitHub (Mar 19, 2023).

I am developing an Electron.NET application using .NET Core 6 and I want to prevent my SQLite database file from being overwritten during the installation process if it already exists. I have added the necessary code to my manually created 'main.js' file to check for the existence of the file and copy it only if it doesn't exist. However, when I build the package using 'electronize build /target win', the code in my 'main.js' file is not reflected in the built package. Is this a bug or am I missing something?

`const fs = require('fs');
const path = require('path');
const { app } = require('electron');

// Get the installation directory
const appPath = app.getPath('exe');
const installDir = path.dirname(appPath);

// Check if the database file already exists
const dbFile = path.join(installDir, 'mydb.sqlite');
if (!fs.existsSync(dbFile)) {
const resourcePath = path.join(__dirname, 'mydb.sqlite');
fs.copyFileSync(resourcePath, dbFile);
}`

Originally created by @krishrana17 on GitHub (Mar 19, 2023). I am developing an Electron.NET application using .NET Core 6 and I want to prevent my SQLite database file from being overwritten during the installation process if it already exists. I have added the necessary code to my manually created 'main.js' file to check for the existence of the file and copy it only if it doesn't exist. However, when I build the package using 'electronize build /target win', the code in my 'main.js' file is not reflected in the built package. Is this a bug or am I missing something? `const fs = require('fs'); const path = require('path'); const { app } = require('electron'); // Get the installation directory const appPath = app.getPath('exe'); const installDir = path.dirname(appPath); // Check if the database file already exists const dbFile = path.join(installDir, 'mydb.sqlite'); if (!fs.existsSync(dbFile)) { const resourcePath = path.join(__dirname, 'mydb.sqlite'); fs.copyFileSync(resourcePath, dbFile); }`
claunia added the invalid label 2026-01-29 16:50:09 +00:00
Author
Owner

@FlorianRappl commented on GitHub (Mar 19, 2023):

Can you create a full MWE e.g., as a GitHub repository? It would help to see what you actually do and if this is indeed a bug.

@FlorianRappl commented on GitHub (Mar 19, 2023): Can you create a full [MWE](https://stackoverflow.com/help/minimal-reproducible-example) e.g., as a GitHub repository? It would help to see what you actually do and if this is indeed a bug.
Author
Owner

@krishrana17 commented on GitHub (Mar 20, 2023):

Let me try to give you the more detail. There is only one page which simply displays the list of users (dummy users) from "Users" table from Sqlite db.
To create table in Sqlite db, i am using "FluentMigrator" and have configured it like this in my program.cs file.

`
builder.Services.AddFluentMigratorCore()

            .ConfigureRunner(rb => rb
                // Add SQLite support to FluentMigrator
                .AddSQLite()
                .WithGlobalConnectionString(builder.Configuration.GetConnectionString("UserConnectionString"))
                .ScanIn(Assembly.GetExecutingAssembly()).For.Migrations()
            ).AddLogging(lb => lb.AddFluentMigratorConsole());

`

and in the appsettings.json file, I have defined the connection string like this -
"ConnectionStrings": { "UserConnectionString": "Data Source=mydb.sqlite" },

So when you run the application, "FluentMigrator" will create the table schema and insert the dummy data using following code.

`[Migration(20230318)]
public class Migrate_CreateUsersTable : Migration
{

public override void Down()
{
    Delete.Table("Users");
}

public override void Up()
{
    Create.Table("Users")
        .WithColumn("UserId").AsInt32().NotNullable().PrimaryKey().Identity()
        .WithColumn("FirstName").AsString(100).Nullable()
        .WithColumn("LastName").AsString(100).Nullable()
        .WithColumn("DesignationId").AsInt32().Nullable()
        .WithColumn("IsSynced").AsBoolean().Nullable().WithDefaultValue(0);
}

}`

Now in my Visual Studio solution, I have mark this sqlite db file as "Embed Resource" as shown in below image.

image

Basically what I am trying to do is - when I will first time install this electron app in client machine at that time I want electron setup to copy sqlite db file in client machine. Now after few days, let say If I have released some new feature then in the second installation I don't want electron setup will copy that sqlite db file because it was already there in the client machine. And I don't want electron setup will replace that file on subsequent installation. Can anybody helped here where to look for that @GregorBiswanger? Write now I am trying to control that by writing below code in "main.js" file. But that's approach not working.

`const fs = require('fs');
const path = require('path');
const { app } = require('electron');

// Get the installation directory
const appPath = app.getPath('exe');
const installDir = path.dirname(appPath);

// Check if the database file already exists
const dbFile = path.join(installDir, 'mydb.sqlite');
if (!fs.existsSync(dbFile)) {
const resourcePath = path.join(__dirname, 'mydb.sqlite');
fs.copyFileSync(resourcePath, dbFile);
}`

@krishrana17 commented on GitHub (Mar 20, 2023): Let me try to give you the more detail. There is only one page which simply displays the list of users (dummy users) from **"Users"** table from Sqlite db. To create table in Sqlite db, i am using **"FluentMigrator"** and have configured it like this in my program.cs file. ` builder.Services.AddFluentMigratorCore() .ConfigureRunner(rb => rb // Add SQLite support to FluentMigrator .AddSQLite() .WithGlobalConnectionString(builder.Configuration.GetConnectionString("UserConnectionString")) .ScanIn(Assembly.GetExecutingAssembly()).For.Migrations() ).AddLogging(lb => lb.AddFluentMigratorConsole()); ` and in the **appsettings.json** file, I have defined the connection string like this - ` "ConnectionStrings": { "UserConnectionString": "Data Source=mydb.sqlite" },` So when you run the application, "FluentMigrator" will create the table schema and insert the dummy data using following code. `[Migration(20230318)] public class Migrate_CreateUsersTable : Migration { public override void Down() { Delete.Table("Users"); } public override void Up() { Create.Table("Users") .WithColumn("UserId").AsInt32().NotNullable().PrimaryKey().Identity() .WithColumn("FirstName").AsString(100).Nullable() .WithColumn("LastName").AsString(100).Nullable() .WithColumn("DesignationId").AsInt32().Nullable() .WithColumn("IsSynced").AsBoolean().Nullable().WithDefaultValue(0); } }` Now in my Visual Studio solution, I have mark this sqlite db file as "Embed Resource" as shown in below image. ![image](https://user-images.githubusercontent.com/16319416/226333091-e241c536-4f87-4947-86d1-0372e6b972d0.png) Basically what I am trying to do is - when I will first time install this electron app in client machine at that time I want electron setup to copy sqlite db file in client machine. Now after few days, let say If I have released some new feature then in the second installation I don't want electron setup will copy that sqlite db file because it was already there in the client machine. And I don't want electron setup will replace that file on subsequent installation. Can anybody helped here where to look for that @GregorBiswanger? Write now I am trying to control that by writing below code in "main.js" file. But that's approach not working. `const fs = require('fs'); const path = require('path'); const { app } = require('electron'); // Get the installation directory const appPath = app.getPath('exe'); const installDir = path.dirname(appPath); // Check if the database file already exists const dbFile = path.join(installDir, 'mydb.sqlite'); if (!fs.existsSync(dbFile)) { const resourcePath = path.join(__dirname, 'mydb.sqlite'); fs.copyFileSync(resourcePath, dbFile); }`
Author
Owner

@FlorianRappl commented on GitHub (Mar 20, 2023):

Well, again, what I'm asking is a simple reproducible with a GitHub repository. If I understood the OP the problem is not at all related to sqlite (not sure why this was brought up then), but rather the usage of your main.js.

So again, just provide a MWE showing your exact problem - once we have a reproducible we can take care of it.

@FlorianRappl commented on GitHub (Mar 20, 2023): Well, again, what I'm asking is a simple reproducible with a GitHub repository. If I understood the OP the problem is not at all related to sqlite (not sure why this was brought up then), but rather the usage of your main.js. So again, just provide a MWE showing your exact problem - once we have a reproducible we can take care of it.
Author
Owner

@Elanchezhiyan-P commented on GitHub (Jun 24, 2024):

@krishrana17 , How do you package the electron application? When you use Visual studio project installer, it has the capability to create new or replace option when installed freshly or updating it to the existing application.

@Elanchezhiyan-P commented on GitHub (Jun 24, 2024): @krishrana17 , How do you package the electron application? When you use Visual studio project installer, it has the capability to create new or replace option when installed freshly or updating it to the existing application.
Author
Owner

@FlorianRappl commented on GitHub (Nov 10, 2025):

Stale issue that does not seem to originate from Electron.NET.

@FlorianRappl commented on GitHub (Nov 10, 2025): Stale issue that does not seem to originate from Electron.NET.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/Electron.NET#852