Search code examples
rustrust-diesel

is it possible to make diesel to handle multi database?


I am using diesel as my rust orm query lib, now when I using a new database, the diesel migration run command would override the last scheme.rs file. how to make diesel could handle multiple database. For example, each database use a different schema.rs file, and database A would not override the database B shema.rs content. what should I do to make it work as expect? This is my diesel dependencies config:

diesel = { version = "1.4.7", features = ["postgres","32-column-tables"] }

I tried this way from this issue:

[print_schema]
include_schemas = ["schema1", "schema2", "schema3"]
exclude_schemas = ["pg_catalog", "information_schema"] # Implicit defaults include xor exclude
patch_file = ""

[print_schema.schema1]
file = "src/models/schema1.rs"
filter = { except_tables = ["spatial_ref_sys"] }

[print_schema.schema2]
file = "src/models/schema2.rs"
filter = { except_tables = ["ignored_system_table"] }

but give me tips:

$ diesel migration run                                  ‹ruby-2.7.2›
unknown field `include_schemas`, expected one of `file`, `with_docs`, `filter`, `schema`, `patch_file`, `import_types` for key `print_schema`
(base)

what should I do to make it work?


Solution

  • You can use separate config files and use --config-file to pick one when running migrations:

    diesel1.toml:

    [print_schema]
    file = "src/models/schema1.rs"
    filter = { except_tables = ["spatial_ref_sys"] }
    

    diesel2.toml:

    [print_schema]
    file = "src/models/schema2.rs"
    filter = { except_tables = ["ignored_system_table"] }
    

    Use as:

    diesel migration run --config-file=diesel1.toml
    diesel migration run --config-file=diesel2.toml