kotlin SQLDelight迁移

f87krz0w  于 2023-02-24  发布在  Kotlin
关注(0)|答案(2)|浏览(140)

我正在尝试在SQLDelight的表中添加更多列。我创建了迁移文件1.sqm。在迁移文件中,它给出了找不到表的错误。

我的版本.gradle.kts:

sqldelight {
    database("AppDatabase") {
        packageName = "com.jetbrains.handson.kmm.shared.cache"
        schemaOutputDirectory = file("shared/src/commonMain/sqldelight/com/jetbrains/handson/kmm/shared/cache/AppDatabase.sq")
        migrationOutputDirectory = file("shared/src/commonMain/sqldelight/migrations/1.sqm")
        migrationOutputFileFormat = ".sqm" 
        schemaOutputDirectory
        deriveSchemaFromMigrations = true
    }
}

创建表语句:

CREATE TABLE pos_orders(id INTEGER AS Int PRIMARY KEY, orderId INTEGER AS Int , dateandtime TEXT, restaurant_id INTEGER AS Int, restaurant_name TEXT, deliveryType_description TEXT, paymentType_description TEXT, totalAmount REAL AS Float,
process_order INTEGER AS Int, pos_accept INTEGER AS Int, pos_printed INTEGER AS Int, status INTEGER AS Int, prep_time INTEGER AS Int, desired_delivery_time TEXT, restaurant_address TEXT, customerNo TEXT, title TEXT, firstName TEXT, lastName TEXT,company TEXT, street TEXT, houseNo TEXT, zip TEXT, city TEXT, state TEXT,floor TEXT, bezirk TEXT, bellname TEXT, email TEXT, phoneNo TEXT, fax TEXT, taxAmount REAL AS Float, total REAL AS Float , deliverycharges REAL AS Float,
deliveryType INTEGER AS Int, paymentType INTEGER AS Int, comment TEXT, order_note TEXT, sender TEXT, cancelation_cause TEXT, demo_order INTEGER AS Int, orderDate TEXT, updated TEXT);
dpiehjr4

dpiehjr41#

sqldelight {
    database("AppDatabase") {
        packageName = "com.jetbrains.handson.kmm.shared.cache"
        schemaOutputDirectory = file("com.jetbrains.handson.kmm.shared.cache")
        migrationOutputDirectory = file("com.jetbrains.handson.kmm.shared.cache")
        deriveSchemaFromMigrations = true
        verifyMigrations = true
    }
}

将www.example.com更改gradle.build为上面的文件夹,然后在路径中完全按照我对两个文件夹***sqldelight***编写的路径

  • 共享的/源代码/公用主/sql喜悦/com/jetbrains/handson/kmm/共享的/缓存/sql喜悦 *

在sqldleight文件夹中放置所有数据库文件AppDatabase.sq、1.sqm、2.sqm...等等

诀窍就在这里在AppDatabase.sq中只放置数据库查询。

AppDatabase应如下所示

getAll:
SELECT * FROM pos_orders;

deleteAll:
DELETE FROM pos_orders;

创建另一个1.sqm来放置原始表,创建2.sqm来放置迁移。

  1. sqm应类似
CREATE TABLE pos_orders(id INTEGER AS Int PRIMARY KEY, orderId INTEGER AS Int , dateandtime TEXT, restaurant_id INTEGER AS Int, restaurant_name TEXT, deliveryType_description TEXT, paymentType_description TEXT, totalAmount REAL AS Float,
process_order INTEGER AS Int, pos_accept INTEGER AS Int, pos_printed INTEGER AS Int, status INTEGER AS Int, prep_time INTEGER AS Int, desired_delivery_time TEXT, restaurant_address TEXT, customerNo TEXT, title TEXT, firstName TEXT, lastName TEXT,company TEXT, street TEXT, houseNo TEXT, zip TEXT, city TEXT, state TEXT,floor TEXT, bezirk TEXT, bellname TEXT, email TEXT, phoneNo TEXT, fax TEXT, taxAmount REAL AS Float, total REAL AS Float , deliverycharges REAL AS Float,
deliveryType INTEGER AS Int, paymentType INTEGER AS Int, comment TEXT, order_note TEXT, sender TEXT, cancelation_cause TEXT, demo_order INTEGER AS Int, orderDate TEXT, updated TEXT);
  1. sqm应类似
ALTER TABLE pos_orders ADD COLUMN year INTEGER ;
jhdbpxl9

jhdbpxl92#

当我从gradle中删除除packageName之外的所有内容时,错误“未找到表...”仍然存在,但迁移按预期工作。
下面是我的示例,我需要从表中删除一些列

sqldelight {
    database("AppDatabase") {
        packageName = "com.my.domain.db"
    }
}

1.平方米文件

CREATE TEMPORARY TABLE profileTemp (
id TEXT NOT NULL PRIMARY KEY,
name TEXT,
birthdate TEXT,
);
INSERT INTO profileTemp SELECT id, name, birthdate, FROM ProfileSQL;
DROP TABLE ProfileSQL;
CREATE TABLE ProfileSQL (
id TEXT NOT NULL PRIMARY KEY,
name TEXT,
birthdate TEXT,
);
INSERT INTO ProfileSQL SELECT id, name, birthdate, FROM profileTemp;
DROP TABLE profileTemp;

相关问题