Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regenerate migrations #223

Merged
merged 5 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ jobs:
- macos-latest
- windows-2019
runs-on: ${{ matrix.os }}
defaults:
run:
shell: bash

steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -92,3 +95,9 @@ jobs:
run: cd example && cargo +stable test --all-features
- name: Test
run: cd butane && cargo +stable test --all-features
- name: Check example migrations
run: |
set -ex
make regenerate-example-migrations
git add -A
git diff --exit-code
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ edition = "2021"

[workspace.dependencies]
butane = { version = "0.6", path = "butane" }
butane_cli = { version = "0.6", path = "butane_cli" }
butane_core = { version = "0.6", path = "butane_core" }
butane_codegen = { version = "0.6", path = "butane_codegen" }
butane_test_helper = { path = "butane_test_helper" }
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,6 @@ docview :

install :
cd butane_cli && $(CARGO) install --path .

regenerate-example-migrations :
for dir in examples/*; do (cd $$dir; cargo +stable run -p butane_cli --all-features -- regenerate); done
27 changes: 27 additions & 0 deletions butane_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,33 @@ pub fn remove_backend(base_dir: &Path, backend_name: &str) -> Result<()> {
Ok(())
}

/// Regenerate migrations.
pub fn regenerate_migrations(base_dir: &Path) -> Result<()> {
let backends = load_latest_migration_backends(base_dir)?;

let mut migrations = get_migrations(base_dir)?;
let migration_list = migrations.all_migrations()?;

let mut from_migration_name: Option<String> = None;

for m in migration_list {
println!("Updating {}", m.name());
let to_db = m.db()?;
let mut from_migration = None;
if let Some(from_migration_name) = from_migration_name {
from_migration = migrations.get_migration(&from_migration_name);
}

migrations.create_migration_to(&backends, &m.name(), from_migration.as_ref(), to_db)?;

from_migration_name = Some(m.name().to_string());
}

update_embedded(base_dir)?;

Ok(())
}

/// Load the [`db::Backend`]s used in the latest migration.
/// Error if there are no existing migrations.
pub fn load_latest_migration_backends(base_dir: &Path) -> Result<NonEmpty<Box<dyn db::Backend>>> {
Expand Down
5 changes: 4 additions & 1 deletion butane_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::path::PathBuf;
use butane_cli::{
add_backend, base_dir, clean, clear_data, collapse_migrations, delete_table,
detach_latest_migration, embed, get_migrations, handle_error, init, list_backends,
list_migrations, make_migration, migrate, remove_backend, rollback,
list_migrations, make_migration, migrate, regenerate_migrations, remove_backend, rollback,
};
use clap::{ArgAction, Parser, Subcommand};

Expand Down Expand Up @@ -57,6 +57,8 @@ However if the migration has been manually edited, it will need to be manually r
/// Migration to migrate to.
name: Option<String>,
},
/// Regenerate migrations in place.
Regenerate,
/// List migrations.
List,
/// Replace all migrations with a single migration representing the current model state.
Expand Down Expand Up @@ -164,6 +166,7 @@ fn main() {
BackendCommands::List => handle_error(list_backends(&base_dir)),
},
Commands::MakeMigration { name } => handle_error(make_migration(&base_dir, Some(name))),
Commands::Regenerate => handle_error(regenerate_migrations(&base_dir)),
Commands::DetachMigration => handle_error(detach_latest_migration(&base_dir)),
Commands::Migrate { name } => handle_error(migrate(&base_dir, name.to_owned())),
Commands::Rollback { name } => handle_error(rollback(&base_dir, name.to_owned())),
Expand Down
7 changes: 5 additions & 2 deletions butane_core/src/migrations/fsmigrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,11 @@ impl MigrationMut for FsMigration {
self.write_sql(&format!("{backend_name}_up"), up_sql)?;
self.write_sql(&format!("{backend_name}_down"), down_sql)?;
let mut info = self.info()?;
info.backends.push(backend_name.to_string());
self.write_info(&info)?;
let backend_name = backend_name.to_string();
if !info.backends.contains(&backend_name) {
info.backends.push(backend_name.to_string());
self.write_info(&info)?;
}
Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions butane_test_helper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ macro_rules! maketest {
log::info!("connecting to {}..", &$connstr);
let mut conn = backend.connect(&$connstr).expect("Could not connect backend");
butane_test_helper::setup_db(backend, &mut conn, $migrate);
log::info!("running test on {}..", &$connstr);
$fname(conn);
butane_test_helper::[<$backend _teardown>]($dataname);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
{"name":"Blog","columns":[{"name":"id","sqltype":{"Known":"BigInt"},"nullable":false,"pk":true,"auto":true,"default":null},{"name":"name","sqltype":{"Known":"Text"},"nullable":false,"pk":false,"auto":false,"default":null}]}
{
"name": "Blog",
"columns": [
{
"name": "id",
"sqltype": {
"Known": "BigInt"
},
"nullable": false,
"pk": true,
"auto": true,
"unique": false,
"default": null
},
{
"name": "name",
"sqltype": {
"Known": "Text"
},
"nullable": false,
"pk": false,
"auto": false,
"unique": false,
"default": null
}
]
}
Original file line number Diff line number Diff line change
@@ -1 +1,71 @@
{"name":"Post","columns":[{"name":"id","sqltype":{"Known":"Int"},"nullable":false,"pk":true,"auto":true,"default":null},{"name":"title","sqltype":{"Known":"Text"},"nullable":false,"pk":false,"auto":false,"default":null},{"name":"body","sqltype":{"Known":"Text"},"nullable":false,"pk":false,"auto":false,"default":null},{"name":"published","sqltype":{"Known":"Bool"},"nullable":false,"pk":false,"auto":false,"default":null},{"name":"blog","sqltype":{"Known":"BigInt"},"nullable":false,"pk":false,"auto":false,"default":null},{"name":"byline","sqltype":{"Known":"Text"},"nullable":true,"pk":false,"auto":false,"default":null}]}
{
"name": "Post",
"columns": [
{
"name": "id",
"sqltype": {
"Known": "Int"
},
"nullable": false,
"pk": true,
"auto": true,
"unique": false,
"default": null
},
{
"name": "title",
"sqltype": {
"Known": "Text"
},
"nullable": false,
"pk": false,
"auto": false,
"unique": false,
"default": null
},
{
"name": "body",
"sqltype": {
"Known": "Text"
},
"nullable": false,
"pk": false,
"auto": false,
"unique": false,
"default": null
},
{
"name": "published",
"sqltype": {
"Known": "Bool"
},
"nullable": false,
"pk": false,
"auto": false,
"unique": false,
"default": null
},
{
"name": "blog",
"sqltype": {
"Known": "BigInt"
},
"nullable": false,
"pk": false,
"auto": false,
"unique": false,
"default": null
},
{
"name": "byline",
"sqltype": {
"Known": "Text"
},
"nullable": true,
"pk": false,
"auto": false,
"unique": false,
"default": null
}
]
}
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
{"name":"Post_tags_Many","columns":[{"name":"owner","sqltype":{"Known":"Int"},"nullable":false,"pk":false,"auto":false,"default":null},{"name":"has","sqltype":{"Known":"Text"},"nullable":false,"pk":false,"auto":false,"default":null}]}
{
"name": "Post_tags_Many",
"columns": [
{
"name": "owner",
"sqltype": {
"Known": "Int"
},
"nullable": false,
"pk": false,
"auto": false,
"unique": false,
"default": null
},
{
"name": "has",
"sqltype": {
"Known": "Text"
},
"nullable": false,
"pk": false,
"auto": false,
"unique": false,
"default": null
}
]
}
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
{"name":"Tag","columns":[{"name":"tag","sqltype":{"Known":"Text"},"nullable":false,"pk":true,"auto":false,"default":null}]}
{
"name": "Tag",
"columns": [
{
"name": "tag",
"sqltype": {
"Known": "Text"
},
"nullable": false,
"pk": true,
"auto": false,
"unique": false,
"default": null
}
]
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DROP TABLE Blog;
DROP TABLE Post_tags_Many;
DROP TABLE Post;
DROP TABLE Tag;
DROP TABLE Post_tags_Many;
DROP TABLE Tag;
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
CREATE TABLE Tag (
tag TEXT NOT NULL PRIMARY KEY
);
CREATE TABLE Post_tags_Many (
owner INTEGER NOT NULL,
has TEXT NOT NULL
CREATE TABLE Blog (
id INTEGER NOT NULL PRIMARY KEY,
"name" TEXT NOT NULL
);
CREATE TABLE Post (
id INTEGER NOT NULL PRIMARY KEY,
title TEXT NOT NULL,
body TEXT NOT NULL,
published INTEGER NOT NULL,
blog INTEGER NOT NULL,
byline TEXT
byline TEXT
);
CREATE TABLE Blog (
id INTEGER NOT NULL PRIMARY KEY,
name TEXT NOT NULL
CREATE TABLE Post_tags_Many (
owner INTEGER NOT NULL,
has TEXT NOT NULL
);
CREATE TABLE Tag (
tag TEXT NOT NULL PRIMARY KEY
);
CREATE TABLE IF NOT EXISTS butane_migrations (
"name" TEXT NOT NULL PRIMARY KEY
);
CREATE TABLE butane_migrations (
name TEXT NOT NULL PRIMARY KEY
);
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
{"name":"Blog","columns":[{"name":"id","sqltype":{"Known":"BigInt"},"nullable":false,"pk":true,"auto":true,"default":null},{"name":"name","sqltype":{"Known":"Text"},"nullable":false,"pk":false,"auto":false,"default":null}]}
{
"name": "Blog",
"columns": [
{
"name": "id",
"sqltype": {
"Known": "BigInt"
},
"nullable": false,
"pk": true,
"auto": true,
"unique": false,
"default": null
},
{
"name": "name",
"sqltype": {
"Known": "Text"
},
"nullable": false,
"pk": false,
"auto": false,
"unique": false,
"default": null
}
]
}
Loading