源碼
/*** This is a cute function used to replace migration up/down functionality.* It performs check storage schema with actual db schema and:* * if there are excess tables exist in db they are ignored (not dropped)* * every table from storage is compared with it's db analog and* * if table doesn't exist it is being created* * if table exists its colums are being compared with table_info from db and* * if there are columns in db that do not exist in storage (excess) table will be dropped and* recreated* * if there are columns in storage that do not exist in db they will be added using `ALTER TABLE* ... ADD COLUMN ...' command* * if there is any column existing in both db and storage but differs by any of* properties/constraints (type, pk, notnull, dflt_value) table will be dropped and recreated Be aware that* `sync_schema` doesn't guarantee that data will not be dropped. It guarantees only that it will make db* schema the same as you specified in `make_storage` function call. A good point is that if you have no db* file at all it will be created and all tables also will be created with exact tables and columns you* specified in `make_storage`, `make_table` and `make_column` call. The best practice is to call this* function right after storage creation.* @param preserve affects on function behaviour in case it is needed to remove a column. If it is `false`* so table will be dropped if there is column to remove, if `true` - table is being copied into another* table, dropped and copied table is renamed with source table name. Warning: sync_schema doesn't check* foreign keys cause it is unable to do so in sqlite3. If you know how to get foreign key info please* submit an issue https://github.com/fnc12/sqlite_orm/issues* @return std::map with std::string key equal table name and `sync_schema_result` as value.* `sync_schema_result` is a enum value that stores table state after syncing a schema. `sync_schema_result`* can be printed out on std::ostream with `operator<<`.*/std::map<std::string, sync_schema_result> sync_schema(bool preserve = false) {auto con = this->get_connection();std::map<std::string, sync_schema_result> result;auto db = con.get();this->impl.for_each([&result, db, preserve, this](auto &tableImpl) {auto res = this->sync_table(tableImpl, db, preserve);result.insert({tableImpl.table.name, res});});return result;}
翻譯
/*** 這是個可愛的功能,用來替代上/下遷移功能。它用實際的db模式來檢查存儲模式,并且 * * 如果db中存在多余的表,它們會被忽略(不被丟棄)。* 如果數據庫中存在多余的表,它們將被忽略(而不是丟棄)。* *存儲的每一個表都與它的db模擬表進行比較,然后再進行比較。* * 如果表不存在,則正在創建。* * 如果表存在,它的列與來自db的table_info進行比較,然后再進行比較。* *如果db中的列在存儲中不存在(多余的)表將會被刪除,并被刪除。* 重現的* * 如果存儲中的列在db中不存在,將使用`ALTER TABLE'添加。* ... ADD COLUMN ...' 命令* *如果db和存儲中都有任何一列,但因任何一個不同而不同。* 屬性/約束(type, pk, notnull, dflt_value)表將被刪除并重新創建。* `sync_schema`并不能保證數據不會被丟棄。它只保證它將使db* 模式與你在`make_storage`函數調用中指定的模式相同。一個很好的觀點是,如果你沒有db_storage* 文件將被創建,所有的表也將被創建為與你的表和列完全一致的表和列。* 在 "make_storage"、"make_table "和 "make_column "調用中指定。最好的做法是調用這個* 在創建存儲后立即執行函數。* @param preserve在需要刪除一列時影響函數的行為。如果是`false`,則會影響函數的行為。* 因此,如果有列要刪除,表將被刪除,如果`true`----表被復制到另一個表中。* 表,刪除和復制的表用源表名重命名。警告: sync_schema不檢查* 外鍵的原因是在sqlite3中無法做到。如果你知道如何獲取外鍵信息,請告訴我* 提交一個問題https://github.com/fnc12/sqlite_orm/issues* @return std::map with std::string key equal table name and `sync_schema_result` as value.* `sync_schema_result`是一個枚舉值,用于存儲同步模式后表的狀態。`sync_schema_result`是同步模式后存儲表狀態的枚舉值。* 可以用`operator<<`在std::ostream上打印出來。*/
?