pub const HISTORY_TABLE: &str = "_rustyroad_history";
pub const TYPE_MIGRATION: &str = "migration";
pub const TYPE_BASELINE: &str = "baseline";
pub(super) fn create_table() -> String {
format!(
"CREATE TABLE IF NOT EXISTS {HISTORY_TABLE} (
name TEXT NOT NULL PRIMARY KEY,
parent TEXT REFERENCES {HISTORY_TABLE} (name),
done BOOLEAN NOT NULL DEFAULT FALSE,
migration_type TEXT NOT NULL DEFAULT '{TYPE_MIGRATION}',
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
)"
)
}
pub(super) fn create_indexes() -> Vec<String> {
vec![
format!(
"CREATE UNIQUE INDEX IF NOT EXISTS {HISTORY_TABLE}_only_one_active \
ON {HISTORY_TABLE} ((done)) WHERE done = FALSE"
),
format!(
"CREATE UNIQUE INDEX IF NOT EXISTS {HISTORY_TABLE}_single_root \
ON {HISTORY_TABLE} ((parent IS NULL)) WHERE parent IS NULL"
),
format!(
"CREATE UNIQUE INDEX IF NOT EXISTS {HISTORY_TABLE}_linear \
ON {HISTORY_TABLE} (parent)"
),
]
}