Skip to content

Instantly share code, notes, and snippets.

@sgrif
Created April 16, 2016 19:09
Show Gist options
  • Save sgrif/975bfd8418cde8766ab82ac001f1cd84 to your computer and use it in GitHub Desktop.
Save sgrif/975bfd8418cde8766ab82ac001f1cd84 to your computer and use it in GitHub Desktop.
mod embedded_migrations {
struct EmbeddedMigration {
version: &'static str,
up_sql: &'static str,
};
impl Migration for EmbeddedMigration {
fn version(&self) -> &str {
self.version
}
fn run(&self, conn: &SimpleConnection) -> Result<(), RunMigrationsError> {
conn.batch_execute(self.up_sql).map_err(Into::into)
}
fn revert(&self, conn: &SimpleConnection) -> Result<(), RunMigrationsError> {
unreachable!()
}
}
const ALL_MIGRATIONS: &'static [&'static Migration] = &[
&EmbeddedMigration {
version: "20151219180527",
up_sql: "CREATE TABLE users (\n id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\n name VARCHAR NOT NULL,\n hair_color VARCHAR\n);\n\nCREATE TABLE posts (\n id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\n user_id INTEGER NOT NULL,\n title VARCHAR NOT NULL,\n body TEXT\n);\n\nCREATE TABLE comments (\n id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\n post_id INTEGER NOT NULL,\n text TEXT NOT NULL\n);\n",
},
&EmbeddedMigration {
version: "20160116104628",
up_sql: "CREATE TABLE special_posts (\n id INTEGER PRIMARY KEY AUTOINCREMENT,\n user_id INTEGER NOT NULL,\n title VARCHAR NOT NULL\n);\n\nCREATE TABLE special_comments (\n id INTEGER PRIMARY KEY AUTOINCREMENT,\n special_post_id INTEGER NOT NULL\n);\n",
},
];
pub fn run<C: MigrationConnection>(conn: &C) -> Result<(), RunMigrationsError> {
run_with_output(conn, &mut io::sink())
}
pub fn run_with_output<C: MigrationConnection>(conn: &C, out: &mut io::Write) -> Result<(), RunMigrationsError> {
run_migrations(conn, ALL_MIGRATIONS.iter().map(|v| *v), out)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment