Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate an empty SQLite database during build #139

Closed
Riduidel opened this issue Oct 16, 2018 · 7 comments
Closed

Generate an empty SQLite database during build #139

Riduidel opened this issue Oct 16, 2018 · 7 comments
Labels
development Build and development related

Comments

@Riduidel
Copy link
Contributor

THis database could be given as input to SchemaSpy in order to obtain a good documentation of calendula DB.

Even better, we could also create database similar to the production one, which makes schema dump way more useful.

@AlvaroBrey
Copy link
Contributor

If I'm not mistaken, you can pull the empty SQLite file from an emulator/Android device using ADB just after installation. It should be under /data/data/es.usc.citius.servando.calendula/databases/calendula.db .

You may need to use adb root to be able to access this path, however.

@AlvaroBrey AlvaroBrey added the development Build and development related label Oct 17, 2018
@Riduidel
Copy link
Contributor Author

Riduidel commented Oct 17, 2018 via email

@Riduidel
Copy link
Contributor Author

So, I've added a test in DBTest which full content is

/**
 * Abuse test framework to export database into a project folder, allowing further analysis of database schema
 */
@Test
fun exportDatabase() {
    val db = DB.helper().writableDatabase;
    val destination = File("../db")
    destination.mkdirs()
    System.out.println(String.format("Exporting application database %s to %s", db.path, destination.absolutePath))
    FileUtils.copyFileToDirectory(File(db.path), destination)
    assertNotNull(db.path);
}

It works ... correctly, since I indeed have a db/calendula.db file generated.
But it seems to be incomplete. As an example, I can't find the ActiveIngredients table that should be generated by the ActiveIngredientDAO init code. I'm still debugging, but am quite confused.

@AlvaroBrey
Copy link
Contributor

AlvaroBrey commented Oct 18, 2018

Is this a robolectric test, or just a plain JUnit one? You should make sure DatabaseHelper.init(Context) is called sometime before you get the file.

If it is a robolectric test the framework should already call this on application startup, but in case of a JUnit test you may need to call it manually.

@AlvaroBrey
Copy link
Contributor

AlvaroBrey commented Oct 18, 2018

By the way, if you use a debug variant, you can use Chrome to inspect the database via Stetho.
Just open a chrome window in your PC, go to chrome://inspect/#devices, and you should find the database under Resources > Web SQL.

@angelpinheiro
Copy link
Contributor

Calling DB.helper().close() before copying the file does the trick for me! I assume it's probably due to some pending SQLite statement that now is forced to finish.

    @Test
    fun exportDatabase() {
        val dbPath = DB.helper().writableDatabase.path
        DB.helper().close()
        val destination = File("../db")
        destination.mkdirs()
        System.out.println(String.format("Exporting application database %s to %s", dbPath, destination.absolutePath))
        FileUtils.copyFileToDirectory(File(dbPath), destination)
        assertNotNull(dbPath)
    }

@AlvaroBrey
Copy link
Contributor

Closing this issue since the relevant PR is merged, and this is related to development, so no need to wait for release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
development Build and development related
Projects
None yet
Development

No branches or pull requests

3 participants