scala - Table creation in play 2.4 with play-slick 1.0 -


i got play-slick module , running , using evolution in order create required tables in database during application start.

for evolution work required write 1.sql script contains table definitions want create. @ moment looks this:

# --- !ups  create table users (   id uuid not null,   email varchar(255) not null,   password varchar(255) not null,   firstname varchar(255),   lastname varchar(255),   username varchar(255),   age varchar(255),   primary key (id) );  # --- !downs  drop table users; 

so far slick work correctly need know definition of table. have userdao object looks this:

class userdao @inject()(protected val dbconfigprovider: databaseconfigprovider) extends hasdatabaseconfigprovider[jdbcprofile] {   import driver.api._    private val users = tablequery[userstable]    def all(): future[seq[user]] = db.run(users.result)    def insert(user: user): future[user] = db.run(users += user).map { _ => user }    //table definition   private class userstable(tag:tag) extends table[user](tag,"users"){     def id = column[uuid]("id", o.primarykey)     def email = column[string]("email")     def password = column[string]("password")     def firstname = column[option[string]]("firstname")     def lastname = column[option[string]]("lastname")     def username = column[option[string]]("username")     def age = column[int]("age")      def * = (id, email,password,firstname,lastname,username,age) <> ((user.apply _).tupled, user.unapply)   } } 

i have same table definition in 2 different places now. once in 1.sql script , once in userdao class.

i don’t design @ all! having same table definitions in 2 different places doesn't seem right.

is there way generate evolution scripts table definitions inside userdao classes? or there different way generate table definitions during startup (perhaps using slick)? use slick table definition , rid of annoying sql scripts.

i using play-2.4 , play-slick-1.0

thanks lot.

great question - in same boat you!

i'd have dao , code:

tablequery[userstable].schema.create 

which'll create database table you. no need .sql.

correspondingly, drop, use .drop instead of .create.

you can combine table creation of several tables using reduceleft. here's how it:

lazy val alltables = array(   tablequery[acceptancetable].schema,   [... many more ...]   tablequery[usertable].schema ).reduceleft(_ ++ _)  /** create tables in database */ def create = {   alltables.create }  /** delete tables in database */ def drop = {   alltables.drop } 

all need driver api in scope such as:

val profile = slick.driver.h2driver import profile.api._ 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -