Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Slick support changing the schema dynamically per query?

I want to use postgresql to support a multi-tenant environment, so each customer will have its own schema.

When I make a sql query, can I change the schema dynamically when using Slick?

If so, how?

like image 660
loyalflow Avatar asked Feb 28 '14 22:02

loyalflow


1 Answers

You can parameterize your Table classes and TableQuery factories.

trait Schema{
  def name: String
}
case object Customer1 extends Schema{
  def name = "CUSTOMER_1"
}
case object Customer2 extends Schema{
  def name = "CUSTOMER_2"
}
class MyTable(tag: Tag, schema: Option[String]) extends Table[...](tag, schema){
  ...
}

def myTable(schema: Schema) = new TableQuery( new MyTable(_,Some(schema.name)) )

myTable(Customer1).filter(_.id == 5).run
like image 63
cvogt Avatar answered Nov 07 '22 03:11

cvogt