sqldelight icon indicating copy to clipboard operation
sqldelight copied to clipboard

Unable to create two database using sqldelight

Open yuvaraj119 opened this issue 9 months ago • 4 comments

SQLDelight Version

2.0.0,2.0.2

Application Operating System

Android,iOS

Describe the Bug

In project i have 2 modules who has their own DB creation and implementation. Both the module has sqldelight plugin applied sqldelight { databases{ create("CoreDb") { packageName.set("com.xxxxxxx.db") schemaOutputDirectory.file("src/commonMain/sqldelight/databases") } } }

sqldelight { databases{ create("MusicDb") { packageName.set("com.xxxxxxx.music") schemaOutputDirectory.file("src/commonMain/sqldelight/databases") } } }

Through Driver of Android and Native creating the Database. On App launch in MyApplication of Android the db creation starts and implementation follows First create CoreDb then next create MusicDb Here I see only MusicDb is created and i don't find any db/tables related to CoreDb If I create only CoreDb then it gets created and same with MusicDb. But when i try creating both it is creating only Second/Last db only.

I tried using same DB name for both but as CoreDb version is 2 and MusicDb version is 1 Android error example :

android.database.sqlite.SQLiteException: Can't downgrade database from version 2 to 1

Not sure i'm missing some configuration here or not.

Stacktrace

No response

yuvaraj119 avatar Apr 26 '24 20:04 yuvaraj119

Seems like you're using the same filename for both. Where's your driver initialization code?

JakeWharton avatar Apr 26 '24 20:04 JakeWharton

Seems like you're using the same filename for both. Where's your driver initialization code? I call this function to create any db

override fun createDriver(): SqlDriver { val factory = SupportOpenHelperFactory(databaseInfo.password.toByteArray(StandardCharsets.UTF_8)) return AndroidSqliteDriver( schema = databaseInfo.schema, context = context, name = databaseInfo.name, factory = factory, callback = object : AndroidSqliteDriver.Callback(databaseInfo.schema) { override fun onOpen(db: SupportSQLiteDatabase) { db.setForeignKeyConstraintsEnabled(true) } } ) }

yuvaraj119 avatar Apr 26 '24 20:04 yuvaraj119

Seems like you're using the same filename for both. Where's your driver initialization code?

Even if I use different db name it gets override with last db initialization/creation. First db is not created/overrriden

yuvaraj119 avatar Apr 26 '24 20:04 yuvaraj119

Seems like you're using the same filename for both. Where's your driver initialization code?

Driver is initialized in Myapplication initKoin(androidContext(this@MyApplication) modules( createDbModule( DatabaseInfo( schema = coreDatabaseSchema, name = CORE_DB_NAME, password = resources.getString(R.string.app_name) ) ), coreDatabaseFactory, createDbModule( DatabaseInfo( schema = musicDatabaseSchema, name = MUSIC_DB_NAME, password = resources.getString(R.string.app_name) ) ), musicDatabaseFactory ) }

    Hope this gives some insight.
    
    We have 2 usecase 
    1. With different DB_NAME only last db is created/overriden
    2. With same DB_Name i have issue with version conflict on db creation

I tried with 2.0.0 still same issue

yuvaraj119 avatar Apr 26 '24 23:04 yuvaraj119

Found the issue and fixed.

yuvaraj119 avatar Aug 08 '24 15:08 yuvaraj119