Files
pj_supermarket_cloud/lib/common/services/database/databaseMigration.dart
T
2026-03-26 11:04:13 +08:00

158 lines
5.4 KiB
Dart

import 'package:floor/floor.dart';
abstract class DatabaseMigration {
/// 从版本1到版本2的迁移:添加productNamePy字段
static Migration migration1To2 = Migration(1, 2, (database) async {
// 先检查列是否存在,不存在才添加
final result = await database.rawQuery(
"PRAGMA table_info(supermarket_spu)",
);
bool columnExists = false;
for (var column in result) {
if (column['name'] == 'productNamePy') {
columnExists = true;
break;
}
}
if (!columnExists) {
await database
.execute('ALTER TABLE supermarket_spu ADD COLUMN productNamePy TEXT');
}
});
/// 从版本2到版本3的迁移:添加orderListJson字段
static Migration migration2To3 = 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,
carListJson TEXT,
userJson TEXT,
price INTEGER DEFAULT 0 NOT NULL,
extraPrice INTEGER DEFAULT 0 NOT NULL,
payPrice INTEGER DEFAULT 0 NOT NULL,
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 hasCarListJson = false;
bool hasOrderListJson = false;
bool hasUserJson = false;
bool hasPrice = false;
bool hasExtraPrice = false;
bool hasPayPrice = false;
bool hasCreateTime = false;
for (final c in columns) {
final name = c['name'];
if (name == 'sysType') hasSysType = true;
if (name == 'carListJson') hasCarListJson = true;
if (name == 'orderListJson') hasOrderListJson = true;
if (name == 'userJson') hasUserJson = true;
if (name == 'price') hasPrice = true;
if (name == 'extraPrice') hasExtraPrice = true;
if (name == 'payPrice') hasPayPrice = true;
if (name == 'createTime') hasCreateTime = true;
}
if (!hasSysType) {
await database.execute(
'ALTER TABLE order_hold_bill ADD COLUMN sysType TEXT',
);
}
if (!hasCarListJson) {
await database.execute(
'ALTER TABLE order_hold_bill ADD COLUMN carListJson TEXT',
);
if (hasOrderListJson) {
// 旧列迁移到新列
await database.execute(
"UPDATE order_hold_bill SET carListJson = orderListJson WHERE carListJson IS NULL",
);
}
}
if (!hasUserJson) {
await database.execute(
'ALTER TABLE order_hold_bill ADD COLUMN userJson TEXT',
);
}
if (!hasPrice) {
await database.execute(
'ALTER TABLE order_hold_bill ADD COLUMN price INTEGER DEFAULT 0 NOT NULL',
);
}
if (!hasExtraPrice) {
await database.execute(
'ALTER TABLE order_hold_bill ADD COLUMN extraPrice INTEGER DEFAULT 0 NOT NULL',
);
}
if (!hasPayPrice) {
await database.execute(
'ALTER TABLE order_hold_bill ADD COLUMN payPrice INTEGER DEFAULT 0 NOT NULL',
);
}
if (!hasCreateTime) {
await database.execute(
'ALTER TABLE order_hold_bill ADD COLUMN createTime TEXT DEFAULT "" 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)",
);
}
});
/// 从版本3到版本4的迁移:添加为user_record表添加discountsList字段
static Migration migration3To4 = Migration(3, 4, (database) async {
final columns = await database.rawQuery("PRAGMA table_info(user_record)");
bool hasDiscountsList = false;
for (final c in columns) {
if (c['name'] == 'discountsList') {
hasDiscountsList = true;
break;
}
}
if (!hasDiscountsList) {
await database
.execute("ALTER TABLE user_record ADD COLUMN discountsList TEXT");
await database.execute(
"UPDATE user_record SET discountsList = '[]' WHERE discountsList IS NULL",
);
}
});
static Migration migration4To5 = Migration(4, 5, (database) async {
final columns = await database.rawQuery("PRAGMA table_info(user_record)");
bool hasDiscountsUpdateTime = false;
for (final c in columns) {
if (c['name'] == 'discountsUpdateTime') {
hasDiscountsUpdateTime = true;
break;
}
}
if (!hasDiscountsUpdateTime) {
await database.execute(
"ALTER TABLE user_record ADD COLUMN discountsUpdateTime TEXT");
}
});
static List<Migration> getDatabaseMigrations() {
return [migration1To2, migration2To3, migration3To4];
}
}