2626
2727def upgrade () -> None :
2828 # ### commands auto generated by Alembic - please adjust! ###
29- op .execute ("DELETE FROM sessions.launchers" )
30- op .drop_column ("launchers" , "default_url" , schema = "sessions" )
31- op .drop_column ("launchers" , "environment_kind" , schema = "sessions" )
32- op .drop_column ("launchers" , "container_image" , schema = "sessions" )
29+ # Migrate session launchers
3330 op .execute ("DROP TYPE environmentkind CASCADE" )
3431 op .execute ("CREATE TYPE environmentkind AS ENUM ('GLOBAL', 'CUSTOM')" )
35- op .add_column ("environments" , sa .Column ("port" , sa .Integer (), nullable = True ), schema = "sessions" )
36- op .add_column ("environments" , sa .Column ("working_directory" , sa .String (), nullable = True ), schema = "sessions" )
37- op .add_column ("environments" , sa .Column ("mount_directory" , sa .String (), nullable = True ), schema = "sessions" )
38- op .add_column ("environments" , sa .Column ("uid" , sa .Integer (), nullable = True ), schema = "sessions" )
39- op .add_column ("environments" , sa .Column ("gid" , sa .Integer (), nullable = True ), schema = "sessions" )
4032 op .add_column (
4133 "environments" ,
4234 sa .Column ("environment_kind" , sa .Enum ("GLOBAL" , "CUSTOM" , name = "environmentkind" ), nullable = True ),
4335 schema = "sessions" ,
4436 )
37+ op .execute ("UPDATE sessions.environments SET environment_kind = 'GLOBAL' WHERE environment_kind is NULL" )
38+ # NOTE: When the session launcher has environment_id set to null then it is a custom environment
39+ # NOTE: We populate the name column in the new environment with the session launcher id
40+ # This way we can join the newly add environments with their launchers and update the foreign key in the launchers
41+ # with the newly created environments.
42+ # NOTE: Since postgres cannot autogenerate ulids we use the session launcher ulid when creating the environment
43+ op .execute (
44+ "INSERT INTO sessions.environments(id, name, created_by_id, creation_date, container_image, default_url, environment_kind) "
45+ "SELECT generate_ulid(), id, created_by_id, creation_date, container_image, default_url, 'CUSTOM' "
46+ "FROM sessions.launchers "
47+ "WHERE environment_id IS NULL"
48+ )
49+ op .execute (
50+ "UPDATE sessions.launchers "
51+ "SET environment_id = sessions.environments.id "
52+ "FROM sessions.environments "
53+ "WHERE sessions.environments.name = sessions.launchers.id"
54+ )
55+ # NOTE: Make the environment name human readable when the join and udpate is done
56+ op .execute (
57+ "UPDATE sessions.environments "
58+ "SET name = CONCAT('Custom environment for session launcher ID ', name) "
59+ "WHERE environment_kind = 'CUSTOM'"
60+ )
61+ # Drop unused fields from session launchers
62+ op .drop_column ("launchers" , "default_url" , schema = "sessions" )
63+ # op.drop_column("launchers", "environment_kind", schema="sessions")
64+ op .drop_column ("launchers" , "container_image" , schema = "sessions" )
65+ # Add new fields to environment
66+ op .add_column ("environments" , sa .Column ("port" , sa .Integer (), nullable = True ), schema = "sessions" )
67+ op .add_column ("environments" , sa .Column ("working_directory" , sa .String (), nullable = True ), schema = "sessions" )
68+ op .add_column ("environments" , sa .Column ("mount_directory" , sa .String (), nullable = True ), schema = "sessions" )
69+ op .add_column ("environments" , sa .Column ("uid" , sa .Integer (), nullable = True ), schema = "sessions" )
70+ op .add_column ("environments" , sa .Column ("gid" , sa .Integer (), nullable = True ), schema = "sessions" )
4571 op .execute (sa .text ("UPDATE sessions.environments SET port = :port WHERE port is NULL" ).bindparams (port = port ))
4672 op .execute (
4773 sa .text (
@@ -55,12 +81,12 @@ def upgrade() -> None:
5581 )
5682 op .execute (sa .text ("UPDATE sessions.environments SET uid = :uid WHERE uid is NULL" ).bindparams (uid = uid ))
5783 op .execute (sa .text ("UPDATE sessions.environments SET gid = :gid WHERE gid is NULL" ).bindparams (gid = gid ))
58- op .execute ("UPDATE sessions.environments SET environment_kind = 'GLOBAL' WHERE environment_kind is NULL" )
5984 op .execute (
6085 sa .text ("UPDATE sessions.environments SET default_url = :default_url WHERE default_url is NULL" ).bindparams (
6186 default_url = default_url
6287 )
6388 )
89+ # Set proper nullable constraints
6490 op .alter_column ("environments" , "port" , nullable = False , schema = "sessions" )
6591 op .alter_column ("environments" , "working_directory" , nullable = False , schema = "sessions" )
6692 op .alter_column ("environments" , "mount_directory" , nullable = False , schema = "sessions" )
@@ -75,13 +101,7 @@ def upgrade() -> None:
75101
76102def downgrade () -> None :
77103 # ### commands auto generated by Alembic - please adjust! ###
78- op .drop_column ("environments" , "environment_kind" , schema = "sessions" )
79- op .drop_column ("environments" , "gid" , schema = "sessions" )
80- op .drop_column ("environments" , "uid" , schema = "sessions" )
81- op .drop_column ("environments" , "mount_directory" , schema = "sessions" )
82- op .drop_column ("environments" , "working_directory" , schema = "sessions" )
83- op .drop_column ("environments" , "port" , schema = "sessions" )
84- op .execute ("DROP TYPE environmentkind" )
104+ op .execute ("ALTER TYPE environmentkind RENAME TO environmentkind_old;" )
85105 op .execute ("CREATE TYPE environmentkind AS ENUM ('global_environment', 'container_image')" )
86106 op .add_column (
87107 "launchers" ,
@@ -94,7 +114,7 @@ def downgrade() -> None:
94114 "environment_kind" ,
95115 postgresql .ENUM ("global_environment" , "container_image" , name = "environmentkind" ),
96116 autoincrement = False ,
97- nullable = False ,
117+ nullable = True ,
98118 ),
99119 schema = "sessions" ,
100120 )
@@ -103,7 +123,26 @@ def downgrade() -> None:
103123 sa .Column ("default_url" , sa .VARCHAR (length = 200 ), autoincrement = False , nullable = True ),
104124 schema = "sessions" ,
105125 )
126+ # Move the custom environments spec back into the session launcher table
127+ op .execute (
128+ "UPDATE sessions.launchers "
129+ "SET default_url = environments.default_url, "
130+ "container_image = environments.container_image, "
131+ "environment_kind = 'container_image' "
132+ "FROM sessions.environments "
133+ "WHERE launchers.environment_id = environments.id AND "
134+ "environments.environment_kind = 'CUSTOM'"
135+ )
136+ op .execute ("UPDATE sessions.launchers SET environment_kind = 'global_environment' WHERE environment_kind IS NULL" )
137+ op .alter_column ("launchers" , "environment_kind" , nullable = False , schema = "sessions" )
106138 op .alter_column (
107139 "environments" , "default_url" , existing_type = sa .VARCHAR (length = 200 ), nullable = True , schema = "sessions"
108140 )
141+ op .drop_column ("environments" , "environment_kind" , schema = "sessions" )
142+ op .drop_column ("environments" , "gid" , schema = "sessions" )
143+ op .drop_column ("environments" , "uid" , schema = "sessions" )
144+ op .drop_column ("environments" , "mount_directory" , schema = "sessions" )
145+ op .drop_column ("environments" , "working_directory" , schema = "sessions" )
146+ op .drop_column ("environments" , "port" , schema = "sessions" )
147+ op .execute ("DROP TYPE environmentkind_old CASCADE" )
109148 # ### end Alembic commands ###
0 commit comments