2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2016-05-24 22:38:11 +02:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2016-05-24 22:38:11 +02:00
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2016-05-24 22:38:11 +02:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-05-24 22:38:11 +02:00
|
|
|
|
2016-07-06 12:05:23 +02:00
|
|
|
//! Tests for migrations.
|
|
|
|
//! A random temp directory is created. A database is created within it, and migrations
|
|
|
|
//! are performed in temp sub-directories.
|
2016-05-24 22:38:11 +02:00
|
|
|
|
2018-01-17 10:36:40 +01:00
|
|
|
#[macro_use]
|
|
|
|
extern crate macros;
|
|
|
|
extern crate kvdb_rocksdb;
|
2018-04-13 21:14:53 +02:00
|
|
|
extern crate migration_rocksdb as migration;
|
2020-08-05 06:08:03 +02:00
|
|
|
extern crate tempdir;
|
2017-10-20 12:11:34 +02:00
|
|
|
|
2017-10-12 15:36:27 +02:00
|
|
|
use kvdb_rocksdb::Database;
|
2020-08-05 06:08:03 +02:00
|
|
|
use migration::{Batch, ChangeColumns, Config, Manager, Migration, SimpleMigration};
|
|
|
|
use std::{
|
|
|
|
collections::BTreeMap,
|
|
|
|
io,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
sync::Arc,
|
|
|
|
};
|
|
|
|
use tempdir::TempDir;
|
2016-07-06 12:05:23 +02:00
|
|
|
|
2017-10-20 12:11:34 +02:00
|
|
|
#[inline]
|
2016-07-06 12:05:23 +02:00
|
|
|
fn db_path(path: &Path) -> PathBuf {
|
2020-08-05 06:08:03 +02:00
|
|
|
path.join("db")
|
2016-07-06 12:05:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// initialize a database at the given directory with the given values.
|
|
|
|
fn make_db(path: &Path, pairs: BTreeMap<Vec<u8>, Vec<u8>>) {
|
2020-08-05 06:08:03 +02:00
|
|
|
let db = Database::open_default(path.to_str().unwrap()).expect("failed to open temp database");
|
|
|
|
{
|
|
|
|
let mut transaction = db.transaction();
|
|
|
|
for (k, v) in pairs {
|
|
|
|
transaction.put(None, &k, &v);
|
|
|
|
}
|
|
|
|
|
|
|
|
db.write(transaction)
|
|
|
|
.expect("failed to write db transaction");
|
|
|
|
}
|
2016-07-06 12:05:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// helper for verifying a migrated database.
|
|
|
|
fn verify_migration(path: &Path, pairs: BTreeMap<Vec<u8>, Vec<u8>>) {
|
2020-08-05 06:08:03 +02:00
|
|
|
let db = Database::open_default(path.to_str().unwrap()).unwrap();
|
2016-07-06 12:05:23 +02:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
for (k, v) in pairs {
|
|
|
|
let x = db.get(None, &k).unwrap().unwrap();
|
2016-07-06 12:05:23 +02:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
assert_eq!(&x[..], &v[..]);
|
|
|
|
}
|
2016-05-24 22:38:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Migration0;
|
|
|
|
|
2016-07-06 12:05:23 +02:00
|
|
|
impl SimpleMigration for Migration0 {
|
2020-08-05 06:08:03 +02:00
|
|
|
fn columns(&self) -> Option<u32> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn version(&self) -> u32 {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
|
|
|
|
fn migrated_column_index(&self) -> Option<u32> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn simple_migrate(
|
|
|
|
&mut self,
|
|
|
|
mut key: Vec<u8>,
|
|
|
|
mut value: Vec<u8>,
|
|
|
|
) -> Option<(Vec<u8>, Vec<u8>)> {
|
|
|
|
key.push(0x11);
|
|
|
|
value.push(0x22);
|
|
|
|
|
|
|
|
Some((key, value))
|
|
|
|
}
|
2016-05-24 22:38:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Migration1;
|
|
|
|
|
2016-07-06 12:05:23 +02:00
|
|
|
impl SimpleMigration for Migration1 {
|
2020-08-05 06:08:03 +02:00
|
|
|
fn columns(&self) -> Option<u32> {
|
|
|
|
None
|
|
|
|
}
|
2018-02-22 14:53:10 +01:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
fn version(&self) -> u32 {
|
|
|
|
2
|
|
|
|
}
|
2016-07-28 23:46:24 +02:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
fn migrated_column_index(&self) -> Option<u32> {
|
|
|
|
None
|
|
|
|
}
|
2016-05-24 22:38:11 +02:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
fn simple_migrate(&mut self, key: Vec<u8>, _value: Vec<u8>) -> Option<(Vec<u8>, Vec<u8>)> {
|
|
|
|
Some((key, vec![]))
|
|
|
|
}
|
2016-05-24 22:38:11 +02:00
|
|
|
}
|
|
|
|
|
2016-09-26 18:24:58 +02:00
|
|
|
struct AddsColumn;
|
|
|
|
|
|
|
|
impl Migration for AddsColumn {
|
2020-08-05 06:08:03 +02:00
|
|
|
fn pre_columns(&self) -> Option<u32> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn columns(&self) -> Option<u32> {
|
|
|
|
Some(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn version(&self) -> u32 {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
|
|
|
|
fn migrate(
|
|
|
|
&mut self,
|
|
|
|
source: Arc<Database>,
|
|
|
|
config: &Config,
|
|
|
|
dest: &mut Database,
|
|
|
|
col: Option<u32>,
|
|
|
|
) -> io::Result<()> {
|
|
|
|
let mut batch = Batch::new(config, col);
|
|
|
|
|
|
|
|
for (key, value) in source.iter(col).into_iter().flat_map(|inner| inner) {
|
|
|
|
batch.insert(key.into_vec(), value.into_vec(), dest)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if col == Some(1) {
|
|
|
|
batch.insert(vec![1, 2, 3], vec![4, 5, 6], dest)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
batch.commit(dest)
|
|
|
|
}
|
2016-09-26 18:24:58 +02:00
|
|
|
}
|
|
|
|
|
2016-05-24 22:38:11 +02:00
|
|
|
#[test]
|
|
|
|
fn one_simple_migration() {
|
2020-08-05 06:08:03 +02:00
|
|
|
let tempdir = TempDir::new("").unwrap();
|
|
|
|
let db_path = db_path(tempdir.path());
|
|
|
|
let mut manager = Manager::new(Config::default());
|
|
|
|
make_db(&db_path, map![vec![] => vec![], vec![1] => vec![1]]);
|
|
|
|
let expected = map![vec![0x11] => vec![0x22], vec![1, 0x11] => vec![1, 0x22]];
|
2016-05-24 22:38:11 +02:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
manager.add_migration(Migration0).unwrap();
|
|
|
|
let end_path = manager.execute(&db_path, 0).unwrap();
|
2016-07-06 12:05:23 +02:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
verify_migration(&end_path, expected);
|
2016-05-24 22:38:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-05-26 18:24:51 +02:00
|
|
|
#[should_panic]
|
2016-05-24 22:38:11 +02:00
|
|
|
fn no_migration_needed() {
|
2020-08-05 06:08:03 +02:00
|
|
|
let tempdir = TempDir::new("").unwrap();
|
|
|
|
let db_path = db_path(tempdir.path());
|
|
|
|
let mut manager = Manager::new(Config::default());
|
|
|
|
make_db(&db_path, map![vec![] => vec![], vec![1] => vec![1]]);
|
2016-05-24 22:38:11 +02:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
manager.add_migration(Migration0).unwrap();
|
|
|
|
manager.execute(&db_path, 1).unwrap();
|
2016-05-24 22:38:11 +02:00
|
|
|
}
|
|
|
|
|
2016-07-28 12:05:41 +02:00
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn wrong_adding_order() {
|
2020-08-05 06:08:03 +02:00
|
|
|
let tempdir = TempDir::new("").unwrap();
|
|
|
|
let db_path = db_path(tempdir.path());
|
|
|
|
let mut manager = Manager::new(Config::default());
|
|
|
|
make_db(&db_path, map![vec![] => vec![], vec![1] => vec![1]]);
|
2016-07-28 12:05:41 +02:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
manager.add_migration(Migration1).unwrap();
|
|
|
|
manager.add_migration(Migration0).unwrap();
|
2016-07-28 12:05:41 +02:00
|
|
|
}
|
|
|
|
|
2016-05-24 22:38:11 +02:00
|
|
|
#[test]
|
|
|
|
fn multiple_migrations() {
|
2020-08-05 06:08:03 +02:00
|
|
|
let tempdir = TempDir::new("").unwrap();
|
|
|
|
let db_path = db_path(tempdir.path());
|
|
|
|
let mut manager = Manager::new(Config::default());
|
|
|
|
make_db(&db_path, map![vec![] => vec![], vec![1] => vec![1]]);
|
|
|
|
let expected = map![vec![0x11] => vec![], vec![1, 0x11] => vec![]];
|
2016-05-24 22:38:11 +02:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
manager.add_migration(Migration0).unwrap();
|
|
|
|
manager.add_migration(Migration1).unwrap();
|
|
|
|
let end_path = manager.execute(&db_path, 0).unwrap();
|
2016-07-06 12:05:23 +02:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
verify_migration(&end_path, expected);
|
2016-05-24 22:38:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn second_migration() {
|
2020-08-05 06:08:03 +02:00
|
|
|
let tempdir = TempDir::new("").unwrap();
|
|
|
|
let db_path = db_path(tempdir.path());
|
|
|
|
let mut manager = Manager::new(Config::default());
|
|
|
|
make_db(&db_path, map![vec![] => vec![], vec![1] => vec![1]]);
|
|
|
|
let expected = map![vec![] => vec![], vec![1] => vec![]];
|
2016-05-24 22:38:11 +02:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
manager.add_migration(Migration0).unwrap();
|
|
|
|
manager.add_migration(Migration1).unwrap();
|
|
|
|
let end_path = manager.execute(&db_path, 1).unwrap();
|
2016-07-06 12:05:23 +02:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
verify_migration(&end_path, expected);
|
2016-05-24 22:38:11 +02:00
|
|
|
}
|
2016-05-26 18:24:51 +02:00
|
|
|
|
2016-07-28 12:05:41 +02:00
|
|
|
#[test]
|
|
|
|
fn first_and_noop_migration() {
|
2020-08-05 06:08:03 +02:00
|
|
|
let tempdir = TempDir::new("").unwrap();
|
|
|
|
let db_path = db_path(tempdir.path());
|
|
|
|
let mut manager = Manager::new(Config::default());
|
|
|
|
make_db(&db_path, map![vec![] => vec![], vec![1] => vec![1]]);
|
|
|
|
let expected = map![vec![0x11] => vec![0x22], vec![1, 0x11] => vec![1, 0x22]];
|
2016-07-28 12:05:41 +02:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
manager.add_migration(Migration0).unwrap();
|
|
|
|
let end_path = manager.execute(&db_path, 0).unwrap();
|
2016-07-28 12:05:41 +02:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
verify_migration(&end_path, expected);
|
2016-07-28 12:05:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn noop_and_second_migration() {
|
2020-08-05 06:08:03 +02:00
|
|
|
let tempdir = TempDir::new("").unwrap();
|
|
|
|
let db_path = db_path(tempdir.path());
|
|
|
|
let mut manager = Manager::new(Config::default());
|
|
|
|
make_db(&db_path, map![vec![] => vec![], vec![1] => vec![1]]);
|
|
|
|
let expected = map![vec![] => vec![], vec![1] => vec![]];
|
2016-07-28 12:05:41 +02:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
manager.add_migration(Migration1).unwrap();
|
|
|
|
let end_path = manager.execute(&db_path, 0).unwrap();
|
2016-07-28 12:05:41 +02:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
verify_migration(&end_path, expected);
|
2016-07-28 12:05:41 +02:00
|
|
|
}
|
|
|
|
|
2016-05-26 18:24:51 +02:00
|
|
|
#[test]
|
|
|
|
fn is_migration_needed() {
|
2020-08-05 06:08:03 +02:00
|
|
|
let mut manager = Manager::new(Config::default());
|
|
|
|
manager.add_migration(Migration0).unwrap();
|
|
|
|
manager.add_migration(Migration1).unwrap();
|
2016-05-26 18:24:51 +02:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
assert!(manager.is_needed(0));
|
|
|
|
assert!(manager.is_needed(1));
|
|
|
|
assert!(!manager.is_needed(2));
|
2016-05-26 18:24:51 +02:00
|
|
|
}
|
2016-09-26 18:24:58 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn pre_columns() {
|
2020-08-05 06:08:03 +02:00
|
|
|
let mut manager = Manager::new(Config::default());
|
|
|
|
manager.add_migration(AddsColumn).unwrap();
|
2016-09-26 18:24:58 +02:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
let tempdir = TempDir::new("").unwrap();
|
|
|
|
let db_path = db_path(tempdir.path());
|
2016-09-26 18:24:58 +02:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
// this shouldn't fail to open the database even though it's one column
|
|
|
|
// short of the one before it.
|
|
|
|
manager.execute(&db_path, 0).unwrap();
|
2016-09-26 18:24:58 +02:00
|
|
|
}
|
2017-02-26 19:22:51 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn change_columns() {
|
2020-08-05 06:08:03 +02:00
|
|
|
use kvdb_rocksdb::DatabaseConfig;
|
|
|
|
|
|
|
|
let mut manager = Manager::new(Config::default());
|
|
|
|
manager
|
|
|
|
.add_migration(ChangeColumns {
|
|
|
|
pre_columns: None,
|
|
|
|
post_columns: Some(4),
|
|
|
|
version: 1,
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let tempdir = TempDir::new("").unwrap();
|
|
|
|
let db_path = db_path(tempdir.path());
|
|
|
|
|
|
|
|
let new_path = manager.execute(&db_path, 0).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
db_path, new_path,
|
|
|
|
"Changing columns is an in-place migration."
|
|
|
|
);
|
|
|
|
|
|
|
|
let config = DatabaseConfig::with_columns(Some(4));
|
|
|
|
let db = Database::open(&config, new_path.to_str().unwrap()).unwrap();
|
|
|
|
assert_eq!(db.num_columns(), 4);
|
2017-02-26 19:22:51 +01:00
|
|
|
}
|