feat:创建挂单数据库,和迁移文件

This commit is contained in:
dzc committed 2026-02-06 10:35:24 +08:00
1 parent 815cb2ac8b
commit 988247f3cd
7 files changed
+248 -3

No files matched your search

+74 -1
View File
@@ -10,6 +10,7 @@ part 'database.g.dart';
const entities = <Type>[
BroadcastEntity,
OrderEntity,
OrderHoldBillEntity,
CanteenSpuEntity,
SupermarketSkuEntity,
SupermarketSpuEntity,
@@ -35,11 +36,12 @@ const typeConverters = <Type>[
BoolConverter,
];
@Database(version: 2, entities: entities)
@Database(version: 3, entities: entities)
@TypeConverters(typeConverters)
abstract class AppDatabase extends FloorDatabase {
BroadcastDao get getBroadcastDao;
OrderDao get getOrderDao;
OrderHoldBillDao get getOrderHoldBillDao;
CanteenSpuDao get getCanteenSpuDao;
SupermarketSkuDao get getSupermarketSkuDao;
SupermarketSpuDao get getSupermarketSpuDao;
@@ -73,6 +75,77 @@ abstract class AppDatabase extends FloorDatabase {
'ALTER TABLE supermarket_spu ADD COLUMN productNamePy TEXT');
}
}),
Migration(2, 3, (database) async {
// 如果表不存在则创建,存在则校验列并补齐缺失项
final tables = await database.rawQuery(
"SELECT name FROM sqlite_master WHERE type='table' AND name='order_hold_bill'",
);
if (tables.isEmpty) {
await database.execute('''
CREATE TABLE IF NOT EXISTS order_hold_bill (
id INTEGER PRIMARY KEY AUTOINCREMENT,
sysType TEXT NOT NULL,
orderListJson TEXT,
userJson TEXT,
orderJson TEXT,
createTime TEXT NOT NULL
)
''');
await database.execute(
"CREATE INDEX IF NOT EXISTS idx_order_hold_bill_sysType ON order_hold_bill(sysType)",
);
await database.execute(
"CREATE INDEX IF NOT EXISTS idx_order_hold_bill_createTime ON order_hold_bill(createTime)",
);
} else {
final columns =
await database.rawQuery("PRAGMA table_info(order_hold_bill)");
bool hasSysType = false;
bool hasOrderListJson = false;
bool hasUserJson = false;
bool hasOrderJson = false;
bool hasCreateTime = false;
for (final c in columns) {
final name = c['name'];
if (name == 'sysType') hasSysType = true;
if (name == 'orderListJson') hasOrderListJson = true;
if (name == 'userJson') hasUserJson = true;
if (name == 'orderJson') hasOrderJson = true;
if (name == 'createTime') hasCreateTime = true;
}
if (!hasSysType) {
await database.execute(
'ALTER TABLE order_hold_bill ADD COLUMN sysType TEXT',
);
}
if (!hasOrderListJson) {
await database.execute(
'ALTER TABLE order_hold_bill ADD COLUMN orderListJson TEXT',
);
}
if (!hasUserJson) {
await database.execute(
'ALTER TABLE order_hold_bill ADD COLUMN userJson TEXT',
);
}
if (!hasOrderJson) {
await database.execute(
'ALTER TABLE order_hold_bill ADD COLUMN orderJson TEXT',
);
}
if (!hasCreateTime) {
await database.execute(
'ALTER TABLE order_hold_bill ADD COLUMN createTime TEXT',
);
}
await database.execute(
"CREATE INDEX IF NOT EXISTS idx_order_hold_bill_sysType ON order_hold_bill(sysType)",
);
await database.execute(
"CREATE INDEX IF NOT EXISTS idx_order_hold_bill_createTime ON order_hold_bill(createTime)",
);
}
}),
];
}