github.com/shoshinnikita/budget-manager@v0.7.1-0.20220131195411-8c46ff1c6778/internal/db/pg/migrations/2_add_not_null.go (about)

     1  package migrations
     2  
     3  import (
     4  	"database/sql"
     5  )
     6  
     7  func addNotNullMigration(tx *sql.Tx) error {
     8  	_, err := tx.Exec(`
     9  		ALTER TABLE months
    10  		    ALTER COLUMN year SET NOT NULL,
    11  		    ALTER COLUMN month SET NOT NULL,
    12  		    ALTER COLUMN daily_budget SET NOT NULL,
    13  		    ALTER COLUMN daily_budget SET DEFAULT 0,
    14  		    ALTER COLUMN total_income SET NOT NULL,
    15  		    ALTER COLUMN total_income SET DEFAULT 0,
    16  		    ALTER COLUMN total_spend SET NOT NULL,
    17  		    ALTER COLUMN total_spend SET DEFAULT 0,
    18  		    ALTER COLUMN result SET NOT NULL,
    19  		    ALTER COLUMN result SET DEFAULT 0;
    20  
    21  		ALTER TABLE days
    22  		    ALTER COLUMN month_id SET NOT NULL,
    23  		    ALTER COLUMN day SET NOT NULL,
    24  		    ALTER COLUMN saldo SET NOT NULL,
    25  		    ALTER COLUMN saldo SET DEFAULT 0;
    26  
    27  		ALTER TABLE incomes
    28  		    ALTER COLUMN month_id SET NOT NULL,
    29  		    ALTER COLUMN title SET NOT NULL,
    30  			ALTER COLUMN income SET NOT NULL;
    31  
    32  		ALTER TABLE monthly_payments
    33  		    ALTER COLUMN month_id SET NOT NULL,
    34  		    ALTER COLUMN title SET NOT NULL,
    35  		    ALTER COLUMN cost SET NOT NULL;
    36  
    37  		ALTER TABLE spends
    38  		    ALTER COLUMN day_id SET NOT NULL,
    39  		    ALTER COLUMN title SET NOT NULL,
    40  		    ALTER COLUMN cost SET NOT NULL;
    41  
    42  		ALTER TABLE spend_types
    43  		    ALTER COLUMN name SET NOT NULL;`,
    44  	)
    45  	return err
    46  }