Support migration of the database schema and data #184
Comments
|
Is this the flow you would be proposing during an upgrade ?
|
|
@sbose78 no, so far we have the Here's a possible layout for a migration/schema-version table:
The The
Does that sound logical? I've implemented the above logic in SQL once and it worked okay. @tsmaeder any ideas? |
|
Can we check this project? |
|
Taken from @aslakknutsen 's gist: var order [][]func()
order = append(order, []func(db) {ExecSQLFile("add_customer.sql"), MigrateCustomerFieldName})
order = append(order, []func(db) {ExecSQLFile("add_some_other")})
// db.tx.begin // TODO: Make sure currentVersion is read inside TX
for verison := currentVersion() && version < size(order) {
for migrationFun := order[version] {
migrationFunc(db)
}
updateVersion()
}
// db.tx.comit |
|
Idea from @aslakknutsen: turn on postgres driver output during migration for better mid-way error detection . |
|
Group idea: Think about how to handle great version deltas: If delta is bigger than 2, require a downtime. |
Hi @baijum . Just to recap yesterday's meeting: We decided that we ( @aslakknutsen , @tsmaeder , @sbose78 , and @pranavgore09 ) won't use this library for a few reasons. It is mostly related to the features not being pretty relevant to us.
|
|
We should embed the migration in the code by using |
|
@kwk jut fyi, we're already using bindata; https://github.com/almighty/almighty-core/blob/master/Makefile#L160 |
Thanks @aslakknutsen for pointing out. |
|
Two points I'd like to make:
|
|
@kwk I found a project with similar scope and removed unwanted features. Here it is: https://github.com/baijum/pgmigration |
…nd data Build sqlbindata.go with go-bindata from migration/sql-files To be able to have all migration/sql-files/*.sql files bundled in the alm core binary, I've used go-bindata to pack them into the migration/sqlbindata.go file. The prefix `migration/sql-files` is removed from the filepaths inside of `migration/sqlbinata.go`. The first sql file (./migration/sql-files/000-bootstrap.sql) creates the basic layout of the versioning table. The auto migration portion in migration/migration.go has been removed. Logic to get the current version and to execute all remaining version is implemented.
…nd data Build sqlbindata.go with go-bindata from migration/sql-files To be able to have all migration/sql-files/*.sql files bundled in the alm core binary, I've used go-bindata to pack them into the migration/sqlbindata.go file. The prefix `migration/sql-files` is removed from the filepaths inside of `migration/sqlbinata.go`. The first sql file (./migration/sql-files/000-bootstrap.sql) creates the basic layout of the versioning table. The auto migration portion in migration/migration.go has been removed. Logic to get the current version and to execute all remaining version is implemented.
…nd data Build sqlbindata.go with go-bindata from migration/sql-files To be able to have all migration/sql-files/*.sql files bundled in the alm core binary, I've used go-bindata to pack them into the migration/sqlbindata.go file. The prefix `migration/sql-files` is removed from the filepaths inside of `migration/sqlbinata.go`. The first sql file (./migration/sql-files/000-bootstrap.sql) creates the basic layout of the versioning table. The auto migration portion in migration/migration.go has been removed. Logic to get the current version and to execute all remaining version is implemented.
…nd data Build sqlbindata.go with go-bindata from migration/sql-files To be able to have all migration/sql-files/*.sql files bundled in the alm core binary, I've used go-bindata to pack them into the migration/sqlbindata.go file. The prefix `migration/sql-files` is removed from the filepaths inside of `migration/sqlbinata.go`. The first sql file (./migration/sql-files/000-bootstrap.sql) creates the basic layout of the versioning table. The auto migration portion in migration/migration.go has been removed. Logic to get the current version and to execute all remaining version is implemented.
…nd data Build sqlbindata.go with go-bindata from migration/sql-files To be able to have all migration/sql-files/*.sql files bundled in the alm core binary, I've used go-bindata to pack them into the migration/sqlbindata.go file. The prefix `migration/sql-files` is removed from the filepaths inside of `migration/sqlbinata.go`. The first sql file (./migration/sql-files/000-bootstrap.sql) creates the basic layout of the versioning table. The auto migration portion in migration/migration.go has been removed. Logic to get the current version and to execute all remaining version is implemented.
…nd data Build sqlbindata.go with go-bindata from migration/sql-files To be able to have all migration/sql-files/*.sql files bundled in the alm core binary, I've used go-bindata to pack them into the migration/sqlbindata.go file. The prefix `migration/sql-files` is removed from the filepaths inside of `migration/sqlbinata.go`. The first sql file (./migration/sql-files/000-bootstrap.sql) creates the basic layout of the versioning table. The auto migration portion in migration/migration.go has been removed. Logic to get the current version and to execute all remaining version is implemented.
…nd data Build sqlbindata.go with go-bindata from migration/sql-files To be able to have all migration/sql-files/*.sql files bundled in the alm core binary, I've used go-bindata to pack them into the migration/sqlbindata.go file. The prefix `migration/sql-files` is removed from the filepaths inside of `migration/sqlbinata.go`. The first sql file (./migration/sql-files/000-bootstrap.sql) creates the basic layout of the versioning table. The auto migration portion in migration/migration.go has been removed. Logic to get the current version and to execute all remaining version is implemented.
Fixes fabric8-services#184 * To be able to have all migration/sql-files/*.sql files bundled in the alm core binary, I've used go-bindata to pack them intothe migration/sqlbindata.go file. The prefix migration/sql-files is removed from the filepaths inside of migration/sqlbinata.go. The purpose of the migration/sql-files is to store all files that are relevant for updating the database over time. The SQL files themselves are packaged into the alm core binary with go-bindata. The filenames of the SQL files have no meaning but we prefix them with the version they stand for so it is easier to find out what's happening. The migration.go file has the control over the updates and the SQL files are not blindly executed just because they exist. Instead we allow the developers to run Go code as well. I've made sqlbindata.go a dependency of test-unit and test-integration. Otherwise the migration/sqlbindata.go file will not exist. * The auto migration portion in migration/migration.go and other places has been removed. * Added postgres.database config option * Added pg_dump of all tables except version This was command used to create all the common tables that are currently in existence. $ pg_dump \ --schema-only \ --exclude-table=version \ --no-privileges \ --no-owner \ --encoding=utf-8 \ --serializable-deferrable \ --host=$(\ docker inspect \ --format '{{ .NetworkSettings.IPAddress }}' \ make_postgres_integration_test_1 \ ) \ --username=postgres -W \ > migration/sql-files/001-common.sql As you can see I've tried to limit the schema as must as possible to make it universally applicable. Still in a follow up commit I'm going to have to make some adjustments to this dump before we can apply it. After dumping the database schema, I've manipulated it: - Simplified primary key in tables - Removed most sequences and ALTER statements and used simpler bigserial type and PRIMARY KEY constraint. See https://www.postgresql.org/docs/current/static/datatype-numeric.html * Added -migrateDatabase CLI switch If the -migrateDatabase switch is given, we start the core as usal but exit as soon as the migration of the database is done. If -printConfig is also given, -printConfig wins. That means, after printing the config, we exit the program before even going to the migration. The -migrateDatabase switch can also be toggled from the "migrate-database" make target We run the database migration before running integration tests * Make sure the database is populated with the correct types (e.g. system.bug etc.) It is okay for integration tests to create the database entries that they need. That is what the migration.PopulateCommonTypes() function does. Previously this function was called PopulateCommonTypes() for historical reasons. The actual structural migration of a database is performed in the Migrate() function and not in PopulateCommonTypes(). Whether or not this should be outsourced can be discussed. But for now the integration tests that need some test data to be present in the database are responsible for initiating the population themselves. (We can refactor the whole concept of resource.Require() in another issue but not here.) * Added config option populate.commontypes By default populate.commontypes is `true` to ensure that the system will try to create the common work item types such as system.bug, systen, feature, and so forth. * Test support for concurrent migrations (by @baijum) * Serializable transactions during migration No matter what the default connection setting is for transactions, the transactions created for the migration code will be serializable. This is the most secure way that doesn't allow other transactions to read or write. We ignore a postgres error of serialization_failure in migration. * Lock version table In addition to the transaction isolation level "serializable" we're loccking the "version" table" for exclusive access for the current transaction.
To be able to have all `migration/sql-files/*.sql` files bundled in the alm core binary, I've used go-bindata to pack them intothe `migration/sqlbindata.go` file. The prefix `migration/sql-files` is removed from the filepaths inside of `migration/sqlbinata.go`. The purpose of the `migration/sql-files` is to store all files that are relevant for updating the database over time. The SQL files themselves are packaged into the alm core binary with [go-bindata](https://github.com/jteeuwen/go-bindata). The filenames of the SQL files have no meaning but we prefix them with the version they stand for so it is easier to find out what's happening. The `migration.go` file has the control over the updates and the SQL files are *not* blindly executed just because they exist. Instead we allow the developers to run Go code as well. ## Version 0 The first sql file (`./migration/sql-files/000-bootstrap.sql`) creates the basic layout of the versioning table. ### Database table layout This is how the `version` table looks like in Postgres: ``` postgres=# select * from version; id | updated_at | version ----+-------------------------------+--------- 1 | 2016-09-27 10:13:30.423206+00 | 0 (1 row) ``` The auto migration portion in migration/migration.go has been removed. Logic to get the current version and to execute all remaining version is implemented. * Support migration of the database schema and data * Added -migrateDatabase CLI switch If the -migrateDatabase switch is given, we start the core as usal but exit as soon as the migration of the database is done. If -printConfig is also given, -printConfig wins. That means, after printing the config, we exit the program before even going to the migration. The -migrateDatabase switch can also be toggled from the "migrate-database" make target We run the database migration before running integration tests * Make sure the database is populated with the correct types (e.g. system.bug etc.) It is okay for integration tests to create the database entries that they need. That is what the migration.PopulateCommonTypes() function does. Previously this function was called PopulateCommonTypes() for historical reasons. The actual structural migration of a database is performed in the Migrate() function and not in PopulateCommonTypes(). Whether or not this should be outsourced can be discussed. But for now the integration tests that need some test data to be present in the database are responsible for initiating the population themselves. * Added config option populate.commontypes By default populate.commontypes is `true` to ensure that the system will try to create the common work item types such as system.bug, systen, feature, and so forth. * Test support for concurrent migrations (by @baijum) * Lock version table In addition to the transaction isolation level "serializable" we're loccking the "version" table" for exclusive access for the current transaction. * Introduce configuration.GetPostgresConfigString() GetPostgresConfigString returns a ready to use string for usage in sql.Open() Contributed-by Konrad Kleine <kwk@users.noreply.github.com> Contributed-by Baiju Muthukadan <baiju.m.mail@gmail.com> Fixes #184
We could add a version table which helps in figuring out what migration code needs to be executed.
The text was updated successfully, but these errors were encountered: