源码
/*** 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上打印出来。*/