veloren_server/persistence/
diesel_to_rusqlite.rs

1use crate::persistence::{VelorenConnection, error::PersistenceError};
2use tracing::{debug, info};
3
4/// Performs a one-time migration from diesel to refinery migrations. Copies
5/// diesel's __diesel_schema_migrations table records to refinery_schema_history
6/// and drops __diesel_schema_migrations.
7// At some point in the future, when it is deemed no longer necessary to
8// support migrations from pre-rusqlite databases this method should be deleted.
9pub(crate) fn migrate_from_diesel(
10    connection: &mut VelorenConnection,
11) -> Result<(), PersistenceError> {
12    let transaction = connection
13        .connection
14        .transaction()
15        .expect("failed to start transaction");
16    #[rustfmt::skip]
17    let mut stmt = transaction.prepare("
18        SELECT  COUNT(1)
19        FROM    sqlite_master
20        WHERE   type='table'
21        AND     name='__diesel_schema_migrations';
22    ",
23    )?;
24
25    let diesel_migrations_table_exists = stmt.query_row([], |row| {
26        let row_count: i32 = row.get(0)?;
27        Ok(row_count > 0)
28    })?;
29    drop(stmt);
30
31    if !diesel_migrations_table_exists {
32        debug!(
33            "__diesel_schema_migrations table does not exist, skipping diesel to refinery \
34             migration"
35        );
36        return Ok(());
37    }
38
39    #[rustfmt::skip]
40    transaction.execute_batch("
41        -- Create temporary table to store Diesel > Refinery mapping data in
42        CREATE TEMP TABLE IF NOT EXISTS _migration_map (
43            diesel_version VARCHAR(50) NOT NULL,
44            refinery_version INT4 NOT NULL,
45            refinery_name VARCHAR(255) NOT NULL,
46            refinery_checksum VARCHAR(255) NOT NULL
47        );
48
49        -- Insert mapping records to _migration_map
50        INSERT INTO _migration_map VALUES ('20200411202519',1,'character','18301154882232874638');
51        INSERT INTO _migration_map VALUES ('20200419025352',2,'body','6687048722955029379');
52        INSERT INTO _migration_map VALUES ('20200420072214',3,'stats','2322064461300660230');
53        INSERT INTO _migration_map VALUES ('20200524235534',4,'race_species','16440275012526526388');
54        INSERT INTO _migration_map VALUES ('20200527145044',5,'inventory','13535458920968937266');
55        INSERT INTO _migration_map VALUES ('20200528210610',6,'loadout','18209914188629128082');
56        INSERT INTO _migration_map VALUES ('20200602210738',7,'inv_increase','3368217138206467823');
57        INSERT INTO _migration_map VALUES ('20200703194516',8,'skills','9202176632428664476');
58        INSERT INTO _migration_map VALUES ('20200707201052',9,'add_missing_inv_loadout','9127886123837666846');
59        INSERT INTO _migration_map VALUES ('20200710162552',10,'dash_melee_energy_cost_fix','14010543160640061685');
60        INSERT INTO _migration_map VALUES ('20200716044718',11,'migrate_armour_stats','1617484395098403184');
61        INSERT INTO _migration_map VALUES ('20200719223917',12,'update_item_stats','12571040280459413049');
62        INSERT INTO _migration_map VALUES ('20200724191205',13,'fix_projectile_stats','5178981757717265745');
63        INSERT INTO _migration_map VALUES ('20200729204534',14,'power_stat_for_weapons','17299186713398844906');
64        INSERT INTO _migration_map VALUES ('20200806212413',15,'fix_various_problems','17258097957115914749');
65        INSERT INTO _migration_map VALUES ('20200816130513',16,'item_persistence','18222209741267759587');
66        INSERT INTO _migration_map VALUES ('20200925200504',17,'move_sceptres','8956411670404874637');
67        INSERT INTO _migration_map VALUES ('20201107182406',18,'rename_npcweapons','10703468376963165521');
68        INSERT INTO _migration_map VALUES ('20201116173524',19,'move_waypoint_to_stats','10083555685813984571');
69        INSERT INTO _migration_map VALUES ('20201128205542',20,'item_storage','11912657465469442777');
70        INSERT INTO _migration_map VALUES ('20201213172324',21,'shinygem_to_diamond','7188502861698656149');
71        INSERT INTO _migration_map VALUES ('20210124141845',22,'skills','1249519966980586191');
72        INSERT INTO _migration_map VALUES ('20210125202618',23,'purge_duplicate_items','10597564860189510441');
73        INSERT INTO _migration_map VALUES ('20210212054315',24,'remove_duplicate_possess_stick','10774303849135897742');
74        INSERT INTO _migration_map VALUES ('20210220191847',25,'starter_gear','7937903884108396352');
75        INSERT INTO _migration_map VALUES ('20210224230149',26,'weapon_replacements','16314806319051099277');
76        INSERT INTO _migration_map VALUES ('20210301053817',27,'armor_reorganization','17623676960765703100');
77        INSERT INTO _migration_map VALUES ('20210302023541',28,'fix_sturdy_red_backpack','10808562637001569925');
78        INSERT INTO _migration_map VALUES ('20210302041950',29,'fix_other_backpacks','3143452502889073613');
79        INSERT INTO _migration_map VALUES ('20210302182544',30,'fix_leather_set','5238543158379875836');
80        INSERT INTO _migration_map VALUES ('20210303195917',31,'fix_debug_armor','13228825131487923091');
81        INSERT INTO _migration_map VALUES ('20210306213310',32,'reset_sceptre_skills','626800208872263587');
82        INSERT INTO _migration_map VALUES ('20210329012510',33,'fix_amethyst_staff','11008696478673746982');
83
84        -- Create refinery_schema_history table
85        CREATE TABLE refinery_schema_history (
86            version INT4 PRIMARY KEY,
87            name VARCHAR(255),
88            applied_on VARCHAR(255),
89            checksum VARCHAR(255)
90        );
91
92        -- Migrate diesel migration records to refinery migrations table
93        INSERT INTO refinery_schema_history
94        SELECT	m.refinery_version,
95                m.refinery_name,
96                '2021-03-27T00:00:00.000000000+00:00',
97                m.refinery_checksum
98        FROM 	_migration_map m
99        JOIN 	__diesel_schema_migrations d ON (d.version = m.diesel_version);
100
101        DROP TABLE __diesel_schema_migrations;"
102    )?;
103
104    transaction.commit()?;
105    info!("Successfully performed one-time diesel to refinery migration");
106
107    Ok(())
108}