2

I'm building an application with a database where I'd like to populate the DB with some data at first start up (so that the user has a few examples of how the application can be used)

To achieve this I'd like to detect if the application is started the first time and if that's the case add these initial rows to the database, and otherwise, skip this

What would you recommend to do to indicate that the application has been started before?

Some alternatives I've thought about:

  • Using the application settings
  • Having an empty file in the application directory which is removed as soon as the application has been started once

Are there other alternatives? What can you recommend and why?

4
  • Why you prefer to do it on startup instead and not during installation?
    – Vlad
    Aug 30 '17 at 6:01
  • @Vlad Good question :) In my case I don't have an installation procedure, but this would be a good option otherwise
    – sunyata
    Aug 30 '17 at 10:53
  • 1
    Where does the database come from? Does your application have to create the database when it doesn't already exist? If so, that answers your question.
    – user253751
    Aug 31 '17 at 5:56
  • @immibis Yes my application creates the db when starting, so just checking if that file exists would be a solution (i agree)
    – sunyata
    Sep 1 '17 at 11:20
7

Either

  • deploy the initial instance of your database with all the samples, so the data is already there before the application starts the first time, or

  • test if any of the data is already there in the database, and populate if it is not. Depending on how the data is structured, a simple "SELECT COUNT(*)" may be enough for this.

Furthermore, think about the case when a user deletes all the samples from the DB, how shall the application behave:

  • let the samples deleted (which fits to the first approach)

  • recreate the samples automatically (which the second strategy will do)

  • ask the user once if the application shall recreate the samples, and even if he answers "no", do not ask him again

You might use a separate indicator somewhere else (in the database, in an extra table, not outside in the file system!) to give you the possibility to check if the initial population has happened. However, this is a somewhat redundant information, it is generally safer to validate the existence of some data by actually testing for that data directly, instead of relying on a secondary meta data flag. On the other hand, if you give the user the possibility to decide if he wants the samples (or not), you will surely need some indicator in the db to remember his decision.

1
  • why not outside in the file system along with a SELECT COUNT(*) so even if it gets deleted later it shouldn't matter because the db should be populated by that point?
    – yitzih
    Aug 31 '17 at 21:12
1

Look into "database migration scripts"

These usually involve a table of version number and changes per database which can then be queried to determine what setup or changes (if any) to run on startup.

0

Since you already have a DB, you could create some initialization module that is called in your App's entrypoint. This module's logic would be something like:

//initialization module
init_info = db.get_initialization_info()
if (init_info.last_startup_date == null) {
    save_sample_data_for_user(db)
}
init_info.last_startup_date = datetime_now()
db.save_init_info()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.