@@ -48,24 +48,19 @@ type DatabaseInfo struct {
4848func NewDatabaseManager (projectName , dataDir string ) (* DatabaseManager , error ) {
4949 ctx := context .Background ()
5050
51- // Create Docker client
5251 dockerClient , err := client .NewClientWithOpts (client .FromEnv , client .WithAPIVersionNegotiation ())
5352 if err != nil {
5453 return nil , fmt .Errorf ("failed to create Docker client: %w" , err )
5554 }
5655
57- // Try to use standard PostgreSQL port (5432), fall back to random if unavailable
5856 port , err := netx .ReservePort (5432 )
5957 if err != nil {
60- // Port 5432 is taken, get a random available port
61- port , err = netx .GetNextPort (netx .MinPort (5432 ), netx .MaxPort (6000 ))
58+ port , err = netx .GetNextPort (netx .MinPort (5433 ), netx .MaxPort (6000 ))
6259 if err != nil {
6360 return nil , fmt .Errorf ("failed to allocate port for PostgreSQL: %w" , err )
6461 }
6562 }
6663
67- // Generate a consistent volume name for this project
68- // Sanitize project name for Docker volume naming (alphanumeric, hyphens, underscores only)
6964 sanitizedName := sanitizeVolumeName (projectName )
7065 volumeName := fmt .Sprintf ("suga-%s-postgres-data" , sanitizedName )
7166
@@ -80,10 +75,8 @@ func NewDatabaseManager(projectName, dataDir string) (*DatabaseManager, error) {
8075
8176// Start initializes and starts the PostgreSQL Docker container
8277func (m * DatabaseManager ) Start () error {
83- // Create or verify the Docker volume exists
8478 _ , err := m .dockerClient .VolumeInspect (m .ctx , m .volumeName )
8579 if err != nil {
86- // Volume doesn't exist, create it
8780 _ , err = m .dockerClient .VolumeCreate (m .ctx , volume.CreateOptions {
8881 Name : m .volumeName ,
8982 Labels : map [string ]string {
@@ -96,7 +89,6 @@ func (m *DatabaseManager) Start() error {
9689 }
9790 }
9891
99- // Pull the PostgreSQL image
10092 fmt .Printf ("Pulling PostgreSQL image %s (this may take some time on first run)...\n \n " ,
10193 style .Cyan (postgresImage ))
10294 reader , err := m .dockerClient .ImagePull (m .ctx , postgresImage , image.PullOptions {})
@@ -108,7 +100,6 @@ func (m *DatabaseManager) Start() error {
108100 // Wait for image pull to complete
109101 _ , _ = io .Copy (io .Discard , reader )
110102
111- // Create container configuration
112103 containerConfig := & container.Config {
113104 Image : postgresImage ,
114105 Env : []string {
@@ -140,7 +131,6 @@ func (m *DatabaseManager) Start() error {
140131 AutoRemove : true ,
141132 }
142133
143- // Create the container
144134 resp , err := m .dockerClient .ContainerCreate (
145135 m .ctx ,
146136 containerConfig ,
@@ -155,12 +145,10 @@ func (m *DatabaseManager) Start() error {
155145
156146 m .containerID = resp .ID
157147
158- // Start the container
159148 if err := m .dockerClient .ContainerStart (m .ctx , m .containerID , container.StartOptions {}); err != nil {
160149 return fmt .Errorf ("failed to start PostgreSQL container: %w" , err )
161150 }
162151
163- // Wait for PostgreSQL to be ready
164152 if err := m .waitForPostgres (); err != nil {
165153 return fmt .Errorf ("PostgreSQL failed to become ready: %w" , err )
166154 }
@@ -196,7 +184,6 @@ func (m *DatabaseManager) waitForPostgres() error {
196184
197185// CreateDatabase creates a new database in the PostgreSQL instance if it doesn't exist
198186func (m * DatabaseManager ) CreateDatabase (name string , intent schema.DatabaseIntent ) error {
199- // Connect to the default postgres database
200187 connStr := fmt .Sprintf ("host=localhost port=%d user=%s password=%s dbname=%s sslmode=disable" ,
201188 m .port , postgresUser , postgresPassword , postgresDB )
202189
@@ -219,7 +206,6 @@ func (m *DatabaseManager) CreateDatabase(name string, intent schema.DatabaseInte
219206 // Database already exists, continue
220207 }
221208
222- // Store database info
223209 m .databases [name ] = & DatabaseInfo {
224210 name : name ,
225211 intent : intent ,
@@ -286,19 +272,15 @@ func (m *DatabaseManager) GetDatabases() []string {
286272// sanitizeVolumeName sanitizes a project name for use in Docker volume names
287273// Docker volume names must match: [a-zA-Z0-9][a-zA-Z0-9_.-]+
288274func sanitizeVolumeName (name string ) string {
289- // Convert to lowercase
290275 result := strings .ToLower (name )
291276
292- // Replace invalid characters with hyphens
293277 invalidChars := regexp .MustCompile (`[^a-z0-9_.-]` )
294278 result = invalidChars .ReplaceAllString (result , "-" )
295279
296- // Ensure it doesn't start with a hyphen or dot
297280 if len (result ) > 0 && (result [0 ] == '-' || result [0 ] == '.' ) {
298281 result = "app" + result
299282 }
300283
301- // Fallback if empty
302284 if result == "" {
303285 result = "app"
304286 }
0 commit comments