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

How to upgrade the database version #726

Closed
yixiu30 opened this issue Jan 3, 2023 · 5 comments
Closed

How to upgrade the database version #726

yixiu30 opened this issue Jan 3, 2023 · 5 comments
Assignees

Comments

@yixiu30
Copy link

yixiu30 commented Jan 3, 2023

In the test process, I added the field nickname for the first time. I added two more fields, chinesename and englishname, as required. But at this time, if I upgrade the database one version at a time, there is no problem, such as versions three to four. However, if I upgrade the database from the online version two to the latest version four, I will not report an error
this is the example from three to four version,
// create migration
final migration1to2 = Migration(3, 4, (database) {
database.execute('ALTER TABLE person ADD COLUMN chinesename TEXT');
database.execute('ALTER TABLE person ADD COLUMN englishnameTEXT');
});
final database = await $FloorAppDatabase
.databaseBuilder('app_database.db')
.addMigrations([migration1to2])
.build();

@dkaera
Copy link
Collaborator

dkaera commented Jan 3, 2023

Hey @yixiu30
Sorry, it's not clear what problem you hit with Floor.
Basically the database version declares in @Database(version: <your_actual_version>) annotation.
Migration which should be applied in the Migration(<from_version>, <to_version>, ....) object
and the migrations are executed in order, one after the other
1 -> 2, 2 -> 3, 3 -> 4
if it's declared, of course
Here is more info https://pinchbv.github.io/floor/migrations/

@dkaera
Copy link
Collaborator

dkaera commented Jan 3, 2023

and you lost the space between englishname and TEXT
I'm not sure if this is an issue or just a sample code

@dkaera dkaera self-assigned this Jan 3, 2023
@yixiu30
Copy link
Author

yixiu30 commented Jan 4, 2023

thank you very much!
but i find anthor problem,you see this

[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: DatabaseException(Error Domain=FMDatabase Code=1 "table chatrecord has no column named nameEn" UserInfo={NSLocalizedDescription=table chatrecord has no column named nameEn}) sql 'INSERT OR ABORT INTO chatrecord (chatListId, head_url, name, content, type, time, groupId, chatId, sendId, staffId, origingUrl, compressUrl, width, height, picture_local, title, fileType, filesize, filelocal, fileurl, isDownload, chatUuid, file_id, system, audioOpenEd, audioRecord, audioClick, dailyReportId, dailyReportType, isDailyReport, briefId, addContent, href, dialogHeight, upDateText, nameEn) VALUES (NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' args [https://test.xiamenjwzz.com:9999/portalfile/userPi..., weijiwo, , 1, 2023-01-03 16:43:20.945, 4872098147432661014, 225739, 4804247830982033430, 2fb135026aae410d81330a4162a6983d, https://files-1304239070.cos.ap-nanjing.myqcloud.c..., https://files-1304239070.cos.ap-nanjing.myqcloud.c..., 0, 0, , , , , , , 0, cb293d40-ba08-44e3-a8a2-36fac2f9e392, d5ffd44b596845268acb84e6ba20f7f2, 0, 0, , 0, , , 0, , report, , , , ]
#0 wrapDatabaseException (package:sqflite/src/exception_impl.dart:11)

#1 SqfliteDatabaseMixin.txnRawInsert. (package:sqflite_common/src/database_mixin.dart:392)

#2 BasicLock.synchronized (package:synchronized/src/basic_lock.dart:33)

#3 SqfliteDatabaseMixin.txnSynchronized (package:sqflite_common/src/database_mixin.dart:344)

#4 InsertionAdapter._insert (package:floor/src/adapter/insertion_adapter.dart:77)

#5 InsertionAdapter.insert (package:floor/src/adapter/insertion_adapter.dart:28)

#6 _$ChatRecordDao.insertChatRecord (package:flutterhtm/sql/database.g.dart:731)

but the database update i have add the column nameEn

final migration1to2 = Migration(7, 8, (database) async {
  await database.execute('ALTER TABLE chatrecord ADD COLUMN href TEXT NOT NULL DEFAULT "0";');     
  await database.execute('ALTER TABLE chatrecord ADD COLUMN dialogHeight TEXT NOT NULL DEFAULT "0";');
  await database.execute('ALTER TABLE chatrecord ADD COLUMN upDateText TEXT NOT NULL DEFAULT "0";');
  await database.execute('ALTER TABLE chatrecord ADD COLUMN nameEn TEXT NOT NULL DEFAULT "0";');         

});

final database = await $FloorAppDatabase
    .databaseBuilder(GroupManager.dbName)
    .addMigrations([migration1to2])
    .build();

@dkaera
Copy link
Collaborator

dkaera commented Jan 4, 2023

@yixiu30
Please check your database version in @Database annotation
in your case, to execute the migration code above it should be 8

@dkaera
Copy link
Collaborator

dkaera commented Jan 31, 2023

no answer for a while

@dkaera dkaera closed this as completed Jan 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants